1
2import json
3from pathlib import Path
4import re
5import shutil
6import sys
7import toml
8import util
9import yaml
10
11
12registry_path = Path(sys.argv[1])
13package_overrides = json.loads(sys.argv[2])
14desired_packages_path = Path(sys.argv[3])
15out_path = Path(sys.argv[4])
16
17with open(desired_packages_path, "r") as f:
18 desired_packages = yaml.safe_load(f) or []
19
20registry = toml.load(registry_path / "Registry.toml")
21
22def ensure_version_valid(version):
23 """
24 Ensure a version string is a valid Julia-parsable version.
25 It doesn't really matter what it looks like as it's just used for overrides.
26 """
27 return re.sub('[^0-9\.]','', version)
28
29with open(out_path, "w") as f:
30 f.write("{fetchgit}:\n")
31 f.write("{\n")
32 for pkg in desired_packages:
33 uuid = pkg["uuid"]
34
35 if pkg["name"] in package_overrides:
36 treehash = util.get_commit_info(package_overrides[pkg["name"]])["tree"]
37 f.write(f""" "{uuid}" = {{
38 src = null; # Overridden: will fill in later
39 name = "{pkg["name"]}";
40 version = "{ensure_version_valid(pkg["version"])}";
41 treehash = "{treehash}";
42 }};\n""")
43 elif uuid in registry["packages"]:
44 registry_info = registry["packages"][uuid]
45 path = registry_info["path"]
46 packageToml = toml.load(registry_path / path / "Package.toml")
47
48 versions_toml = registry_path / path / "Versions.toml"
49 all_versions = toml.load(versions_toml)
50 if not pkg["version"] in all_versions: continue
51 version_to_use = all_versions[pkg["version"]]
52
53 if not "nix-sha256" in version_to_use:
54 raise KeyError(f"""Couldn't find nix-sha256 hash for {pkg["name"]} {pkg["version"]} in {versions_toml}. This might indicate that we failed to prefetch the hash when computing the augmented registry. Was there a relevant failure in {registry_path / "failures.yml"}?""")
55
56 repo = packageToml["repo"]
57 f.write(f""" "{uuid}" = {{
58 src = fetchgit {{
59 url = "{repo}";
60 rev = "{version_to_use["git-tree-sha1"]}";
61 sha256 = "{version_to_use["nix-sha256"]}";
62 }};
63 name = "{pkg["name"]}";
64 version = "{pkg["version"]}";
65 treehash = "{version_to_use["git-tree-sha1"]}";
66 }};\n""")
67 else:
68 # print("Warning: couldn't figure out what to do with pkg in sources_nix.py", pkg)
69 pass
70
71 f.write("}")