Update search functionality to locate paths based on file extension or name pattern

This commit is contained in:
Hoang Hong Quan
2024-07-29 15:13:29 +07:00
parent 12171978d8
commit 1e00ad5730
3 changed files with 26 additions and 31 deletions
+3 -3
View File
@@ -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:
+8 -10
View File
@@ -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({
+15 -18
View File
@@ -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, "")))