Simplify a few methods in environments (#49682)

Return a single scope from environment.env_config_scope
Return a single scope from environment_path_scope

---------

Signed-off-by: Massimiliano Culpo <massimiliano.culpo@gmail.com>
This commit is contained in:
Massimiliano Culpo 2025-04-03 18:19:04 +02:00 committed by GitHub
parent 63b437ddf9
commit b1ac661ba8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 43 deletions

View File

@ -566,7 +566,7 @@
display_specs, display_specs,
environment_dir_from_name, environment_dir_from_name,
environment_from_name_or_dir, environment_from_name_or_dir,
environment_path_scopes, environment_path_scope,
exists, exists,
initialize_environment_dir, initialize_environment_dir,
installed_specs, installed_specs,
@ -603,7 +603,7 @@
"display_specs", "display_specs",
"environment_dir_from_name", "environment_dir_from_name",
"environment_from_name_or_dir", "environment_from_name_or_dir",
"environment_path_scopes", "environment_path_scope",
"exists", "exists",
"initialize_environment_dir", "initialize_environment_dir",
"installed_specs", "installed_specs",

View File

@ -97,16 +97,15 @@ def environment_name(path: Union[str, pathlib.Path]) -> str:
return path_str return path_str
def ensure_no_disallowed_env_config_mods(scopes: List[spack.config.ConfigScope]) -> None: def ensure_no_disallowed_env_config_mods(scope: spack.config.ConfigScope) -> None:
for scope in scopes: config = scope.get_section("config")
config = scope.get_section("config") if config and "environments_root" in config["config"]:
if config and "environments_root" in config["config"]: raise SpackEnvironmentError(
raise SpackEnvironmentError( "Spack environments are prohibited from modifying 'config:environments_root' "
"Spack environments are prohibited from modifying 'config:environments_root' " "because it can make the definition of the environment ill-posed. Please "
"because it can make the definition of the environment ill-posed. Please " "remove from your environment and place it in a permanent scope such as "
"remove from your environment and place it in a permanent scope such as " "defaults, system, site, etc."
"defaults, system, site, etc." )
)
def default_manifest_yaml(): def default_manifest_yaml():
@ -2717,9 +2716,9 @@ def __init__(self, manifest_dir: Union[pathlib.Path, str], name: Optional[str] =
self.scope_name = f"env:{self.name}" self.scope_name = f"env:{self.name}"
self.config_stage_dir = os.path.join(env_subdir_path(manifest_dir), "config") self.config_stage_dir = os.path.join(env_subdir_path(manifest_dir), "config")
#: Configuration scopes associated with this environment. Note that these are not #: Configuration scope associated with this environment. Note that this is not
#: invalidated by a re-read of the manifest file. #: invalidated by a re-read of the manifest file.
self._config_scopes: Optional[List[spack.config.ConfigScope]] = None self._env_config_scope: Optional[spack.config.ConfigScope] = None
if not self.manifest_file.exists(): if not self.manifest_file.exists():
msg = f"cannot find '{manifest_name}' in {self.manifest_dir}" msg = f"cannot find '{manifest_name}' in {self.manifest_dir}"
@ -2957,33 +2956,27 @@ def __str__(self):
return str(self.manifest_file) return str(self.manifest_file)
@property @property
def env_config_scopes(self) -> List[spack.config.ConfigScope]: def env_config_scope(self) -> spack.config.ConfigScope:
"""A list of all configuration scopes for the environment manifest. On the first call this """The configuration scope for the environment manifest"""
instantiates all the scopes, on subsequent calls it returns the cached list.""" if self._env_config_scope is None:
if self._config_scopes is not None: self._env_config_scope = spack.config.SingleFileScope(
return self._config_scopes
scopes: List[spack.config.ConfigScope] = [
spack.config.SingleFileScope(
self.scope_name, self.scope_name,
str(self.manifest_file), str(self.manifest_file),
spack.schema.env.schema, spack.schema.env.schema,
yaml_path=[TOP_LEVEL_KEY], yaml_path=[TOP_LEVEL_KEY],
) )
] ensure_no_disallowed_env_config_mods(self._env_config_scope)
ensure_no_disallowed_env_config_mods(scopes) return self._env_config_scope
self._config_scopes = scopes
return scopes
def prepare_config_scope(self) -> None: def prepare_config_scope(self) -> None:
"""Add the manifest's scopes to the global configuration search path.""" """Add the manifest's scope to the global configuration search path."""
for scope in self.env_config_scopes: spack.config.CONFIG.push_scope(
spack.config.CONFIG.push_scope(scope, priority=ConfigScopePriority.ENVIRONMENT) self.env_config_scope, priority=ConfigScopePriority.ENVIRONMENT
)
def deactivate_config_scope(self) -> None: def deactivate_config_scope(self) -> None:
"""Remove any of the manifest's scopes from the global config path.""" """Remove the manifest's scope from the global config path."""
for scope in self.env_config_scopes: spack.config.CONFIG.remove_scope(self.env_config_scope.name)
spack.config.CONFIG.remove_scope(scope.name)
@contextlib.contextmanager @contextlib.contextmanager
def use_config(self): def use_config(self):
@ -2994,8 +2987,8 @@ def use_config(self):
self.deactivate_config_scope() self.deactivate_config_scope()
def environment_path_scopes(name: str, path: str) -> Optional[List[spack.config.ConfigScope]]: def environment_path_scope(name: str, path: str) -> Optional[spack.config.ConfigScope]:
"""Retrieve the suitably named environment path scopes """Retrieve the suitably named environment path scope
Arguments: Arguments:
name: configuration scope name name: configuration scope name
@ -3010,11 +3003,9 @@ def environment_path_scopes(name: str, path: str) -> Optional[List[spack.config.
else: else:
return None return None
for scope in manifest.env_config_scopes: manifest.env_config_scope.name = f"{name}:{manifest.env_config_scope.name}"
scope.name = f"{name}:{scope.name}" manifest.env_config_scope.writable = False
scope.writable = False return manifest.env_config_scope
return manifest.env_config_scopes
class SpackEnvironmentError(spack.error.SpackError): class SpackEnvironmentError(spack.error.SpackError):

View File

@ -871,8 +871,8 @@ def add_command_line_scopes(
""" """
for i, path in enumerate(command_line_scopes): for i, path in enumerate(command_line_scopes):
name = f"cmd_scope_{i}" name = f"cmd_scope_{i}"
scopes = ev.environment_path_scopes(name, path) scope = ev.environment_path_scope(name, path)
if scopes is None: if scope is None:
if os.path.isdir(path): # directory with config files if os.path.isdir(path): # directory with config files
cfg.push_scope( cfg.push_scope(
spack.config.DirectoryConfigScope(name, path, writable=False), spack.config.DirectoryConfigScope(name, path, writable=False),
@ -885,8 +885,7 @@ def add_command_line_scopes(
else: else:
raise spack.error.ConfigError(f"Invalid configuration scope: {path}") raise spack.error.ConfigError(f"Invalid configuration scope: {path}")
for scope in scopes: cfg.push_scope(scope, priority=ConfigScopePriority.CUSTOM)
cfg.push_scope(scope, priority=ConfigScopePriority.CUSTOM)
def _main(argv=None): def _main(argv=None):