nixpkgs mirror (for testing) github.com/NixOS/nixpkgs
nix
at python-updates 74 lines 2.7 kB view raw
1#! /usr/bin/env nix-shell 2#! nix-shell -i python3 -p python3 python3.pkgs.packaging python3.pkgs.requests 3import gzip 4import json 5import logging 6import pathlib 7import re 8import subprocess 9import sys 10 11from packaging import version 12import requests 13 14logging.basicConfig(stream=sys.stdout, level=logging.DEBUG) 15 16current_path = pathlib.Path(__file__).parent 17DATA_JSON = current_path.joinpath("data.json").resolve() 18logging.debug(f"Path to version file: {DATA_JSON}") 19last_new_version = None 20 21with open(DATA_JSON, "r") as versions_file: 22 versions = json.load(versions_file) 23 24def find_latest_version(arch): 25 CHECK_URL = f'https://apt.enpass.io/dists/stable/main/binary-{arch}/Packages.gz' 26 packages = gzip.decompress(requests.get(CHECK_URL).content).decode() 27 28 # Loop every package to find the newest one! 29 version_selector = re.compile("Version: (?P<version>.+)") 30 path_selector = re.compile("Filename: (?P<path>.+)") 31 hash_selector = re.compile("SHA256: (?P<sha256>.+)") 32 last_version = version.parse("0") 33 for package in packages.split("\n\n"): 34 matches = version_selector.search(package) 35 matched_version = matches.group('version') if matches and matches.group('version') else "0" 36 parsed_version = version.parse(matched_version) 37 if parsed_version > last_version: 38 path = path_selector.search(package).group('path') 39 sha256 = hash_selector.search(package).group('sha256') 40 last_version = parsed_version 41 return {"path": path, "sha256": sha256, "version": matched_version} 42 43for arch in versions.keys(): 44 current_version = versions[arch]['version'] 45 logging.info(f"Current Version for {arch} is {current_version}") 46 new_version = find_latest_version(arch) 47 48 if not new_version or new_version['version'] == current_version: 49 continue 50 51 last_current_version = current_version 52 last_new_version = new_version 53 logging.info(f"Update found ({arch}): enpass: {current_version} -> {new_version['version']}") 54 versions[arch]['path'] = new_version['path'] 55 versions[arch]['sha256'] = new_version['sha256'] 56 versions[arch]['version'] = new_version['version'] 57 58 59if not last_new_version: 60 logging.info('#### No update found ####') 61 sys.exit(0) 62 63# write new versions back 64with open(DATA_JSON, "w") as versions_file: 65 json.dump(versions, versions_file, indent=2) 66 versions_file.write("\n") 67 68# Commit the result: 69logging.info("Committing changes...") 70commit_message = f"enpass: {last_current_version} -> {last_new_version['version']}" 71subprocess.run(['git', 'add', DATA_JSON], check=True) 72subprocess.run(['git', 'commit', '--file=-'], input=commit_message.encode(), check=True) 73 74logging.info("Done.")