MCPcopy Create free account
hub / github.com/ClangBuiltLinux/boot-utils / find_first_file

Function find_first_file

utils.py:76–106  ·  view source on GitHub ↗

Attempts to find the first option available in the list of files relative to a specified root folder. Parameters: relative_root (Path): A Path object containing the folder to search for files within. possible_files (list): A list of Paths t

(
    relative_root: Path,
    possible_files: Iterable[Path | str],
    required: bool = True,
)

Source from the content-addressed store, hash-verified

74 raise RuntimeError(msg)
75
76
77def find_first_file(
78 relative_root: Path,
79 possible_files: Iterable[Path | str],
80 required: bool = True,
81) -> Path:
82 """
83 Attempts to find the first option available in the list of files relative
84 to a specified root folder.
85
86 Parameters:
87 relative_root (Path): A Path object containing the folder to search for
88 files within.
89 possible_files (list): A list of Paths that may be within the relative
90 root folder. They will be automatically appended
91 to relative_root.
92 required (bool): Whether or not the file is required, which determines
93 if not finding the file is an error.
94 Returns:
95 The full path to the first file found in the list. If none could be
96 found, an Exception is raised.
97 """
98 for possible_file in possible_files:
99 if (full_path := relative_root.joinpath(possible_file)).exists():
100 return full_path
101 if required:
102 files_str = "', '".join([str(elem) for elem in possible_files])
103 msg = f"No files from list ('{files_str}') could be found within '{relative_root}'!"
104 raise FileNotFoundError(
105 msg,
106 )
107 return UNINIT_PATH
108
109

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected