diff options
author | Harmen Stoppels <me@harmenstoppels.nl> | 2024-08-26 12:49:58 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-26 12:49:58 +0200 |
commit | 02f329a8af859a7246298378c6ea5871674575a4 (patch) | |
tree | 40b5e79df6c28fa88db985eec28e5e5365a82a6c /lib | |
parent | 2de712b35f02b985d60af8e2b39c5d4e403d907d (diff) | |
download | spack-02f329a8af859a7246298378c6ea5871674575a4.tar.gz spack-02f329a8af859a7246298378c6ea5871674575a4.tar.bz2 spack-02f329a8af859a7246298378c6ea5871674575a4.tar.xz spack-02f329a8af859a7246298378c6ea5871674575a4.zip |
compilers: avoid redundant fs operations and cache (#46031)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/spack/llnl/util/lang.py | 27 | ||||
-rw-r--r-- | lib/spack/spack/compilers/__init__.py | 3 |
2 files changed, 17 insertions, 13 deletions
diff --git a/lib/spack/llnl/util/lang.py b/lib/spack/llnl/util/lang.py index 8449c4ca8c..1856620bb7 100644 --- a/lib/spack/llnl/util/lang.py +++ b/lib/spack/llnl/util/lang.py @@ -16,7 +16,7 @@ from datetime import datetime, timedelta from typing import Any, Callable, Iterable, List, Tuple # Ignore emacs backups when listing modules -ignore_modules = [r"^\.#", "~$"] +ignore_modules = r"^\.#|~$" def index_by(objects, *funcs): @@ -164,19 +164,22 @@ def list_modules(directory, **kwargs): order.""" list_directories = kwargs.setdefault("directories", True) - for name in os.listdir(directory): - if name == "__init__.py": - continue + ignore = re.compile(ignore_modules) + + with os.scandir(directory) as it: + for entry in it: + if entry.name == "__init__.py" or entry.name == "__pycache__": + continue - path = os.path.join(directory, name) - if list_directories and os.path.isdir(path): - init_py = os.path.join(path, "__init__.py") - if os.path.isfile(init_py): - yield name + if ( + list_directories + and entry.is_dir() + and os.path.isfile(os.path.join(entry.path, "__init__.py")) + ): + yield entry.name - elif name.endswith(".py"): - if not any(re.search(pattern, name) for pattern in ignore_modules): - yield re.sub(".py$", "", name) + elif entry.name.endswith(".py") and entry.is_file() and not ignore.search(entry.name): + yield entry.name[:-3] # strip .py def decorator_with_or_without_args(decorator): diff --git a/lib/spack/spack/compilers/__init__.py b/lib/spack/spack/compilers/__init__.py index b44fa92ab2..a03de45d7d 100644 --- a/lib/spack/spack/compilers/__init__.py +++ b/lib/spack/spack/compilers/__init__.py @@ -394,8 +394,9 @@ def all_compiler_names() -> List[str]: return [replace_apple_clang(name) for name in all_compiler_module_names()] +@llnl.util.lang.memoized def all_compiler_module_names() -> List[str]: - return [name for name in llnl.util.lang.list_modules(spack.paths.compilers_path)] + return list(llnl.util.lang.list_modules(spack.paths.compilers_path)) @_auto_compiler_spec |