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