Refactor folder creation function

This commit is contained in:
Hoang Hong Quan
2024-09-13 17:41:54 +07:00
parent 3288145189
commit 2540798def
5 changed files with 16 additions and 18 deletions

View File

@@ -338,12 +338,10 @@ class ACPIGuru:
return None
def check_acpi_directory(self, acpi_directory):
if acpi_directory:
if not os.path.isdir(acpi_directory):
self.utils.mkdirs(acpi_directory)
return acpi_directory
else:
return acpi_directory
self.utils.create_folder(acpi_directory, remove_content=True)
if os.path.exists(acpi_directory):
return acpi_directory
return None
def write_ssdt(self, ssdt_name, ssdt_content, compile=True):

View File

@@ -379,7 +379,7 @@ class builder:
def build_efi(self, hardware, macos_version):
efi_directory = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), "Results")
self.utils.mkdirs(efi_directory)
self.utils.create_folder(efi_directory, remove_content=True)
if not os.path.exists(self.kext.ock_files_dir):
raise Exception("Directory '{}' does not exist.".format(self.kext.ock_files_dir))

View File

@@ -278,7 +278,7 @@ class gatheringFiles:
ock_data = self.get_bootloader_kexts_data()
self.utils.mkdirs(self.temporary_dir)
self.utils.create_folder(self.temporary_dir)
for product_data in ock_data:
@@ -287,7 +287,7 @@ class gatheringFiles:
continue
asset_dir = os.path.join(self.ock_files_dir, product_data.get("product_name"))
self.utils.mkdirs(asset_dir)
self.utils.create_folder(asset_dir, remove_content=True)
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)

View File

@@ -85,14 +85,14 @@ class Utils:
def sort_dict_by_key(self, input_dict, sort_key):
return dict(sorted(input_dict.items(), key=lambda item: item[1].get(sort_key, "")))
def mkdirs(self, *directories):
for directory in directories:
if not os.path.exists(os.path.dirname(directory)):
os.makedirs(os.path.dirname(directory))
if os.path.exists(directory):
shutil.rmtree(directory)
os.mkdir(directory)
def create_folder(self, path, remove_content=False):
if os.path.exists(path):
if remove_content:
shutil.rmtree(path)
os.makedirs(path)
else:
os.makedirs(path)
def hex_to_bytes(self, string):
try:
# Remove non-hex characters (e.g., hyphens)

View File

@@ -34,7 +34,7 @@ class Updater:
return latest_commit.get("sha") or "0506fb67111d5c5230bf59b826c318ea4251dfc4"
def download_update(self):
self.utils.mkdirs(self.temporary_dir)
self.utils.create_folder(self.temporary_dir)
file_path = os.path.join(self.temporary_dir, os.path.basename(self.download_repo_url))
self.fetcher.download_and_save_file(self.download_repo_url, file_path)
self.utils.extract_zip_file(file_path)