-
Notifications
You must be signed in to change notification settings - Fork 0
/
_header_guards_regen.py
executable file
·95 lines (66 loc) · 2.04 KB
/
_header_guards_regen.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
#!/bin/python3
import os.path
import datetime
def edit_file(fullpath_first, fullpath):
lns = []
relp = os.path.relpath(fullpath, fullpath_first)
print("editing: {}".format(relp))
with open(fullpath, 'r') as f:
lns = f.readlines()
for i in range(len(lns)-1, -1, -1):
lns[i] = lns[i].rstrip()
if len(lns) > 0:
if lns[0].startswith('#ifndef'):
del lns[0]
else:
print(f"skipping 1 {relp}")
return
if len(lns) > 0:
if lns[0].startswith('#define'):
del lns[0]
else:
print(f"skipping 2 {relp}")
return
while len(lns) > 0:
if lns[-1].isspace():
del lns[-1]
else:
break
if len(lns) > 0:
if lns[-1].startswith('#endif'):
del lns[-1]
else:
print(f"skipping 4 {relp}: {lns[-1]}")
return
a = datetime.datetime.now(datetime.UTC)
gened_name = 'WAYROUND_I2P_{:04d}{:02d}{:02d}_{:02d}{:02d}{:02d}_{:d}'.format(
a.year, a.month, a.day, a.hour, a.minute, a.second, a.microsecond
)
lns.insert(0, "#define {}".format(gened_name))
lns.insert(0, "#ifndef {}".format(gened_name))
lns.append("#endif")
lns.append("")
with open(fullpath, 'w') as f:
f.write("\n".join(lns))
def walk_dir(fullpath_first, fullpath):
relp = os.path.relpath(fullpath, fullpath_first)
print("working in: {}".format(relp))
names = os.listdir(fullpath)
for i in names:
joined = os.path.join(fullpath, i)
if os.path.islink(joined):
continue
if os.path.isdir(joined):
if i in ['.git', 'build']:
continue
walk_dir(fullpath_first, joined)
continue
if i.endswith(".hpp"):
edit_file(fullpath_first, joined)
def main():
a = os.path.dirname(__file__)
a = os.path.abspath(a)
print(f"walking {a} dirs and replacing .hpp headers")
walk_dir(a, a)
if __name__ == '__main__':
main()