Skip to content

Commit 6da8759

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 c52765f commit 6da8759

File tree

1 file changed

+52
-33
lines changed

1 file changed

+52
-33
lines changed

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

+52-33
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()
244-
except subprocess.CalledProcessError:
245-
print(arduino_platform + " is not installed!")
246-
quit()
257+
print("No arduino-cli config!")
258+
quit(1)
247259

248260

249261
def load_core_config():
@@ -386,7 +398,7 @@ def manage_inos():
386398
break
387399
else:
388400
print("Sketch {} path does not exist!".format(args.ino))
389-
quit()
401+
quit(1)
390402
# Sketches listed in a file
391403
elif args.file:
392404
assert os.path.exists(args.file), "Sketches list file does not exist"
@@ -417,7 +429,7 @@ def manage_inos():
417429
sketch_list.append(sketch_default)
418430
if len(sketch_list) == 0:
419431
print("No sketch to build for " + arduino_platform + "!")
420-
quit()
432+
quit(1)
421433

422434

423435
# Find all .ino files and save directory
@@ -449,32 +461,41 @@ 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"])
459-
if not len(fqbn_list_tmp):
460-
raise subprocess.CalledProcessError(2, "No fqbn")
461-
else:
462-
raise subprocess.CalledProcessError(1, "No fqbn")
463-
except subprocess.CalledProcessError:
464-
print("No fqbn found for " + arduino_platform + "!")
465-
quit()
476+
if not len(fqbn_list_tmp):
477+
print("No boards found for " + arduino_platform)
478+
quit(1)
466479

467480
# For STM32 core, pnum is requested
468481
for fqbn in fqbn_list_tmp:
469482
try:
470483
output = subprocess.check_output(
471484
[arduino_cli, "board", "details", "--format", "json", fqbn],
472-
stderr=subprocess.DEVNULL,
485+
stderr=subprocess.STDOUT,
473486
).decode("utf-8")
487+
except subprocess.CalledProcessError as e:
488+
print(
489+
'"' + " ".join(e.cmd) + '" failed with code: {}!'.format(e.returncode)
490+
)
491+
print(e.stdout)
492+
quit(e.returncode)
493+
else:
474494
board_detail = json.loads(output)
475495
if board_detail is not None:
476496
if "config_options" not in board_detail:
477-
raise subprocess.CalledProcessError(3, "No config_options")
497+
print("No config_options found for " + fqbn)
498+
quit(1)
478499
for option in board_detail["config_options"]:
479500
if option["option"] == "pnum":
480501
for value in option["values"]:
@@ -486,14 +507,12 @@ def find_board():
486507
)
487508
break
488509
else:
489-
raise subprocess.CalledProcessError(1, "No fqbn")
490-
except subprocess.CalledProcessError as e:
491-
print("No fqbn detail found for " + e.cmd + "!")
510+
print('No detail found for:"' + fqbn + '"!')
492511
if board_found:
493512
board_fqbn = collections.OrderedDict(sorted(board_found.items()))
494513
else:
495514
print("No board found for " + arduino_platform + "!")
496-
quit()
515+
quit(1)
497516

498517

499518
# Check the status

0 commit comments

Comments
 (0)