Skip to content

Lint firmware generator #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 32 additions & 19 deletions firmware/generator.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/env python3

import argparse
import hashlib
import json
import os
import shutil
import json
import hashlib
import subprocess
import sys
from pathlib import Path
import argparse
import subprocess

DOWNLOAD_URL = "https://cloud-downloads.arduino.cc"

Expand All @@ -16,10 +16,7 @@
"crypto": "binaries/provision/crypto",
}

SKETCH_NAMES = {
"lora": "LoraProvision",
"crypto": "CryptoProvision"
}
SKETCH_NAMES = {"lora": "LoraProvision", "crypto": "CryptoProvision"}

INDEX_PATH = "binaries/index.json"

Expand All @@ -40,23 +37,25 @@ 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=[]
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"]]
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_file = sketch_files[0]

sketch_dest = f"{bin_path}/{simple_fqbn}/{sketch_file.name}"
file_hash = sha2(sketch_file)
Expand All @@ -67,6 +66,7 @@ def provision_binary_details(board):
"size": f"{sketch_file.stat().st_size}",
}


def generate_index(boards):
index_json = {"boards": []}
for board in boards:
Expand All @@ -78,25 +78,38 @@ def generate_index(boards):
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,
])
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
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"])
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(
Expand Down