|
| 1 | +import os |
| 2 | +import urllib.request |
| 3 | +from multiprocessing import Pool |
| 4 | +import zipfile |
| 5 | +import re |
| 6 | +from functools import partial |
| 7 | + |
| 8 | +# Download a variant's bootloader |
| 9 | +def download_variant(variant, version): |
| 10 | + |
| 11 | + # Download from bootloader release |
| 12 | + f_zip = f"tinyuf2-{variant}-{version}.zip" |
| 13 | + url_prefix = ( |
| 14 | + f"https://github.com/fobe-projects/fobe-tinyuf2/releases/download/{version}/" |
| 15 | + ) |
| 16 | + |
| 17 | + # remove existing bootloader files |
| 18 | + if os.path.exists(f"variants/{variant}/bootloader-tinyuf2.bin"): |
| 19 | + os.remove(f"variants/{variant}/bootloader-tinyuf2.bin") |
| 20 | + if os.path.exists(f"variants/{variant}/tinyuf2.uf2"): |
| 21 | + os.remove(f"variants/{variant}/tinyuf2.uf2") |
| 22 | + |
| 23 | + print(f"Downloading {f_zip}") |
| 24 | + urllib.request.urlretrieve(url_prefix + f_zip, f"variants/{variant}/{f_zip}") |
| 25 | + if os.path.exists(f"variants/{variant}/{f_zip}"): |
| 26 | + print(f"Downloaded {f_zip}") |
| 27 | + with zipfile.ZipFile(f"variants/{variant}/{f_zip}", "r") as zip_ref: |
| 28 | + for member in zip_ref.namelist(): |
| 29 | + if member.endswith('tinyuf2.bin'): |
| 30 | + # Extract and rename tinyuf2.bin |
| 31 | + zip_ref.extract(member, f"variants/{variant}/") |
| 32 | + extracted_path = f"variants/{variant}/{member}" |
| 33 | + new_name = f"variants/{variant}/tinyuf2-{version}.bin" |
| 34 | + os.rename(extracted_path, new_name) |
| 35 | + elif member.endswith('bootloader.bin'): |
| 36 | + # Extract and rename bootloader.bin |
| 37 | + zip_ref.extract(member, f"variants/{variant}/") |
| 38 | + extracted_path = f"variants/{variant}/{member}" |
| 39 | + new_name = f"variants/{variant}/bootloader-tinyuf2-{version}.bin" |
| 40 | + os.rename(extracted_path, new_name) |
| 41 | + |
| 42 | + # Clean up the zip file |
| 43 | + os.remove(f"variants/{variant}/{f_zip}") |
| 44 | + print(f"Cleaned up {f_zip}") |
| 45 | + |
| 46 | + |
| 47 | +if __name__ == "__main__": |
| 48 | + # Detect version from boards.txt |
| 49 | + version = "" |
| 50 | + with open("boards.txt") as pf: |
| 51 | + platform_txt = pf.read() |
| 52 | + match = re.search(r"bootloader-tinyuf2-(\d+\.\d+\.\d+)", platform_txt) |
| 53 | + if match: |
| 54 | + version = match.group(1) |
| 55 | + |
| 56 | + print(f"version {version}") |
| 57 | + |
| 58 | + # Get all variants |
| 59 | + all_variant = [] |
| 60 | + for entry in os.scandir("variants"): |
| 61 | + if entry.is_dir(): |
| 62 | + all_variant.append(entry.name) |
| 63 | + all_variant.sort() |
| 64 | + |
| 65 | + download_with_version = partial(download_variant, version=version) |
| 66 | + with Pool() as p: |
| 67 | + p.map(download_with_version, all_variant) |
0 commit comments