about summary refs log tree commit diff
path: root/nixpkgs/pkgs/development/libraries/ndi/update.py
blob: e80260210368b828bb09ee9bd9d50324f61bd380 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env nix-shell
#! nix-shell -i python -p "python3.withPackages (ps: with ps; [ ps.absl-py ps.requests ])"

import hashlib
import io
import json
import os.path
import tarfile

import requests
from absl import app, flags

BASE_NAME = "Install_NDI_SDK_v5_Linux"
NDI_SDK_URL = f"https://downloads.ndi.tv/SDK/NDI_SDK_Linux/{BASE_NAME}.tar.gz"
NDI_EXEC = f"{BASE_NAME}.sh"

NDI_ARCHIVE_MAGIC = b"__NDI_ARCHIVE_BEGIN__\n"

FLAG_out = flags.DEFINE_string("out", None, "Path to read/write version.json from/to.")


def find_version_json() -> str:
    if FLAG_out.value:
        return FLAG_out.value
    try_paths = ["pkgs/development/libraries/ndi/version.json", "version.json"]
    for path in try_paths:
        if os.path.exists(path):
            return path
    raise Exception(
        "Couldn't figure out where to write version.json; try specifying --out"
    )


def fetch_tarball() -> bytes:
    r = requests.get(NDI_SDK_URL)
    r.raise_for_status()
    return r.content


def read_version(tarball: bytes) -> str:
    # Find the inner script.
    outer_tarfile = tarfile.open(fileobj=io.BytesIO(tarball), mode="r:gz")
    eula_script = outer_tarfile.extractfile(NDI_EXEC).read()

    # Now find the archive embedded within the script.
    archive_start = eula_script.find(NDI_ARCHIVE_MAGIC) + len(NDI_ARCHIVE_MAGIC)
    inner_tarfile = tarfile.open(
        fileobj=io.BytesIO(eula_script[archive_start:]), mode="r:gz"
    )

    # Now find Version.txt...
    version_txt = (
        inner_tarfile.extractfile("NDI SDK for Linux/Version.txt")
        .read()
        .decode("utf-8")
    )
    _, _, version = version_txt.strip().partition(" v")
    return version


def main(argv):
    tarball = fetch_tarball()

    sha256 = hashlib.sha256(tarball).hexdigest()
    version = {
        "hash": f"sha256:{sha256}",
        "version": read_version(tarball),
    }

    out_path = find_version_json()
    with open(out_path, "w") as f:
        json.dump(version, f)
        f.write("\n")


if __name__ == "__main__":
    app.run(main)