Skip to content

Commit 0f5e062

Browse files
authored
Merge pull request #14939 from jeromecoutant/PR_PIN_ALIAS
Standard Pin Names validation script update
2 parents 662bd59 + 97cd8e9 commit 0f5e062

File tree

1 file changed

+71
-30
lines changed

1 file changed

+71
-30
lines changed

hal/tests/pinvalidate/pinvalidate.py

Lines changed: 71 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def find_target_by_path(target_path):
5656

5757
with open(target_path) as pin_names_file:
5858
pin_names_file_content = pin_names_file.read()
59-
59+
6060
target_list_match = re.search(
6161
"\/* MBED TARGET LIST: ([0-9A-Z_,* \n]+)*\/",
6262
pin_names_file_content
@@ -71,7 +71,7 @@ def find_target_by_path(target_path):
7171
re.MULTILINE,
7272
)
7373
)
74-
74+
7575
if not target_list:
7676
print("WARNING: MBED TARGET LIST marker invalid or not found in file " + target_path)
7777
print("Target could not be determined. Only the generic test suite will run. You can manually specify additional suites.")
@@ -98,18 +98,18 @@ def find_target_by_path(target_path):
9898
def find_target_by_name(target_name=""):
9999
"""Find a target by name."""
100100
mbed_os_root = pathlib.Path(__file__).absolute().parents[3]
101-
101+
102102
targets = dict()
103103

104104
for f in mbed_os_root.joinpath('targets').rglob("PinNames.h"):
105105
with open(f) as pin_names_file:
106106
pin_names_file_content = pin_names_file.read()
107-
107+
108108
target_list_match = re.search(
109109
"\/* MBED TARGET LIST: ([0-9A-Z_,* \n]+)*\/",
110110
pin_names_file_content
111111
)
112-
112+
113113
target_list = []
114114
if target_list_match:
115115
target_list = list(
@@ -127,7 +127,7 @@ def find_target_by_name(target_name=""):
127127
else:
128128
for target in target_list:
129129
targets[target] = f
130-
130+
131131
return targets
132132

133133

@@ -150,12 +150,12 @@ def check_markers(test_mode=False):
150150
for f in search_dir.rglob("PinNames.h"):
151151
with open(f) as pin_names_file:
152152
pin_names_file_content = pin_names_file.read()
153-
153+
154154
target_list_match = re.search(
155155
"\/* MBED TARGET LIST: ([0-9A-Z_,* \n]+)*\/",
156156
pin_names_file_content
157157
)
158-
158+
159159
marker_target_list = []
160160
if target_list_match:
161161
marker_target_list = list(
@@ -165,7 +165,7 @@ def check_markers(test_mode=False):
165165
re.MULTILINE,
166166
)
167167
)
168-
168+
169169
if not marker_target_list:
170170
print("WARNING: MBED TARGET LIST marker invalid or not found in file " + str(f))
171171
errors.append({ "file": str(f), "error": "marker invalid or not found"})
@@ -181,7 +181,7 @@ def check_markers(test_mode=False):
181181
if not target_is_valid:
182182
print("WARNING: MBED TARGET LIST in file " + str(f) + " includes target '" + target + "' which doesn't exist in targets.json or is not public")
183183
errors.append({ "file": str(f), "error": "target not found"})
184-
184+
185185
return errors
186186

187187

@@ -190,7 +190,7 @@ def check_duplicate_pinnames_files(test_mode=False):
190190
mbed_os_root = pathlib.Path(__file__).absolute().parents[3]
191191

192192
errors = []
193-
193+
194194
file_hash_dict = dict()
195195

196196
if test_mode:
@@ -202,20 +202,20 @@ def check_duplicate_pinnames_files(test_mode=False):
202202
with open(f) as pin_names_file:
203203
pin_names_file_content = pin_names_file.read()
204204
file_hash_dict[str(f)] = hashlib.md5(pin_names_file_content.encode('utf-8')).hexdigest()
205-
206-
rev_dict = {}
207-
for key, value in file_hash_dict.items():
208-
rev_dict.setdefault(value, set()).add(key)
209-
duplicates = [key for key, values in rev_dict.items()
210-
if len(values) > 1]
211-
205+
206+
rev_dict = {}
207+
for key, value in file_hash_dict.items():
208+
rev_dict.setdefault(value, set()).add(key)
209+
duplicates = [key for key, values in rev_dict.items()
210+
if len(values) > 1]
211+
212212
for duplicate in duplicates:
213213
print("WARNING: Duplicate files")
214214
for file_path, file_hash in file_hash_dict.items():
215215
if file_hash == duplicate:
216216
errors.append({ "file": file_path, "error": "duplicate file"})
217217
print("\t" + file_path)
218-
218+
219219
return errors
220220

221221
def check_duplicate_markers(test_mode=False):
@@ -225,7 +225,7 @@ def check_duplicate_markers(test_mode=False):
225225
errors = []
226226

