Move extract zip function to utils

This commit is contained in:
Hoang Hong Quan
2024-07-29 15:14:51 +07:00
parent 1e00ad5730
commit ea98f0c950
5 changed files with 15 additions and 16 deletions

View File

@@ -320,7 +320,7 @@ class DSDT:
zfile = os.path.basename(url)
#print("Downloading {}".format(os.path.basename(url)))
#self.dl.stream_to_file(url, os.path.join(ztemp,zfile), progress=False, headers=self.h)
self.fetcher.download_and_save_file(url, os.path.join(ztemp,zfile), False)
self.fetcher.download_and_save_file(url, os.path.join(ztemp,zfile))
search_dir = ztemp
if zfile.lower().endswith(".zip"):
print(" - Extracting")

View File

@@ -282,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)
self.utils.extract_zip_file(zip_path)
if self.move_bootloader_kexts_to_product_directory(product_data.get("product_name")):
if product_index is None:

View File

@@ -2,7 +2,6 @@ import requests
import ssl
import os
import certifi
import zipfile
import plistlib
class ResourceFetcher:
@@ -54,16 +53,4 @@ class ResourceFetcher:
file_writer.write(chunk)
bytes_downloaded += len(chunk)
if total_size != -1:
print(f"Downloaded {bytes_downloaded / (1024 * 1024):.2f} MB of {total_size / (1024 * 1024):.2f} MB", end='\r')
if extract and destination_path.lower().endswith('.zip'):
self.extract_zip_file(destination_path)
def extract_zip_file(self, zip_path, extraction_directory=None):
if extraction_directory is None:
extraction_directory = os.path.splitext(zip_path)[0]
os.makedirs(extraction_directory, exist_ok=True)
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(extraction_directory)
print(f"Downloaded {bytes_downloaded / (1024 * 1024):.2f} MB of {total_size / (1024 * 1024):.2f} MB", end='\r')

View File

@@ -127,6 +127,15 @@ class Utils:
return little_endian_hex.upper()
def extract_zip_file(self, zip_path, extraction_directory=None):
if extraction_directory is None:
extraction_directory = os.path.splitext(zip_path)[0]
os.makedirs(extraction_directory, exist_ok=True)
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(extraction_directory)
def contains_any(self, data, search_item, start=0, end=None):
return next((item for item in data[start:end] if item.lower() in search_item.lower()), None)

View File

@@ -34,7 +34,9 @@ class Updater:
def download_update(self):
self.utils.mkdirs(self.temporary_dir)
self.fetcher.download_and_save_file(self.download_repo_url, os.path.join(self.temporary_dir, os.path.basename(self.download_repo_url)))
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)
def update_files(self):
target_dir = os.path.join(self.temporary_dir, "main", "OpCore-Simplify-main")