detection/common.py: catch is_file() inside loop (#50042)

This commit is contained in:
Harmen Stoppels 2025-04-16 09:40:14 +02:00 committed by GitHub
parent 8ac5398576
commit 1dc9bac745
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -20,7 +20,7 @@
import sys import sys
from typing import Dict, List, Optional, Set, Tuple, Union from typing import Dict, List, Optional, Set, Tuple, Union
import llnl.util.tty from llnl.util import tty
import spack.config import spack.config
import spack.error import spack.error
@ -93,14 +93,13 @@ def _spec_is_valid(spec: spack.spec.Spec) -> bool:
except spack.error.SpackError: except spack.error.SpackError:
# It is assumed here that we can at least extract the package name from the spec so we # It is assumed here that we can at least extract the package name from the spec so we
# can look up the implementation of determine_spec_details # can look up the implementation of determine_spec_details
msg = f"Constructed spec for {spec.name} does not have a string representation" tty.warn(f"Constructed spec for {spec.name} does not have a string representation")
llnl.util.tty.warn(msg)
return False return False
try: try:
spack.spec.Spec(str(spec)) spack.spec.Spec(str(spec))
except spack.error.SpackError: except spack.error.SpackError:
llnl.util.tty.warn( tty.warn(
"Constructed spec has a string representation but the string" "Constructed spec has a string representation but the string"
" representation does not evaluate to a valid spec: {0}".format(str(spec)) " representation does not evaluate to a valid spec: {0}".format(str(spec))
) )
@ -109,20 +108,24 @@ def _spec_is_valid(spec: spack.spec.Spec) -> bool:
return True return True
def path_to_dict(search_paths: List[str]): def path_to_dict(search_paths: List[str]) -> Dict[str, str]:
"""Return dictionary[fullpath]: basename from list of paths""" """Return dictionary[fullpath]: basename from list of paths"""
path_to_lib = {} path_to_lib: Dict[str, str] = {}
# Reverse order of search directories so that a lib in the first # Reverse order of search directories so that a lib in the first
# entry overrides later entries # entry overrides later entries
for search_path in reversed(search_paths): for search_path in reversed(search_paths):
try: try:
with os.scandir(search_path) as entries: dir_iter = os.scandir(search_path)
path_to_lib.update(
{entry.path: entry.name for entry in entries if entry.is_file()}
)
except OSError as e: except OSError as e:
msg = f"cannot scan '{search_path}' for external software: {str(e)}" tty.debug(f"cannot scan '{search_path}' for external software: {e}")
llnl.util.tty.debug(msg) continue
with dir_iter as entries:
for entry in entries:
try:
if entry.is_file():
path_to_lib[entry.path] = entry.name
except OSError as e:
tty.debug(f"cannot scan '{search_path}' for external software: {e}")
return path_to_lib return path_to_lib