Use spack.traverse.traverse_nodes where useful (#33677)

This commit is contained in:
Harmen Stoppels 2022-11-03 11:34:24 +01:00 committed by GitHub
parent 30da20c1bc
commit 243dfe91e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -34,6 +34,7 @@
import spack.stage import spack.stage
import spack.store import spack.store
import spack.subprocess_context import spack.subprocess_context
import spack.traverse
import spack.user_environment as uenv import spack.user_environment as uenv
import spack.util.cpus import spack.util.cpus
import spack.util.environment import spack.util.environment
@ -1760,22 +1761,14 @@ def install_specs(self, specs=None, **install_args):
def all_specs(self): def all_specs(self):
"""Return all specs, even those a user spec would shadow.""" """Return all specs, even those a user spec would shadow."""
all_specs = set() roots = [self.specs_by_hash[h] for h in self.concretized_order]
for h in self.concretized_order: specs = [s for s in spack.traverse.traverse_nodes(roots, lambda s: s.dag_hash())]
try: specs.sort()
spec = self.specs_by_hash[h] return specs
except KeyError:
tty.warn(
"Environment %s appears to be corrupt: missing spec " '"%s"' % (self.name, h)
)
continue
all_specs.update(spec.traverse())
return sorted(all_specs)
def all_hashes(self): def all_hashes(self):
"""Return hashes of all specs.""" """Return hashes of all specs."""
return list(set(s.dag_hash() for s in self.all_specs())) return [s.dag_hash() for s in self.all_specs()]
def roots(self): def roots(self):
"""Specs explicitly requested by the user *in this environment*. """Specs explicitly requested by the user *in this environment*.
@ -1812,11 +1805,10 @@ def concretized_specs(self):
def get_by_hash(self, dag_hash): def get_by_hash(self, dag_hash):
matches = {} matches = {}
for _, root in self.concretized_specs(): roots = [self.specs_by_hash[h] for h in self.concretized_order]
for spec in root.traverse(root=True): for spec in spack.traverse.traverse_nodes(roots, key=lambda s: s.dag_hash()):
dep_hash = spec.dag_hash() if spec.dag_hash().startswith(dag_hash):
if dep_hash.startswith(dag_hash): matches[spec.dag_hash()] = spec
matches[dep_hash] = spec
return list(matches.values()) return list(matches.values())
def get_one_by_hash(self, dag_hash): def get_one_by_hash(self, dag_hash):
@ -1918,28 +1910,27 @@ def _get_environment_specs(self, recurse_dependencies=True):
If these specs appear under different user_specs, only one copy If these specs appear under different user_specs, only one copy
is added to the list returned. is added to the list returned.
""" """
spec_list = list() specs = [self.specs_by_hash[h] for h in self.concretized_order]
for spec_hash in self.concretized_order: if recurse_dependencies:
spec = self.specs_by_hash[spec_hash] specs.extend(
spack.traverse.traverse_nodes(
specs, root=False, deptype=("link", "run"), key=lambda s: s.dag_hash()
)
)
specs = spec.traverse(deptype=("link", "run")) if recurse_dependencies else (spec,) return specs
spec_list.extend(specs)
return spec_list
def _to_lockfile_dict(self): def _to_lockfile_dict(self):
"""Create a dictionary to store a lockfile for this environment.""" """Create a dictionary to store a lockfile for this environment."""
concrete_specs = {} concrete_specs = {}
for spec in self.specs_by_hash.values(): for s in spack.traverse.traverse_nodes(
for s in spec.traverse(): self.specs_by_hash.values(), key=lambda s: s.dag_hash()
dag_hash = s.dag_hash() ):
if dag_hash not in concrete_specs: spec_dict = s.node_dict_with_hashes(hash=ht.dag_hash)
spec_dict = s.node_dict_with_hashes(hash=ht.dag_hash) # Assumes no legacy formats, since this was just created.
# Assumes no legacy formats, since this was just created. spec_dict[ht.dag_hash.name] = s.dag_hash()
spec_dict[ht.dag_hash.name] = s.dag_hash() concrete_specs[s.dag_hash()] = spec_dict
concrete_specs[dag_hash] = spec_dict
hash_spec_list = zip(self.concretized_order, self.concretized_user_specs) hash_spec_list = zip(self.concretized_order, self.concretized_user_specs)
@ -2050,19 +2041,16 @@ def write(self, regenerate=True):
# ensure the prefix/.env directory exists # ensure the prefix/.env directory exists
fs.mkdirp(self.env_subdir_path) fs.mkdirp(self.env_subdir_path)
for spec in self.new_specs: for spec in spack.traverse.traverse_nodes(self.new_specs):
for dep in spec.traverse(): if not spec.concrete:
if not dep.concrete: raise ValueError("specs passed to environment.write() " "must be concrete!")
raise ValueError(
"specs passed to environment.write() " "must be concrete!"
)
root = os.path.join(self.repos_path, dep.namespace) root = os.path.join(self.repos_path, spec.namespace)
repo = spack.repo.create_or_construct(root, dep.namespace) repo = spack.repo.create_or_construct(root, spec.namespace)
pkg_dir = repo.dirname_for_package_name(dep.name) pkg_dir = repo.dirname_for_package_name(spec.name)
fs.mkdirp(pkg_dir) fs.mkdirp(pkg_dir)
spack.repo.path.dump_provenance(dep, pkg_dir) spack.repo.path.dump_provenance(spec, pkg_dir)
self._update_and_write_manifest(raw_yaml_dict, yaml_dict) self._update_and_write_manifest(raw_yaml_dict, yaml_dict)