summaryrefslogtreecommitdiff
path: root/pkgs/development/julia-modules/python/minimal_registry.py
blob: ab33ac366ca86f688bfe2a334db7638b02dc815e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

from collections import defaultdict
import copy
import json
import os
from pathlib import Path
import shutil
import subprocess
import sys
import tempfile
import toml
import util
import yaml


registry_path = Path(sys.argv[1])
desired_packages_path = Path(sys.argv[2])
package_overrides = json.loads(sys.argv[3])
dependencies_path = Path(sys.argv[4])
out_path = Path(sys.argv[5])

with open(desired_packages_path, "r") as f:
  desired_packages = yaml.safe_load(f) or []

uuid_to_versions = defaultdict(list)
for pkg in desired_packages:
  uuid_to_versions[pkg["uuid"]].append(pkg["version"])

with open(dependencies_path, "r") as f:
  uuid_to_store_path = yaml.safe_load(f)

os.makedirs(out_path)

full_registry = toml.load(registry_path / "Registry.toml")
registry = full_registry.copy()
registry["packages"] = {k: v for k, v in registry["packages"].items() if k in uuid_to_versions}

for (uuid, versions) in uuid_to_versions.items():
  if uuid in package_overrides:
    info = package_overrides[uuid]

    # Make a registry entry based on the info from the package override
    path = Path(info["name"][0].upper()) / Path(info["name"])
    registry["packages"][uuid] = {
      "name": info["name"],
      "path": str(path),
    }

    os.makedirs(out_path / path)

    # Read the Project.yaml from the src
    project = toml.load(Path(info["src"]) / "Project.toml")

    # Generate all the registry files
    with open(out_path / path / Path("Compat.toml"), "w") as f:
      f.write('["%s"]\n' % info["version"])
      # Write nothing in Compat.toml, because we've already resolved everything
    with open(out_path / path / Path("Deps.toml"), "w") as f:
      f.write('["%s"]\n' % info["version"])
      if "deps" in project:
        toml.dump(project["deps"], f)
    with open(out_path / path / Path("Versions.toml"), "w") as f:
      f.write('["%s"]\n' % info["version"])
      f.write('git-tree-sha1 = "%s"\n' % info["treehash"])
    with open(out_path / path / Path("Package.toml"), "w") as f:
      toml.dump({
        "name": info["name"],
        "uuid": uuid,
        "repo": "file://" + info["src"],
      }, f)

  elif uuid in registry["packages"]:
    registry_info = registry["packages"][uuid]
    name = registry_info["name"]
    path = registry_info["path"]

    os.makedirs(out_path / path)

    # Copy some files to the minimal repo unchanged
    for f in ["Compat.toml", "Deps.toml", "WeakCompat.toml", "WeakDeps.toml"]:
      if (registry_path / path / f).exists():
        shutil.copy2(registry_path / path / f, out_path / path)

    # Copy the Versions.toml file, trimming down to the versions we care about.
    # In the case where versions=None, this is a weak dep, and we keep all versions.
    all_versions = toml.load(registry_path / path / "Versions.toml")
    versions_to_keep = {k: v for k, v in all_versions.items() if k in versions} if versions != None else all_versions
    for k, v in versions_to_keep.items():
      del v["nix-sha256"]
    with open(out_path / path / "Versions.toml", "w") as f:
      toml.dump(versions_to_keep, f)

    if versions is None:
      # This is a weak dep; just grab the whole Package.toml
      shutil.copy2(registry_path / path / "Package.toml", out_path / path / "Package.toml")
    elif uuid in uuid_to_store_path:
      # Fill in the local store path for the repo
      package_toml = toml.load(registry_path / path / "Package.toml")
      package_toml["repo"] = "file://" + uuid_to_store_path[uuid]
      with open(out_path / path / "Package.toml", "w") as f:
        toml.dump(package_toml, f)

# Look for missing weak deps and include them. This can happen when our initial
# resolve step finds dependencies, but we fail to resolve them at the project.py
# stage. Usually this happens because the package that depends on them does so
# as a weak dep, but doesn't have a Package.toml in its repo making this clear.
for pkg in desired_packages:
  for dep in (pkg.get("deps", []) or []):
    uuid = dep["uuid"]
    if not uuid in uuid_to_versions:
      entry = full_registry["packages"].get(uuid)
      if not entry:
        print(f"""WARNING: found missing UUID but couldn't resolve it: {uuid}""")
        continue

      # Add this entry back to the minimal Registry.toml
      registry["packages"][uuid] = entry

      # Bring over the Package.toml
      path = Path(entry["path"])
      if (out_path / path / "Package.toml").exists():
        continue
      Path(out_path / path).mkdir(parents=True, exist_ok=True)
      shutil.copy2(registry_path / path / "Package.toml", out_path / path / "Package.toml")

# Finally, dump the Registry.toml
with open(out_path / "Registry.toml", "w") as f:
    toml.dump(registry, f)