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 ```
This commit is contained in:
parent
ff38ff25cb
commit
d4e714bb2e
@ -55,13 +55,6 @@ def setup_parser(subparser):
|
|||||||
choices=formatters,
|
choices=formatters,
|
||||||
help="format to be used to print the output [default: name_only]",
|
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(
|
subparser.add_argument(
|
||||||
"-v",
|
"-v",
|
||||||
"--virtuals",
|
"--virtuals",
|
||||||
@ -71,6 +64,22 @@ def setup_parser(subparser):
|
|||||||
)
|
)
|
||||||
arguments.add_common_arguments(subparser, ["tags"])
|
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):
|
def filter_by_name(pkgs, args):
|
||||||
"""
|
"""
|
||||||
@ -320,6 +329,9 @@ def list(parser, args):
|
|||||||
with open(args.update, "w") as f:
|
with open(args.update, "w") as f:
|
||||||
formatter(sorted_packages, f)
|
formatter(sorted_packages, f)
|
||||||
|
|
||||||
|
elif args.count:
|
||||||
|
# just print the number of packages in the result
|
||||||
|
print(len(sorted_packages))
|
||||||
else:
|
else:
|
||||||
# Print to stdout
|
# print formatted package list
|
||||||
formatter(sorted_packages, sys.stdout)
|
formatter(sorted_packages, sys.stdout)
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
import sys
|
import sys
|
||||||
from textwrap import dedent
|
from textwrap import dedent
|
||||||
|
|
||||||
|
import spack.repo
|
||||||
from spack.main import SpackCommand
|
from spack.main import SpackCommand
|
||||||
|
|
||||||
list = SpackCommand("list")
|
list = SpackCommand("list")
|
||||||
@ -123,3 +124,13 @@ def test_list_tags(mock_packages):
|
|||||||
output = list("--tag", "tag3")
|
output = list("--tag", "tag3")
|
||||||
assert "mpich\n" not in output
|
assert "mpich\n" not in output
|
||||||
assert "mpich2" 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]
|
||||||
|
)
|
||||||
|
@ -1197,7 +1197,7 @@ _spack_license_update_copyright_year() {
|
|||||||
_spack_list() {
|
_spack_list() {
|
||||||
if $list_options
|
if $list_options
|
||||||
then
|
then
|
||||||
SPACK_COMPREPLY="-h --help -d --search-description --format --update -v --virtuals -t --tag"
|
SPACK_COMPREPLY="-h --help -d --search-description --format -v --virtuals -t --tag --count --update"
|
||||||
else
|
else
|
||||||
_all_packages
|
_all_packages
|
||||||
fi
|
fi
|
||||||
|
Loading…
Reference in New Issue
Block a user