1#!/usr/bin/env python
2#
3# SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
4# SPDX-License-Identifier: GPL-2.0-or-later
5
6import glob
7import os
8import urllib.request
9
10STUBS = (
11    {
12        "STUB_SET_VERSION": "1",
13        "DOWNLOAD_URL": "https://github.com/espressif/esptool-legacy-flasher-stub/releases/download",
14        "TAG_URL": "https://github.com/espressif/esptool-legacy-flasher-stub/releases/tag",
15        "VERSION": "v1.3.0",
16        "FILE_LIST": (
17            "esp32",
18            "esp32c2",
19            "esp32c3",
20            "esp32c5",
21            "esp32c5beta3",
22            "esp32c6",
23            "esp32c61",
24            "esp32c6beta",
25            "esp32h2",
26            "esp32h2beta1",
27            "esp32h2beta2",
28            "esp32p4",
29            "esp32s2",
30            "esp32s3",
31            "esp32s3beta2",
32            "esp8266",
33        ),
34        "LICENSE": "released as Free Software under GNU General Public License Version 2 or later",
35    },
36    {
37        "STUB_SET_VERSION": "2",
38        "DOWNLOAD_URL": "https://github.com/esp-rs/esp-flasher-stub/releases/download",
39        "TAG_URL": "https://github.com/esp-rs/esp-flasher-stub/releases/tag",
40        "VERSION": "v0.3.0",
41        "FILE_LIST": (
42            "esp32",
43            "esp32c2",
44            "esp32c3",
45            "esp32c6",
46            "esp32h2",
47            "esp32s2",
48            "esp32s3",
49        ),
50        "LICENSE": "dual licensed under the Apache License Version 2.0 or the MIT license",
51    },
52)
53
54DESTINATION_DIR = os.path.join("esptool", "targets", "stub_flasher")
55
56README_TEMPLATE = """# Licensing
57
58The binaries in JSON format distributed in this directory are {LICENSE}. They were released at {URL} from where the sources can be obtained.
59"""
60
61
62def main():
63    for stub_set in STUBS:
64        dest_sub_dir = os.path.join(DESTINATION_DIR, stub_set["STUB_SET_VERSION"])
65
66        """ The directory is cleaned up so we would detect if a stub was just committed into the repository but the
67        name was not added into the FILE_LIST of STUBS. This would be an unwanted state because the checker would not
68        detect any changes in that stub."""
69        for old_file in glob.glob(os.path.join(dest_sub_dir, "*.json")):
70            print(f"Removing old file {old_file}")
71            os.remove(old_file)
72
73        for file_name in stub_set["FILE_LIST"]:
74            file = ".".join((file_name, "json"))
75            url = "/".join((stub_set["DOWNLOAD_URL"], stub_set["VERSION"], file))
76            dest = os.path.join(dest_sub_dir, file)
77            print(f"Downloading {url} to {dest}")
78            urllib.request.urlretrieve(url, dest)
79
80        with open(os.path.join(dest_sub_dir, "README.md"), "w") as f:
81            print(f"Writing README to {f.name}")
82            f.write(
83                README_TEMPLATE.format(
84                    LICENSE=stub_set["LICENSE"],
85                    URL="/".join((stub_set["TAG_URL"], stub_set["VERSION"])),
86                )
87            )
88
89
90if __name__ == "__main__":
91    main()
92