From 1e00ad5730aea5f851f4ca9d01b9b8c03fda974b Mon Sep 17 00:00:00 2001 From: Hoang Hong Quan Date: Mon, 29 Jul 2024 15:13:29 +0700 Subject: [PATCH] Update search functionality to locate paths based on file extension or name pattern --- Scripts/efi_builder.py | 6 +++--- Scripts/gathering_files.py | 18 ++++++++---------- Scripts/utils.py | 33 +++++++++++++++------------------ 3 files changed, 26 insertions(+), 31 deletions(-) diff --git a/Scripts/efi_builder.py b/Scripts/efi_builder.py index 5b7ab59..46f96ff 100755 --- a/Scripts/efi_builder.py +++ b/Scripts/efi_builder.py @@ -335,21 +335,21 @@ class builder: files_to_remove = [] drivers_directory = os.path.join(efi_directory, "EFI", "OC", "Drivers") - driver_list = self.utils.recursively_search(drivers_directory, ".efi") + driver_list = self.utils.find_matching_paths(drivers_directory, ".efi") driver_loaded = [kext.get("Path") for kext in config.get("UEFI").get("Drivers")] for driver_path 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.recursively_search(kexts_directory, ".kext") + kext_list = self.utils.find_matching_paths(kexts_directory, ".kext") kext_loaded = [kext.get("BundlePath") for kext in config.get("Kernel").get("Add")] for kext_path in kext_list: if not 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.recursively_search(tools_directory, ".efi") + tool_list = self.utils.find_matching_paths(tools_directory, ".efi") tool_loaded = [tool.get("Path") for tool in config.get("Misc").get("Tools")] for tool_path in tool_list: if not tool_path in tool_loaded: diff --git a/Scripts/gathering_files.py b/Scripts/gathering_files.py index 31e99e7..046bb56 100755 --- a/Scripts/gathering_files.py +++ b/Scripts/gathering_files.py @@ -219,16 +219,14 @@ class gatheringFiles: return None def move_bootloader_kexts_to_product_directory(self, product_name): - product_directory = os.path.join(self.ock_files_dir, product_name) - if not os.path.exists(self.temporary_dir): raise FileNotFoundError(f"The directory {self.temporary_dir} does not exist.") if not "OpenCore" in product_name: - kext_paths = self.utils.recursively_search(self.temporary_dir, ".kext") + kext_paths = self.utils.find_matching_paths(os.path.join(self.temporary_dir, product_name), ".kext") for kext_path in kext_paths: - source_kext_path = os.path.join(self.temporary_dir, kext_path) - destination_kext_path = os.path.join(product_directory, os.path.basename(kext_path)) + 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)) if "Contents" in kext_path or "Debug".lower() in kext_path.lower(): continue @@ -237,17 +235,16 @@ class gatheringFiles: else: source_bootloader_path = os.path.join(self.temporary_dir, product_name, "X64", "EFI") if os.path.exists(source_bootloader_path): - destination_efi_path = os.path.join(product_directory, os.path.basename(source_bootloader_path)) + destination_efi_path = os.path.join(self.ock_files_dir, product_name, os.path.basename(source_bootloader_path)) shutil.move(source_bootloader_path, destination_efi_path) 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.recursively_search(self.temporary_dir, product_name, matching_file_name_pattern="macserial") + macserial_paths = self.utils.find_matching_paths(os.path.join(self.temporary_dir, product_name), target_name_pattern="macserial") if macserial_paths: for macserial_path in macserial_paths: - file_name = os.path.basename(macserial_path) - source_macserial_path = os.path.join(self.temporary_dir, macserial_path) - destination_macserial_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), file_name) + 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) if os.name != "nt": subprocess.run(["chmod", "+x", destination_macserial_path]) @@ -285,6 +282,7 @@ class gatheringFiles: zip_path = os.path.join(self.temporary_dir, product_data.get("product_name")) + ".zip" self.fetcher.download_and_save_file(product_data.get("url"), zip_path) + if self.move_bootloader_kexts_to_product_directory(product_data.get("product_name")): if product_index is None: download_history["versions"].append({ diff --git a/Scripts/utils.py b/Scripts/utils.py index b5c8bff..9a2e079 100755 --- a/Scripts/utils.py +++ b/Scripts/utils.py @@ -6,6 +6,7 @@ import re import binascii import subprocess import pathlib +import zipfile class Utils: def __init__(self, script_name = "OpCore Simplify"): @@ -58,32 +59,28 @@ class Utils: return {} - def recursively_search(self, directory, matching_file_extension=None, matching_file_name_pattern=None, parent_directory=""): - matching_paths = [] + def find_matching_paths(self, directory, target_file_extension=None, target_name_pattern=None): + found_paths = [] if not os.path.exists(directory): print(f"Error: The directory {directory} does not exist.") - return matching_paths - - for child_directory in os.listdir(directory): - if "MACOSX" in child_directory or "dSYM" in child_directory: + return found_paths + + for root, dirs, files in os.walk(directory): + if "MACOSX" in root: continue - file_name = os.path.splitext(os.path.basename(child_directory))[0] - file_extension = os.path.splitext(os.path.basename(child_directory))[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 (matching_file_extension is not None and matching_file_extension == file_extension) or (matching_file_name_pattern is not None and matching_file_name_pattern in file_name): - path = os.path.join(parent_directory, child_directory) - full_path = os.path.join(directory, child_directory) + for file in files: + file_name, file_extension = os.path.splitext(file) - if not os.path.isdir(full_path) or not os.path.exists(os.path.join(full_path, file_name + file_extension)): - matching_paths.append(path) + 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:]) - child_directory = os.path.join(directory, child_directory) - if os.path.isdir(child_directory): - matching_paths.extend(self.recursively_search(child_directory, matching_file_extension, matching_file_name_pattern, os.path.join(parent_directory, os.path.basename(child_directory)))) - - return matching_paths + return found_paths def sort_dict_by_key(self, input_dict, sort_key): return dict(sorted(input_dict.items(), key=lambda item: item[1].get(sort_key, "")))