|
| 1 | +import os |
| 2 | +import re |
| 3 | +import sys |
| 4 | +import json |
| 5 | +import subprocess |
| 6 | +import argparse |
| 7 | + |
| 8 | + |
| 9 | +# List |
| 10 | +fqbn_list = [] |
| 11 | +arduino_cli = "" |
| 12 | +arduino_cli_path = "" |
| 13 | +stm32_url = "https://github.com/stm32duino/BoardManagerFiles/raw/master/STM32/package_stm_index.json" |
| 14 | +arduino_platform = "STM32:stm32" |
| 15 | + |
| 16 | +# Parser |
| 17 | +parser = argparse.ArgumentParser( |
| 18 | + description="List STM32 core fqbn(s) using arduino-cli (Default list all)." |
| 19 | +) |
| 20 | + |
| 21 | +parser.add_argument( |
| 22 | + "-b", "--board", metavar="pattern", help="pattern to find one or more board(s) fqbn" |
| 23 | +) |
| 24 | +parser.add_argument( |
| 25 | + "-p", "--path", metavar="<arduino-cli path>", help="Path to the arduino-cli tool.", |
| 26 | +) |
| 27 | +args = parser.parse_args() |
| 28 | + |
| 29 | + |
| 30 | +def check_config(): |
| 31 | + try: |
| 32 | + output = subprocess.check_output( |
| 33 | + [arduino_cli, "core", "search", "stm32", "--additional-urls", stm32_url], |
| 34 | + stderr=subprocess.DEVNULL, |
| 35 | + ) |
| 36 | + if arduino_platform not in output.decode("utf-8"): |
| 37 | + raise subprocess.CalledProcessError(1, "re") |
| 38 | + except subprocess.CalledProcessError: |
| 39 | + print(arduino_platform + " is not installed!") |
| 40 | + quit() |
| 41 | + |
| 42 | + |
| 43 | +def get_fqbn_list(): |
| 44 | + fqbn_list_tmp = [] |
| 45 | + try: |
| 46 | + output = subprocess.check_output( |
| 47 | + [arduino_cli, "board", "listall", "--format", "json"], |
| 48 | + stderr=subprocess.DEVNULL, |
| 49 | + ).decode("utf-8") |
| 50 | + boards_list = json.loads(output) |
| 51 | + if boards_list is not None: |
| 52 | + for board in boards_list["boards"]: |
| 53 | + if arduino_platform in board["FQBN"]: |
| 54 | + fqbn_list_tmp.append(board["FQBN"]) |
| 55 | + if not len(fqbn_list_tmp): |
| 56 | + raise subprocess.CalledProcessError(2, "No fqbn") |
| 57 | + else: |
| 58 | + raise subprocess.CalledProcessError(1, "No fqbn") |
| 59 | + except subprocess.CalledProcessError: |
| 60 | + print("No fqbn found for " + arduino_platform + "!") |
| 61 | + quit() |
| 62 | + # For STM32 core, pnum is requested |
| 63 | + for fqbn in fqbn_list_tmp: |
| 64 | + try: |
| 65 | + output = subprocess.check_output( |
| 66 | + [arduino_cli, "board", "details", "--format", "json", fqbn], |
| 67 | + stderr=subprocess.DEVNULL, |
| 68 | + ).decode("utf-8") |
| 69 | + board_detail = json.loads(output) |
| 70 | + if board_detail is not None: |
| 71 | + if "config_options" not in board_detail: |
| 72 | + raise subprocess.CalledProcessError(3, "No config_options") |
| 73 | + for option in board_detail["config_options"]: |
| 74 | + if option["option"] == "pnum": |
| 75 | + for value in option["values"]: |
| 76 | + fqbn_list.append(fqbn + ":pnum=" + value["value"]) |
| 77 | + break |
| 78 | + else: |
| 79 | + raise subprocess.CalledProcessError(1, "No fqbn") |
| 80 | + except subprocess.CalledProcessError as e: |
| 81 | + print("No fqbn detail found for " + e.cmd + "!") |
| 82 | + if len(fqbn_list) == 0: |
| 83 | + print("No fqbn found for " + arduino_platform + "!") |
| 84 | + quit() |
| 85 | + |
| 86 | + |
| 87 | +def main(): |
| 88 | + global arduino_cli_path |
| 89 | + global arduino_cli |
| 90 | + if args.path: |
| 91 | + arduino_cli_path = args.path |
| 92 | + assert os.path.exists( |
| 93 | + arduino_cli_path |
| 94 | + ), "Path does not exist: '{}'. Please check the path!".format( |
| 95 | + arduino_cli_path |
| 96 | + ) |
| 97 | + if sys.platform.startswith("win32"): |
| 98 | + arduino_cli = os.path.join(arduino_cli_path, "arduino-cli.exe") |
| 99 | + else: |
| 100 | + arduino_cli = os.path.join(arduino_cli_path, "arduino-cli") |
| 101 | + check_config() |
| 102 | + get_fqbn_list() |
| 103 | + |
| 104 | + if args.board: |
| 105 | + arg_board_pattern = re.compile(args.board, re.IGNORECASE) |
| 106 | + |
| 107 | + for fqbn in fqbn_list: |
| 108 | + if args.board: |
| 109 | + if arg_board_pattern.search(fqbn) is None: |
| 110 | + continue |
| 111 | + print(fqbn) |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + main() |
0 commit comments