csh: don't require SPACK_ROOT for sourcing setup-env.csh (#18225)

Don't require SPACK_ROOT for sourcing setup-env.csh and make output more consistent
This commit is contained in:
Todd Gamblin
2020-10-23 18:54:34 -07:00
committed by GitHub
parent 4cf85ed5f7
commit 560beb098e
15 changed files with 240 additions and 120 deletions

View File

@@ -3,8 +3,7 @@
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
from spack.cmd.common import print_module_placeholder_help
import spack.cmd.common
import spack.cmd.location
description = "cd to spack directories in the shell"
@@ -20,4 +19,8 @@ def setup_parser(subparser):
def cd(parser, args):
print_module_placeholder_help()
spec = " ".join(args.spec) if args.spec else "SPEC"
spack.cmd.common.shell_init_instructions(
"spack cd",
"cd `spack location --install-dir %s`" % spec
)

View File

@@ -3,35 +3,51 @@
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
import llnl.util.tty as tty
import llnl.util.tty.color as color
import spack.paths
from llnl.util import tty
shell_init_instructions = [
"To initialize spack's shell commands:",
"",
" # for bash and zsh",
" . %s/setup-env.sh" % spack.paths.share_path,
"",
" # for csh and tcsh",
" setenv SPACK_ROOT %s" % spack.paths.prefix,
" source %s/setup-env.csh" % spack.paths.share_path, ""
]
def shell_init_instructions(cmd, equivalent):
"""Print out instructions for users to initialize shell support.
def print_module_placeholder_help():
"""
For use by commands to tell user how to activate shell support.
Arguments:
cmd (str): the command the user tried to run that requires
shell support in order to work
equivalent (str): a command they can run instead, without
enabling shell support
"""
shell_specific = "{sh_arg}" in equivalent
msg = [
"This command requires spack's shell integration.", ""
] + shell_init_instructions + [
"This exposes a 'spack' shell function, which you can use like",
" $ spack load package-foo", "",
"Running the Spack executable directly (for example, invoking",
"./bin/spack) will bypass the shell function and print this",
"placeholder message, even if you have sourced one of the above",
"shell integration scripts."
"`%s` requires spack's shell support." % cmd,
"",
"To set up shell support, run the command below for your shell.",
"",
color.colorize("@*c{For bash/zsh/sh:}"),
" . %s/setup-env.sh" % spack.paths.share_path,
"",
color.colorize("@*c{For csh/tcsh:}"),
" source %s/setup-env.csh" % spack.paths.share_path,
"",
color.colorize("@*c{For fish:}"),
" source %s/setup-env.fish" % spack.paths.share_path,
"",
"Or, if you do not want to use shell support, run " + (
"one of these" if shell_specific else "this") + " instead:",
"",
]
tty.msg(*msg)
if shell_specific:
msg += [
equivalent.format(sh_arg="--sh ") + " # bash/zsh/sh",
equivalent.format(sh_arg="--csh ") + " # csh/tcsh",
equivalent.format(sh_arg="--fish") + " # fish",
]
else:
msg += [" " + equivalent]
msg += ['']
tty.error(*msg)

View File

@@ -84,17 +84,10 @@ def env_activate_setup_parser(subparser):
def env_activate(args):
env = args.activate_env
if not args.shell:
msg = [
"This command works best with Spack's shell support",
""
] + spack.cmd.common.shell_init_instructions + [
'Or, if you want to use `spack env activate` without initializing',
'shell support, you can run one of these:',
'',
' eval `spack env activate --sh %s` # for bash/sh' % env,
' eval `spack env activate --csh %s` # for csh/tcsh' % env,
]
tty.msg(*msg)
spack.cmd.common.shell_init_instructions(
"spack env activate",
" eval `spack env activate {sh_arg} %s`" % env,
)
return 1
if ev.exists(env) and not args.dir:
@@ -141,17 +134,10 @@ def env_deactivate_setup_parser(subparser):
def env_deactivate(args):
if not args.shell:
msg = [
"This command works best with Spack's shell support",
""
] + spack.cmd.common.shell_init_instructions + [
'Or, if you want to use `spack env activate` without initializing',
'shell support, you can run one of these:',
'',
' eval `spack env deactivate --sh` # for bash/sh',
' eval `spack env deactivate --csh` # for csh/tcsh',
]
tty.msg(*msg)
spack.cmd.common.shell_init_instructions(
"spack env deactivate",
" eval `spack env deactivate {sh_arg}`",
)
return 1
if 'SPACK_ENV' not in os.environ:

View File

@@ -5,8 +5,6 @@
import sys
import llnl.util.tty as tty
import spack.cmd
import spack.cmd.common.arguments as arguments
import spack.environment as ev
@@ -62,18 +60,11 @@ def load(parser, args):
for spec in spack.cmd.parse_specs(args.specs)]
if not args.shell:
specs_string = ' '.join(args.specs)
msg = [
"This command works best with Spack's shell support",
""
] + spack.cmd.common.shell_init_instructions + [
'Or, if you want to use `spack load` without initializing',
'shell support, you can run one of these:',
'',
' eval `spack load --sh %s` # for bash/sh' % specs_string,
' eval `spack load --csh %s` # for csh/tcsh' % specs_string,
]
tty.msg(*msg)
specs_str = ' '.join(args.specs) or "SPECS"
spack.cmd.common.shell_init_instructions(
"spack load",
" eval `spack load {sh_arg}` %s" % specs_str,
)
return 1
with spack.store.db.read_transaction():

View File

@@ -6,8 +6,6 @@
import sys
import os
import llnl.util.tty as tty
import spack.cmd
import spack.cmd.common.arguments as arguments
import spack.util.environment
@@ -53,17 +51,12 @@ def unload(parser, args):
specs = spack.store.db.query(hashes=hashes)
if not args.shell:
msg = [
"This command works best with Spack's shell support",
""
] + spack.cmd.common.shell_init_instructions + [
'Or, if you want to use `spack unload` without initializing',
'shell support, you can run one of these:',
'',
' eval `spack unload --sh %s` # for bash/sh' % args.specs,
' eval `spack unload --csh %s` # for csh/tcsh' % args.specs,
]
tty.msg(*msg)
specs_str = ' '.join(args.specs) or "SPECS"
spack.cmd.common.shell_init_instructions(
"spack unload",
" eval `spack unload {sh_arg}` %s" % specs_str,
)
return 1
env_mod = spack.util.environment.EnvironmentModifications()

View File

@@ -14,4 +14,4 @@ def test_cd():
out = cd()
assert "To initialize spack's shell commands:" in out
assert "To set up shell support" in out

View File

@@ -1185,7 +1185,7 @@ def test_env_activate_view_fails(
tmpdir, mock_stage, mock_fetch, install_mockery, env_deactivate):
"""Sanity check on env activate to make sure it requires shell support"""
out = env('activate', 'test')
assert "To initialize spack's shell commands:" in out
assert "To set up shell support" in out
def test_stack_yaml_definitions(tmpdir):

View File

@@ -102,7 +102,7 @@ def test_load_fails_no_shell(install_mockery, mock_fetch, mock_archive,
install('mpileaks')
out = load('mpileaks', fail_on_error=False)
assert "To initialize spack's shell commands" in out
assert "To set up shell support" in out
def test_unload(install_mockery, mock_fetch, mock_archive, mock_packages,
@@ -135,4 +135,4 @@ def test_unload_fails_no_shell(install_mockery, mock_fetch, mock_archive,
os.environ[uenv.spack_loaded_hashes_var] = mpileaks_spec.dag_hash()
out = unload('mpileaks', fail_on_error=False)
assert "To initialize spack's shell commands" in out
assert "To set up shell support" in out