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 # The treehash is missing for stdlib packages. Don't bother downloading these.
45 if (not ("tree_hash" in pkg)) or pkg["tree_hash"] == "nothing": continue
46
47 registry_info = registry["packages"][uuid]
48 path = registry_info["path"]
49 packageToml = toml.load(registry_path / path / "Package.toml")
50
51 versions_toml = registry_path / path / "Versions.toml"
52 all_versions = toml.load(versions_toml)
53 if not pkg["version"] in all_versions: continue
54 version_to_use = all_versions[pkg["version"]]
55
56 if not "nix-sha256" in version_to_use:
57 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"}?""")
58
59 repo = packageToml["repo"]
60 f.write(f""" "{uuid}" = {{
61 src = fetchgit {{
62 url = "{repo}";
63 rev = "{version_to_use["git-tree-sha1"]}";
64 sha256 = "{version_to_use["nix-sha256"]}";
65 }};
66 name = "{pkg["name"]}";
67 version = "{pkg["version"]}";
68 treehash = "{version_to_use["git-tree-sha1"]}";
69 }};\n""")
70 else:
71 # This is probably a stdlib
72 # print("WARNING: couldn't figure out what to do with pkg in sources_nix.py", pkg)
73 pass
74
75 f.write("}")