Skip to content

Commit 55e4695

Browse files
committed
ci: harden python scripts
Signed-off-by: Frederic Pillon <[email protected]>
1 parent 06547a2 commit 55e4695

File tree

5 files changed

+122
-132
lines changed

5 files changed

+122
-132
lines changed

CI/update/fqbn.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def get_fqbn_list():
6666
for fqbn in fqbn_list_tmp:
6767
try:
6868
output = subprocess.check_output(
69-
[arduino_cli, "board", "details", "--format", "json", fqbn],
69+
[arduino_cli, "board", "details", "--format", "json", "-b", fqbn],
7070
stderr=subprocess.DEVNULL,
7171
).decode("utf-8")
7272
board_detail = json.loads(output)
@@ -106,8 +106,7 @@ def main():
106106
arg_board_pattern = re.compile(args.board, re.IGNORECASE)
107107

108108
for fqbn in fqbn_list:
109-
if args.board:
110-
if arg_board_pattern.search(fqbn) is None:
109+
if args.board and arg_board_pattern.search(fqbn) is None:
111110
continue
112111
print(fqbn)
113112

CI/update/stm32cube.py

+34-36
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,8 @@ def checkConfig():
9696
config_file_path = script_path / "update_config.json"
9797
if config_file_path.is_file():
9898
try:
99-
config_file = open(config_file_path, "r")
100-
path_config = json.load(config_file)
101-
config_file.close()
99+
with open(config_file_path, "r") as config_file:
100+
path_config = json.load(config_file)
102101
# Common path
103102
if "REPO_LOCAL_PATH" not in path_config:
104103
path_config["REPO_LOCAL_PATH"] = str(repo_local_path)
@@ -201,9 +200,8 @@ def createSystemFiles(serie):
201200
stm32_hal_conf_file = system_serie / stm32yyxx_hal_conf_file.replace(
202201
"yy", serie.lower()
203202
)
204-
out_file = open(stm32_hal_conf_file, "w", newline="\n")
205-
out_file.write(stm32yyxx_hal_conf_file_template.render(serie=serie))
206-
out_file.close()
203+
with open(stm32_hal_conf_file, "w", newline="\n") as out_file:
204+
out_file.write(stm32yyxx_hal_conf_file_template.render(serie=serie))
207205
# Copy system_stm32*.c file from CMSIS device template
208206
system_stm32_path = cmsis_dest_path / f"STM32{serie}xx" / "Source" / "Templates"
209207
filelist = sorted(system_stm32_path.glob("system_stm32*.c"))
@@ -365,36 +363,36 @@ def parseVersion(path, patterns):
365363
sub1_found = False
366364
sub2_found = False
367365
rc_found = False
368-
369-
for i, line in enumerate(open(path, encoding="utf8", errors="ignore")):
370-
for match in re.finditer(patterns[0], line):
371-
VERSION_MAIN = int(match.group(1), 16)
372-
main_found = True
373-
for match in re.finditer(patterns[1], line):
374-
VERSION_SUB1 = int(match.group(1), 16)
375-
sub1_found = True
376-
for match in re.finditer(patterns[2], line):
377-
VERSION_SUB2 = int(match.group(1), 16)
378-
sub2_found = True
379-
for match in re.finditer(patterns[3], line):
380-
VERSION_RC = int(match.group(1), 16)
381-
rc_found = True
382-
if main_found and sub1_found and sub2_found and rc_found:
383-
break
384-
else:
385-
print(f"Could not find the full version in {path}")
386-
if main_found:
387-
print(f"main version found: {VERSION_MAIN}")
388-
VERSION_MAIN = "FF"
389-
if sub1_found:
390-
print(f"sub1 version found: {VERSION_SUB1}")
391-
VERSION_SUB1 = "FF"
392-
if sub2_found:
393-
print(f"sub2 version found: {VERSION_SUB2}")
394-
VERSION_SUB2 = "FF"
395-
if rc_found:
396-
print(f"rc version found: {VERSION_RC}")
397-
VERSION_RC = "FF"
366+
with open(path, encoding="utf8", errors="ignore") as fp:
367+
for _i, line in enumerate(fp):
368+
for match in re.finditer(patterns[0], line):
369+
VERSION_MAIN = int(match.group(1), 16)
370+
main_found = True
371+
for match in re.finditer(patterns[1], line):
372+
VERSION_SUB1 = int(match.group(1), 16)
373+
sub1_found = True
374+
for match in re.finditer(patterns[2], line):
375+
VERSION_SUB2 = int(match.group(1), 16)
376+
sub2_found = True
377+
for match in re.finditer(patterns[3], line):
378+
VERSION_RC = int(match.group(1), 16)
379+
rc_found = True
380+
if main_found and sub1_found and sub2_found and rc_found:
381+
break
382+
else:
383+
print(f"Could not find the full version in {path}")
384+
if main_found:
385+
print(f"main version found: {VERSION_MAIN}")
386+
VERSION_MAIN = "FF"
387+
if sub1_found:
388+
print(f"sub1 version found: {VERSION_SUB1}")
389+
VERSION_SUB1 = "FF"
390+
if sub2_found:
391+
print(f"sub2 version found: {VERSION_SUB2}")
392+
VERSION_SUB2 = "FF"
393+
if rc_found:
394+
print(f"rc version found: {VERSION_RC}")
395+
VERSION_RC = "FF"
398396

