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