-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_ordered_headers.py
55 lines (40 loc) · 1.27 KB
/
check_ordered_headers.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
import pathlib
import os
import sys
def check_file(filepath: str, basepath: str) -> bool:
header_groups = []
group = []
found = False
try:
file_content = pathlib.Path(filepath).read_text()
except UnicodeDecodeError:
return False
for line in file_content.splitlines():
if line.startswith("#include"):
if ' ' not in line:
print(f"cannot parse: [{line!r}]")
else:
group.append(line.split(' ')[1])
elif group:
header_groups.append(group.copy())
group = []
for group in header_groups:
correct = sorted(group)
if correct == group:
continue
print('!', filepath.replace(basepath, '.'))
print("\n".join(f"[[ {g} ]]" for g in group))
print(end="\n")
found = True
return found
def main():
found = False
search_path = os.path.expanduser(sys.argv[1] if len(sys.argv) > 1 else ".")
print("Searching in", search_path)
for dirs, _, files in os.walk(search_path):
for filename in files:
if filename[-2:] in {".c", ".h"}:
found |= check_file(os.path.join(dirs, filename), search_path)
return found
if __name__ == "__main__":
sys.exit(main())