399397
ret = f"{VERSION_MAIN}.{VERSION_SUB1}.{VERSION_SUB2}"
400398

CI/update/stm32svd.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ def checkConfig():
2828
config_file_path = script_path / "update_config.json"
2929
if config_file_path.is_file():
3030
try:
31-
config_file = open(config_file_path, "r")
32-
path_config = json.load(config_file)
33-
config_file.close()
31+
with open(config_file_path, "r") as config_file:
32+
path_config = json.load(config_file)
3433

3534
if "STM32CUBECLT_PATH" not in path_config:
3635
path_config["STM32CUBECLT_PATH"] = str(
@@ -159,8 +158,9 @@ def main():
159158
for serie in stm32_list:
160159
serie_dir = stm32_svd_dir / f"STM32{serie}xx"
161160
if not any(serie_dir.glob("*")):
162-
print (f"Folder {serie_dir} is empty.")
161+
print(f"Folder {serie_dir} is empty.")
163162
serie_dir.rmdir()
164163

164+
165165
if __name__ == "__main__":
166166
main()

CI/update/stm32variant.py

+54-61
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,8 @@ def get_gpio_af_numF1_default(pintofind, iptofind):
382382
# return "AFIO_" + iptofind .split("_")[0] + "_DISABLE"
383383
ip = iptofind.split("_")[0]
384384
afio_default = "AFIO_NONE"
385-
if pintofind in default_afio_f1:
386-
if ip in default_afio_f1[pintofind]:
387-
afio_default = default_afio_f1[pintofind][ip]
385+
if pintofind in default_afio_f1 and ip in default_afio_f1[pintofind]:
386+
afio_default = default_afio_f1[pintofind][ip]
388387
return afio_default
389388

390389

@@ -559,10 +558,9 @@ def store_xspi(pin, name, signal):
559558

560559
# Store SYS pins
561560
def store_sys(pin, name, signal):
562-
if "_WKUP" in signal:
563-
if not any(pin.replace("_C", "") in i for i in syswkup_list):
564-
signal = signal.replace("PWR", "SYS")
565-
syswkup_list.append([pin, name, signal])
561+
if "_WKUP" in signal and not any(pin.replace("_C", "") in i for i in syswkup_list):
562+
signal = signal.replace("PWR", "SYS")
563+
syswkup_list.append([pin, name, signal])
566564

567565

568566
# Store USB pins
@@ -1341,7 +1339,7 @@ def print_pinamevar():
13411339
alt_syswkup_list = []
13421340
for idx, syswkup_list in enumerate(syswkup_pins_list, start=1):
13431341
if len(syswkup_list) > 1:
1344-
for idx2, lst in enumerate(syswkup_list[1:], start=1):
1342+
for idx2, _lst in enumerate(syswkup_list[1:], start=1):
13451343
alt_syswkup_list.append(f"{idx}_{idx2}")
13461344
return alt_syswkup_list
13471345

@@ -1375,13 +1373,13 @@ def spi_pins_variant():
13751373
for ss in spissel_list:
13761374
ss_inst = ss[2].split("_", 1)[0]
13771375
if mosi_inst == ss_inst:
1378-
if "PNUM_NOT_DEFINED" == ss_pin:
1376+
if ss_pin == "PNUM_NOT_DEFINED":
13791377
ss_pin = ss[0].replace("_", "", 1)
1380-
elif "PNUM_NOT_DEFINED" == ss1_pin:
1378+
elif ss1_pin == "PNUM_NOT_DEFINED":
13811379
ss1_pin = ss[0].replace("_", "", 1)
1382-
elif "PNUM_NOT_DEFINED" == ss2_pin:
1380+
elif ss2_pin == "PNUM_NOT_DEFINED":
13831381
ss2_pin = ss[0].replace("_", "", 1)
1384-
elif "PNUM_NOT_DEFINED" == ss3_pin:
1382+
elif ss3_pin == "PNUM_NOT_DEFINED":
13851383
ss3_pin = ss[0].replace("_", "", 1)
13861384
break
13871385
break
@@ -1449,7 +1447,7 @@ def serial_pins_variant():
14491447
print("No serial instance number found!")
14501448
serialnum = "-1"
14511449
else:
1452-
serialtx_pin = serialtx_pin = "PNUM_NOT_DEFINED"
1450+
serialtx_pin = "PNUM_NOT_DEFINED"
14531451
serialnum = "-1"
14541452
print("No serial found!")
14551453
return dict(instance=serialnum, rx=serialrx_pin, tx=serialtx_pin)
@@ -1653,10 +1651,13 @@ def search_product_line(valueline: str, extra: str) -> str:
16531651
else:
16541652
break
16551653
if pline >= vline:
1656-
if extra and len(product_line_list) > idx_pline + 1:
1657-
if product_line_list[idx_pline + 1] == (product_line + extra):
1658-
# Look for the next product line if contains the extra
1659-
product_line = product_line_list[idx_pline + 1]
1654+
if (
1655+
extra
1656+
and len(product_line_list) > idx_pline + 1
1657+
and product_line_list[idx_pline + 1] == (product_line + extra)
1658+
):
1659+
# Look for the next product line if contains the extra
1660+
product_line = product_line_list[idx_pline + 1]
16601661
break
16611662
else:
16621663
# In case of CMSIS device does not exist
@@ -1700,9 +1701,7 @@ def parse_stm32targets():
17001701

17011702

17021703
def search_svdfile(mcu_name):
1703-
svd_file = ""
1704-
if mcu_name in svd_dict:
1705-
svd_file = svd_dict[mcu_name]
1704+
svd_file = svd_dict.get(mcu_name, "")
17061705
return svd_file
17071706

17081707

@@ -2275,7 +2274,7 @@ def merge_dir(out_temp_path, group_mcu_dir, mcu_family, periph_xml, variant_exp)
22752274
# Save board entry
22762275
skip = False
22772276
with open(dir_name / boards_entry_filename) as fp:
2278-
for index, line in enumerate(fp):
2277+
for _index, line in enumerate(fp):
22792278
# Skip until next empty line (included)
22802279
if skip:
22812280
if line == "\n":
@@ -2391,24 +2390,23 @@ def aggregate_dir():
23912390
periph_xml_tmp = []
23922391
variant_exp_tmp = []
23932392
for index2, fname in enumerate(mcu_dir1_files_list):
2394-
with open(fname, "r") as f1:
2395-
with open(mcu_dir2_files_list[index2], "r") as f2:
2396-
diff = set(f1).symmetric_difference(f2)
2397-
diff.discard("\n")
2398-
if not diff or len(diff) == 2:
2399-
if index2 == 0:
2400-
for line in diff:
2401-
periph_xml_tmp += periperalpins_regex.findall(
2402-
line
2403-
)
2404-
elif index2 == 2:
2405-
for line in diff:
2406-
variant_exp_tmp += variant_regex.findall(line)
2407-
continue
2408-
else:
2409-
# Not the same directory compare with the next one
2410-
index += 1
2411-
break
2393+
with open(fname, "r") as f1, open(
2394+
mcu_dir2_files_list[index2], "r"
2395+
) as f2:
2396+
diff = set(f1).symmetric_difference(f2)
2397+
diff.discard("\n")
2398+
if not diff or len(diff) == 2:
2399+
if index2 == 0:
2400+
for line in diff:
2401+
periph_xml_tmp += periperalpins_regex.findall(line)
2402+
elif index2 == 2:
2403+
for line in diff:
2404+
variant_exp_tmp += variant_regex.findall(line)
2405+
continue
2406+
else:
2407+
# Not the same directory compare with the next one
2408+
index += 1
2409+
break
24122410
# All files compared and matched
24132411
else:
24142412
# Concatenate lists without duplicate
@@ -2505,10 +2503,11 @@ def checkConfig():
25052503
default_cubemxdir()
25062504
if config_filename.is_file():
25072505
try:
2508-
config_file = open(config_filename, "r")
2509-
path_config = json.load(config_file)
2510-
config_file.close()
2511-
2506+
# config_file = open(config_filename, "r")
2507+
# path_config = json.load(config_file)
2508+
# config_file.close()
2509+
with open(config_filename, "r") as config_file:
2510+
path_config = json.load(config_file)
25122511
if "REPO_LOCAL_PATH" not in path_config:
25132512
path_config["REPO_LOCAL_PATH"] = str(repo_local_path)
25142513
defaultConfig(config_filename, path_config)
@@ -2779,21 +2778,21 @@ def manage_repo():
27792778
generic_clock_filepath = out_temp_path / generic_clock_filename
27802779
out_temp_path.mkdir(parents=True, exist_ok=True)
27812780

2782-
# open output file
2783-
periph_c_file = open(periph_c_filepath, "w", newline="\n")
2784-
pinvar_h_file = open(pinvar_h_filepath, "w", newline="\n")
2785-
variant_cpp_file = open(variant_cpp_filepath, "w", newline="\n")
2786-
variant_h_file = open(variant_h_filepath, "w", newline="\n")
2787-
boards_entry_file = open(boards_entry_filepath, "w", newline="\n")
2788-
generic_clock_file = open(generic_clock_filepath, "w", newline="\n")
27892781
parse_pins()
27902782
manage_af_and_alternate()
27912783

2792-
generic_list = print_boards_entry()
2793-
print_general_clock(generic_list)
2794-
print_peripheral()
2795-
alt_syswkup_list = print_pinamevar()
2796-
print_variant(generic_list, alt_syswkup_list)
2784+
with open(boards_entry_filepath, "w", newline="\n") as boards_entry_file:
2785+
generic_list = print_boards_entry()
2786+
with open(generic_clock_filepath, "w", newline="\n") as generic_clock_file:
2787+
print_general_clock(generic_list)
2788+
with open(periph_c_filepath, "w", newline="\n") as periph_c_file:
2789+
print_peripheral()
2790+
with open(pinvar_h_filepath, "w", newline="\n") as pinvar_h_file:
2791+
alt_syswkup_list = print_pinamevar()
2792+
with open(variant_cpp_filepath, "w", newline="\n") as variant_cpp_file, open(
2793+
variant_h_filepath, "w", newline="\n"
2794+
) as variant_h_file:
2795+
print_variant(generic_list, alt_syswkup_list)
27972796
del alt_syswkup_list[:]
27982797
del generic_list[:]
27992798
sum_io = len(io_list) + len(alt_list) + len(dualpad_list) + len(remap_list)
@@ -2810,12 +2809,6 @@ def manage_repo():
28102809

28112810
clean_all_lists()
28122811

2813-
periph_c_file.close()
2814-
pinvar_h_file.close()
2815-
variant_h_file.close()
2816-
variant_cpp_file.close()
2817-
boards_entry_file.close()
2818-
generic_clock_file.close()
28192812
xml_mcu.unlink()
28202813
xml_gpio.unlink()
28212814

0 commit comments

Comments
 (0)