1
2from collections import defaultdict
3import copy
4import json
5import os
6from pathlib import Path
7import shutil
8import subprocess
9import sys
10import tempfile
11import toml
12import util
13import yaml
14
15
16registry_path = Path(sys.argv[1])
17desired_packages_path = Path(sys.argv[2])
18package_overrides = json.loads(sys.argv[3])
19dependencies_path = Path(sys.argv[4])
20out_path = Path(sys.argv[5])
21
22with open(desired_packages_path, "r") as f:
23 desired_packages = yaml.safe_load(f) or []
24
25uuid_to_versions = defaultdict(list)
26for pkg in desired_packages:
27 uuid_to_versions[pkg["uuid"]].append(pkg["version"])
28
29with open(dependencies_path, "r") as f:
30 uuid_to_store_path = yaml.safe_load(f)
31
32os.makedirs(out_path)
33
34registry = toml.load(registry_path / "Registry.toml")
35registry["packages"] = {k: v for k, v in registry["packages"].items() if k in uuid_to_versions}
36
37for (uuid, versions) in uuid_to_versions.items():
38 if uuid in package_overrides:
39 info = package_overrides[uuid]
40
41 # Make a registry entry based on the info from the package override
42 path = Path(info["name"][0].upper()) / Path(info["name"])
43 registry["packages"][uuid] = {
44 "name": info["name"],
45 "path": str(path),
46 }
47
48 os.makedirs(out_path / path)
49
50 # Read the Project.yaml from the src
51 project = toml.load(Path(info["src"]) / "Project.toml")
52
53 # Generate all the registry files
54 with open(out_path / path / Path("Compat.toml"), "w") as f:
55 f.write('["%s"]\n' % info["version"])
56 # Write nothing in Compat.toml, because we've already resolved everything
57 with open(out_path / path / Path("Deps.toml"), "w") as f:
58 f.write('["%s"]\n' % info["version"])
59 if "deps" in project:
60 toml.dump(project["deps"], f)
61 with open(out_path / path / Path("Versions.toml"), "w") as f:
62 f.write('["%s"]\n' % info["version"])
63 f.write('git-tree-sha1 = "%s"\n' % info["treehash"])
64 with open(out_path / path / Path("Package.toml"), "w") as f:
65 toml.dump({
66 "name": info["name"],
67 "uuid": uuid,
68 "repo": "file://" + info["src"],
69 }, f)
70
71 elif uuid in registry["packages"]:
72 registry_info = registry["packages"][uuid]
73 name = registry_info["name"]
74 path = registry_info["path"]
75
76 os.makedirs(out_path / path)
77
78 # Copy some files to the minimal repo unchanged
79 for f in ["Compat.toml", "Deps.toml", "WeakCompat.toml", "WeakDeps.toml"]:
80 if (registry_path / path / f).exists():
81 shutil.copy2(registry_path / path / f, out_path / path)
82
83 # Copy the Versions.toml file, trimming down to the versions we care about
84 all_versions = toml.load(registry_path / path / "Versions.toml")
85 versions_to_keep = {k: v for k, v in all_versions.items() if k in versions}
86 for k, v in versions_to_keep.items():
87 del v["nix-sha256"]
88 with open(out_path / path / "Versions.toml", "w") as f:
89 toml.dump(versions_to_keep, f)
90
91 # Fill in the local store path for the repo
92 if not uuid in uuid_to_store_path: continue
93 package_toml = toml.load(registry_path / path / "Package.toml")
94 package_toml["repo"] = "file://" + uuid_to_store_path[uuid]
95 with open(out_path / path / "Package.toml", "w") as f:
96 toml.dump(package_toml, f)
97
98with open(out_path / "Registry.toml", "w") as f:
99 toml.dump(registry, f)