|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +from __future__ import print_function |
| 4 | +import glob |
| 5 | +import re |
| 6 | +import sys |
| 7 | + |
| 8 | + |
| 9 | +def do_exist(file_name, lines, imported): |
| 10 | + if not any(not re.match('using \w+::{0};'.format(imported), line) and |
| 11 | + re.search(imported, line) for line in lines): |
| 12 | + print('File "{0}" does not use "{1}"'.format(file_name, imported)) |
| 13 | + return False |
| 14 | + return True |
| 15 | + |
| 16 | + |
| 17 | +def is_valid(file_name): |
| 18 | + with open(file_name) as source_file: |
| 19 | + lines = [line.strip() for line in source_file] |
| 20 | + |
| 21 | + usings, importeds, line_numbers, valid = [], [], [], True |
| 22 | + for idx, line in enumerate(lines, 1): |
| 23 | + matches = re.search(r'^using (\w+::(\w+));$', line) |
| 24 | + if matches: |
| 25 | + line_numbers.append(idx) |
| 26 | + usings.append(matches.group(1)) |
| 27 | + importeds.append(matches.group(2)) |
| 28 | + |
| 29 | + valid = all([do_exist(file_name, lines, imported) for imported in importeds]) |
| 30 | + |
| 31 | + sorted_usings = sorted(usings, key=lambda x: x.lower()) |
| 32 | + if sorted_usings != usings: |
| 33 | + print("using statements aren't sorted in '{0}'.".format(file_name)) |
| 34 | + for num, actual, expected in zip(line_numbers, usings, sorted_usings): |
| 35 | + if actual != expected: |
| 36 | + print('\tLine {0}: Actual: {1}, Expected: {2}' |
| 37 | + .format(num, actual, expected)) |
| 38 | + return False |
| 39 | + else: |
| 40 | + return valid |
| 41 | + |
| 42 | +sys.exit(0 if all(map(is_valid, glob.iglob('src/*.cc'))) else 1) |
0 commit comments