Refactor directory traversal function

This commit is contained in:
Hoang Hong Quan
2024-09-28 06:05:46 +07:00
parent b7a4a5ab6a
commit d61189171a
3 changed files with 32 additions and 27 deletions

View File

@@ -340,23 +340,23 @@ class builder:
files_to_remove = []
drivers_directory = os.path.join(efi_directory, "EFI", "OC", "Drivers")
driver_list = self.utils.find_matching_paths(drivers_directory, ".efi")
driver_list = self.utils.find_matching_paths(drivers_directory, extension_filter=".efi")
driver_loaded = [kext.get("Path") for kext in config.get("UEFI").get("Drivers")]
for driver_path in driver_list:
for driver_path, type in driver_list:
if not driver_path in driver_loaded:
files_to_remove.append(os.path.join(drivers_directory, driver_path))
kexts_directory = os.path.join(efi_directory, "EFI", "OC", "Kexts")
kext_list = self.utils.find_matching_paths(kexts_directory, ".kext")
kext_list = self.utils.find_matching_paths(kexts_directory, extension_filter=".kext")
kext_loaded = [os.path.basename(kext.get("BundlePath")) for kext in config.get("Kernel").get("Add")]
for kext_path in kext_list:
for kext_path, type in kext_list:
if not os.path.basename(kext_path) in kext_loaded:
files_to_remove.append(os.path.join(kexts_directory, kext_path))
tools_directory = os.path.join(efi_directory, "EFI", "OC", "Tools")
tool_list = self.utils.find_matching_paths(tools_directory, ".efi")
tool_list = self.utils.find_matching_paths(tools_directory, extension_filter=".efi")
tool_loaded = [tool.get("Path") for tool in config.get("Misc").get("Tools")]
for tool_path in tool_list:
for tool_path, type in tool_list:
if not tool_path in tool_loaded:
files_to_remove.append(os.path.join(tools_directory, tool_path))

View File

@@ -70,8 +70,8 @@ class gatheringFiles:
raise FileNotFoundError("The directory {} does not exist.".format(self.temporary_dir))
if not "OpenCore" in product_name:
kext_paths = self.utils.find_matching_paths(os.path.join(self.temporary_dir, product_name), ".kext")
for kext_path in kext_paths:
kext_paths = self.utils.find_matching_paths(os.path.join(self.temporary_dir, product_name), extension_filter=".kext")
for kext_path, type in kext_paths:
source_kext_path = os.path.join(self.temporary_dir, product_name, kext_path)
destination_kext_path = os.path.join(self.ock_files_dir, product_name, os.path.basename(kext_path))
@@ -87,9 +87,9 @@ class gatheringFiles:
source_config_path = os.path.join(os.path.dirname(os.path.dirname(source_bootloader_path)), "Docs", "Sample.plist")
destination_config_path = os.path.join(destination_efi_path, "OC", "config.plist")
shutil.move(source_config_path, destination_config_path)
macserial_paths = self.utils.find_matching_paths(os.path.join(self.temporary_dir, product_name), target_name_pattern="macserial")
macserial_paths = self.utils.find_matching_paths(os.path.join(self.temporary_dir, product_name), name_filter="macserial", type_filter="file")
if macserial_paths:
for macserial_path in macserial_paths:
for macserial_path, type in macserial_paths:
source_macserial_path = os.path.join(self.temporary_dir, product_name, macserial_path)
destination_macserial_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), os.path.basename(macserial_path))
shutil.move(source_macserial_path, destination_macserial_path)

View File

@@ -59,28 +59,33 @@ class Utils:
return {}
def find_matching_paths(self, directory, target_file_extension=None, target_name_pattern=None):
def find_matching_paths(self, root_path, extension_filter=None, name_filter=None, type_filter=None):
def is_valid_item(name):
if name.startswith("."):
return False
if extension_filter and not name.lower().endswith(extension_filter.lower()):
return False
if name_filter and name_filter not in name:
return False
return True
found_paths = []
if not os.path.exists(directory):
print("Error: The directory {} does not exist.".format(directory))
return found_paths
for root, dirs, files in os.walk(directory):
if "MACOSX" in root:
continue
for root, dirs, files in os.walk(root_path):
relative_root = root.replace(root_path, "")[1:]
if target_file_extension and root.endswith(target_file_extension) or target_name_pattern and target_name_pattern in root:
if not os.path.exists(os.path.join(root, os.path.basename(root))):
found_paths.append(root.replace(directory, "")[1:])
if type_filter in (None, "dir"):
for d in dirs:
if is_valid_item(d):
found_paths.append((os.path.join(relative_root, d), "dir"))
for file in files:
file_name, file_extension = os.path.splitext(file)
if type_filter in (None, "file"):
for file in files:
if is_valid_item(file):
found_paths.append((os.path.join(relative_root, file), "file"))
if target_file_extension and file_extension.endswith(target_file_extension) or target_name_pattern and target_name_pattern in file_name:
found_paths.append(os.path.join(root, file).replace(directory, "")[1:])
return found_paths
return sorted(found_paths, key=lambda path: path[0])
def sort_dict_by_key(self, input_dict, sort_key):
return dict(sorted(input_dict.items(), key=lambda item: item[1].get(sort_key, "")))