1#!/usr/bin/env nix-shell
2#! nix-shell -i python -p "python3.withPackages (ps: with ps; [ ps.absl-py ps.requests ])"
3
4import hashlib
5import io
6import json
7import os.path
8import tarfile
9
10import requests
11from absl import app, flags
12
13BASE_NAME = "Install_NDI_SDK_v5_Linux"
14NDI_SDK_URL = f"https://downloads.ndi.tv/SDK/NDI_SDK_Linux/{BASE_NAME}.tar.gz"
15NDI_EXEC = f"{BASE_NAME}.sh"
16
17NDI_ARCHIVE_MAGIC = b"__NDI_ARCHIVE_BEGIN__\n"
18
19FLAG_out = flags.DEFINE_string("out", None, "Path to read/write version.json from/to.")
20
21
22def 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"
31 )
32
33
34def fetch_tarball() -> bytes:
35 r = requests.get(NDI_SDK_URL)
36 r.raise_for_status()
37 return r.content
38
39
40def 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()
44
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"
49 )
50
51 # Now find Version.txt...
52 version_txt = (
53 inner_tarfile.extractfile("NDI SDK for Linux/Version.txt")
54 .read()
55 .decode("utf-8")
56 )
57 _, _, version = version_txt.strip().partition(" v")
58 return version
59
60
61def main(argv):
62 tarball = fetch_tarball()
63
64 sha256 = hashlib.sha256(tarball).hexdigest()
65 version = {
66 "hash": f"sha256:{sha256}",
67 "version": read_version(tarball),
68 }
69
70 out_path = find_version_json()
71 with open(out_path, "w") as f:
72 json.dump(version, f)
73 f.write("\n")
74
75
76if __name__ == "__main__":
77 app.run(main)