2016-04-07 18:56:05 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
import glob
|
2019-11-02 12:35:22 +00:00
|
|
|
import io
|
2016-04-07 18:56:05 +00:00
|
|
|
import re
|
|
|
|
import sys
|
2020-07-31 15:59:38 +00:00
|
|
|
import itertools
|
2016-04-07 18:56:05 +00:00
|
|
|
|
|
|
|
def do_exist(file_name, lines, imported):
|
2023-10-15 15:14:57 +00:00
|
|
|
if not any(not re.match(fr'using \w+::{imported};', line) and
|
|
|
|
re.search(fr'\b{imported}\b', line) for line in lines):
|
|
|
|
print(f'File "{file_name}" does not use "{imported}"')
|
2016-04-07 18:56:05 +00:00
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def is_valid(file_name):
|
2019-11-02 12:35:22 +00:00
|
|
|
with io.open(file_name, encoding='utf-8') as source_file:
|
2016-04-07 18:56:05 +00:00
|
|
|
lines = [line.strip() for line in source_file]
|
|
|
|
|
|
|
|
usings, importeds, line_numbers, valid = [], [], [], True
|
|
|
|
for idx, line in enumerate(lines, 1):
|
|
|
|
matches = re.search(r'^using (\w+::(\w+));$', line)
|
|
|
|
if matches:
|
|
|
|
line_numbers.append(idx)
|
|
|
|
usings.append(matches.group(1))
|
|
|
|
importeds.append(matches.group(2))
|
|
|
|
|
2023-10-15 15:14:57 +00:00
|
|
|
valid = all(do_exist(file_name, lines, imported) for imported in importeds)
|
2016-04-07 18:56:05 +00:00
|
|
|
|
|
|
|
sorted_usings = sorted(usings, key=lambda x: x.lower())
|
|
|
|
if sorted_usings != usings:
|
2023-10-15 15:14:57 +00:00
|
|
|
print(f"using statements aren't sorted in '{file_name}'.")
|
2016-04-07 18:56:05 +00:00
|
|
|
for num, actual, expected in zip(line_numbers, usings, sorted_usings):
|
|
|
|
if actual != expected:
|
2023-10-15 15:14:57 +00:00
|
|
|
print(f'\tLine {num}: Actual: {actual}, Expected: {expected}')
|
2016-04-07 18:56:05 +00:00
|
|
|
return False
|
2023-10-15 15:14:57 +00:00
|
|
|
return valid
|
2016-04-07 18:56:05 +00:00
|
|
|
|
2020-05-06 15:20:14 +00:00
|
|
|
if __name__ == '__main__':
|
2020-07-31 15:59:38 +00:00
|
|
|
if len(sys.argv) > 1:
|
|
|
|
files = []
|
|
|
|
for pattern in sys.argv[1:]:
|
|
|
|
files = itertools.chain(files, glob.iglob(pattern))
|
|
|
|
else:
|
|
|
|
files = glob.iglob('src/*.cc')
|
|
|
|
sys.exit(0 if all(list(map(is_valid, files))) else 1)
|