Refactor installed_dependents -> installed_relatives

This commit is contained in:
Todd Gamblin 2017-06-17 19:20:05 -07:00
parent 36b3dd8cfe
commit c8b2100630
4 changed files with 34 additions and 15 deletions

View File

@ -45,8 +45,7 @@ def setup_parser(subparser):
'-t', '--transitive', action='store_true', default=False, '-t', '--transitive', action='store_true', default=False,
help="Show all transitive dependents.") help="Show all transitive dependents.")
subparser.add_argument( subparser.add_argument(
'spec', nargs=argparse.REMAINDER, 'spec', nargs=argparse.REMAINDER, help="spec or package name")
help="spec or package name")
def inverted_dependencies(): def inverted_dependencies():
@ -104,7 +103,8 @@ def dependents(parser, args):
spec = spack.cmd.disambiguate_spec(specs[0]) spec = spack.cmd.disambiguate_spec(specs[0])
tty.msg("Dependents of %s" % spec.cformat('$_$@$%@$/')) tty.msg("Dependents of %s" % spec.cformat('$_$@$%@$/'))
deps = spack.store.db.installed_dependents(spec) deps = spack.store.db.installed_relatives(
spec, 'parents', args.transitive)
if deps: if deps:
spack.cmd.display_specs(deps) spack.cmd.display_specs(deps)
else: else:

View File

@ -128,8 +128,8 @@ def installed_dependents(specs):
""" """
dependents = {} dependents = {}
for item in specs: for item in specs:
lst = [x for x in spack.store.db.installed_dependents(item) installed = spack.store.db.installed_relatives(item, 'parents', True)
if x not in specs] lst = [x for x in installed if x not in specs]
if lst: if lst:
lst = list(set(lst)) lst = list(set(lst))
dependents[item] = lst dependents[item] = lst
@ -157,7 +157,9 @@ def do_uninstall(specs, force):
# Sort packages to be uninstalled by the number of installed dependents # Sort packages to be uninstalled by the number of installed dependents
# This ensures we do things in the right order # This ensures we do things in the right order
def num_installed_deps(pkg): def num_installed_deps(pkg):
return len(spack.store.db.installed_dependents(pkg.spec)) dependents = spack.store.db.installed_relatives(
pkg.spec, 'parents', True)
return len(dependents)
packages.sort(key=num_installed_deps) packages.sort(key=num_installed_deps)
for item in packages: for item in packages:

View File

@ -711,18 +711,34 @@ def remove(self, spec):
return self._remove(spec) return self._remove(spec)
@_autospec @_autospec
def installed_dependents(self, spec, transitive=True): def installed_relatives(self, spec, direction='children', transitive=True):
"""List the installed specs that depend on this one.""" """Return installed specs related to this one."""
dependents = set() if direction not in ('parents', 'children'):
raise ValueError("Invalid direction: %s" % direction)
relatives = set()
for spec in self.query(spec): for spec in self.query(spec):
if transitive: if transitive:
to_add = spec.traverse(direction='parents', root=False) to_add = spec.traverse(direction=direction, root=False)
else: elif direction == 'parents':
to_add = spec.dependents() to_add = spec.dependents()
else: # direction == 'children'
to_add = spec.dependencies()
for dependent in to_add: for relative in to_add:
dependents.add(dependent) hash_key = relative.dag_hash()
return dependents if hash_key not in self._data:
reltype = ('Dependent' if direction == 'parents'
else 'Dependency')
tty.warn("Inconsistent state! %s %s of %s not in DB"
% (reltype, hash_key, spec.dag_hash()))
continue
if not self._data[hash_key].installed:
continue
relatives.add(relative)
return relatives
@_autospec @_autospec
def installed_extensions_for(self, extendee_spec): def installed_extensions_for(self, extendee_spec):

View File

@ -1582,7 +1582,8 @@ def uninstall_by_spec(spec, force=False):
raise InstallError(str(spec) + " is not installed.") raise InstallError(str(spec) + " is not installed.")
if not force: if not force:
dependents = spack.store.db.installed_dependents(spec) dependents = spack.store.db.installed_relatives(
spec, 'parents', True)
if dependents: if dependents:
raise PackageStillNeededError(spec, dependents) raise PackageStillNeededError(spec, dependents)