-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathaci_pinvalidate.py
677 lines (558 loc) · 19.3 KB
/
aci_pinvalidate.py
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
#!/usr/bin/env python
"""
Copyright (c) 2020 ARM Limited
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import argparse
import json
import pathlib
import re
import sys
import os
from tabulate import tabulate
from itertools import chain
from enum import Enum
class ReturnCode(Enum):
"""Return codes."""
SUCCESS = 0
ERROR = 1
INVALID_OPTIONS = 2
class TestCaseError(Exception):
"""An exception for test case failure."""
class ArgumentParserWithDefaultHelp(argparse.ArgumentParser):
"""Subclass that always shows the help message on invalid arguments."""
def error(self, message):
"""Error handler."""
sys.stderr.write("error: {}\n".format(message))
self.print_help()
raise SystemExit(ReturnCode.INVALID_OPTIONS.value)
def find_target_by_path(target_path):
"""Find a target by path."""
parsed_target = os.path.basename(os.path.dirname(target_path)).replace("TARGET_", "")
targets = dict()
with (
pathlib.Path().joinpath("custom_targets.json")
).open() as targets_json_file:
target_data = json.load(targets_json_file)
if parsed_target in target_data:
targets[parsed_target] = target_path
return targets
def find_target_by_name(target_name=""):
"""Find a target by name."""
targets = dict()
for f in pathlib.Path().rglob("PinNames.h"):
if "mbed-os" in str(f):
continue
parsed_target = os.path.basename(os.path.dirname(f)).replace("TARGET_", "")
if target_name:
if target_name == parsed_target:
targets[target_name] = f
break
else:
targets[parsed_target] = f
return targets
def target_has_arduino_form_factor(target_name):
"""Check if the target has the Arduino form factor."""
mbed_os_root = pathlib.Path(__file__).absolute().parents[0]
with (
mbed_os_root.joinpath("custom_targets.json")
).open() as targets_json_file:
target_data = json.load(targets_json_file)
if target_name in target_data:
if "supported_form_factors" in target_data[target_name]:
form_factors = target_data[target_name]["supported_form_factors"]
if "ARDUINO_UNO" in form_factors:
return True
return False
def pin_name_to_dict(pin_name_file_content):
pin_name_enum_dict = dict()
pin_name_enum_match = re.search(
"typedef enum {\n([^}]*)\n} PinName;", pin_name_file_content
)
if pin_name_enum_match:
pin_name_enum_body = pin_name_enum_match.group(1)
pin_name_enum_dict = dict(
re.findall(
r"^\s*([a-zA-Z0-9_]+)\s*=\s*([a-zA-Z0-9_]+)",
pin_name_enum_body,
re.MULTILINE,
)
)
pin_name_define_dict = dict(
re.findall(
r"^#define\s+([a-zA-Z0-9_]+)\s+([a-zA-Z0-9_]+)",
pin_name_file_content,
re.MULTILINE,
)
)
return {**pin_name_enum_dict, **pin_name_define_dict}
def identity_assignment_check(pin_name_dict):
invalid_items = []
for key, val in pin_name_dict.items():
if val == key:
message = "cannot assign value to itself"
invalid_items.append({"key": key, "val": val, "message": message})
return invalid_items
def nc_assignment_check(pin_name_dict):
invalid_items = []
for key, val in pin_name_dict.items():
if re.match(r"^((LED|BUTTON)\d*|CONSOLE_TX|CONSOLE_RX|USBTX|USBRX)$", key):
if val == "NC":
message = "cannot be NC"
invalid_items.append(
{"key": key, "val": val, "message": message}
)
return invalid_items
def duplicate_assignment_check(pin_name_dict):
used_pins = []
used_pins_friendly = []
invalid_items = []
for key, val in pin_name_dict.items():
if re.match(r"^((LED|BUTTON)\d*|CONSOLE_TX|CONSOLE_RX|USBTX|USBRX)$", key):
if val == "NC":
continue
# resolve to literal
realval = val
depth = 0
while not re.match(
"(0x[0-9a-fA-F]+|[1-9][0-9]*|0[1-7][0-7]+|0b[01]+)[uUlL]{0,2}",
realval,
):
try:
realval = pin_name_dict[realval]
depth += 1
except KeyError:
break
if depth > 10:
break
if realval in used_pins:
message = (
"already assigned to "
+ used_pins_friendly[used_pins.index(realval)]
)
invalid_items.append(
{"key": key, "val": val, "message": message}
)
continue
used_pins.append(realval)
used_pins_friendly.append(key + " = " + val)
return invalid_items
def arduino_duplicate_assignment_check(pin_name_dict):
used_pins = []
used_pins_friendly = []
invalid_items = []
for key, val in pin_name_dict.items():
if re.match(r"^ARDUINO_UNO_[AD]\d+$", key):
if val == "NC":
continue
# resolve to literal
realval = val
depth = 0
while not re.match(
"(0x[0-9a-fA-F]+|[1-9][0-9]*|0[1-7][0-7]+|0b[01]+)[uUlL]{0,2}",
realval,
):
try:
realval = pin_name_dict[realval]
depth += 1
except KeyError:
break
if depth > 10:
break
if realval in used_pins:
message = (
"already assigned to "
+ used_pins_friendly[used_pins.index(realval)]
)
invalid_items.append(
{"key": key, "val": val, "message": message}
)
continue
used_pins.append(realval)
used_pins_friendly.append(key + " = " + val)
return invalid_items
def arduino_existence_check(pin_name_dict):
analog_pins = (f"ARDUINO_UNO_A{i}" for i in range(6))
digital_pins = (f"ARDUINO_UNO_D{i}" for i in range(16))
return [
{"key": pin, "val": "", "message": pin + " not defined"}
for pin in chain(analog_pins, digital_pins)
if pin not in pin_name_dict
]
def arduino_nc_assignment_check(pin_name_dict):
invalid_items = []
for key, val in pin_name_dict.items():
if re.match(r"^ARDUINO_UNO_[AD]\d+$", key):
if val == "NC":
message = "cannot be NC"
invalid_items.append(
{"key": key, "val": val, "message": message}
)
return invalid_items
def legacy_assignment_check(pin_name_content):
invalid_items = []
legacy_assignments = dict(
re.findall(
r"^\s*((?:LED|BUTTON)\d*)\s*=\s*([a-zA-Z0-9_]+)",
pin_name_content,
re.MULTILINE,
)
)
for key, val in legacy_assignments.items():
message = "legacy assignment; LEDs and BUTTONs must be #define'd"
invalid_items.append({"key": key, "val": val, "message": message})
return invalid_items
def legacy_alias_check(pin_name_content):
invalid_items = []
legacy_assignments = dict(
re.findall(
r"^\s*((?:I2C|SPI)_\w*)\s*=\s*([a-zA-Z0-9_]+)",
pin_name_content,
re.MULTILINE,
)
)
for key, val in legacy_assignments.items():
message = "alias assignment; I2C_xx and SPI_xxx must be #define'd"
invalid_items.append({"key": key, "val": val, "message": message})
return invalid_items
def legacy_uart_check(pin_name_dict):
invalid_items = []
if "CONSOLE_TX" not in pin_name_dict or "CONSOLE_RX" not in pin_name_dict:
message = "CONSOLE_TX or CONSOLE_RX are not defined; USBTX and USBRX are deprecated"
invalid_items.append({"key": "", "val": "", "message": message})
return invalid_items
def print_summary(report):
targets = set([case["platform_name"] for case in report])
table = []
for target in targets:
error_count = 0
for case in report:
if (
case["platform_name"] == target
and case["result"] == "FAILED"
):
error_count += 1
table.append(
[target, "FAILED" if error_count else "PASSED", error_count]
)
return tabulate(
table,
headers=["Platform name", "Result", "Error count"],
tablefmt="grid",
)
def print_suite_summary(report):
targets = set([case["platform_name"] for case in report])
table = []
for target in targets:
suites = set([
case["suite_name"]
for case in report
if case["platform_name"] == target
])
for suite in suites:
result = "PASSED"
error_count = 0
for case in report:
if case["platform_name"] != target:
continue
if case["suite_name"] != suite:
continue
if case["result"] == "FAILED":
result = "FAILED"
error_count += 1
table.append([target, suite, result, error_count])
return tabulate(
table,
headers=["Platform name", "Test suite", "Result", "Error count"],
tablefmt="grid",
)
def print_report(report, print_error_detail, tablefmt="grid"):
table = []
for case in report:
errors_str = []
for error in case["errors"]:
errors_str.append("\n")
if error["key"]:
errors_str.append(error["key"])
if error["val"]:
errors_str.append(" = ")
errors_str.append(error["val"])
if error["message"]:
errors_str.append(" <-- ")
errors_str.append(error["message"])
if not errors_str:
errors_str = "None"
if print_error_detail:
table.append(
(
case["platform_name"],
case["suite_name"],
case["case_name"],
case["result"],
len(case["errors"]),
"None" if not errors_str else "".join(errors_str).lstrip(),
)
)
else:
table.append(
(
case["platform_name"],
case["suite_name"],
case["case_name"],
case["result"],
len(case["errors"]),
)
)
return tabulate(
table,
headers=[
"Platform name",
"Test suite",
"Test case",
"Result",
"Error count",
"Errors",
],
tablefmt=tablefmt,
)
def print_pretty_html_report(report):
output = []
output.append("<html><head><style>table, td, tr { border: 2px solid black; border-collapse: collapse; padding: 5px; font-family: Helvetica, serif; }</style></head>")
output.append('<body><p><button onclick=\'e=document.getElementsByTagName("details");for(var i=0;i<e.length;i++){e[i].setAttribute("open","true")};\'>Expand all errors</button></p><table>')
output.append("<tr><th>Platform name</th><th>Test suite</th><th>Test case</th><th>Result</th><th>Error count</th><th>Errors</th></tr>")
for case in report:
output.append("<tr>")
if case["errors"]:
error_details = ["<details><summary>View errors</summary><table>"]
for error in case["errors"]:
error_details.append("<tr>")
if error["key"]:
error_details.append("<td>")
error_details.append(error["key"])
error_details.append("</td>")
if error["val"]:
error_details.append("<td>")
error_details.append(error["val"])
error_details.append("</td>")
if error["message"]:
error_details.append("<td>")
error_details.append(error["message"])
error_details.append("</td>")
error_details.append("</tr>")
error_details.append("</table></details>")
else:
error_details = []
output.append("<td>")
output.append(case["platform_name"])
output.append("</td>")
output.append("<td>")
output.append(case["suite_name"])
output.append("</td>")
output.append("<td>")
output.append(case["case_name"])
output.append("</td>")
if case["result"] == "PASSED":
color = "green"
count_color = "black"
else:
color = "red"
count_color = "red"
output.append("<td style='color:")
output.append(color)
output.append("'>")
output.append(case["result"])
output.append("</td>")
output.append("<td style='color:")
output.append(count_color)
output.append("'>")
output.append(str(len(case["errors"])))
output.append("</td>")
output.append("<td>")
output.extend(error_details)
output.append("</td>")
output.append("</tr>")
output.append("</table></body></table>")
return "".join(output)
def has_passed_all_test_cases(report):
"""Check that all test cases passed."""
for case in report:
if case["result"] == "FAILED":
return False
return True
test_cases = [
{
"suite_name": "generic",
"case_name": "identity",
"case_function": identity_assignment_check,
"case_input": "dict",
},
{
"suite_name": "generic",
"case_name": "nc",
"case_function": nc_assignment_check,
"case_input": "dict",
},
{
"suite_name": "generic",
"case_name": "duplicate",
"case_function": duplicate_assignment_check,
"case_input": "dict",
},
{
"suite_name": "generic",
"case_name": "legacy",
"case_function": legacy_assignment_check,
"case_input": "content",
},
{
"suite_name": "generic",
"case_name": "alias",
"case_function": legacy_alias_check,
"case_input": "content",
},
{
"suite_name": "generic",
"case_name": "uart",
"case_function": legacy_uart_check,
"case_input": "content",
},
{
"suite_name": "arduino_uno",
"case_name": "duplicate",
"case_function": arduino_duplicate_assignment_check,
"case_input": "dict",
},
{
"suite_name": "arduino_uno",
"case_name": "existence",
"case_function": arduino_existence_check,
"case_input": "dict",
},
{
"suite_name": "arduino_uno",
"case_name": "nc",
"case_function": arduino_nc_assignment_check,
"case_input": "dict",
},
]
def validate_pin_names(args):
"""Entry point for validating the Pin names."""
suites = []
targets = dict()
if args.paths:
paths = args.paths.split(",")
for path in paths:
targets = {**targets, **find_target_by_path(path)}
elif args.targets:
target_names = args.targets.split(",")
for target_name in target_names:
targets = {**targets, **find_target_by_name(target_name)}
elif args.all:
targets = find_target_by_name()
report = []
for target, path in targets.items():
pin_name_content = open(path).read()
pin_name_dict = pin_name_to_dict(pin_name_content)
arduino_support = target_has_arduino_form_factor(target)
for case in test_cases:
if suites:
if case["suite_name"] not in suites:
continue
else:
if not arduino_support and case["suite_name"] == "arduino_uno":
continue
if case["case_input"] == "dict":
case_input = pin_name_dict
elif case["case_input"] == "content":
case_input = pin_name_content
case_output = case["case_function"](case_input)
case_result = "FAILED" if case_output else "PASSED"
platform_name = target
report.append(
{
"platform_name": platform_name,
"suite_name": case["suite_name"],
"case_name": case["case_name"],
"result": case_result,
"errors": case_output,
}
)
generate_output(report, args.verbose)
if not has_passed_all_test_cases(report):
raise TestCaseError("One or more test cases failed")
def generate_output(report, verbosity):
"""Generate the output."""
if verbosity == 0:
output = print_summary(report)
elif verbosity == 1:
output = print_suite_summary(report)
elif verbosity == 2:
output = print_report(report, False)
elif verbosity > 2:
output = print_report(report, True)
print(output)
def parse_args():
"""Parse the command line args."""
parser = ArgumentParserWithDefaultHelp(
description="Pin names validation",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"-v",
"--verbose",
action="count",
default=0,
help=(
"Verbosity of the report (none to -vvv)."
" Only applies for 'prettytext' output format."
),
)
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
"-t",
"--targets",
help=(
"Target name. Use comma to seperate multiple targets."
"THIS FEATURE IS EXPERIMENTAL!"
),
)
group.add_argument(
"-p",
"--paths",
help="Path to PinNames.h file. Use comma to seperate multiple paths.",
)
group.add_argument(
"-a", "--all", action="store_true", help="Run tests on all targets."
)
parser.set_defaults(func=validate_pin_names)
args_namespace = parser.parse_args()
# We want to fail gracefully, with a consistent
# help message, in the no argument case.
# So here's an obligatory hasattr hack.
if not hasattr(args_namespace, "func"):
parser.error("No arguments given!")
else:
return args_namespace
def run_pin_validate():
"""Application main algorithm."""
args = parse_args()
args.func(args)
def _main():
"""Run pinvalidate."""
try:
run_pin_validate()
except Exception as error:
print(error)
return ReturnCode.ERROR.value
else:
return ReturnCode.SUCCESS.value
if __name__ == "__main__":
sys.exit(_main())