Skip to content

Commit

Permalink
updating docs and print of diff
Browse files Browse the repository at this point in the history
Instead of colify, we just print each diff attribute to a line.
This commit also updates the docs to show calculating a diff using
just the hashes, updates the docs to reflect this, and uses a consistent
identifier for the spec names that are diffed

Signed-off-by: vsoch <vsoch@users.noreply.github.com>
  • Loading branch information
vsoch committed Mar 15, 2021
1 parent 7d0daf3 commit 3325df3
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 29 deletions.
52 changes: 34 additions & 18 deletions lib/spack/docs/basic_usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -728,33 +728,45 @@ real quickly since we have two!
b) specify the spec by its hash (e.g. `spack uninstall /hash`), or
c) use `spack uninstall --all` to uninstall ALL matching specs.
Oh no! This is a good use case for ``spack diff``, which can easily show us the
"diff" or set difference between properties for two packages. Let's try it out.
Since the only difference between these two is the hash, we provide the hash.
Oh no! We can see from the above that we have two different versions of zlib installed,
and the only difference between the two is the hash. This is a good use case for
``spack diff``, which can easily show us the "diff" or set difference
between properties for two packages. Let's try it out.
Since the only difference between these two is the hash, we provide the hashes:

.. code-block::console
$ spack diff zlib@1.2.11/efzjziy zlib@1.2.11/sl7m27m
$ spack diff /efzjziy /sl7m27m
==> diff(zlib@1.2.11/efzjziy, zlib@1.2.11/sl7m27m)
VARIANT_SET zlib optimize bool(False)
VARIANT_SET
zlib optimize bool(False)
==> diff(zlib@1.2.11/sl7m27m, zlib@1.2.11/efzjziy)
VARIANT_SET zlib optimize bool(True)
VARIANT_SET
zlib optimize bool(True)
Awesome! The above tells us that our first zlib was built without optimize (False)
and the second was built with optimize (True). This is a small example, but there are
actually several different types of difference properties you can view, each one being
a fact about a particular spec. The way we calculate the difference is basically by
taking a set difference based on these facts. The first package that you provide (A)
actually several different kinds of differences that you can view, a ``VARIANT_SET``
being just one of them. The first package that you provide (A)
being diffed against B means that we see what is in A but not B. Here is another example
with one more difference type:
with one more difference type, ``VERSION``:

.. code-block::console
$ spack diff python@2.7.8 python@3.8.8
==> diff(python@2.7.8, python@3.8.8)
VARIANT_SET python patches a8c52415a8b03c0e5f28b5d52ae498f7a7e602007db2b9554df28cd5685839b8 VERSION python Version(2.7.8) openssl Version(1.0.2u)
==> diff(python@3.8.8, python@2.7.8)
VARIANT_SET python patches 0d98e93189bc278fbc37a50ed7f183bd8aaf249a8e1670a465f0db6bb4f8cf87 VERSION openssl Version(1.1.1j) python Version(3.8.8)
==> diff(python@2.7.8/7oknfqf, python@3.8.8/vrp4fmj)
VARIANT_SET
python patches a8c52415a8b03c0e5f28b5d52ae498f7a7e602007db2b9554df28cd5685839b8
VERSION
openssl Version(1.0.2u)
python Version(2.7.8)
==> diff(python@3.8.8/vrp4fmj, python@2.7.8/7oknfqf)
VARIANT_SET
python patches 0d98e93189bc278fbc37a50ed7f183bd8aaf249a8e1670a465f0db6bb4f8cf87
VERSION
python Version(3.8.8)
openssl Version(1.1.1j)
Let's say that we were only interested in one kind of attribute above, versions!
We could first see the options by asking the command for help:
Expand All @@ -774,10 +786,14 @@ Here is how you would filter to show just versions:
.. code-block:: console
$ spack diff --diff-type version python@2.7.8 python@3.8.8
==> diff(python@2.7.8, python@3.8.8)
VERSION python Version(2.7.8) openssl Version(1.0.2u)
==> diff(python@3.8.8, python@2.7.8)
VERSION openssl Version(1.1.1j) python Version(3.8.8)
==> diff(python@2.7.8/7oknfqf, python@3.8.8/vrp4fmj)
VERSION
python Version(2.7.8)
openssl Version(1.0.2u)
==> diff(python@3.8.8/vrp4fmj, python@2.7.8/7oknfqf)
VERSION
openssl Version(1.1.1j)
python Version(3.8.8)
Finally, if you want to view the data as json (and possibly pipe into an output file)
just add ``--json``:
Expand Down
13 changes: 6 additions & 7 deletions lib/spack/spack/cmd/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
import spack.util.environment

