about summary refs log tree commit diff
path: root/pkgs/by-name/se/segger-jlink/update.py
blob: 73e5e274ab69fb6df46eedc23dc0bb32ccbdd86f (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
78
79
80
81
82
83
84
85
86
87
#! /usr/bin/env nix-shell
#! nix-shell -i python3 -p python3 python3Packages.beautifulsoup4 python3Packages.requests

import requests
import subprocess

from bs4 import BeautifulSoup
from pathlib import Path
from tempfile import NamedTemporaryFile
from textwrap import indent, dedent


ARCH_MAP = {
    'x86_64-linux': 'x86_64',
    'i686-linux': 'i386',
    'aarch64-linux': 'arm64',
    'armv7l-linux': 'arm',
}


def find_latest_jlink_version() -> str:
    try:
        response = requests.get('https://www.segger.com/downloads/jlink/')
        response.raise_for_status()
    except requests.RequestException as e:
        raise RuntimeError(f"Error fetching J-Link version: {e}")

    soup = BeautifulSoup(response.text, 'html.parser')

    jlink_download_tile = soup.find(lambda tag: tag.name == 'tbody' and "J-Link Software and Documentation pack" in tag.text)
    version_select = jlink_download_tile.find('select')
    version = next(o.text for o in version_select.find_all('option'))

    if version is None:
        raise RuntimeError("Could not find the J-Link version on the download page.")

    return version.removeprefix('V').replace('.', '')


def nar_hash(url: str) -> str:
    try:
        response = requests.post(url, data={'accept_license_agreement': 'accepted'})
        response.raise_for_status()
    except requests.RequestException as e:
        raise RuntimeError(f"Error downloading file from {url}: {e}")

    with NamedTemporaryFile() as tmpfile:
        tmpfile.write(response.content)
        tmpfile.flush()
        output = subprocess.check_output([
            "nix",
            "--extra-experimental-features", "nix-command",
            "hash", "file", "--sri", tmpfile.name
        ]).decode("utf8")

    return output.strip()


def calculate_package_hashes(version: str) -> list[tuple[str, str, str]]:
    result = []
    for (arch_nix, arch_web) in ARCH_MAP.items():
        url = f"https://www.segger.com/downloads/jlink/JLink_Linux_V{version}_{arch_web}.tgz";
        nhash = nar_hash(url)
        result.append((arch_nix, arch_web, nhash))
    return result


def update_source(version: str, package_hashes: list[tuple[str, str, str]]):
    content = f'version = "{version}";\n'
    for arch_nix, arch_web, nhash in package_hashes:
        content += dedent(f'''
        {arch_nix} = {{
          name = "{arch_web}";
          hash = "{nhash}";
        }};''').strip() + '\n'

    content = '{\n' + indent(content, '  ') + '}\n'

    with open(Path(__file__).parent / 'source.nix', 'w') as file:
        file.write(content)


if __name__ == '__main__':
    version = find_latest_jlink_version()
    package_hashes = calculate_package_hashes(version)
    update_source(version, package_hashes)