diff options
author | Massimiliano Culpo <massimiliano.culpo@gmail.com> | 2021-05-14 06:51:41 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-13 21:51:41 -0700 |
commit | f47066967b844c0ace0ac1c9208a5c9ad9483d7b (patch) | |
tree | 19dc242658737568040c4d63b1155ff9ed2ae2ad /lib/spack | |
parent | b70bf073b5f647dd5a7f1917ed55dfd39d1a2a0c (diff) | |
download | spack-f47066967b844c0ace0ac1c9208a5c9ad9483d7b.tar.gz spack-f47066967b844c0ace0ac1c9208a5c9ad9483d7b.tar.bz2 spack-f47066967b844c0ace0ac1c9208a5c9ad9483d7b.tar.xz spack-f47066967b844c0ace0ac1c9208a5c9ad9483d7b.zip |
spec: simplify __str__ implementation (#23593)
The implementation for __str__ has been simplified to traverse the spec directly,
and doesn't call anymore the flat_dependencies method. Dead code has been
removed.
Diffstat (limited to 'lib/spack')
-rw-r--r-- | lib/spack/spack/spec.py | 19 | ||||
-rw-r--r-- | lib/spack/spack/test/spec_semantics.py | 4 |
2 files changed, 10 insertions, 13 deletions
diff --git a/lib/spack/spack/spec.py b/lib/spack/spack/spec.py index 54ae03092e..aceceda081 100644 --- a/lib/spack/spack/spec.py +++ b/lib/spack/spack/spec.py @@ -3566,11 +3566,6 @@ class Spec(object): else: return any(s.satisfies(spec) for s in self.traverse(root=False)) - def sorted_deps(self): - """Return a list of all dependencies sorted by name.""" - deps = self.flat_dependencies() - return tuple(deps[name] for name in sorted(deps)) - def eq_dag(self, other, deptypes=True, vs=None, vo=None): """True if the full dependency DAGs of specs are equal.""" if vs is None: @@ -3880,7 +3875,9 @@ class Spec(object): 'Format string terminated while reading attribute.' 'Missing terminating }.' ) - return out.getvalue() + + formatted_spec = out.getvalue() + return formatted_spec.strip() def old_format(self, format_string='$_$@$%@+$+$=', **kwargs): """ @@ -4136,12 +4133,12 @@ class Spec(object): kwargs.setdefault('color', None) return self.format(*args, **kwargs) - def dep_string(self): - return ''.join(" ^" + dep.format() for dep in self.sorted_deps()) - def __str__(self): - ret = self.format() + self.dep_string() - return ret.strip() + sorted_nodes = [self] + sorted( + self.traverse(root=False), key=lambda x: x.name + ) + spec_str = " ^".join(d.format() for d in sorted_nodes) + return spec_str.strip() def install_status(self): """Helper for tree to print DB install status.""" diff --git a/lib/spack/spack/test/spec_semantics.py b/lib/spack/spack/test/spec_semantics.py index 9fb5eaf2b6..16a4cf77ce 100644 --- a/lib/spack/spack/test/spec_semantics.py +++ b/lib/spack/spack/test/spec_semantics.py @@ -776,7 +776,7 @@ class TestSpecSematics(object): sigil_package_segments = [("{@VERSIONS}", '@' + str(spec.version)), ("{%compiler}", '%' + str(spec.compiler)), ("{arch=architecture}", - ' arch=' + str(spec.architecture))] + 'arch=' + str(spec.architecture))] compiler_segments = [("{compiler.name}", "name"), ("{compiler.version}", "versions")] @@ -798,7 +798,7 @@ class TestSpecSematics(object): for named_str, prop in package_segments: expected = getattr(spec, prop, "") actual = spec.format(named_str) - assert str(expected) == actual + assert str(expected).strip() == actual for named_str, expected in sigil_package_segments: actual = spec.format(named_str) |