|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import shutil |
| 5 | +import json |
| 6 | +import hashlib |
| 7 | +import sys |
| 8 | +from pathlib import Path |
| 9 | +import argparse |
| 10 | +import subprocess |
| 11 | + |
| 12 | +DOWNLOAD_URL = "https://cloud-downloads.arduino.cc" |
| 13 | + |
| 14 | +PROVISION_BINARY_PATHS = { |
| 15 | + "lora": "binaries/provision/lora", |
| 16 | + "crypto": "binaries/provision/crypto", |
| 17 | +} |
| 18 | + |
| 19 | +SKETCH_NAMES = { |
| 20 | + "lora": "LoraProvision", |
| 21 | + "crypto": "CryptoProvision" |
| 22 | +} |
| 23 | + |
| 24 | +INDEX_PATH = "binaries/index.json" |
| 25 | + |
| 26 | +BOARDS = [ |
| 27 | + {"type": "crypto", "ext": ".bin", "fqbn": "arduino:samd:nano_33_iot"}, |
| 28 | + {"type": "crypto", "ext": ".bin", "fqbn": "arduino:samd:mkrwifi1010"}, |
| 29 | + {"type": "crypto", "ext": ".elf", "fqbn": "arduino:mbed_nano:nanorp2040connect"}, |
| 30 | + {"type": "crypto", "ext": ".bin", "fqbn": "arduino:mbed_portenta:envie_m7"}, |
| 31 | + {"type": "crypto", "ext": ".bin", "fqbn": "arduino:samd:mkr1000"}, |
| 32 | + {"type": "crypto", "ext": ".bin", "fqbn": "arduino:samd:mkrgsm1400"}, |
| 33 | + {"type": "crypto", "ext": ".bin", "fqbn": "arduino:samd:mkrnb1500"}, |
| 34 | + {"type": "lora", "ext": ".bin", "fqbn": "arduino:samd:mkrwan1300"}, |
| 35 | + {"type": "lora", "ext": ".bin", "fqbn": "arduino:samd:mkrwan1310"}, |
| 36 | +] |
| 37 | + |
| 38 | +# Generates file SHA256 |
| 39 | +def sha2(file_path): |
| 40 | + with open(file_path, "rb") as f: |
| 41 | + return hashlib.sha256(f.read()).hexdigest() |
| 42 | + |
| 43 | +# Runs arduino-cli |
| 44 | +def arduino_cli(cli_path, args=None): |
| 45 | + if args is None: |
| 46 | + args=[] |
| 47 | + res = subprocess.run([cli_path, *args], capture_output=True, text=True, check=True) |
| 48 | + return res.stdout, res.stderr |
| 49 | + |
| 50 | +def provision_binary_details(board): |
| 51 | + bin_path = PROVISION_BINARY_PATHS[board["type"]] |
| 52 | + simple_fqbn = board["fqbn"].replace(":", ".") |
| 53 | + sketch_dir = Path(__file__).parent / bin_path / simple_fqbn |
| 54 | + sketch_files = list(sketch_dir.iterdir()) |
| 55 | + # there should be only one binary file |
| 56 | + if len(sketch_files) != 1: |
| 57 | + print(f"Invalid binaries found in {sketch_dir}") |
| 58 | + sys.exit(1) |
| 59 | + sketch_file = sketch_files[0] |
| 60 | + |
| 61 | + sketch_dest = f"{bin_path}/{simple_fqbn}/{sketch_file.name}" |
| 62 | + file_hash = sha2(sketch_file) |
| 63 | + |
| 64 | + return { |
| 65 | + "url": f"{DOWNLOAD_URL}/{sketch_dest}", |
| 66 | + "checksum": f"SHA-256:{file_hash}", |
| 67 | + "size": f"{sketch_file.stat().st_size}", |
| 68 | + } |
| 69 | + |
| 70 | +def generate_index(boards): |
| 71 | + index_json = {"boards": []} |
| 72 | + for board in boards: |
| 73 | + index_board = {"fqbn": board["fqbn"]} |
| 74 | + index_board["provision"] = provision_binary_details(board) |
| 75 | + index_json["boards"].append(index_board) |
| 76 | + |
| 77 | + p = Path(__file__).parent / INDEX_PATH |
| 78 | + with open(p, "w") as f: |
| 79 | + json.dump(index_json, f, indent=2) |
| 80 | + |
| 81 | +def generate_binaries(arduino_cli_path, boards): |
| 82 | + for board in boards: |
| 83 | + sketch_path = Path(__file__).parent / "provision" / SKETCH_NAMES[board["type"]] |
| 84 | + print(f"Compiling for {board['fqbn']}") |
| 85 | + res, err = arduino_cli(arduino_cli_path, args=[ |
| 86 | + "compile", |
| 87 | + "-e", |
| 88 | + "-b", board["fqbn"], |
| 89 | + sketch_path, |
| 90 | + ]) |
| 91 | + print(res, err) |
| 92 | + simple_fqbn = board["fqbn"].replace(":", ".") |
| 93 | + # Make output directory |
| 94 | + out = Path(__file__).parent / PROVISION_BINARY_PATHS[board["type"]] / simple_fqbn |
| 95 | + os.makedirs(out, exist_ok=True) |
| 96 | + # Copy the new binary file in the correct output directory |
| 97 | + compiled_bin = sketch_path / "build" / simple_fqbn / (SKETCH_NAMES[board["type"]] + ".ino" + board["ext"]) |
| 98 | + shutil.copy2(compiled_bin, out / ("provision" + board["ext"])) |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + parser = argparse.ArgumentParser(prog="generator.py") |
| 102 | + parser.add_argument( |
| 103 | + "-a", |
| 104 | + "--arduino-cli", |
| 105 | + default="arduino-cli", |
| 106 | + help="Path to arduino-cli executable", |
| 107 | + ) |
| 108 | + args = parser.parse_args(sys.argv[1:]) |
| 109 | + generate_binaries(args.arduino_cli, BOARDS) |
| 110 | + generate_index(BOARDS) |
0 commit comments