systemd/tools/check-includes.py

33 lines
898 B
Python
Raw Normal View History

2023-05-05 15:03:12 +08:00
#!/usr/bin/env python3
# SPDX-License-Identifier: LGPL-2.1-or-later
2023-07-18 00:37:17 +08:00
# pylint: disable=consider-using-with
2023-05-05 15:03:12 +08:00
import os
import pathlib
2023-05-05 15:03:12 +08:00
import re
import sys
PROJECT_ROOT = pathlib.Path(os.getenv('PROJECT_SOURCE_ROOT', '.'))
2023-05-05 15:03:12 +08:00
def check_file(filename):
seen = set()
good = True
for n, line in enumerate(open(filename)):
m = re.match(r'^\s*#\s*include\s*[<"](\S*)[>"]', line)
if m:
2023-05-05 15:03:12 +08:00
include = m.group(1)
if include in seen:
try:
filename = pathlib.Path(filename).resolve().relative_to(PROJECT_ROOT)
except ValueError:
pass
2023-05-05 15:03:12 +08:00
print(f'{filename}:{n}: {line.strip()}')
good = False
seen.add(include)
return good
if __name__ == '__main__':
2023-07-18 00:37:17 +08:00
all_good = all(check_file(name) for name in sys.argv[1:])
sys.exit(0 if all_good else 1)