-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerator.py
executable file
·126 lines (106 loc) · 4.04 KB
/
generator.py
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python3
import argparse
import hashlib
import json
import os
import shutil
import subprocess
import sys
from pathlib import Path
DOWNLOAD_URL = "https://cloud-downloads.arduino.cc"
PROVISION_BINARY_PATHS = {
"lora": "binaries/provision/lora",
"crypto": "binaries/provision/crypto",
}
SKETCH_NAMES = {"lora": "LoraProvision", "crypto": "CryptoProvision"}
INDEX_PATH = "binaries/index.json"
BOARDS = [
{"type": "crypto", "ext": ".bin", "fqbn": "arduino:samd:nano_33_iot"},
{"type": "crypto", "ext": ".bin", "fqbn": "arduino:samd:mkrwifi1010"},
{"type": "crypto", "ext": ".elf", "fqbn": "arduino:mbed_nano:nanorp2040connect"},
{"type": "crypto", "ext": ".bin", "fqbn": "arduino:mbed_portenta:envie_m7"},
{"type": "crypto", "ext": ".bin", "fqbn": "arduino:mbed_nicla:nicla_vision"},
{"type": "crypto", "ext": ".bin", "fqbn": "arduino:samd:mkr1000"},
{"type": "crypto", "ext": ".bin", "fqbn": "arduino:samd:mkrgsm1400"},
{"type": "crypto", "ext": ".bin", "fqbn": "arduino:samd:mkrnb1500"},
{"type": "crypto", "ext": ".bin", "fqbn": "arduino:mbed_opta:opta"},
{"type": "crypto", "ext": ".bin", "fqbn": "arduino:mbed_giga:giga"},
{"type": "lora", "ext": ".bin", "fqbn": "arduino:samd:mkrwan1300"},
{"type": "lora", "ext": ".bin", "fqbn": "arduino:samd:mkrwan1310"},
]
# Generates file SHA256
def sha2(file_path):
with open(file_path, "rb") as f:
return hashlib.sha256(f.read()).hexdigest()
# Runs arduino-cli
def arduino_cli(cli_path, args=None):
if args is None:
args = []
res = subprocess.run([cli_path, *args], capture_output=True, text=True, check=True)
return res.stdout, res.stderr
def provision_binary_details(board):
bin_path = PROVISION_BINARY_PATHS[board["type"]]
simple_fqbn = board["fqbn"].replace(":", ".")
sketch_dir = Path(__file__).parent / bin_path / simple_fqbn
sketch_files = list(sketch_dir.iterdir())
# there should be only one binary file
if len(sketch_files) != 1:
print(f"Invalid binaries found in {sketch_dir}")
sys.exit(1)
sketch_file = sketch_files[0]
sketch_dest = f"{bin_path}/{simple_fqbn}/{sketch_file.name}"
file_hash = sha2(sketch_file)
return {
"url": f"{DOWNLOAD_URL}/{sketch_dest}",
"checksum": f"SHA-256:{file_hash}",
"size": f"{sketch_file.stat().st_size}",
}
def generate_index(boards):
index_json = {"boards": []}
for board in boards:
index_board = {"fqbn": board["fqbn"]}
index_board["provision"] = provision_binary_details(board)
index_json["boards"].append(index_board)
p = Path(__file__).parent / INDEX_PATH
with open(p, "w") as f:
json.dump(index_json, f, indent=2)
def generate_binaries(arduino_cli_path, boards):
for board in boards:
sketch_path = Path(__file__).parent / "provision" / SKETCH_NAMES[board["type"]]
print(f"Compiling for {board['fqbn']}")
res, err = arduino_cli(
arduino_cli_path,
args=[
"compile",
"-e",
"-b",
board["fqbn"],
sketch_path,
],
)
print(res, err)
simple_fqbn = board["fqbn"].replace(":", ".")
# Make output directory
out = (
Path(__file__).parent / PROVISION_BINARY_PATHS[board["type"]] / simple_fqbn
)
os.makedirs(out, exist_ok=True)
# Copy the new binary file in the correct output directory
compiled_bin = (
sketch_path
/ "build"
/ simple_fqbn
/ (SKETCH_NAMES[board["type"]] + ".ino" + board["ext"])
)
shutil.copy2(compiled_bin, out / ("provision" + board["ext"]))
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="generator.py")
parser.add_argument(
"-a",
"--arduino-cli",
default="arduino-cli",
help="Path to arduino-cli executable",
)
args = parser.parse_args(sys.argv[1:])
generate_binaries(args.arduino_cli, BOARDS)
generate_index(BOARDS)