base16-schemes: unstable-2024-06-21 -> unstable-2024-11-12 (#356361)
[NixPkgs.git] / pkgs / tools / graphics / vulkan-cts / vk-cts-sources.py
blobf3e42bd82e7a0c56481ac532d72cada2e9e45ab7
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"
5 import json
6 import re
7 import subprocess
8 import sys
10 import fetch_sources
12 def 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,
19 j = json.loads(result.stdout)
20 # Remove False values
21 return {k: v for k, v in j.items() if v}
23 def main():
24 pkgs = fetch_sources.PACKAGES
25 pkgs.sort(key = lambda pkg: pkg.baseDir)
26 existing_sources = {}
28 # Fetch hashes from existing sources file
29 with open("sources.nix") as f:
30 existing_file = f.read()
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"
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()
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 {");
52 github_re = re.compile("https://github.com/(?P<owner>[^/]+)/(?P<repo>[^/]+).git")
54 for pkg in pkgs:
55 if isinstance(pkg, fetch_sources.GitRepo):
56 ms = github_re.match(pkg.httpsUrl)
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]
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}")
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");
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");
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");
88 f.write(" '';\n");
90 f.write("}\n");
92 if __name__ == "__main__":
93 main()