From 0fee2c234ef1eda5483a2da5c6bd679c48d7ab26 Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Tue, 27 Aug 2024 15:55:44 +0200 Subject: config.py: tell don't ask (#46040) --- lib/spack/spack/config.py | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) (limited to 'lib') diff --git a/lib/spack/spack/config.py b/lib/spack/spack/config.py index 765954c8f2..82962d36fb 100644 --- a/lib/spack/spack/config.py +++ b/lib/spack/spack/config.py @@ -1090,7 +1090,7 @@ def validate( def read_config_file( - filename: str, schema: Optional[YamlConfigDict] = None + path: str, schema: Optional[YamlConfigDict] = None ) -> Optional[YamlConfigDict]: """Read a YAML configuration file. @@ -1100,21 +1100,9 @@ def read_config_file( # to preserve flexibility in calling convention (don't need to provide # schema when it's not necessary) while allowing us to validate against a # known schema when the top-level key could be incorrect. - - if not os.path.exists(filename): - # Ignore nonexistent files. - tty.debug(f"Skipping nonexistent config path {filename}", level=3) - return None - - elif not os.path.isfile(filename): - raise ConfigFileError(f"Invalid configuration. {filename} exists but is not a file.") - - elif not os.access(filename, os.R_OK): - raise ConfigFileError(f"Config file is not readable: {filename}") - try: - tty.debug(f"Reading config from file {filename}") - with open(filename) as f: + with open(path) as f: + tty.debug(f"Reading config from file {path}") data = syaml.load_config(f) if data: @@ -1125,15 +1113,20 @@ def read_config_file( return data - except StopIteration: - raise ConfigFileError(f"Config file is empty or is not a valid YAML dict: {filename}") + except FileNotFoundError: + # Ignore nonexistent files. + tty.debug(f"Skipping nonexistent config path {path}", level=3) + return None + + except OSError as e: + raise ConfigFileError(f"Path is not a file or is not readable: {path}: {str(e)}") from e + + except StopIteration as e: + raise ConfigFileError(f"Config file is empty or is not a valid YAML dict: {path}") from e except syaml.SpackYAMLError as e: raise ConfigFileError(str(e)) from e - except OSError as e: - raise ConfigFileError(f"Error reading configuration file {filename}: {str(e)}") from e - def _override(string: str) -> bool: """Test if a spack YAML string is an override. -- cgit v1.2.3-70-g09d2