env: clean up command access to the active environment
- all commands (except `spack find`, through `ConstraintAction`) now go through get_env() to get the active environment - ev.active was hard to read -- and the name wasn't descriptive. - rename it to _active_environment to be more descriptive and to strongly indicate that spack.environment manages it
This commit is contained in:
parent
562482d9cc
commit
423d3e75ab
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
import spack.cmd
|
import spack.cmd
|
||||||
import spack.config
|
import spack.config
|
||||||
import spack.environment
|
import spack.environment as ev
|
||||||
import spack.modules
|
import spack.modules
|
||||||
import spack.spec
|
import spack.spec
|
||||||
import spack.store
|
import spack.store
|
||||||
@ -54,7 +54,7 @@ def _specs(self, **kwargs):
|
|||||||
|
|
||||||
# If an environment is provided, we'll restrict the search to
|
# If an environment is provided, we'll restrict the search to
|
||||||
# only its installed packages.
|
# only its installed packages.
|
||||||
env = spack.environment.active
|
env = ev._active_environment
|
||||||
if env:
|
if env:
|
||||||
kwargs['hashes'] = set(env.all_hashes())
|
kwargs['hashes'] = set(env.all_hashes())
|
||||||
|
|
||||||
|
@ -258,7 +258,7 @@ def env_list(args):
|
|||||||
|
|
||||||
color_names = []
|
color_names = []
|
||||||
for name in names:
|
for name in names:
|
||||||
if ev.active and name == ev.active.name:
|
if ev.active(name):
|
||||||
name = colorize('@*g{%s}' % name)
|
name = colorize('@*g{%s}' % name)
|
||||||
color_names.append(name)
|
color_names.append(name)
|
||||||
|
|
||||||
|
@ -61,18 +61,20 @@ def setup_parser(subparser):
|
|||||||
help="specs of packages to uninstall")
|
help="specs of packages to uninstall")
|
||||||
|
|
||||||
|
|
||||||
def find_matching_specs(specs, allow_multiple_matches=False, force=False):
|
def find_matching_specs(env, specs, allow_multiple_matches=False, force=False):
|
||||||
"""Returns a list of specs matching the not necessarily
|
"""Returns a list of specs matching the not necessarily
|
||||||
concretized specs given from cli
|
concretized specs given from cli
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
specs: list of specs to be matched against installed packages
|
env (Environment): active environment, or ``None`` if there is not one
|
||||||
allow_multiple_matches : if True multiple matches are admitted
|
specs (list): list of specs to be matched against installed packages
|
||||||
|
allow_multiple_matches (bool): if True multiple matches are admitted
|
||||||
|
|
||||||
Return:
|
Return:
|
||||||
list of specs
|
list of specs
|
||||||
"""
|
"""
|
||||||
hashes = ev.active.all_hashes() if ev.active else None
|
# constrain uninstall resolution to current environment if one is active
|
||||||
|
hashes = env.all_hashes() if env else None
|
||||||
|
|
||||||
# List of specs that match expressions given via command line
|
# List of specs that match expressions given via command line
|
||||||
specs_from_cli = []
|
specs_from_cli = []
|
||||||
@ -90,8 +92,8 @@ def find_matching_specs(specs, allow_multiple_matches=False, force=False):
|
|||||||
|
|
||||||
# No installed package matches the query
|
# No installed package matches the query
|
||||||
if len(matching) == 0 and spec is not any:
|
if len(matching) == 0 and spec is not any:
|
||||||
if ev.active:
|
if env:
|
||||||
pkg_type = "packages in environment '%s'" % ev.active.name
|
pkg_type = "packages in environment '%s'" % env.name
|
||||||
else:
|
else:
|
||||||
pkg_type = 'installed packages'
|
pkg_type = 'installed packages'
|
||||||
tty.die('{0} does not match any {1}.'.format(spec, pkg_type))
|
tty.die('{0} does not match any {1}.'.format(spec, pkg_type))
|
||||||
@ -147,13 +149,14 @@ def dependent_environments(specs):
|
|||||||
return dependents
|
return dependents
|
||||||
|
|
||||||
|
|
||||||
def do_uninstall(specs, force):
|
def do_uninstall(env, specs, force):
|
||||||
"""
|
"""
|
||||||
Uninstalls all the specs in a list.
|
Uninstalls all the specs in a list.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
specs: list of specs to be uninstalled
|
env (Environment): active environment, or ``None`` if there is not one
|
||||||
force: force uninstallation (boolean)
|
specs (list): list of specs to be uninstalled
|
||||||
|
force (bool): force uninstallation (boolean)
|
||||||
"""
|
"""
|
||||||
packages = []
|
packages = []
|
||||||
for item in specs:
|
for item in specs:
|
||||||
@ -165,11 +168,13 @@ def do_uninstall(specs, force):
|
|||||||
# want to uninstall.
|
# want to uninstall.
|
||||||
spack.package.Package.uninstall_by_spec(item, force=True)
|
spack.package.Package.uninstall_by_spec(item, force=True)
|
||||||
|
|
||||||
if ev.active:
|
if env:
|
||||||
try:
|
try:
|
||||||
ev.active.remove(item, force=True)
|
# try removing the spec from the current active
|
||||||
|
# environment. this will fail if the spec is not a root
|
||||||
|
env.remove(item, force=True)
|
||||||
except ev.SpackEnvironmentError:
|
except ev.SpackEnvironmentError:
|
||||||
pass # ignore errors from specs that are not roots
|
pass # ignore non-root specs
|
||||||
|
|
||||||
# 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
|
||||||
@ -183,14 +188,14 @@ def num_installed_deps(pkg):
|
|||||||
item.do_uninstall(force=force)
|
item.do_uninstall(force=force)
|
||||||
|
|
||||||
# write any changes made to the active environment
|
# write any changes made to the active environment
|
||||||
if ev.active:
|
if env:
|
||||||
ev.active.write()
|
env.write()
|
||||||
|
|
||||||
|
|
||||||
def get_uninstall_list(args, specs):
|
def get_uninstall_list(args, specs, env):
|
||||||
# Gets the list of installed specs that match the ones give via cli
|
# Gets the list of installed specs that match the ones give via cli
|
||||||
# takes care of '-a' is given in the cli
|
# takes care of '-a' is given in the cli
|
||||||
uninstall_list = find_matching_specs(specs, args.all, args.force)
|
uninstall_list = find_matching_specs(env, specs, args.all, args.force)
|
||||||
|
|
||||||
# Takes care of '-R'
|
# Takes care of '-R'
|
||||||
spec_dependents = installed_dependents(uninstall_list)
|
spec_dependents = installed_dependents(uninstall_list)
|
||||||
@ -231,7 +236,7 @@ def get_uninstall_list(args, specs):
|
|||||||
msgs.append(
|
msgs.append(
|
||||||
'use `spack env remove` to remove environments, or '
|
'use `spack env remove` to remove environments, or '
|
||||||
'`spack remove` to remove specs from environments.')
|
'`spack remove` to remove specs from environments.')
|
||||||
if ev.active:
|
if env:
|
||||||
msgs.append('consider using `spack remove` to remove the spec '
|
msgs.append('consider using `spack remove` to remove the spec '
|
||||||
'from this environment')
|
'from this environment')
|
||||||
print()
|
print()
|
||||||
@ -246,7 +251,8 @@ def get_uninstall_list(args, specs):
|
|||||||
|
|
||||||
|
|
||||||
def uninstall_specs(args, specs):
|
def uninstall_specs(args, specs):
|
||||||
uninstall_list = get_uninstall_list(args, specs)
|
env = ev.get_env(args, 'uninstall', required=False)
|
||||||
|
uninstall_list = get_uninstall_list(args, specs, env)
|
||||||
|
|
||||||
if not uninstall_list:
|
if not uninstall_list:
|
||||||
tty.warn('There are no package to uninstall.')
|
tty.warn('There are no package to uninstall.')
|
||||||
@ -260,7 +266,7 @@ def uninstall_specs(args, specs):
|
|||||||
tty.die('Will not uninstall any packages.')
|
tty.die('Will not uninstall any packages.')
|
||||||
|
|
||||||
# Uninstall everything on the list
|
# Uninstall everything on the list
|
||||||
do_uninstall(uninstall_list, args.force)
|
do_uninstall(env, uninstall_list, args.force)
|
||||||
|
|
||||||
|
|
||||||
def uninstall(parser, args):
|
def uninstall(parser, args):
|
||||||
|
@ -28,7 +28,7 @@
|
|||||||
|
|
||||||
|
|
||||||
#: currently activated environment
|
#: currently activated environment
|
||||||
active = None
|
_active_environment = None
|
||||||
|
|
||||||
|
|
||||||
#: path where environments are stored in the spack tree
|
#: path where environments are stored in the spack tree
|
||||||
@ -98,14 +98,14 @@ def activate(env, use_env_repo=False):
|
|||||||
TODO: Add support for views here. Activation should set up the shell
|
TODO: Add support for views here. Activation should set up the shell
|
||||||
TODO: environment to use the activated spack environment.
|
TODO: environment to use the activated spack environment.
|
||||||
"""
|
"""
|
||||||
global active
|
global _active_environment
|
||||||
|
|
||||||
active = env
|
_active_environment = env
|
||||||
prepare_config_scope(active)
|
prepare_config_scope(_active_environment)
|
||||||
if use_env_repo:
|
if use_env_repo:
|
||||||
spack.repo.path.put_first(active.repo)
|
spack.repo.path.put_first(_active_environment.repo)
|
||||||
|
|
||||||
tty.debug("Using environmennt '%s'" % active.name)
|
tty.debug("Using environmennt '%s'" % _active_environment.name)
|
||||||
|
|
||||||
|
|
||||||
def deactivate():
|
def deactivate():
|
||||||
@ -116,19 +116,19 @@ def deactivate():
|
|||||||
environment was active.
|
environment was active.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
global active
|
global _active_environment
|
||||||
|
|
||||||
if not active:
|
if not _active_environment:
|
||||||
return
|
return
|
||||||
|
|
||||||
deactivate_config_scope(active)
|
deactivate_config_scope(_active_environment)
|
||||||
|
|
||||||
# use _repo so we only remove if a repo was actually constructed
|
# use _repo so we only remove if a repo was actually constructed
|
||||||
if active._repo:
|
if _active_environment._repo:
|
||||||
spack.repo.path.remove(active._repo)
|
spack.repo.path.remove(_active_environment._repo)
|
||||||
|
|
||||||
tty.debug("Deactivated environmennt '%s'" % active.name)
|
tty.debug("Deactivated environmennt '%s'" % _active_environment.name)
|
||||||
active = None
|
_active_environment = None
|
||||||
|
|
||||||
|
|
||||||
def find_environment(args):
|
def find_environment(args):
|
||||||
@ -219,8 +219,8 @@ def get_env(args, cmd_name, required=True):
|
|||||||
raise SpackEnvironmentError('no environment in %s' % env)
|
raise SpackEnvironmentError('no environment in %s' % env)
|
||||||
|
|
||||||
# try the active environment. This is set by find_environment() (above)
|
# try the active environment. This is set by find_environment() (above)
|
||||||
if active:
|
if _active_environment:
|
||||||
return active
|
return _active_environment
|
||||||
elif not required:
|
elif not required:
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
@ -245,6 +245,11 @@ def exists(name):
|
|||||||
return os.path.isdir(root(name))
|
return os.path.isdir(root(name))
|
||||||
|
|
||||||
|
|
||||||
|
def active(name):
|
||||||
|
"""True if the named environment is active."""
|
||||||
|
return _active_environment and name == _active_environment.name
|
||||||
|
|
||||||
|
|
||||||
def is_env_dir(path):
|
def is_env_dir(path):
|
||||||
"""Whether a directory contains a spack environment."""
|
"""Whether a directory contains a spack environment."""
|
||||||
return os.path.isdir(path) and os.path.exists(
|
return os.path.isdir(path) and os.path.exists(
|
||||||
@ -407,7 +412,7 @@ def name(self):
|
|||||||
@property
|
@property
|
||||||
def active(self):
|
def active(self):
|
||||||
"""True if this environment is currently active."""
|
"""True if this environment is currently active."""
|
||||||
return active and self.path == active.path
|
return _active_environment and self.path == _active_environment.path
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def manifest_path(self):
|
def manifest_path(self):
|
||||||
@ -854,7 +859,7 @@ def write(self):
|
|||||||
_write_yaml(self.yaml, f)
|
_write_yaml(self.yaml, f)
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
self._previous_active = active
|
self._previous_active = _active_environment
|
||||||
activate(self)
|
activate(self)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@
|
|||||||
@pytest.fixture(scope='session', autouse=True)
|
@pytest.fixture(scope='session', autouse=True)
|
||||||
def clean_user_environment():
|
def clean_user_environment():
|
||||||
env_var = ev.spack_env_var in os.environ
|
env_var = ev.spack_env_var in os.environ
|
||||||
active = ev.active
|
active = ev._active_environment
|
||||||
|
|
||||||
if env_var:
|
if env_var:
|
||||||
spack_env_value = os.environ.pop(ev.spack_env_var)
|
spack_env_value = os.environ.pop(ev.spack_env_var)
|
||||||
|
Loading…
Reference in New Issue
Block a user