forked from stm32duino/Arduino_Core_STM32
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfqbn.py
More file actions
115 lines (101 loc) · 3.64 KB
/
fqbn.py
File metadata and controls
115 lines (101 loc) · 3.64 KB
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
import os
import re
import sys
import json
import subprocess
import argparse
# List
fqbn_list = []
arduino_cli = ""
arduino_cli_path = ""
stm32_url = "https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json"
arduino_platform = "STMicroelectronics:stm32"
# Parser
parser = argparse.ArgumentParser(
description="List STM32 core fqbn(s) using arduino-cli (Default list all)."
)
parser.add_argument(
"-b", "--board", metavar="pattern", help="pattern to find one or more board(s) fqbn"
)
parser.add_argument(
"-p",
"--path",
metavar="<arduino-cli path>",
help="Path to the arduino-cli tool.",
)
args = parser.parse_args()
def check_config():
try:
output = subprocess.check_output(
[arduino_cli, "core", "search", "stm32", "--additional-urls", stm32_url],
stderr=subprocess.DEVNULL,
)
if arduino_platform not in output.decode("utf-8"):
raise subprocess.CalledProcessError(1, "re")
except subprocess.CalledProcessError:
print(arduino_platform + " is not installed!")
quit()
def get_fqbn_list():
fqbn_list_tmp = []
try:
output = subprocess.check_output(
[arduino_cli, "board", "listall", "--format", "json"],
stderr=subprocess.DEVNULL,
).decode("utf-8")
boards_list = json.loads(output)
if boards_list is not None:
for board in boards_list["boards"]:
if arduino_platform in board["fqbn"]:
fqbn_list_tmp.append(board["fqbn"])
if not len(fqbn_list_tmp):
raise subprocess.CalledProcessError(2, "No fqbn")
else:
raise subprocess.CalledProcessError(1, "No fqbn")
except subprocess.CalledProcessError:
print("No fqbn found for " + arduino_platform + "!")
quit()
# For STM32 core, pnum is requested
for fqbn in fqbn_list_tmp:
try:
output = subprocess.check_output(
[arduino_cli, "board", "details", "--format", "json", "-b", fqbn],
stderr=subprocess.DEVNULL,
).decode("utf-8")
board_detail = json.loads(output)
if board_detail is not None:
if "config_options" not in board_detail:
raise subprocess.CalledProcessError(3, "No config_options")
for option in board_detail["config_options"]:
if option["option"] == "pnum":
for value in option["values"]:
fqbn_list.append(fqbn + ":pnum=" + value["value"])
break
else:
raise subprocess.CalledProcessError(1, "No fqbn")
except subprocess.CalledProcessError as e:
print("No fqbn detail found for " + e.cmd + "!")
if len(fqbn_list) == 0:
print("No fqbn found for " + arduino_platform + "!")
quit()
def main():
global arduino_cli_path
global arduino_cli
if args.path:
arduino_cli_path = args.path
assert os.path.exists(
arduino_cli_path
), "Path does not exist: '{}'. Please check the path!".format(arduino_cli_path)
if sys.platform.startswith("win32"):
arduino_cli = os.path.join(arduino_cli_path, "arduino-cli.exe")
else:
arduino_cli = os.path.join(arduino_cli_path, "arduino-cli")
check_config()
get_fqbn_list()
if args.board:
arg_board_pattern = re.compile(args.board, re.IGNORECASE)
for fqbn in fqbn_list:
if args.board and arg_board_pattern.search(fqbn) is None:
continue
print(fqbn)
if __name__ == "__main__":
main()