From 37c278ab6378df30cc9f6dc2e2bc4d2ba6aba274 Mon Sep 17 00:00:00 2001 From: foamyguy Date: Wed, 18 Jun 2025 09:32:27 -0500 Subject: [PATCH] is_release_required() function --- .gitignore | 2 +- release_updater.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 release_updater.py diff --git a/.gitignore b/.gitignore index 16c5327..08bb7a8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ dist - +latest_dl diff --git a/release_updater.py b/release_updater.py new file mode 100644 index 0000000..b801efa --- /dev/null +++ b/release_updater.py @@ -0,0 +1,75 @@ +import hashlib +import shutil +from pathlib import Path +from urllib.request import urlretrieve + +import requests + +from build import main as build_main + + +def get_file_sha256(file_path): + """Calculates the SHA256 hash of a file. + + Args: + file_path: The path to the file. + + Returns: + The SHA256 hash of the file as a hexadecimal string. + """ + sha256_hash = hashlib.sha256() + try: + with open(file_path, "rb") as f: + for chunk in iter(lambda: f.read(4096), b""): + sha256_hash.update(chunk) + except FileNotFoundError: + return "File not found." + return sha256_hash.hexdigest() + + +def is_release_required(): + """ + Checks if there are new versions of any apps. When new versions exist + a new release is required. + + :return: True if there are new versions of any apps, and release is required, False otherwise. + """ + + try: + dl_dir = Path("latest_dl") + if dl_dir.exists(): + shutil.rmtree("latest_dl") + dl_dir.mkdir() + except FileExistsError: + pass + + # download built zips from most recent release + RELEASE_API_URL = "https://api.github.com/repos/adafruit/Fruit-Jam-OS/releases?per_page=1" + latest_release_obj = requests.get(RELEASE_API_URL).json()[0] + for asset in latest_release_obj["assets"]: + asset_dl_url = asset["browser_download_url"] + asset_filename = asset_dl_url.split("/")[-1] + urlretrieve(asset_dl_url, f"latest_dl/{asset_filename}") + + # get sha256 hashes for each downloaded zip + downloaded_file_hashes = {} + for dl_file in Path("latest_dl").iterdir(): + downloaded_file_hashes[dl_file.name] = get_file_sha256(dl_file) + + # make a local build + build_main() + + # get sha256 hashes for built zips + dist_file_hashes = {} + for dist_file in Path("dist").iterdir(): + dist_file_hashes[dist_file.name] = get_file_sha256(dist_file) + + # compare hashes + if dist_file_hashes != downloaded_file_hashes: + print("Zip hashes differ, a release is required.") + return True + return False + + +if __name__ == '__main__': + is_release_required()