lol
1#!/usr/bin/env nix-shell
2#!nix-shell -i python3 -p nix-prefetch-github -p git
3#nix-shell -I nixpkgs=../../../../ -i python3 -p "python3.withPackages (ps: with ps; [ nix-prefetch-github ])" -p "git"
4
5import json
6import re
7import subprocess
8import sys
9
10import fetch_sources
11
12def get_github_hash(owner, repo, revision):
13 result = subprocess.run(
14 ["nix-prefetch-github", owner, repo, "--json", "--rev", revision],
15 check=True,
16 capture_output=True,
17 text=True,
18 )
19 j = json.loads(result.stdout)
20 # Remove False values
21 return {k: v for k, v in j.items() if v}
22
23def main():
24 pkgs = fetch_sources.PACKAGES
25 pkgs.sort(key = lambda pkg: pkg.baseDir)
26 existing_sources = {}
27
28 # Fetch hashes from existing sources file
29 with open("sources.nix") as f:
30 existing_file = f.read()
31
32 source_re = re.compile("(?P<name>[^ ]+) = fetchFromGitHub[^\n]*\n"
33 "[^\n]+\n" # owner
34 "[^\n]+\n" # repo
35 " *rev = \"(?P<rev>[^\"]+)\";\n"
36 " *hash = \"(?P<hash>[^\"]+)\";\n"
37 )
38
39 for m in source_re.finditer(existing_file):
40 if m.group("hash").startswith("sha"):
41 print(f"Found {m.group('name')}: {m.group('rev')} -> {m.group('hash')}")
42 existing_sources[m.group("name")] = (m.group("rev"), m.group("hash"))
43 print()
44
45
46 # Write new sources file
47 with open("sources.nix", "w") as f:
48 f.write("# Autogenerated from vk-cts-sources.py\n")
49 f.write("{ fetchurl, fetchFromGitHub }:\n")
50 f.write("rec {");
51
52 github_re = re.compile("https://github.com/(?P<owner>[^/]+)/(?P<repo>[^/]+).git")
53
54 for pkg in pkgs:
55 if isinstance(pkg, fetch_sources.GitRepo):
56 ms = github_re.match(pkg.httpsUrl)
57
58 # Check for known hash
59 hash = None
60 if pkg.baseDir in existing_sources:
61 existing_src = existing_sources[pkg.baseDir]
62 if existing_src[0] == pkg.revision:
63 hash = existing_src[1]
64
65 if hash is None:
66 print(f"Fetching {pkg.baseDir}: {pkg.revision}")
67 hash = get_github_hash(ms.group("owner"), ms.group("repo"), pkg.revision)["hash"]
68 print(f"Got {pkg.baseDir}: {pkg.revision} -> {hash}")
69
70 f.write(f"\n {pkg.baseDir} = fetchFromGitHub {{\n");
71 f.write(f" owner = \"{ms.group('owner')}\";\n");
72 f.write(f" repo = \"{ms.group('repo')}\";\n");
73 f.write(f" rev = \"{pkg.revision}\";\n");
74 f.write(f" hash = \"{hash}\";\n");
75 f.write(f" }};\n");
76
77 f.write("\n\n prePatch = ''\n");
78 f.write(" mkdir -p");
79 for pkg in pkgs:
80 if isinstance(pkg, fetch_sources.GitRepo):
81 f.write(f" external/{pkg.baseDir}")
82 f.write("\n\n");
83
84 for pkg in pkgs:
85 if isinstance(pkg, fetch_sources.GitRepo):
86 f.write(f" cp -r ${{{pkg.baseDir}}} external/{pkg.baseDir}/{pkg.extractDir}\n");
87
88 f.write(" '';\n");
89
90 f.write("}\n");
91
92if __name__ == "__main__":
93 main()