summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorHarmen Stoppels <me@harmenstoppels.nl>2023-11-30 15:35:21 +0100
committerGitHub <noreply@github.com>2023-11-30 15:35:21 +0100
commit4c2734fe14be603c4d3028ad10c44fa3f72b02a2 (patch)
tree6cf94458fa0a529b4a7ea2b876fdbf1652f4d7ef /lib
parent34d791189da2ba941bb8d8e3b8be202f0d855e97 (diff)
downloadspack-4c2734fe14be603c4d3028ad10c44fa3f72b02a2.tar.gz
spack-4c2734fe14be603c4d3028ad10c44fa3f72b02a2.tar.bz2
spack-4c2734fe14be603c4d3028ad10c44fa3f72b02a2.tar.xz
spack-4c2734fe14be603c4d3028ad10c44fa3f72b02a2.zip
--scope: lazy defaults (#41353)
Diffstat (limited to 'lib')
-rw-r--r--lib/spack/spack/cmd/bootstrap.py5
-rw-r--r--lib/spack/spack/cmd/buildcache.py5
-rw-r--r--lib/spack/spack/cmd/common/arguments.py29
-rw-r--r--lib/spack/spack/cmd/compiler.py21
-rw-r--r--lib/spack/spack/cmd/compilers.py6
-rw-r--r--lib/spack/spack/cmd/config.py5
-rw-r--r--lib/spack/spack/cmd/external.py5
-rw-r--r--lib/spack/spack/cmd/mirror.py25
-rw-r--r--lib/spack/spack/cmd/repo.py15
9 files changed, 53 insertions, 63 deletions
diff --git a/lib/spack/spack/cmd/bootstrap.py b/lib/spack/spack/cmd/bootstrap.py
index 7c07625895..c9b20a07ad 100644
--- a/lib/spack/spack/cmd/bootstrap.py
+++ b/lib/spack/spack/cmd/bootstrap.py
@@ -69,10 +69,7 @@ SOURCE_METADATA = {
def _add_scope_option(parser):
parser.add_argument(
- "--scope",
- choices=arguments.ConfigScopeChoices(),
- metavar=spack.config.SCOPES_METAVAR,
- help="configuration scope to read/modify",
+ "--scope", action=arguments.ConfigScope, help="configuration scope to read/modify"
)
diff --git a/lib/spack/spack/cmd/buildcache.py b/lib/spack/spack/cmd/buildcache.py
index 7896b54373..05b7bd783a 100644
--- a/lib/spack/spack/cmd/buildcache.py
+++ b/lib/spack/spack/cmd/buildcache.py
@@ -184,9 +184,8 @@ def setup_parser(subparser: argparse.ArgumentParser):
# used to construct scope arguments below
check.add_argument(
"--scope",
- choices=arguments.ConfigScopeChoices(),
- metavar=spack.config.SCOPES_METAVAR,
- default=spack.config.default_modify_scope(),
+ action=arguments.ConfigScope,
+ default=lambda: spack.config.default_modify_scope(),
help="configuration scope containing mirrors to check",
)
check_spec_or_specfile = check.add_mutually_exclusive_group(required=True)
diff --git a/lib/spack/spack/cmd/common/arguments.py b/lib/spack/spack/cmd/common/arguments.py
index 79f844d0e2..883a7d31c2 100644
--- a/lib/spack/spack/cmd/common/arguments.py
+++ b/lib/spack/spack/cmd/common/arguments.py
@@ -124,14 +124,31 @@ class DeptypeAction(argparse.Action):
setattr(namespace, self.dest, deptype)
-class ConfigScopeChoices:
- """A lazy list of config scope values (values may change at runtime in tests)."""
+class ConfigScope(argparse.Action):
+ """Pick the currently configured config scopes."""
- def __contains__(self, item):
- return item in spack.config.scopes()
+ def __init__(self, *args, **kwargs) -> None:
+ kwargs.setdefault("metavar", spack.config.SCOPES_METAVAR)
+ super().__init__(*args, **kwargs)
- def __iter__(self):
- return iter(spack.config.scopes().keys())
+ @property
+ def default(self):
+ return self._default() if callable(self._default) else self._default
+
+ @default.setter
+ def default(self, value):
+ self._default = value
+
+ @property
+ def choices(self):
+ return spack.config.scopes().keys()
+
+ @choices.setter
+ def choices(self, value):
+ pass
+
+ def __call__(self, parser, namespace, values, option_string=None):
+ setattr(namespace, self.dest, values)
def _cdash_reporter(namespace):
diff --git a/lib/spack/spack/cmd/compiler.py b/lib/spack/spack/cmd/compiler.py
index 6ba6371087..6cfb73c3e8 100644
--- a/lib/spack/spack/cmd/compiler.py
+++ b/lib/spack/spack/cmd/compiler.py
@@ -46,9 +46,8 @@ def setup_parser(subparser):
find_parser.add_argument("add_paths", nargs=argparse.REMAINDER)
find_parser.add_argument(
"--scope",
- choices=arguments.ConfigScopeChoices(),
- metavar=spack.config.SCOPES_METAVAR,
- default=spack.config.default_modify_scope("compilers"),
+ action=arguments.ConfigScope,
+ default=lambda: spack.config.default_modify_scope("compilers"),
help="configuration scope to modify",
)
@@ -59,20 +58,15 @@ def setup_parser(subparser):
)
remove_parser.add_argument("compiler_spec")
remove_parser.add_argument(
- "--scope",
- choices=arguments.ConfigScopeChoices(),
- metavar=spack.config.SCOPES_METAVAR,
- default=None,
- help="configuration scope to modify",
+ "--scope", action=arguments.ConfigScope, default=None, help="configuration scope to modify"
)
# List
list_parser = sp.add_parser("list", help="list available compilers")
list_parser.add_argument(
"--scope",
- choices=arguments.ConfigScopeChoices(),
- metavar=spack.config.SCOPES_METAVAR,
- default=spack.config.default_list_scope(),
+ action=arguments.ConfigScope,
+ default=lambda: spack.config.default_list_scope(),
help="configuration scope to read from",
)
@@ -81,9 +75,8 @@ def setup_parser(subparser):
info_parser.add_argument("compiler_spec")
info_parser.add_argument(
"--scope",
- choices=arguments.ConfigScopeChoices(),
- metavar=spack.config.SCOPES_METAVAR,
- default=spack.config.default_list_scope(),
+ action=arguments.ConfigScope,
+ default=lambda: spack.config.default_list_scope(),
help="configuration scope to read from",
)
diff --git a/lib/spack/spack/cmd/compilers.py b/lib/spack/spack/cmd/compilers.py
index 25831da7fb..7bd8cd4f37 100644
--- a/lib/spack/spack/cmd/compilers.py
+++ b/lib/spack/spack/cmd/compilers.py
@@ -3,7 +3,6 @@
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
-import spack.config
from spack.cmd.common import arguments
from spack.cmd.compiler import compiler_list
@@ -14,10 +13,7 @@ level = "short"
def setup_parser(subparser):
subparser.add_argument(
- "--scope",
- choices=arguments.ConfigScopeChoices(),
- metavar=spack.config.SCOPES_METAVAR,
- help="configuration scope to read/modify",
+ "--scope", action=arguments.ConfigScope, help="configuration scope to read/modify"
)
diff --git a/lib/spack/spack/cmd/config.py b/lib/spack/spack/cmd/config.py
index c99f045876..e7d459ff77 100644
--- a/lib/spack/spack/cmd/config.py
+++ b/lib/spack/spack/cmd/config.py
@@ -28,10 +28,7 @@ level = "long"
def setup_parser(subparser):
# User can only choose one
subparser.add_argument(
- "--scope",
- choices=arguments.ConfigScopeChoices(),
- metavar=spack.config.SCOPES_METAVAR,
- help="configuration scope to read/modify",
+ "--scope", action=arguments.ConfigScope, help="configuration scope to read/modify"
)
sp = subparser.add_subparsers(metavar="SUBCOMMAND", dest="config_command")
diff --git a/lib/spack/spack/cmd/external.py b/lib/spack/spack/cmd/external.py
index c3e791fd6a..eb96dfad98 100644
--- a/lib/spack/spack/cmd/external.py
+++ b/lib/spack/spack/cmd/external.py
@@ -46,9 +46,8 @@ def setup_parser(subparser):
)
find_parser.add_argument(
"--scope",
- choices=arguments.ConfigScopeChoices(),
- metavar=spack.config.SCOPES_METAVAR,
- default=spack.config.default_modify_scope("packages"),
+ action=arguments.ConfigScope,
+ default=lambda: spack.config.default_modify_scope("packages"),
help="configuration scope to modify",
)
find_parser.add_argument(
diff --git a/lib/spack/spack/cmd/mirror.py b/lib/spack/spack/cmd/mirror.py
index a9c93618c7..91a3f27931 100644
--- a/lib/spack/spack/cmd/mirror.py
+++ b/lib/spack/spack/cmd/mirror.py
@@ -94,9 +94,8 @@ def setup_parser(subparser):
add_parser.add_argument("url", help="url of mirror directory from 'spack mirror create'")
add_parser.add_argument(
"--scope",
- choices=arguments.ConfigScopeChoices(),
- metavar=spack.config.SCOPES_METAVAR,
- default=spack.config.default_modify_scope(),
+ action=arguments.ConfigScope,
+ default=lambda: spack.config.default_modify_scope(),
help="configuration scope to modify",
)
add_parser.add_argument(
@@ -114,9 +113,8 @@ def setup_parser(subparser):
remove_parser.add_argument("name", help="mnemonic name for mirror", metavar="mirror")
remove_parser.add_argument(
"--scope",
- choices=arguments.ConfigScopeChoices(),
- metavar=spack.config.SCOPES_METAVAR,
- default=spack.config.default_modify_scope(),
+ action=arguments.ConfigScope,
+ default=lambda: spack.config.default_modify_scope(),
help="configuration scope to modify",
)
@@ -133,9 +131,8 @@ def setup_parser(subparser):
)
set_url_parser.add_argument(
"--scope",
- choices=arguments.ConfigScopeChoices(),
- metavar=spack.config.SCOPES_METAVAR,
- default=spack.config.default_modify_scope(),
+ action=arguments.ConfigScope,
+ default=lambda: spack.config.default_modify_scope(),
help="configuration scope to modify",
)
arguments.add_connection_args(set_url_parser, False)
@@ -162,9 +159,8 @@ def setup_parser(subparser):
set_parser.add_argument("--url", help="url of mirror directory from 'spack mirror create'")
set_parser.add_argument(
"--scope",
- choices=arguments.ConfigScopeChoices(),
- metavar=spack.config.SCOPES_METAVAR,
- default=spack.config.default_modify_scope(),
+ action=arguments.ConfigScope,
+ default=lambda: spack.config.default_modify_scope(),
help="configuration scope to modify",
)
arguments.add_connection_args(set_parser, False)
@@ -173,9 +169,8 @@ def setup_parser(subparser):
list_parser = sp.add_parser("list", help=mirror_list.__doc__)
list_parser.add_argument(
"--scope",
- choices=arguments.ConfigScopeChoices(),
- metavar=spack.config.SCOPES_METAVAR,
- default=spack.config.default_list_scope(),
+ action=arguments.ConfigScope,
+ default=lambda: spack.config.default_list_scope(),
help="configuration scope to read from",
)
diff --git a/lib/spack/spack/cmd/repo.py b/lib/spack/spack/cmd/repo.py
index a8bba6c2dc..71b6cbc0d4 100644
--- a/lib/spack/spack/cmd/repo.py
+++ b/lib/spack/spack/cmd/repo.py
@@ -43,9 +43,8 @@ def setup_parser(subparser):
list_parser = sp.add_parser("list", help=repo_list.__doc__)
list_parser.add_argument(
"--scope",
- choices=arguments.ConfigScopeChoices(),
- metavar=spack.config.SCOPES_METAVAR,
- default=spack.config.default_list_scope(),
+ action=arguments.ConfigScope,
+ default=lambda: spack.config.default_list_scope(),
help="configuration scope to read from",
)
@@ -54,9 +53,8 @@ def setup_parser(subparser):
add_parser.add_argument("path", help="path to a Spack package repository directory")
add_parser.add_argument(
"--scope",
- choices=arguments.ConfigScopeChoices(),
- metavar=spack.config.SCOPES_METAVAR,
- default=spack.config.default_modify_scope(),
+ action=arguments.ConfigScope,
+ default=lambda: spack.config.default_modify_scope(),
help="configuration scope to modify",
)
@@ -67,9 +65,8 @@ def setup_parser(subparser):
)
remove_parser.add_argument(
"--scope",
- choices=arguments.ConfigScopeChoices(),
- metavar=spack.config.SCOPES_METAVAR,
- default=spack.config.default_modify_scope(),
+ action=arguments.ConfigScope,
+ default=lambda: spack.config.default_modify_scope(),
help="configuration scope to modify",
)