This repository has been archived by the owner on May 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlint.py
executable file
·85 lines (70 loc) · 2.23 KB
/
lint.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python
"""
Lint script via clang-format.
Usage
$ ./lint.py [fix]
"""
import sys
import os
import subprocess
import xml.etree.ElementTree as ET
FILE_EXTENSIONS = ('.h', '.c')
CLANGFOMART_NAMES = ['clang-format', 'clang-format-3.5', 'clang-format-3.6']
CLANGFORMAT_LINT_OPTIONS = ['-output-replacements-xml', '-style=file']
CLANGFORMAT_FIX_OPTIONS = ['-i', '-style=file']
def print_usage():
print "Usage: ./lint.py [fix]"
def traverse_files():
"""Get file path list recursively.
"""
paths = []
for root, dirs, files in os.walk("./"):
for filename in files:
if filename.endswith(FILE_EXTENSIONS):
paths.append(os.path.join(root, filename))
return paths
def get_clang_format_bin():
for name in CLANGFOMART_NAMES:
try:
subprocess.check_output([name, "-version"])
except OSError:
continue
else:
return name
raise Exception("No clang-format command available.")
def lint(fix=False):
num_errors = 0
num_error_files = 0
num_fixed_files = 0
clangformat_bin = get_clang_format_bin()
for path in traverse_files():
cmd = [clangformat_bin, path]
cmd.extend(CLANGFORMAT_LINT_OPTIONS)
out = subprocess.check_output(cmd)
root = ET.fromstring(out)
has_error = False
for tag in root.findall('replacement'):
offset = tag.get('offset', None)
length = tag.get("length", None)
if offset is not None:
has_error = True
num_errors += 1
print "{0}:{1},{2}".format(path, offset, length)
if has_error:
num_error_files += 1
if fix:
cmd = [clangformat_bin, path]
cmd.extend(CLANGFORMAT_FIX_OPTIONS)
if subprocess.call(cmd) == 0:
num_fixed_files += 1
if has_error:
print "{} fixed".format(path)
if num_errors > 0 and num_error_files != num_fixed_files:
sys.exit(1)
if __name__ == '__main__':
if len(sys.argv) <= 1: # Case ./program
lint(fix=False)
elif sys.argv[1] == 'fix': # Case ./program fix
lint(fix=True)
else:
print_usage()