winetricks: 20240105 -> 20250102 (#373139)
[NixPkgs.git] / pkgs / build-support / binary-cache / make-binary-cache.py
blob4af168cae978876ddd5ec745de06bbcdbd9e3903
1 from functools import partial
2 import json
3 from multiprocessing import Pool
4 import os
5 from pathlib import Path
6 import subprocess
9 def dropPrefix(path, nixPrefix):
10 return path[len(nixPrefix + "/") :]
13 def processItem(item, nixPrefix, outDir):
14 narInfoHash = dropPrefix(item["path"], nixPrefix).split("-")[0]
16 xzFile = outDir / "nar" / f"{narInfoHash}.nar.xz"
17 with open(xzFile, "wb") as f:
18 subprocess.run(
19 f"nix-store --dump {item['path']} | xz -c",
20 stdout=f,
21 shell=True,
22 check=True,
25 fileHash = (
26 subprocess.run(
27 ["nix-hash", "--base32", "--type", "sha256", "--flat", xzFile],
28 capture_output=True,
29 check=True,
31 .stdout.decode()
32 .strip()
34 fileSize = os.path.getsize(xzFile)
36 finalXzFileName = Path("nar") / f"{fileHash}.nar.xz"
37 os.rename(xzFile, outDir / finalXzFileName)
39 with open(outDir / f"{narInfoHash}.narinfo", "wt") as f:
40 f.write(f"StorePath: {item['path']}\n")
41 f.write(f"URL: {finalXzFileName}\n")
42 f.write("Compression: xz\n")
43 f.write(f"FileHash: sha256:{fileHash}\n")
44 f.write(f"FileSize: {fileSize}\n")
45 f.write(f"NarHash: {item['narHash']}\n")
46 f.write(f"NarSize: {item['narSize']}\n")
47 f.write(f"References: {' '.join(dropPrefix(ref, nixPrefix) for ref in item['references'])}\n")
50 def main():
51 outDir = Path(os.environ["out"])
52 nixPrefix = os.environ["NIX_STORE"]
53 numWorkers = int(os.environ.get("NIX_BUILD_CORES", "4"))
55 with open(os.environ["NIX_ATTRS_JSON_FILE"], "r") as f:
56 closures = json.load(f)["closure"]
58 os.makedirs(outDir / "nar", exist_ok=True)
60 with open(outDir / "nix-cache-info", "w") as f:
61 f.write(f"StoreDir: {nixPrefix}\n")
63 with Pool(processes=numWorkers) as pool:
64 worker = partial(processItem, nixPrefix=nixPrefix, outDir=outDir)
65 pool.map(worker, closures)
68 if __name__ == "__main__":
69 main()