From d4e714bb2efc3a5428cf533cd3df4a671a6d44b3 Mon Sep 17 00:00:00 2001 From: Todd Gamblin Date: Sat, 14 Jan 2023 16:08:40 -0800 Subject: spack list: add `--count` option (#34950) Sometimes I just want to know how many packages of a certain type there are. - [x] add `--count` option to `spack list` that output the number of packages that *would* be listed. ```console > spack list --count 6864 > spack list --count py- 2040 > spack list --count r- 1162 ``` --- lib/spack/spack/cmd/list.py | 28 ++++++++++++++++++++-------- lib/spack/spack/test/cmd/list.py | 11 +++++++++++ 2 files changed, 31 insertions(+), 8 deletions(-) (limited to 'lib') diff --git a/lib/spack/spack/cmd/list.py b/lib/spack/spack/cmd/list.py index fbb7458e7f..619bfc4911 100644 --- a/lib/spack/spack/cmd/list.py +++ b/lib/spack/spack/cmd/list.py @@ -55,13 +55,6 @@ def setup_parser(subparser): choices=formatters, help="format to be used to print the output [default: name_only]", ) - subparser.add_argument( - "--update", - metavar="FILE", - default=None, - action="store", - help="write output to the specified file, if any package is newer", - ) subparser.add_argument( "-v", "--virtuals", @@ -71,6 +64,22 @@ def setup_parser(subparser): ) arguments.add_common_arguments(subparser, ["tags"]) + # Doesn't really make sense to update in count mode. + count_or_update = subparser.add_mutually_exclusive_group() + count_or_update.add_argument( + "--count", + action="store_true", + default=False, + help="display the number of packages that would be listed", + ) + count_or_update.add_argument( + "--update", + metavar="FILE", + default=None, + action="store", + help="write output to the specified file, if any package is newer", + ) + def filter_by_name(pkgs, args): """ @@ -320,6 +329,9 @@ def list(parser, args): with open(args.update, "w") as f: formatter(sorted_packages, f) + elif args.count: + # just print the number of packages in the result + print(len(sorted_packages)) else: - # Print to stdout + # print formatted package list formatter(sorted_packages, sys.stdout) diff --git a/lib/spack/spack/test/cmd/list.py b/lib/spack/spack/test/cmd/list.py index 3ebcb4fa39..8bd36f09e3 100644 --- a/lib/spack/spack/test/cmd/list.py +++ b/lib/spack/spack/test/cmd/list.py @@ -6,6 +6,7 @@ import sys from textwrap import dedent +import spack.repo from spack.main import SpackCommand list = SpackCommand("list") @@ -123,3 +124,13 @@ def test_list_tags(mock_packages): output = list("--tag", "tag3") assert "mpich\n" not in output assert "mpich2" in output + + +def test_list_count(mock_packages): + output = list("--count") + assert int(output.strip()) == len(spack.repo.all_package_names()) + + output = list("--count", "py-") + assert int(output.strip()) == len( + [name for name in spack.repo.all_package_names() if "py-" in name] + ) -- cgit v1.2.3-60-g2f50