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"
12 def get_github_hash(owner
, repo
, revision
):
13 result
= subprocess
.run(
14 ["nix-prefetch-github", owner
, repo
, "--json", "--rev", revision
],
19 j
= json
.loads(result
.stdout
)
21 return {k
: v
for k
, v
in j
.items() if v
}
24 pkgs
= fetch_sources
.PACKAGES
25 pkgs
.sort(key
= lambda pkg
: pkg
.baseDir
)
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"
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"))
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")
52 github_re
= re
.compile("https://github.com/(?P<owner>[^/]+)/(?P<repo>[^/]+).git")
55 if isinstance(pkg
, fetch_sources
.GitRepo
):
56 ms
= github_re
.match(pkg
.httpsUrl
)
58 # Check for known hash
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]
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");
77 f
.write("\n\n prePatch = ''\n");
80 if isinstance(pkg
, fetch_sources
.GitRepo
):
81 f
.write(f
" external/{pkg.baseDir}")
85 if isinstance(pkg
, fetch_sources
.GitRepo
):
86 f
.write(f
" cp -r ${{{pkg.baseDir}}} external/{pkg.baseDir}/{pkg.extractDir}\n");
92 if __name__
== "__main__":