Source code for spack.cmd.diff

# Copyright 2013-2021 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

import sys

import llnl.util.tty as tty
from llnl.util.tty.color import cprint, get_color_when

import spack.cmd
import spack.cmd.common.arguments as arguments
import spack.environment as ev
import spack.solver.asp as asp
import spack.util.environment
import spack.util.spack_json as sjson

description = "compare two specs"
section = "basic"
level = "long"

[docs]def setup_parser(subparser): arguments.add_common_arguments( subparser, ['specs']) subparser.add_argument( '--json', action='store_true', default=False, dest='dump_json', help="Dump json output instead of pretty printing." ) subparser.add_argument( '--first', action='store_true', default=False, dest='load_first', help="load the first match if multiple packages match the spec" ) subparser.add_argument( '-a', '--attribute', action='append', help="select the attributes to show (defaults to all)" )
[docs]def compare_specs(a, b, to_string=False, color=None): """ Generate a comparison, including diffs (for each side) and an intersection. We can either print the result to the console, or parse into a json object for the user to save. We return an object that shows the differences, intersection, and names for a pair of specs a and b. Arguments: a (spack.spec.Spec): the first spec to compare b (spack.spec.Spec): the second spec to compare a_name (str): the name of spec a b_name (str): the name of spec b to_string (bool): return an object that can be json dumped color (bool): whether to format the names for the console """ if color is None: color = get_color_when() # Prepare a solver setup to parse differences setup = asp.SpackSolverSetup() a_facts = set(t for t in setup.spec_clauses(a, body=True, expand_hashes=True)) b_facts = set(t for t in setup.spec_clauses(b, body=True, expand_hashes=True)) # We want to present them to the user as simple key: values intersect = sorted(a_facts.intersection(b_facts)) spec1_not_spec2 = sorted(a_facts.difference(b_facts)) spec2_not_spec1 = sorted(b_facts.difference(a_facts)) # Format the spec names to be colored fmt = "{name}{@version}{/hash}" a_name = a.format(fmt, color=color) b_name = b.format(fmt, color=color) # We want to show what is the same, and then difference for each return { "intersect": flatten(intersect) if to_string else intersect, "a_not_b": flatten(spec1_not_spec2) if to_string else spec1_not_spec2, "b_not_a": flatten(spec2_not_spec1) if to_string else spec2_not_spec1, "a_name": a_name, "b_name": b_name, }
[docs]def flatten(functions): """ Given a list of ASP functions, convert into a list of key: value tuples. We are squashing whatever is after the first index into one string for easier parsing in the interface """ updated = [] for fun in functions: updated.append([, " ".join(str(a) for a in fun.args)]) return updated
[docs]def diff(parser, args): env = ev.active_environment() if len(args.specs) != 2: tty.die("You must provide two specs to diff.") specs = [spack.cmd.disambiguate_spec(spec, env, first=args.load_first) for spec in spack.cmd.parse_specs(args.specs)] # Calculate the comparison (c) color = False if args.dump_json else get_color_when() c = compare_specs(specs[0], specs[1], to_string=True, color=color) # Default to all attributes attributes = args.attribute or ["all"] if args.dump_json: print(sjson.dump(c)) else: tty.warn("This interface is subject to change.\n") print_difference(c, attributes)