Skip to content

Commit

Permalink
--scope: lazy defaults (spack#41353)
Browse files Browse the repository at this point in the history
  • Loading branch information
haampie authored Nov 30, 2023
1 parent 34d7911 commit 4c2734f
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 63 deletions.
5 changes: 1 addition & 4 deletions lib/spack/spack/cmd/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@

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"
)


Expand Down
5 changes: 2 additions & 3 deletions lib/spack/spack/cmd/buildcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 23 additions & 6 deletions lib/spack/spack/cmd/common/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,31 @@ def __call__(self, parser, namespace, values, option_string=None):
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):
Expand Down
21 changes: 7 additions & 14 deletions lib/spack/spack/cmd/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)

Expand All @@ -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",
)

Expand All @@ -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",
)

Expand Down
6 changes: 1 addition & 5 deletions lib/spack/spack/cmd/compilers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -14,10 +13,7 @@

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"
)


Expand Down
5 changes: 1 addition & 4 deletions lib/spack/spack/cmd/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@
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")
Expand Down
5 changes: 2 additions & 3 deletions lib/spack/spack/cmd/external.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
25 changes: 10 additions & 15 deletions lib/spack/spack/cmd/mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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",
)

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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",
)

Expand Down
15 changes: 6 additions & 9 deletions lib/spack/spack/cmd/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)

Expand All @@ -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",
)

Expand All @@ -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",
)

Expand Down

0 comments on commit 4c2734f

Please sign in to comment.