summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTodd Gamblin <tgamblin@llnl.gov>2023-01-14 16:08:40 -0800
committerGitHub <noreply@github.com>2023-01-14 16:08:40 -0800
commitd4e714bb2efc3a5428cf533cd3df4a671a6d44b3 (patch)
tree396b16ce8be5269a29499375c05f81fe6818de20 /lib
parentff38ff25cb3b4071ba7bb5699d857f8ae70c9cb5 (diff)
downloadspack-d4e714bb2efc3a5428cf533cd3df4a671a6d44b3.tar.gz
spack-d4e714bb2efc3a5428cf533cd3df4a671a6d44b3.tar.bz2
spack-d4e714bb2efc3a5428cf533cd3df4a671a6d44b3.tar.xz
spack-d4e714bb2efc3a5428cf533cd3df4a671a6d44b3.zip
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 ```
Diffstat (limited to 'lib')
-rw-r--r--lib/spack/spack/cmd/list.py28
-rw-r--r--lib/spack/spack/test/cmd/list.py11
2 files changed, 31 insertions, 8 deletions
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
@@ -56,13 +56,6 @@ def setup_parser(subparser):
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",
action="store_true",
@@ -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]
+ )