summaryrefslogtreecommitdiff
path: root/modules/home/arkenfox/update-hashes.py
blob: 3834d7a316d329908a29cd438ed86c129962f2c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env nix-shell
#! nix-shell --pure -i python3 -p nix python3 python3Packages.requests
import json
import requests
import subprocess
from pathlib import Path

GITHUB_API = "https://api.github.com"
ARKENFOX_REPO = "arkenfox/user.js"
HASHES_FILE = Path("arkenfox-hashes.json")


def get_tags():
    r = requests.get(
        f"{GITHUB_API}/repos/{ARKENFOX_REPO}/releases",
        headers={
            "Accept": "application/vnd.github+json",
            "X-GitHub-Api-Version": "2022-11-28",
        },
    )
    r.raise_for_status()
    releases = r.json()
    return [release["tag_name"] for release in releases]


def get_hash_for_tag(tag: str):
    print(f"❓️ Calculating hash of Arkenfox v{tag}")
    process = subprocess.run(
        [
            "nix",
            "--experimental-features",
            "nix-command flakes",
            "store",
            "prefetch-file",
            "--unpack",
            "--json",
            f"https://github.com/{ARKENFOX_REPO}/archive/refs/tags/{tag}.tar.gz",
        ],
        check=True,
        capture_output=True,
        text=True,
    )
    output = json.loads(process.stdout)
    return output["hash"]


tracked_tags = {}
if HASHES_FILE.exists():
    with open(HASHES_FILE, "r") as f:
        tracked_tags = json.load(f)

upstream_tags = get_tags()
tags_to_update = [tag for tag in upstream_tags if tag not in tracked_tags.keys()]
old_tracked = tracked_tags.copy()

update_len = len(tags_to_update)
if update_len < 1:
    print("😐️ No updates found")
    exit()

print(f"🏃 Updating with {update_len} tag(s)")
for tag in tags_to_update:
    tracked_tags[tag] = get_hash_for_tag(tag)

if tracked_tags != old_tracked:
    with open(HASHES_FILE, "w") as f:
        json.dump(tracked_tags, f, indent=2, sort_keys=True)

print("👍️ Done!")