summaryrefslogtreecommitdiff
path: root/fw/update_espressif_blobs.py
blob: 55c75828a37f807189f3dd2fc4ebc1e4e49e0fa8 (plain)
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
#!/usr/bin/env python3


import hashlib
import sys
import pathlib
import urllib.request

import yaml


SCRIPT = pathlib.Path(__file__)
FW = SCRIPT.parent
ESPRESSIF_MODULE = FW / "zephyrproject/modules/hal/espressif"
MODULE_YAML = ESPRESSIF_MODULE / "zephyr/module.yml"
BLOBS = ESPRESSIF_MODULE / "zephyr/blobs"


def main() -> None:
    text = MODULE_YAML.read_text()
    data = yaml.safe_load(text)

    for entry in data["blobs"]:
        url = entry["url"]
        checksum = entry["sha256"]
        path = entry["path"]
        get_blob(
            url=url,
            checksum=checksum,
            path=path,
        )


def get_blob(url, checksum, path):
    sys.stdout.write(f"Downloading {path} ...")
    with urllib.request.urlopen(url) as conn:
        content = conn.read()
    sys.stdout.write(" done\n")

    sha256 = hashlib.sha256()
    sha256.update(content)
    calculated = sha256.hexdigest()
    if calculated == checksum:
        print("Checksum matched.")
    else:
        print(f"Checksum mismatch for {path}!")
        print(f"Expected: {checksum}")
        print(f"Got:      {calculated}")

    target: pathlib.Path = BLOBS / path
    target.parent.mkdir(exist_ok=True, parents=True)
    target.write_bytes(content)
    print(f"Saved to {path}")


if __name__ == "__main__":
    main()