is_release_required() function
This commit is contained in:
parent
228f25cbb7
commit
37c278ab63
2 changed files with 76 additions and 1 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -1,2 +1,2 @@
|
||||||
dist
|
dist
|
||||||
|
latest_dl
|
||||||
|
|
|
||||||
75
release_updater.py
Normal file
75
release_updater.py
Normal file
|
|
@ -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()
|
||||||
Loading…
Reference in a new issue