-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_standards.py
More file actions
executable file
·73 lines (60 loc) · 2.48 KB
/
code_standards.py
File metadata and controls
executable file
·73 lines (60 loc) · 2.48 KB
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
#!/usr/bin/env python2.6
# -*- coding: utf-8 -*-
from __future__ import print_function
import optparse
import path_resolv
from path_resolv import Path
def check_file(f, show_info, override_ignores):
text = f.read()
if "\r" in text:
raise Exception("FATAL - dos endlines in %s" %(f))
for i, line in enumerate(text.split("\n")):
def warn(text):
print("%30s %30s:%03d" %("WARNING - " + text, f, i))
def info(text):
if show_info:
print("%30s %30s:%03d" %("INFO - " + text, f, i))
if "\t" in line:
warn("tabs present")
# for now, ignore Eclipse blank comment lines
if line.endswith(" ") and line.strip() != "*":
warn("trailing whitespace")
# the following can be ignored
if "@code standards ignore" in line and not override_ignores:
continue
# the following only apply to uncommented code
if line.lstrip().startswith("//"):
continue
# the following do not apply to this file
if f.endswith("build_util/code_standards.py"):
continue
if "System.exit" in line:
warn("raw system exit")
if "DebugOut.assertSlow" in line:
info("debug assert slow call")
def warn(text):
print("%30s %30s" %("WARNING - " + text, f))
if f.endswith(".java") and not "http://creativecommons.org/licenses/BSD/" in text:
warn("no license")
def main(srcdir, file_extensions, **kwargs):
assert type(file_extensions) == list
for root, dirs, files in Path(srcdir).walk():
for f in files:
f = Path(root, f)
if f.splitext()[-1][1:] in file_extensions:
check_file(f, **kwargs)
if __name__ == "__main__":
cmdopts = optparse.OptionParser(usage="%prog [options]")
cmdopts.add_option("--srcdir", default=Path("."),
help="source directory to look through")
cmdopts.add_option("--file_extensions", default="java,scala,py,sh",
help="comma-sepated list of file extensions")
cmdopts.add_option("--show_info", action="store_true",
help="show info for command")
cmdopts.add_option("--override_ignores", action="store_true",
help="ignore \"@code standards ignore\"")
options, args = cmdopts.parse_args()
options.file_extensions = options.file_extensions.split(",")
if not options.show_info:
print("use --show_info to show more notices")
main(**options.__dict__)