From 4d979b067687cec19cb96a2ae2513adab5edeb8e Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Mon, 11 Nov 2024 12:26:48 +0100 Subject: [PATCH] simplify api: recursive="heuristic" by default --- lib/spack/llnl/util/filesystem.py | 34 ++++++++++++++----------------- lib/spack/spack/spec.py | 6 ++---- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/lib/spack/llnl/util/filesystem.py b/lib/spack/llnl/util/filesystem.py index 98953c5a95f..f48097d32e2 100644 --- a/lib/spack/llnl/util/filesystem.py +++ b/lib/spack/llnl/util/filesystem.py @@ -2108,9 +2108,8 @@ def add_macro(self, macro): def find_headers( headers: Union[List[str], str], root: str, - recursive: bool = False, + recursive: Union[bool, str] = "heuristic", *, - heuristic: bool = True, max_depth: Optional[int] = None, ) -> HeaderList: """Returns an iterable object containing a list of full paths to @@ -2130,10 +2129,8 @@ def find_headers( Parameters: headers: Header name(s) to search for root: The root directory to start searching from - recursive: if False (default) search only root folder, if True recurse from the root. Note - that recursive search does not imply exhaustive search if heuristic is True. - heuristic: if True (default), use a non-exhaustive, faster search. Has no effect - if recursive is False. + recursive: if False search only root folder, if True recurse from the root. Defaults to + "heuristic", which uses a non-exhaustive, faster search. max_depth: if set, don't search below this depth. Cannot be set if recursive is False. Returns: @@ -2147,6 +2144,9 @@ def find_headers( f"first argument [got {type(headers)} instead]" ) + if recursive is False and max_depth is not None: + raise ValueError(f"max_depth ({max_depth}) cannot be set if recursive is False") + # Construct the right suffix for the headers suffixes = [ # C @@ -2167,8 +2167,8 @@ def find_headers( # List of headers we are searching with suffixes headers = [f"{header}.{suffix}" for header in headers for suffix in suffixes] - if not recursive or not heuristic: - return HeaderList(find(root, headers, recursive=recursive)) + if isinstance(recursive, bool): + return HeaderList(find(root, headers, recursive=recursive, max_depth=max_depth)) # The heuristic here is simpler than the one for libraries: restrict search to /include # (if root isn't an include directory itself) and limit search depth so that headers are found @@ -2351,9 +2351,9 @@ def find_libraries( libraries: Union[List[str], str], root: str, shared: bool = True, - recursive: bool = False, + recursive: Union[bool, str] = "heuristic", runtime: bool = True, - heuristic: bool = True, + *, max_depth: Optional[int] = None, ) -> LibraryList: """Find libraries in the specified root directory. @@ -2373,12 +2373,10 @@ def find_libraries( libraries: library name(s) to search for root: the root directory to start searching from shared: if True searches for shared libraries, otherwise for static. Defaults to True. - recursive: if False (default) search only root folder, if True recurse from the root. Note - that recursive search does not imply exhaustive search if heuristic is True. + recursive: if False search only root folder, if True recurse from the root. Defaults to + "heuristic", which uses a non-exhaustive, faster search. runtime: Windows only option, no-op elsewhere. If True (default), search for runtime shared libs (.DLL), otherwise, search for .Lib files. If shared is False, this has no meaning. - heuristic: if True (default), use a non-exhaustive, faster search. Has no effect if - recursive is False. max_depth: if set, don't search below this depth. Cannot be set if recursive is False. Returns: @@ -2393,7 +2391,7 @@ def find_libraries( f"first argument [got {type(libraries)} instead]" ) - if not recursive and max_depth is not None: + if recursive is False and max_depth is not None: raise ValueError(f"max_depth ({max_depth}) cannot be set if recursive is False") if sys.platform == "win32": @@ -2419,10 +2417,8 @@ def find_libraries( # List of libraries we are searching with suffixes libraries = [f"{lib}.{suffix}" for lib in libraries for suffix in suffixes] - if not recursive: - return LibraryList(find(root, libraries, recursive=False)) - elif not heuristic: - return LibraryList(find(root, libraries, recursive=True, max_depth=max_depth)) + if isinstance(recursive, bool): + return LibraryList(find(root, libraries, recursive=recursive, max_depth=max_depth)) # Heuristic search: a form of non-exhaustive iterative deepening, in order to return early if # libraries are found in their usual locations. This is the default behavior for recursive diff --git a/lib/spack/spack/spec.py b/lib/spack/spack/spec.py index acba4fd94f1..fa055692ade 100644 --- a/lib/spack/spack/spec.py +++ b/lib/spack/spack/spec.py @@ -1109,7 +1109,7 @@ def _headers_default_handler(spec: "Spec"): NoHeadersError: If no headers are found """ home = getattr(spec.package, "home") - headers = fs.find_headers("*", root=home, recursive=True, heuristic=True) + headers = fs.find_headers("*", root=home) if headers: return headers @@ -1159,9 +1159,7 @@ def _libs_default_handler(spec: "Spec"): for shared in search_shared: # Since we are searching for link libraries, on Windows search only for # ".Lib" extensions by default as those represent import libraries for implicit links. - libs = fs.find_libraries( - name, home, shared=shared, recursive=True, runtime=False, heuristic=True - ) + libs = fs.find_libraries(name, home, shared=shared, runtime=False) if libs: return libs