nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
1#! /usr/bin/env nix-shell
2#! nix-shell -i python3 -p
3
4import re
5import json
6import os
7import sys
8from urllib.request import urlopen, Request
9
10
11def get_arch_os_key(url) -> str:
12 if "darwin_amd64" in url:
13 return "x86_64-darwin"
14 elif "darwin_arm64" in url:
15 return "aarch64-darwin"
16 elif "linux_amd64" in url:
17 return "x86_64-linux"
18 elif "linux_arm64" in url:
19 return "aarch64-linux"
20 else:
21 raise Exception(f"Unknown architecture: {url}")
22
23
24def fetch_content(url: str) -> str:
25 req = Request(url)
26 if "GITHUB_TOKEN" in os.environ:
27 req.add_header("Authorization", f"Bearer {os.environ['GITHUB_TOKEN']}")
28
29 with urlopen(req) as resp:
30 return resp.read().decode("utf-8")
31
32
33def parse_and_check(content):
34 data = {"version": "", "sources": {}}
35
36 version_match = re.search(r'version\s+"([^"]+)"', content)
37 if not version_match:
38 raise ValueError("Could not find version!")
39
40 version = version_match.group(1)
41 data["version"] = version
42 print(f"Detected version: {version}")
43
44 old_version = os.environ.get("UPDATE_NIX_OLD_VERSION")
45 if old_version == version:
46 print("No newer version available!")
47 sys.exit(0)
48
49 pattern = re.compile(
50 r'url\s+"([^"]+)"(?:,\s*using:\s*\S+)?\s+sha256\s+"([a-fA-F0-9]+)"'
51 )
52 matches = pattern.findall(content)
53
54 if not matches:
55 raise ValueError(
56 "Regex failed to find any URL/SHA256 pairs. The Formula format might have changed."
57 )
58
59 for url, hex_sha in matches:
60 key = get_arch_os_key(url)
61
62 data["sources"][key] = {"url": url, "sha256": hex_sha}
63
64 data["sources"] = dict(sorted(data["sources"].items()))
65
66 return data
67
68
69def main():
70 try:
71 ruby_content = fetch_content(
72 "https://github.com/atlassian/homebrew-acli/raw/refs/heads/main/Formula/acli.rb"
73 )
74 result = parse_and_check(ruby_content)
75
76 output_path = os.path.join(
77 os.path.dirname(os.path.realpath(__file__)), "sources.json"
78 )
79
80 with open(output_path, "w") as f:
81 json.dump(result, f, indent=2)
82 f.write("\n")
83
84 except Exception as e:
85 print(f"Error: {e}", file=sys.stderr)
86 sys.exit(1)
87
88
89if __name__ == "__main__":
90 main()