From 560beb098efdd5b13f02692317fa5460c9b81141 Mon Sep 17 00:00:00 2001 From: Todd Gamblin Date: Fri, 23 Oct 2020 18:54:34 -0700 Subject: 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 --- lib/spack/spack/cmd/cd.py | 9 +++-- lib/spack/spack/cmd/common/__init__.py | 64 +++++++++++++++++++++------------- lib/spack/spack/cmd/env.py | 30 +++++----------- lib/spack/spack/cmd/load.py | 19 +++------- lib/spack/spack/cmd/unload.py | 19 ++++------ lib/spack/spack/test/cmd/cd.py | 2 +- lib/spack/spack/test/cmd/env.py | 2 +- lib/spack/spack/test/cmd/load.py | 4 +-- 8 files changed, 69 insertions(+), 80 deletions(-) (limited to 'lib') diff --git a/lib/spack/spack/cmd/cd.py b/lib/spack/spack/cmd/cd.py index a810e36ef3..cbbe9db04d 100644 --- a/lib/spack/spack/cmd/cd.py +++ b/lib/spack/spack/cmd/cd.py @@ -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 + ) diff --git a/lib/spack/spack/cmd/common/__init__.py b/lib/spack/spack/cmd/common/__init__.py index 00804493cd..35b802db48 100644 --- a/lib/spack/spack/cmd/common/__init__.py +++ b/lib/spack/spack/cmd/common/__init__.py @@ -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) diff --git a/lib/spack/spack/cmd/env.py b/lib/spack/spack/cmd/env.py index 7bd8052528..4699efd26b 100644 --- a/lib/spack/spack/cmd/env.py +++ b/lib/spack/spack/cmd/env.py @@ -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: diff --git a/lib/spack/spack/cmd/load.py b/lib/spack/spack/cmd/load.py index 3938602882..a8f59fef2f 100644 --- a/lib/spack/spack/cmd/load.py +++ b/lib/spack/spack/cmd/load.py @@ -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(): diff --git a/lib/spack/spack/cmd/unload.py b/lib/spack/spack/cmd/unload.py index cbee2fc769..8494453489 100644 --- a/lib/spack/spack/cmd/unload.py +++ b/lib/spack/spack/cmd/unload.py @@ -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() diff --git a/lib/spack/spack/test/cmd/cd.py b/lib/spack/spack/test/cmd/cd.py index e3900c0d8f..eda6994aec 100644 --- a/lib/spack/spack/test/cmd/cd.py +++ b/lib/spack/spack/test/cmd/cd.py @@ -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 diff --git a/lib/spack/spack/test/cmd/env.py b/lib/spack/spack/test/cmd/env.py index 92c9e4bcb5..1a6501440d 100644 --- a/lib/spack/spack/test/cmd/env.py +++ b/lib/spack/spack/test/cmd/env.py @@ -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): diff --git a/lib/spack/spack/test/cmd/load.py b/lib/spack/spack/test/cmd/load.py index e6664a9d39..b1697bbfc4 100644 --- a/lib/spack/spack/test/cmd/load.py +++ b/lib/spack/spack/test/cmd/load.py @@ -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 -- cgit v1.2.3-70-g09d2