sdrangel: fix build on x86_64-darwin
[NixPkgs.git] / pkgs / tools / security / enpass / update_script.py
blobab0b6ce3f48ed57df99e72f702b3ddb8cc203762
1 #! /usr/bin/env nix-shell
2 #! nix-shell -i python3 -p python3 python3.pkgs.packaging python3.pkgs.requests
3 import gzip
4 import json
5 import logging
6 import pathlib
7 import re
8 import subprocess
9 import sys
11 from packaging import version
12 import requests
14 logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
16 current_path = pathlib.Path(__file__).parent
17 DATA_JSON = current_path.joinpath("data.json").resolve()
18 logging.debug(f"Path to version file: {DATA_JSON}")
19 last_new_version = None
21 with open(DATA_JSON, "r") as versions_file:
22 versions = json.load(versions_file)
24 def 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()
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}
43 for 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)
48 if not new_version or new_version['version'] == current_version:
49 continue
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']
59 if not last_new_version:
60 logging.info('#### No update found ####')
61 sys.exit(0)
63 # write new versions back
64 with open(DATA_JSON, "w") as versions_file:
65 json.dump(versions, versions_file, indent=2)
66 versions_file.write("\n")
68 # Commit the result:
69 logging.info("Committing changes...")
70 commit_message = f"enpass: {last_current_version} -> {last_new_version['version']}"
71 subprocess.run(['git', 'add', DATA_JSON], check=True)
72 subprocess.run(['git', 'commit', '--file=-'], input=commit_message.encode(), check=True)
74 logging.info("Done.")