biome: 1.9.2 -> 1.9.3
[NixPkgs.git] / pkgs / applications / version-management / sapling / gen-deps.py
blobddab0080f6406911e7bf2ef03d251611fb4cea8e
1 #!/usr/bin/env nix-shell
2 #!nix-shell -i python3 -p cargo -p "python3.withPackages (ps: with ps; [ requests ])"
3 import json
4 import pathlib
5 import re
6 import tempfile
7 import os
8 import shutil
9 from hashlib import sha1
10 from struct import unpack
11 from subprocess import run
12 import subprocess
14 from requests import get
16 # Fetch the latest stable release metadata from GitHub
17 releaseMetadata = get("https://api.github.com/repos/facebook/sapling/releases/latest").json()
18 latestTag = releaseMetadata["tag_name"]
19 latestTarballURL = releaseMetadata["tarball_url"]
21 [_tarballHash, sourceDirectory] = run(
22 ["nix-prefetch-url", "--print-path", "--unpack", latestTarballURL],
23 check=True,
24 text=True,
25 stdout=subprocess.PIPE,
26 ).stdout.rstrip().splitlines()
28 def updateCargoLock():
29 with tempfile.TemporaryDirectory() as tempDir:
30 tempDir = pathlib.Path(tempDir)
32 # NOTE(strager): We cannot use shutil.tree because it copies the
33 # read-only permissions.
34 for dirpath, dirnames, filenames in os.walk(sourceDirectory):
35 relativeDirpath = os.path.relpath(dirpath, sourceDirectory)
36 for filename in filenames:
37 shutil.copy(os.path.join(dirpath, filename), tempDir / relativeDirpath / filename)
38 for dirname in dirnames:
39 os.mkdir(tempDir / relativeDirpath / dirname)
41 run(["cargo", "fetch"], check=True, cwd=tempDir / "eden" / "scm")
42 shutil.copy(tempDir / "eden" / "scm" / "Cargo.lock", "Cargo.lock")
44 updateCargoLock()
46 def nixPrefetchUrl(url):
47 return run(
48 ["nix-prefetch-url", "--type", "sha256", url],
49 check=True,
50 text=True,
51 capture_output=True,
52 ).stdout.rstrip()
55 # Fetch the `setup.py` source and look for instances of assets being downloaded
56 # from files.pythonhosted.org.
57 setupPy = (pathlib.Path(sourceDirectory) / "eden/scm/setup.py").read_text()
58 foundUrls = re.findall(r'(https://files\.pythonhosted\.org/packages/[^\s]+)"', setupPy)
60 dataDeps = {
61 "links": [{"url": url, "sha256": nixPrefetchUrl(url)} for url in foundUrls],
62 "version": latestTag,
63 # Find latest's git tag which corresponds to the Sapling version. Also
64 # needed is a hash of the version, so calculate that here. Taken from
65 # Sapling source `$root/eden/scm/setup_with_version.py`.
66 "versionHash": str(unpack(">Q", sha1(latestTag.encode("ascii")).digest()[:8])[0]),
69 open("deps.json", "w").write(json.dumps(dataDeps, indent=2, sort_keys=True) + "\n")