import llnl.util.tty as tty
from llnl.util.tty.colify import colify
import spack.util.spack_json as sjson
import operator
import sys
import spack.cmd

description = "compare two specs"
section = "extensions"
Expand Down Expand Up @@ -59,7 +58,7 @@ def bold(string):
return "\033[1m" + string + "\033[0m"


def compare_specs(a, b, a_name, b_name, to_string=False):
def compare_specs(a, b, to_string=False):
"""Generate a comparison, including diffs (for each side) along with
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
Expand Down Expand Up @@ -90,8 +89,8 @@ def compare_specs(a, b, a_name, b_name, to_string=False):
"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,
"a_name": a.name_version_build_hash,
"b_name": b.name_version_build_hash,
}


Expand Down Expand Up @@ -145,7 +144,7 @@ def print_difference(diffset, diff_type="all"):
rows.append(bold(category.upper()))
rows.append(entry[1])

colify(rows, indent=0, output=sys.stdout)
print('\n'.join(rows))


def diff(parser, args):
Expand All @@ -158,7 +157,7 @@ def diff(parser, args):
for spec in spack.cmd.parse_specs(args.specs)]

# Calculate the comparison (c)
c = compare_specs(specs[0], specs[1], args.specs[0], args.specs[1], to_string=True)
c = compare_specs(specs[0], specs[1], to_string=True)

if args.dump_json:
print(sjson.dump(c))
Expand Down
10 changes: 10 additions & 0 deletions lib/spack/spack/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -1550,6 +1550,16 @@ def dag_hash_bit_prefix(self, bits):
"""Get the first <bits> bits of the DAG hash as an integer type."""
return base32_prefix_bits(self.dag_hash(), bits)

@property
def name_version_build_hash(self):
"""A convenience function to print the name, version, and first 7
characters for a spec, intended for quick printing to the screen
to identify the spec for diff and other purposes. This mimics
cmd.display_specs but is a much simpler version to generate and return
a string instead of writing to an output.
"""
return "%s@%s/%s" % (self.name, self.version, self.build_hash()[0:7])

def to_node_dict(self, hash=ht.dag_hash):
"""Create a dictionary representing the state of this Spec.
Expand Down
9 changes: 5 additions & 4 deletions lib/spack/spack/test/cmd/diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ def test_diff(install_mockery, mock_fetch, mock_archive, mock_packages):
specB = spack.spec.Spec('mpileaks+debug').concretized()

# Specs should be the same as themselves
c = spack.cmd.diff.compare_specs(specA, specA, "A", "A", to_string=True)
c = spack.cmd.diff.compare_specs(specA, specA, to_string=True)
assert len(c['a_not_b']) == 0
assert len(c['b_not_a']) == 0

# Calculate the comparison (c)
c = spack.cmd.diff.compare_specs(specA, specB, "A", "B", to_string=True)
c = spack.cmd.diff.compare_specs(specA, specB, to_string=True)
assert len(c['a_not_b']) == 1
assert len(c['b_not_a']) == 1
assert c['a_not_b'][0] == ['variant_set', 'mpileaks debug bool(False)']
Expand All @@ -54,8 +54,9 @@ def test_load_first(install_mockery, mock_fetch, mock_archive, mock_packages):

assert len(result['a_not_b']) == 0
assert len(result['b_not_a']) == 0
assert result['a_name'] == 'mpileaks'
assert result['b_name'] == 'mpileaks'

assert 'mpileaks' in result['a_name']
assert 'mpileaks' in result['b_name']
assert "intersect" in result and len(result['intersect']) > 50

# After we install another version, it should ask us to disambiguate
Expand Down

0 comments on commit 3325df3

Please sign in to comment.