227227
markers = dict()
228-
228+
229229
if test_mode:
230230
search_dir = pathlib.Path(__file__).parent.joinpath('test_files').absolute()
231231
else:
@@ -234,12 +234,12 @@ def check_duplicate_markers(test_mode=False):
234234
for f in search_dir.rglob("PinNames.h"):
235235
with open(f) as pin_names_file:
236236
pin_names_file_content = pin_names_file.read()
237-
237+
238238
target_list_match = re.search(
239239
"\/* MBED TARGET LIST: ([0-9A-Z_,* \n]+)*\/",
240240
pin_names_file_content
241241
)
242-
242+
243243
marker_target_list = []
244244
if target_list_match:
245245
marker_target_list = list(
@@ -249,7 +249,7 @@ def check_duplicate_markers(test_mode=False):
249249
re.MULTILINE,
250250
)
251251
)
252-
252+
253253
for target in marker_target_list:
254254
if target in markers:
255255
print("WARNING: target duplicate in " + str(f) + ", " + target + " first listed in " + markers[target])
@@ -260,7 +260,7 @@ def check_duplicate_markers(test_mode=False):
260260
return errors
261261

262262

263-
def target_has_arduino_form_factor(target_name):
263+
def target_has_form_factor(target_name, form_factor):
264264
"""Check if the target has the Arduino form factor."""
265265
mbed_os_root = pathlib.Path(__file__).absolute().parents[3]
266266

@@ -272,7 +272,7 @@ def target_has_arduino_form_factor(target_name):
272272
if target_name in target_data:
273273
if "supported_form_factors" in target_data[target_name]:
274274
form_factors = target_data[target_name]["supported_form_factors"]
275-
if "ARDUINO_UNO" in form_factors:
275+
if form_factor in form_factors:
276276
return True
277277

278278
return False
@@ -440,6 +440,22 @@ def legacy_assignment_check(pin_name_content):
440440
invalid_items.append({"key": key, "val": val, "message": message})
441441
return invalid_items
442442

443+
444+
def legacy_alias_check(pin_name_content):
445+
invalid_items = []
446+
legacy_assignments = dict(
447+
re.findall(
448+
r"^\s*((?:SPI|I2C)_\w*)\s*=\s*([a-zA-Z0-9_]+)",
449+
pin_name_content,
450+
re.MULTILINE,
451+
)
452+
)
453+
for key, val in legacy_assignments.items():
454+
message = "legacy assignment; SPI_xxx and I2C_xxx must be #define'd"
455+
invalid_items.append({"key": key, "val": val, "message": message})
456+
return invalid_items
457+
458+
443459
def legacy_uart_check(pin_name_dict):
444460
invalid_items = []
445461
if "CONSOLE_TX" not in pin_name_dict or "CONSOLE_RX" not in pin_name_dict:
@@ -448,6 +464,13 @@ def legacy_uart_check(pin_name_dict):
448464
return invalid_items
449465

450466

467+
def legacy_arduino_uno_check(arduino_form_factor):
468+
invalid_items = []
469+
if arduino_form_factor == True:
470+
message = "ARDUINO form factor is deprecated, should be replaced by ARDUINO_UNO"
471+
invalid_items.append({"key": "", "val": "", "message": message})
472+
return invalid_items
473+
451474
def print_summary(report):
452475
targets = set([case["platform_name"] for case in report])
453476

@@ -610,13 +633,13 @@ def print_pretty_html_report(report):
610633
output.append("'>")
611634
output.append(case["result"])
612635
output.append("</td>")
613-
636+
614637
output.append("<td style='color:")
615638
output.append(count_color)
616639
output.append("'>")
617640
output.append(str(len(case["errors"])))
618641
output.append("</td>")
619-
642+
620643
output.append("<td>")
621644
output.extend(error_details)
622645
output.append("</td>")
@@ -662,12 +685,24 @@ def has_passed_all_test_cases(report):
662685
"case_function": legacy_assignment_check,
663686
"case_input": "content",
664687
},
688+
{
689+
"suite_name": "generic",
690+
"case_name": "alias",
691+
"case_function": legacy_alias_check,
692+
"case_input": "content",
693+
},
665694
{
666695
"suite_name": "generic",
667696
"case_name": "uart",
668697
"case_function": legacy_uart_check,
669698
"case_input": "content",
670699
},
700+
{
701+
"suite_name": "generic",
702+
"case_name": "arduino_formfactor",
703+
"case_function": legacy_arduino_uno_check,
704+
"case_input": "arduino_form_factor",
705+
},
671706
{
672707
"suite_name": "arduino_uno",
673708
"case_name": "duplicate",
@@ -718,21 +753,27 @@ def validate_pin_names(args):
718753

719754
pin_name_dict = pin_name_to_dict(pin_name_content)
720755

721-
arduino_support = target_has_arduino_form_factor(target)
756+
arduino_uno_support = target_has_form_factor(target, "ARDUINO_UNO")
757+
758+
arduino_support = target_has_form_factor(target, "ARDUINO")
722759

723760
for case in test_cases:
724761
if suites:
725762
if case["suite_name"] not in suites:
726763
continue
727764
else:
728-
if not arduino_support and case["suite_name"] == "arduino_uno":
765+
if not arduino_uno_support and case["suite_name"] == "arduino_uno":
729766
continue
767+
if not arduino_uno_support and not arduino_support and case["case_name"] == "arduino_formfactor":
768+
continue
769+
730770

731771
if case["case_input"] == "dict":
732772
case_input = pin_name_dict
733773
elif case["case_input"] == "content":
734774
case_input = pin_name_content
735-
775+
elif case["case_input"] == "arduino_form_factor":
776+
case_input = arduino_support
736777
case_output = case["case_function"](case_input)
737778

738779
case_result = "FAILED" if case_output else "PASSED"

0 commit comments

Comments
 (0)