Skip to content

Commit 09ad104

Browse files
committed
[CI] Harden exception handling
If the executed command failed return the output and the error code. Signed-off-by: Frederic Pillon <[email protected]>
1 parent d830f6a commit 09ad104

File tree

1 file changed

+50
-28
lines changed

1 file changed

+50
-28
lines changed

Diff for: CI/build/arduino-cli.py

+50-28
Original file line numberDiff line numberDiff line change
@@ -200,50 +200,62 @@ def check_config():
200200
output = subprocess.check_output(
201201
[arduino_cli, "version"], stderr=subprocess.DEVNULL,
202202
)
203+
except subprocess.CalledProcessError as e:
204+
print('"' + " ".join(e.cmd) + '" failed with code: {}!'.format(e.returncode))
205+
print(e.stdout)
206+
quit(e.returncode)
207+
else:
203208
res = re.match(r".*Version:\s+(\d+\.\d+\.\d+).*", output.decode("utf-8"))
204-
205209
if res:
206210
arduino_cli_version = res.group(1)
207211
print("Arduino CLI version used: " + arduino_cli_version)
208212
else:
209-
raise subprocess.CalledProcessError(1, "re")
210-
except subprocess.CalledProcessError:
211-
print(
212-
"Unable to define Arduino CLI version, use default: "
213-
+ arduino_cli_default_version
214-
)
213+
print(
214+
"Unable to define Arduino CLI version, use default: "
215+
+ arduino_cli_default_version
216+
)
215217

216218
try:
217219
output = subprocess.check_output(
218220
[arduino_cli, "core", "search", "stm32", "--additional-urls", stm32_url],
219221
stderr=subprocess.DEVNULL,
220222
)
223+
except subprocess.CalledProcessError as e:
224+
print('"' + " ".join(e.cmd) + '" failed with code: {}!'.format(e.returncode))
225+
print(e.stdout)
226+
quit(e.returncode)
227+
else:
221228
if arduino_platform not in output.decode("utf-8"):
222-
raise subprocess.CalledProcessError(1, "re")
229+
print(arduino_platform + " is not installed!")
230+
quit(1)
223231
# Add core and library path to sketches_path_list
224232
try:
225233
output = subprocess.check_output(
226234
[arduino_cli, "config", "dump", "--format", "json"],
227235
stderr=subprocess.DEVNULL,
228236
).decode("utf-8")
237+
except subprocess.CalledProcessError as e:
238+
print(
239+
'"' + " ".join(e.cmd) + '" failed with code: {}!'.format(e.returncode)
240+
)
241+
print(e.stdout)
242+
quit(e.returncode)
243+
else:
229244
cli_config = json.loads(output)
230245
if cli_config is not None:
231246
if cli_config["directories"]["data"] is not None:
232247
sketches_path_list.append(cli_config["directories"]["data"])
233248
else:
234-
raise subprocess.CalledProcessError(3, "No data directory")
249+
print("No data directory")
250+
quit(1)
235251
if cli_config["directories"]["user"] is not None:
236252
sketches_path_list.append(cli_config["directories"]["user"])
237253
else:
238-
raise subprocess.CalledProcessError(2, "No user directory")
254+
print("No user directory!")
255+
quit(1)
239256
else:
240-
raise subprocess.CalledProcessError(1, "No fqbn")
241-
except subprocess.CalledProcessError:
242-
print("No arduino-cli config!")
243-
quit(1)
244-
except subprocess.CalledProcessError:
245-
print(arduino_platform + " is not installed!")
246-
quit(1)
257+
print("No arduino-cli config!")
258+
quit(1)
247259

248260

249261
def load_core_config():
@@ -449,32 +461,44 @@ def find_board():
449461
try:
450462
output = subprocess.check_output(
451463
[arduino_cli, "board", "listall", "--format", "json"],
452-
stderr=subprocess.DEVNULL,
464+
stderr=subprocess.STDOUT,
453465
).decode("utf-8")
466+
except subprocess.CalledProcessError as e:
467+
print('"' + " ".join(e.cmd) + '" failed with code: {}!'.format(e.returncode))
468+
print(e.stdout)
469+
quit(e.returncode)
470+
else:
454471
boards_list = json.loads(output)
455472
if boards_list is not None:
456473
for board in boards_list["boards"]:
457474
if arduino_platform in board["FQBN"]:
458475
fqbn_list_tmp.append(board["FQBN"])
459476
if not len(fqbn_list_tmp):
460-
raise subprocess.CalledProcessError(2, "No fqbn")
477+
print("No boards found for " + arduino_platform)
478+
quit(1)
461479
else:
462-
raise subprocess.CalledProcessError(1, "No fqbn")
463-
except subprocess.CalledProcessError:
464-
print("No fqbn found for " + arduino_platform + "!")
465-
quit(1)
480+
print("No boards found" + arduino_platform)
481+
quit(1)
466482

467483
# For STM32 core, pnum is requested
468484
for fqbn in fqbn_list_tmp:
469485
try:
470486
output = subprocess.check_output(
471487
[arduino_cli, "board", "details", "--format", "json", fqbn],
472-
stderr=subprocess.DEVNULL,
488+
stderr=subprocess.STDOUT,
473489
).decode("utf-8")
490+
except subprocess.CalledProcessError as e:
491+
print(
492+
'"' + " ".join(e.cmd) + '" failed with code: {}!'.format(e.returncode)
493+
)
494+
print(e.stdout)
495+
quit(e.returncode)
496+
else:
474497
board_detail = json.loads(output)
475498
if board_detail is not None:
476499
if "config_options" not in board_detail:
477-
raise subprocess.CalledProcessError(3, "No config_options")
500+
print("No config_options found for " + fqbn)
501+
quit(1)
478502
for option in board_detail["config_options"]:
479503
if option["option"] == "pnum":
480504
for value in option["values"]:
@@ -486,9 +510,7 @@ def find_board():
486510
)
487511
break
488512
else:
489-
raise subprocess.CalledProcessError(1, "No fqbn")
490-
except subprocess.CalledProcessError as e:
491-
print('No fqbn detail found for:"' + " ".join(e.cmd) + '"!')
513+
print('No detail found for:"' + fqbn + '"!')
492514
if board_found:
493515
board_fqbn = collections.OrderedDict(sorted(board_found.items()))
494516
else:

0 commit comments

Comments
 (0)