From 7b58db7020b6671b6681308c0fbd06962b66f614 Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Sat, 28 May 2022 03:30:20 -0400 Subject: [PATCH] [O] Separate files --- debug.py | 4 ++ ocpm/interaction.py | 4 +- ocpm/main.py | 123 +++++++++++++------------------------------- ocpm/models.py | 77 +++++++++++++++++++++++++++ setup.py | 3 +- 5 files changed, 121 insertions(+), 90 deletions(-) create mode 100644 debug.py create mode 100644 ocpm/models.py diff --git a/debug.py b/debug.py new file mode 100644 index 0000000..9b38baa --- /dev/null +++ b/debug.py @@ -0,0 +1,4 @@ +from ocpm.main import run + +if __name__ == '__main__': + run() diff --git a/ocpm/interaction.py b/ocpm/interaction.py index 198b0c4..97413a4 100644 --- a/ocpm/interaction.py +++ b/ocpm/interaction.py @@ -1,6 +1,8 @@ +from __future__ import annotations + from hypy_utils import printc -from main import Kext, Release +from .models import Kext, Release def ver_diff(src: str, to: str): diff --git a/ocpm/main.py b/ocpm/main.py index 44407cc..2c56508 100755 --- a/ocpm/main.py +++ b/ocpm/main.py @@ -3,101 +3,18 @@ from __future__ import annotations import argparse import os -import plistlib -from dataclasses import dataclass -from datetime import datetime from pathlib import Path -from packaging import version +from tempfile import TemporaryDirectory -import dateutil.parser -import pandas import requests -import tqdm as tqdm import ruamel.yaml -import semver +import tqdm as tqdm from hypy_utils import printc -from pandas import DataFrame +from packaging import version from tqdm.contrib.concurrent import thread_map -from interaction import print_updates, sizeof_fmt - - -@dataclass() -class Kext: - path: Path - - name: str - id: str - version: str - sdk_os: str - min_os: str - - def __init__(self, path: Path): - self.path = path - - # Find plist file - plist = path / 'Contents' / 'Info.plist' - if not plist.is_file(): - print(f'Error loading {path.name}: Cannot find Info.plist') - - # Load plist file - plist = plistlib.loads(plist.read_bytes()) - - self.name = plist['CFBundleName'] - self.id = plist['CFBundleIdentifier'] - self.version = plist['CFBundleVersion'] - self.sdk_os = plist.get('DTSDKName') - self.min_os = plist.get('LSMinimumSystemVersion') - - if self.sdk_os: - self.sdk_os = self.sdk_os.replace('macosx', '') - - -def print_kexts(kexts: list[Kext]): - df = DataFrame(kexts) - df = df.drop(columns=['path', 'id']) - # df['path'] = df['path'].apply(lambda x: x.name.replace('.kext', '')) - print(df.to_string()) - - -@dataclass() -class Artifact: - size: int - url: str - name: str - - @classmethod - def from_github(cls, obj: dict) -> "Artifact": - return cls(obj['size'], obj['browser_download_url'], obj['name']) - - -def find_artifact(raw: dict) -> Artifact: - assets = raw['assets'] - if len(assets) == 1: - return Artifact.from_github(assets[0]) - - # Filter out DEBUG artifacts - assets = [a for a in assets if not a['name'].endswith('DEBUG.zip')] - return Artifact.from_github(assets[0]) - - -@dataclass() -class Release: - tag: str - raw: dict - artifact: Artifact - date: datetime - - @classmethod - def from_github(cls, raw: dict) -> "Release": - tag = raw['tag_name'] - if tag.startswith('v'): - tag = tag[1:] - - date = dateutil.parser.parse(raw['published_at']) - artifact = find_artifact(raw) - - return cls(tag, raw, artifact, date) +from .interaction import print_updates, sizeof_fmt +from .models import Kext, Release def get_latest_release(kext: Kext, repos: dict, pre: bool): @@ -130,6 +47,34 @@ def get_latest_release(kext: Kext, repos: dict, pre: bool): return Release.from_github(latest) +def download_file(url: str, file: str | Path): + """ + Helper method handling downloading large files from `url` to `filename`. + Returns a pointer to `filename`. + + https://stackoverflow.com/a/42071418/7346633 + """ + chunk_size = 1024 + r = requests.get(url, stream=True) + with open(file, 'wb') as f: + pbar = tqdm.tqdm(unit="B", total=int(r.headers['Content-Length'])) + for chunk in r.iter_content(chunk_size=chunk_size): + if chunk: + pbar.update(len(chunk)) + f.write(chunk) + return file + + +def download_updates(updates: list[tuple[Kext, Release]]): + # Create temporary directory + with TemporaryDirectory() as tmp: + tmp = Path(tmp) + + print('Downloading zip files...') + for k, r in updates: + download_file(r.artifact.url, r.artifact.name) + + def run(): parser = argparse.ArgumentParser(description='OpenCore Kext Updater by HyDEV') parser.add_argument('efi_path', help='EFI Directory Path') @@ -187,6 +132,8 @@ def run(): print('😕 Huh, okay') exit(0) + download_updates(updates) + if __name__ == '__main__': run() diff --git a/ocpm/models.py b/ocpm/models.py new file mode 100644 index 0000000..9c7dc99 --- /dev/null +++ b/ocpm/models.py @@ -0,0 +1,77 @@ +from __future__ import annotations + +import plistlib +from dataclasses import dataclass +from datetime import datetime +from pathlib import Path + + +@dataclass() +class Kext: + path: Path + + name: str + id: str + version: str + sdk_os: str + min_os: str + + def __init__(self, path: Path): + self.path = path + + # Find plist file + plist = path / 'Contents' / 'Info.plist' + if not plist.is_file(): + print(f'Error loading {path.name}: Cannot find Info.plist') + + # Load plist file + plist = plistlib.loads(plist.read_bytes()) + + self.name = plist['CFBundleName'] + self.id = plist['CFBundleIdentifier'] + self.version = plist['CFBundleVersion'] + self.sdk_os = plist.get('DTSDKName') + self.min_os = plist.get('LSMinimumSystemVersion') + + if self.sdk_os: + self.sdk_os = self.sdk_os.replace('macosx', '') + + +@dataclass() +class Artifact: + size: int + url: str + name: str + + @classmethod + def from_github(cls, obj: dict) -> "Artifact": + return cls(obj['size'], obj['browser_download_url'], obj['name']) + + @classmethod + def find_from_release(cls, raw: dict) -> "Artifact": + assets = raw['assets'] + if len(assets) == 1: + return Artifact.from_github(assets[0]) + + # Filter out DEBUG artifacts + assets = [a for a in assets if not a['name'].endswith('DEBUG.zip')] + return Artifact.from_github(assets[0]) + + +@dataclass() +class Release: + tag: str + raw: dict + artifact: Artifact + # date: datetime + + @classmethod + def from_github(cls, raw: dict) -> "Release": + tag = raw['tag_name'] + if tag.startswith('v'): + tag = tag[1:] + + # date = dateutil.parser.parse(raw['published_at']) + artifact = Artifact.find_from_release(raw) + + return cls(tag, raw, artifact) diff --git a/setup.py b/setup.py index 6aa6808..44115a6 100644 --- a/setup.py +++ b/setup.py @@ -32,7 +32,8 @@ setup( packages=['ocpm'], package_data={'ocpm': ['ocpm/*']}, include_package_data=True, - install_requires=['setuptools', 'hypy_utils'], + install_requires=['setuptools', 'hypy_utils', 'ruamel.yaml', 'requests', 'tqdm', + 'packaging'], entry_points={ "console_scripts": [ "ocpm=ocpm.main:run",