forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_cpplint.py
executable file
·65 lines (51 loc) · 1.86 KB
/
add_cpplint.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
#!/usr/bin/env python3
import os
import subprocess
"""
Iterate through scripts that are not covered by cpplint, and ensure that they
are properly covered.
"""
def subshell(cmd, shell=True, strip=True):
output = subprocess.check_output(cmd, shell=shell).decode("utf8")
if strip:
output = output.strip()
return output
class FileProcessor(object):
def __init__(self):
# Files processed
self.build_files = set()
def ensure_source_covered(self, source_file):
print("Ensure source covered: {}".format(source_file))
build_file = (subshell("bazel query --output location {}"
.format(source_file))
.split(':')[0])
self.ensure_cpplint(build_file)
def ensure_cpplint(self, build_file):
print(" Check build file: {}".format(build_file))
if build_file in self.build_files:
print(" Already covered")
return
with open(build_file) as f:
text = f.read()
# Not robust, but hoping it's mentioned
if "add_lint_tests()" not in text:
text = text.rstrip() + "\n\nadd_lint_tests()\n"
with open(build_file, 'w') as f:
f.write(text)
print(" Updated file")
else:
print(" add_lint_tests() already present")
self.build_files.add(build_file)
def do_main():
workspace = subshell("bazel info workspace").strip()
os.chdir(workspace)
subshell("./tools/dev/check_missing_sources.sh")
files_missed = (
subshell(r'diff -u /tmp/cc_files.txt /tmp/cpplint_files.txt'
+ r' | grep "^-\w" | sed "s#^\-##g"')
.strip().split('\n'))
file_proc = FileProcessor()
for source_file in files_missed:
file_proc.ensure_source_covered(source_file)
if __name__ == "__main__":
do_main()