diff --git a/autoupdate_app_sources/autoupdate_app_sources.py b/autoupdate_app_sources/autoupdate_app_sources.py index 9333fe8..8733539 100644 --- a/autoupdate_app_sources/autoupdate_app_sources.py +++ b/autoupdate_app_sources/autoupdate_app_sources.py @@ -1,3 +1,4 @@ +import hashlib import re import sys import requests @@ -39,6 +40,7 @@ class AppAutoUpdater(): if not os.path.exists(app_path + "/manifest.toml"): raise Exception("manifest.toml doesnt exists?") + self.app_path = app_path manifest = toml.load(open(app_path + "/manifest.toml")) self.current_version = manifest["version"].split("~")[0] @@ -68,7 +70,57 @@ class AppAutoUpdater(): print(f"Current version in manifest: {self.current_version}") print(f"Newest version on upstream: {version}") - print(assets) + + if source == "main": + if self.current_version == version: + print(f"Version is still {version}, no update required for {source}") + continue + else: + if isinstance(assets, str) and infos["url"] == assets: + print(f"URL is still up to date for asset {source}") + continue + elif isinstance(assets, dict) and assets == {k: infos[k]["url"] for k in assets.keys()}: + print(f"URLs are still up to date for asset {source}") + continue + + if isinstance(assets, str): + sha256 = self.sha256_of_remote_file(assets) + elif isinstance(assets, dict): + sha256 = {url: self.sha256_of_remote_file(url) for url in assets.values()} + + # FIXME: should create a tmp dir in which to make those changes + + if source == "main": + self.replace_upstream_version_in_manifest(version) + if isinstance(assets, str): + self.replace_string_in_manifest(infos["url"], assets) + self.replace_string_in_manifest(infos["sha256"], sha256) + elif isinstance(assets, dict): + for key, url in assets.items(): + self.replace_string_in_manifest(infos[key]["url"], url) + self.replace_string_in_manifest(infos[key]["sha256"], sha256[url]) + + def replace_upstream_version_in_manifest(self, new_version): + + # FIXME : should be done in a tmp git clone ...? + manifest_raw = open(self.app_path + "/manifest.toml").read() + + def repl(m): + return m.group(1) + new_version + m.group(3) + + print(re.findall(r"(\s*version\s*=\s*[\"\'])([\d\.]+)(\~ynh\d+[\"\'])", manifest_raw)) + + manifest_new = re.sub(r"(\s*version\s*=\s*[\"\'])([\d\.]+)(\~ynh\d+[\"\'])", repl, manifest_raw) + + open(self.app_path + "/manifest.toml", "w").write(manifest_new) + + def replace_string_in_manifest(self, pattern, replace): + + manifest_raw = open(self.app_path + "/manifest.toml").read() + + manifest_new = manifest_raw.replace(pattern, replace) + + open(self.app_path + "/manifest.toml", "w").write(manifest_new) def get_latest_version_and_asset(self, strategy, asset, infos): @@ -120,6 +172,17 @@ class AppAutoUpdater(): assert r.status_code == 200, r return r.json() + def sha256_of_remote_file(self, url): + try: + r = requests.get(url, stream=True) + m = hashlib.sha256() + for data in r.iter_content(8192): + m.update(data) + return m.hexdigest() + except Exception as e: + print(f"Failed to compute sha256 for {url} : {e}") + return None + if __name__ == "__main__": AppAutoUpdater(sys.argv[1]).run()