-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate_nanotest_header.py
executable file
·84 lines (61 loc) · 2.18 KB
/
generate_nanotest_header.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
#!/usr/bin/env python
import re
import sys
import argparse
class Test:
def __init__(self, suite, name):
self.suite = suite
self.name = name
def __str__(self):
return "{}_{}".format(self.suite, self.name)
class Tests:
def __init__(self, files = None):
self.tests = []
self.delimiter = '\n'
if files:
self.parse_files(files)
def __str__(self):
return self.delimiter.join(self.get_names())
def parse_file(self, file):
for line in file.readlines():
# Strict matching to avoid any surprises
m = re.match('^NANOTEST\((\w*),\s*(\w*)\)\s*({?)\s*$', line)
if m:
self.tests.append(Test(m.group(1), m.group(2)))
def parse_files(self, file_list):
if not isinstance(file_list, list):
self.parse_file(file_list)
elif isinstance(file_list[0], str):
for filename in file_list:
with open(filename, 'r') as file:
self.parse_files(file)
def get_names(self):
return [str(i) for i in self.tests]
def gen_header(self):
names = self.get_names()
out = []
out.append('/*')
out.append(' * This file is autogenerated, don\'t modify!')
out.append(' */')
out.append('')
out.append('#include "nanotest.h"')
out.append('')
for n in names:
out.append('extern struct nanotest_unit test_{};'.format(n))
out.append('\nstruct nanotest_unit* autogen_tests[] = {');
for n in names:
out.append(' &test_{},'.format(n))
out.append(' NULL,')
out.append('};');
out.append('');
return '\n'.join(out)
def write_header(self, dest):
with open(dest, 'w') if dest else sys.stdout as file:
file.write(self.gen_header())
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("file_list", nargs="*", help="Source file to search")
parser.add_argument("--output", help="Destination header file")
args = parser.parse_args()
tests = Tests(args.file_list)
tests.write_header(args.output)