11#!/usr/bin/env python3
22
3+ import argparse
4+ import hashlib
5+ import json
36import os
47import shutil
5- import json
6- import hashlib
8+ import subprocess
79import sys
810from pathlib import Path
9- import argparse
10- import subprocess
1111
1212DOWNLOAD_URL = "https://cloud-downloads.arduino.cc"
1313
1616 "crypto" : "binaries/provision/crypto" ,
1717}
1818
19- SKETCH_NAMES = {
20- "lora" : "LoraProvision" ,
21- "crypto" : "CryptoProvision"
22- }
19+ SKETCH_NAMES = {"lora" : "LoraProvision" , "crypto" : "CryptoProvision" }
2320
2421INDEX_PATH = "binaries/index.json"
2522
@@ -41,23 +38,25 @@ def sha2(file_path):
4138 with open (file_path , "rb" ) as f :
4239 return hashlib .sha256 (f .read ()).hexdigest ()
4340
41+
4442# Runs arduino-cli
4543def arduino_cli (cli_path , args = None ):
4644 if args is None :
47- args = []
45+ args = []
4846 res = subprocess .run ([cli_path , * args ], capture_output = True , text = True , check = True )
4947 return res .stdout , res .stderr
5048
49+
5150def provision_binary_details (board ):
52- bin_path = PROVISION_BINARY_PATHS [board ["type" ]]
51+ bin_path = PROVISION_BINARY_PATHS [board ["type" ]]
5352 simple_fqbn = board ["fqbn" ].replace (":" , "." )
5453 sketch_dir = Path (__file__ ).parent / bin_path / simple_fqbn
5554 sketch_files = list (sketch_dir .iterdir ())
5655 # there should be only one binary file
5756 if len (sketch_files ) != 1 :
5857 print (f"Invalid binaries found in { sketch_dir } " )
5958 sys .exit (1 )
60- sketch_file = sketch_files [0 ]
59+ sketch_file = sketch_files [0 ]
6160
6261 sketch_dest = f"{ bin_path } /{ simple_fqbn } /{ sketch_file .name } "
6362 file_hash = sha2 (sketch_file )
@@ -68,6 +67,7 @@ def provision_binary_details(board):
6867 "size" : f"{ sketch_file .stat ().st_size } " ,
6968 }
7069
70+
7171def generate_index (boards ):
7272 index_json = {"boards" : []}
7373 for board in boards :
@@ -79,25 +79,38 @@ def generate_index(boards):
7979 with open (p , "w" ) as f :
8080 json .dump (index_json , f , indent = 2 )
8181
82+
8283def generate_binaries (arduino_cli_path , boards ):
8384 for board in boards :
8485 sketch_path = Path (__file__ ).parent / "provision" / SKETCH_NAMES [board ["type" ]]
8586 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+ )
9297 print (res , err )
9398 simple_fqbn = board ["fqbn" ].replace (":" , "." )
9499 # 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+ )
96103 os .makedirs (out , exist_ok = True )
97104 # 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+ )
99111 shutil .copy2 (compiled_bin , out / ("provision" + board ["ext" ]))
100112
113+
101114if __name__ == "__main__" :
102115 parser = argparse .ArgumentParser (prog = "generator.py" )
103116 parser .add_argument (
0 commit comments