Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assign priorities to configuration scopes #48420

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Prev Previous commit
Next Next commit
Simplify Configuration constructor
  • Loading branch information
alalazo committed Jan 6, 2025
commit ce6ecc6e19373003c5eaaca28463ed33cd700863
53 changes: 27 additions & 26 deletions lib/spack/spack/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import os
import re
import sys
from typing import Any, Callable, Dict, Generator, List, Optional, Sequence, Tuple, Union
from typing import Any, Callable, Dict, Generator, List, Optional, Tuple, Union

from llnl.util import filesystem, lang, tty

Expand Down Expand Up @@ -416,23 +416,8 @@ class Configuration:
# convert to typing.OrderedDict when we drop 3.6, or OrderedDict when we reach 3.9
scopes: lang.PriorityOrderedMapping[str, ConfigScope]

def __init__(self, *scopes: ScopeWithOptionalPriority) -> None:
"""Initialize a configuration with an initial list of scopes.

Args:
scopes: list of scopes to add to this
Configuration, ordered from lowest to highest precedence

"""
def __init__(self) -> None:
self.scopes = lang.PriorityOrderedMapping()
for item in scopes:
if isinstance(item, tuple):
priority, scope = item
else:
priority = ConfigScopePriority.CONFIG_FILES
scope = item

self.push_scope(scope, priority=priority)
self.format_updates: Dict[str, List[ConfigScope]] = collections.defaultdict(list)

def ensure_unwrapped(self) -> "Configuration":
Expand Down Expand Up @@ -821,11 +806,10 @@ def create() -> Configuration:
it. It is bundled inside a function so that configuration can be
initialized lazily.
"""
cfg = Configuration()

# first do the builtin, hardcoded defaults
builtin = InternalConfigScope("_builtin", CONFIG_DEFAULTS)
cfg.push_scope(builtin, priority=ConfigScopePriority.BUILTIN)
cfg = create_from(
(ConfigScopePriority.BUILTIN, InternalConfigScope("_builtin", CONFIG_DEFAULTS))
)

# Builtin paths to configuration files in Spack
configuration_paths = [
Expand Down Expand Up @@ -1433,7 +1417,7 @@ def use_configuration(
global CONFIG

# Normalize input and construct a Configuration object
configuration = _config_from(scopes_or_paths)
configuration = create_from(*scopes_or_paths)
CONFIG.clear_caches(), configuration.clear_caches()

saved_config, CONFIG = CONFIG, configuration
Expand All @@ -1460,11 +1444,28 @@ def _normalize_input(entry: Union[ScopeWithOptionalPriority, str]) -> ScopeWithP


@lang.memoized
def _config_from(
scopes_or_paths: Sequence[Union[ScopeWithOptionalPriority, str]]
) -> Configuration:
def create_from(*scopes_or_paths: Union[ScopeWithOptionalPriority, str]) -> Configuration:
"""Creates a configuration object from the scopes passed in input.

Args:
*scopes_or_paths: either a tuple of (priority, ConfigScope), or a ConfigScope, or a string
If priority is not given, it is assumed to be ConfigScopePriority.CONFIG_FILES. If a
string is given, a DirectoryConfigScope is created from it.

Examples:

>>> builtin_scope = InternalConfigScope("_builtin", {"config": {"build_jobs": 1}})
>>> cl_scope = InternalConfigScope("command_line", {"config": {"build_jobs": 10}})
>>> cfg = create_from(
... (ConfigScopePriority.COMMAND_LINE, cl_scope),
... (ConfigScopePriority.BUILTIN, builtin_scope)
... )
"""
scopes_with_priority = [_normalize_input(x) for x in scopes_or_paths]
return Configuration(*scopes_with_priority)
result = Configuration()
for priority, scope in scopes_with_priority:
result.push_scope(scope, priority=priority)
return result


def raw_github_gitlab_url(url: str) -> str:
Expand Down
10 changes: 5 additions & 5 deletions lib/spack/spack/test/build_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ def test_build_jobs_sequential_is_sequential():
spack.config.determine_number_of_jobs(
parallel=False,
max_cpus=8,
config=spack.config.Configuration(
config=spack.config.create_from(
spack.config.InternalConfigScope("command_line", {"config": {"build_jobs": 8}}),
spack.config.InternalConfigScope("defaults", {"config": {"build_jobs": 8}}),
),
Expand All @@ -560,7 +560,7 @@ def test_build_jobs_command_line_overrides():
spack.config.determine_number_of_jobs(
parallel=True,
max_cpus=1,
config=spack.config.Configuration(
config=spack.config.create_from(
spack.config.InternalConfigScope("command_line", {"config": {"build_jobs": 10}}),
spack.config.InternalConfigScope("defaults", {"config": {"build_jobs": 1}}),
),
Expand All @@ -571,7 +571,7 @@ def test_build_jobs_command_line_overrides():
spack.config.determine_number_of_jobs(
parallel=True,
max_cpus=100,
config=spack.config.Configuration(
config=spack.config.create_from(
spack.config.InternalConfigScope("command_line", {"config": {"build_jobs": 10}}),
spack.config.InternalConfigScope("defaults", {"config": {"build_jobs": 100}}),
),
Expand All @@ -585,7 +585,7 @@ def test_build_jobs_defaults():
spack.config.determine_number_of_jobs(
parallel=True,
max_cpus=10,
config=spack.config.Configuration(
config=spack.config.create_from(
spack.config.InternalConfigScope("defaults", {"config": {"build_jobs": 1}})
),
)
Expand All @@ -595,7 +595,7 @@ def test_build_jobs_defaults():
spack.config.determine_number_of_jobs(
parallel=True,
max_cpus=10,
config=spack.config.Configuration(
config=spack.config.create_from(
spack.config.InternalConfigScope("defaults", {"config": {"build_jobs": 100}})
),
)
Expand Down
5 changes: 1 addition & 4 deletions lib/spack/spack/test/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,10 +712,7 @@ def assert_marked(obj):


def test_internal_config_from_data():
config = spack.config.Configuration()

# add an internal config initialized from an inline dict
config.push_scope(
config = spack.config.create_from(
spack.config.InternalConfigScope(
"_builtin", {"config": {"verify_ssl": False, "build_jobs": 6}}
)
Expand Down