Merge pull request #268619 from tweag/lib-descriptions
[NixPkgs.git] / pkgs / development / libraries / ndi / update.py
blobe80260210368b828bb09ee9bd9d50324f61bd380
1 #!/usr/bin/env nix-shell
2 #! nix-shell -i python -p "python3.withPackages (ps: with ps; [ ps.absl-py ps.requests ])"
4 import hashlib
5 import io
6 import json
7 import os.path
8 import tarfile
10 import requests
11 from absl import app, flags
13 BASE_NAME = "Install_NDI_SDK_v5_Linux"
14 NDI_SDK_URL = f"https://downloads.ndi.tv/SDK/NDI_SDK_Linux/{BASE_NAME}.tar.gz"
15 NDI_EXEC = f"{BASE_NAME}.sh"
17 NDI_ARCHIVE_MAGIC = b"__NDI_ARCHIVE_BEGIN__\n"
19 FLAG_out = flags.DEFINE_string("out", None, "Path to read/write version.json from/to.")
22 def find_version_json() -> str:
23 if FLAG_out.value:
24 return FLAG_out.value
25 try_paths = ["pkgs/development/libraries/ndi/version.json", "version.json"]
26 for path in try_paths:
27 if os.path.exists(path):
28 return path
29 raise Exception(
30 "Couldn't figure out where to write version.json; try specifying --out"
34 def fetch_tarball() -> bytes:
35 r = requests.get(NDI_SDK_URL)
36 r.raise_for_status()
37 return r.content
40 def read_version(tarball: bytes) -> str:
41 # Find the inner script.
42 outer_tarfile = tarfile.open(fileobj=io.BytesIO(tarball), mode="r:gz")
43 eula_script = outer_tarfile.extractfile(NDI_EXEC).read()
45 # Now find the archive embedded within the script.
46 archive_start = eula_script.find(NDI_ARCHIVE_MAGIC) + len(NDI_ARCHIVE_MAGIC)
47 inner_tarfile = tarfile.open(
48 fileobj=io.BytesIO(eula_script[archive_start:]), mode="r:gz"
51 # Now find Version.txt...
52 version_txt = (
53 inner_tarfile.extractfile("NDI SDK for Linux/Version.txt")
54 .read()
55 .decode("utf-8")
57 _, _, version = version_txt.strip().partition(" v")
58 return version
61 def main(argv):
62 tarball = fetch_tarball()
64 sha256 = hashlib.sha256(tarball).hexdigest()
65 version = {
66 "hash": f"sha256:{sha256}",
67 "version": read_version(tarball),
70 out_path = find_version_json()
71 with open(out_path, "w") as f:
72 json.dump(version, f)
73 f.write("\n")
76 if __name__ == "__main__":
77 app.run(main)