From e13e697067d004107655bc206ac18e9191abf26c Mon Sep 17 00:00:00 2001 From: Todd Gamblin Date: Fri, 5 Nov 2021 00:58:29 -0700 Subject: commands: `spack load --list` alias for `spack find --loaded` (#27184) See #25249 and https://github.com/spack/spack/pull/27159#issuecomment-958163679. This adds `spack load --list` as an alias for `spack find --loaded`. The new command is not as powerful as `spack find --loaded`, as you can't combine it with all the queries or formats that `spack find` provides. However, it is more intuitively located in the command structure in that it appears in the output of `spack load --help`. The idea here is that people can use `spack load --list` for simple stuff but fall back to `spack find --loaded` if they need more. - add help to `spack load --list` that references `spack find` - factor some parts of `spack find` out to be called from `spack load` - add shell tests - update docs Co-authored-by: Peter Josef Scheibel Co-authored-by: Richarda Butler <39577672+RikkiButler20@users.noreply.github.com> --- lib/spack/docs/basic_usage.rst | 3 +++ lib/spack/spack/cmd/__init__.py | 23 +++++++++++++++++++++++ lib/spack/spack/cmd/find.py | 11 +++++------ lib/spack/spack/cmd/load.py | 23 +++++++++++++++++++---- share/spack/csh/spack.csh | 1 + share/spack/qa/setup-env-test.fish | 3 +++ share/spack/qa/setup-env-test.sh | 3 +++ share/spack/setup-env.fish | 5 +++++ share/spack/setup-env.sh | 1 + share/spack/spack-completion.bash | 2 +- 10 files changed, 64 insertions(+), 11 deletions(-) diff --git a/lib/spack/docs/basic_usage.rst b/lib/spack/docs/basic_usage.rst index 091b18f6b8..165058e1d1 100644 --- a/lib/spack/docs/basic_usage.rst +++ b/lib/spack/docs/basic_usage.rst @@ -952,6 +952,9 @@ use ``spack find --loaded``. -- linux-debian7 / intel@15.0.0 --------------------------------- libelf@0.8.13 +You can also use ``spack load --list`` to get the same output, but it +does not have the full set of query options that ``spack find`` offers. + We'll learn more about Spack's spec syntax in the next section. diff --git a/lib/spack/spack/cmd/__init__.py b/lib/spack/spack/cmd/__init__.py index e5dbcd0b27..0b0451c6f3 100644 --- a/lib/spack/spack/cmd/__init__.py +++ b/lib/spack/spack/cmd/__init__.py @@ -27,6 +27,7 @@ import spack.extensions import spack.paths import spack.spec import spack.store +import spack.user_environment as uenv import spack.util.spack_json as sjson import spack.util.string @@ -442,6 +443,28 @@ def display_specs(specs, args=None, **kwargs): output.flush() +def filter_loaded_specs(specs): + """Filter a list of specs returning only those that are + currently loaded.""" + hashes = os.environ.get(uenv.spack_loaded_hashes_var, '').split(':') + return [x for x in specs if x.dag_hash() in hashes] + + +def print_how_many_pkgs(specs, pkg_type=""): + """Given a list of specs, this will print a message about how many + specs are in that list. + + Args: + specs (list): depending on how many items are in this list, choose + the plural or singular form of the word "package" + pkg_type (str): the output string will mention this provided + category, e.g. if pkg_type is "installed" then the message + would be "3 installed packages" + """ + tty.msg("%s" % spack.util.string.plural( + len(specs), pkg_type + " package")) + + def spack_is_git_repo(): """Ensure that this instance of Spack is a git clone.""" return is_git_repo(spack.paths.prefix) diff --git a/lib/spack/spack/cmd/find.py b/lib/spack/spack/cmd/find.py index 8378f6ef6e..cc06454817 100644 --- a/lib/spack/spack/cmd/find.py +++ b/lib/spack/spack/cmd/find.py @@ -6,7 +6,6 @@ from __future__ import print_function import copy -import os import sys import llnl.util.lang @@ -18,9 +17,7 @@ import spack.cmd as cmd import spack.cmd.common.arguments as arguments import spack.environment as ev import spack.repo -import spack.user_environment as uenv from spack.database import InstallStatuses -from spack.util.string import plural description = "list and search installed packages" section = "basic" @@ -241,8 +238,7 @@ def _find(parser, args): results = [x for x in results if x.name in packages_with_tags] if args.loaded: - hashes = os.environ.get(uenv.spack_loaded_hashes_var, '').split(':') - results = [x for x in results if x.dag_hash() in hashes] + results = spack.cmd.filter_loaded_specs(results) # Display the result if args.json: @@ -251,7 +247,10 @@ def _find(parser, args): if not args.format: if env: display_env(env, args, decorator) + if sys.stdout.isatty() and args.groups: - tty.msg("%s" % plural(len(results), 'installed package')) + pkg_type = "loaded" if args.loaded else "installed" + spack.cmd.print_how_many_pkgs(results, pkg_type) + cmd.display_specs( results, args, decorator=decorator, all_headers=True) diff --git a/lib/spack/spack/cmd/load.py b/lib/spack/spack/cmd/load.py index ab8f88c7f5..172a35a02b 100644 --- a/lib/spack/spack/cmd/load.py +++ b/lib/spack/spack/cmd/load.py @@ -7,6 +7,7 @@ import sys import spack.cmd import spack.cmd.common.arguments as arguments +import spack.cmd.find import spack.environment as ev import spack.store import spack.user_environment as uenv @@ -20,8 +21,7 @@ level = "short" def setup_parser(subparser): """Parser is only constructed so that this prints a nice help message with -h. """ - arguments.add_common_arguments( - subparser, ['installed_specs']) + arguments.add_common_arguments(subparser, ['constraint']) shells = subparser.add_mutually_exclusive_group() shells.add_argument( @@ -53,14 +53,29 @@ alternatively one can decide to load only the package or only the dependencies""" ) + subparser.add_argument( + '--list', + action='store_true', + default=False, + help="show loaded packages: same as `spack find --loaded`" + ) + def load(parser, args): env = ev.active_environment() + + if args.list: + results = spack.cmd.filter_loaded_specs(args.specs()) + if sys.stdout.isatty(): + spack.cmd.print_how_many_pkgs(results, "loaded") + spack.cmd.display_specs(results) + return + specs = [spack.cmd.disambiguate_spec(spec, env, first=args.load_first) - for spec in spack.cmd.parse_specs(args.specs)] + for spec in spack.cmd.parse_specs(args.constraint)] if not args.shell: - specs_str = ' '.join(args.specs) or "SPECS" + specs_str = ' '.join(args.constraint) or "SPECS" spack.cmd.common.shell_init_instructions( "spack load", " eval `spack load {sh_arg} %s`" % specs_str, diff --git a/share/spack/csh/spack.csh b/share/spack/csh/spack.csh index 7b6bcbf929..b105f4fa89 100644 --- a/share/spack/csh/spack.csh +++ b/share/spack/csh/spack.csh @@ -148,6 +148,7 @@ case unload: # argument and specs with "-h" in the name. if ( " $_sp_spec" =~ "* --sh*" || \ " $_sp_spec" =~ "* --csh*" || \ + " $_sp_spec" =~ "* --list*" || \ " $_sp_spec" =~ "* -h*" || \ " $_sp_spec" =~ "* --help*") then # Args contain --sh, --csh, or -h/--help: just execute. diff --git a/share/spack/qa/setup-env-test.fish b/share/spack/qa/setup-env-test.fish index eaa1e34cee..a7ba965033 100755 --- a/share/spack/qa/setup-env-test.fish +++ b/share/spack/qa/setup-env-test.fish @@ -337,6 +337,9 @@ set _a_ld $_a_loc"/lib" spt_contains "set -gx LD_LIBRARY_PATH $_b_ld" spack -m load --only package --fish b spt_succeeds spack -m load b +set LIST_CONTENT (spack -m load b; spack load --list) +spt_contains "b@" echo $LIST_CONTENT +spt_does_not_contain "a@" echo $LIST_CONTENT # test a variable MacOS clears and one it doesn't for recursive loads spt_contains "set -gx LD_LIBRARY_PATH $_a_ld:$_b_ld" spack -m load --fish a spt_succeeds spack -m load --only dependencies a diff --git a/share/spack/qa/setup-env-test.sh b/share/spack/qa/setup-env-test.sh index b93ac6c0e6..d575cc6f08 100755 --- a/share/spack/qa/setup-env-test.sh +++ b/share/spack/qa/setup-env-test.sh @@ -106,6 +106,9 @@ contains "usage: spack module " spack -m module title 'Testing `spack load`' contains "export PATH=$(spack -m location -i b)/bin" spack -m load --only package --sh b succeeds spack -m load b +LIST_CONTENT=`spack -m load b; spack load --list` +contains "b@" echo $LIST_CONTENT +does_not_contain "a@" echo $LIST_CONTENT fails spack -m load -l # test a variable MacOS clears and one it doesn't for recursive loads contains "export PATH=$(spack -m location -i a)/bin:$(spack -m location -i b)/bin" spack -m load --sh a diff --git a/share/spack/setup-env.fish b/share/spack/setup-env.fish index 85a453d1c4..d16ee617df 100755 --- a/share/spack/setup-env.fish +++ b/share/spack/setup-env.fish @@ -320,6 +320,11 @@ function check_env_activate_flags -d "check spack env subcommand flags for -h, - return 0 end + # looks for a single `--list` + if match_flag $_a "--list" + return 0 + end + end return 1 diff --git a/share/spack/setup-env.sh b/share/spack/setup-env.sh index eb364a3809..5136e8a58a 100755 --- a/share/spack/setup-env.sh +++ b/share/spack/setup-env.sh @@ -180,6 +180,7 @@ _spack_shell_wrapper() { if [ "${_a#* --sh}" != "$_a" ] || \ [ "${_a#* --csh}" != "$_a" ] || \ [ "${_a#* -h}" != "$_a" ] || \ + [ "${_a#* --list}" != "$_a" ] || \ [ "${_a#* --help}" != "$_a" ]; then # Args contain --sh, --csh, or -h/--help: just execute. diff --git a/share/spack/spack-completion.bash b/share/spack/spack-completion.bash index 1b9f45eae2..e9022d47bb 100755 --- a/share/spack/spack-completion.bash +++ b/share/spack/spack-completion.bash @@ -1215,7 +1215,7 @@ _spack_list() { _spack_load() { if $list_options then - SPACK_COMPREPLY="-h --help --sh --csh --fish --first --only" + SPACK_COMPREPLY="-h --help --sh --csh --fish --first --only --list" else _installed_packages fi -- cgit v1.2.3-60-g2f50