check-code.py
264 lines
| 9.4 KiB
| text/x-python
|
PythonLexer
/ contrib / check-code.py
|
r10281 | #!/usr/bin/env python | ||
# | ||||
# check-code - a style and portability checker for Mercurial | ||||
# | ||||
|
r10290 | # Copyright 2010 Matt Mackall <mpm@selenic.com> | ||
|
r10281 | # | ||
# This software may be used and distributed according to the terms of the | ||||
# GNU General Public License version 2 or any later version. | ||||
|
r11604 | import re, glob, os | ||
|
r10895 | import optparse | ||
|
r10281 | |||
def repquote(m): | ||||
|
r10722 | t = re.sub(r"\w", "x", m.group('text')) | ||
|
r10451 | t = re.sub(r"[^\sx]", "o", t) | ||
|
r10722 | return m.group('quote') + t + m.group('quote') | ||
|
r10281 | |||
|
r10727 | def reppython(m): | ||
comment = m.group('comment') | ||||
if comment: | ||||
return "#" * len(comment) | ||||
return repquote(m) | ||||
|
r10281 | |||
def repcomment(m): | ||||
return m.group(1) + "#" * len(m.group(2)) | ||||
def repccomment(m): | ||||
t = re.sub(r"((?<=\n) )|\S", "x", m.group(2)) | ||||
return m.group(1) + t + "*/" | ||||
def repcallspaces(m): | ||||
t = re.sub(r"\n\s+", "\n", m.group(2)) | ||||
return m.group(1) + t | ||||
def repinclude(m): | ||||
return m.group(1) + "<foo>" | ||||
def rephere(m): | ||||
t = re.sub(r"\S", "x", m.group(2)) | ||||
return m.group(1) + t | ||||
testpats = [ | ||||
|
r10374 | (r'(pushd|popd)', "don't use 'pushd' or 'popd', use 'cd'"), | ||
(r'\W\$?\(\([^\)]*\)\)', "don't use (()) or $(()), use 'expr'"), | ||||
|
r10281 | (r'^function', "don't use 'function', use old style"), | ||
|
r10374 | (r'grep.*-q', "don't use 'grep -q', redirect to /dev/null"), | ||
|
r10373 | (r'echo.*\\n', "don't use 'echo \\n', use printf"), | ||
|
r10374 | (r'^diff.*-\w*N', "don't use 'diff -N'"), | ||
|
r10281 | (r'(^| )wc[^|]*$', "filter wc output"), | ||
|
r10374 | (r'head -c', "don't use 'head -c', use 'dd'"), | ||
(r'ls.*-\w*R', "don't use 'ls -R', use 'find'"), | ||||
(r'printf.*\\\d\d\d', "don't use 'printf \NNN', use Python"), | ||||
(r'printf.*\\x', "don't use printf \\x, use Python"), | ||||
|
r10281 | (r'\$\(.*\)', "don't use $(expr), use `expr`"), | ||
(r'rm -rf \*', "don't use naked rm -rf, target a directory"), | ||||
(r'(^|\|\s*)grep (-\w\s+)*[^|]*[(|]\w', | ||||
"use egrep for extended grep syntax"), | ||||
(r'/bin/', "don't use explicit paths for tools"), | ||||
(r'\$PWD', "don't use $PWD, use `pwd`"), | ||||
(r'[^\n]\Z', "no trailing newline"), | ||||
|
r10658 | (r'export.*=', "don't export and assign at once"), | ||
|
r10802 | ('^([^"\']|("[^"]*")|(\'[^\']*\'))*\\^', "^ must be quoted"), | ||
|
r11210 | (r'^source\b', "don't use 'source', use '.'"), | ||
|
r10281 | ] | ||
testfilters = [ | ||||
(r"( *)(#([^\n]*\S)?)", repcomment), | ||||
(r"<<(\S+)((.|\n)*?\n\1)", rephere), | ||||
] | ||||
pypats = [ | ||||
|
r11568 | (r'^\s*def\s*\w+\s*\(.*,\s*\(', | ||
"tuple parameter unpacking not available in Python 3+"), | ||||
(r'lambda\s*\(.*,.*\)', | ||||
"tuple parameter unpacking not available in Python 3+"), | ||||
|
r11569 | (r'\breduce\s*\(.*', "reduce is not available in Python 3+"), | ||
|
r11602 | (r'\.has_key\b', "dict.has_key is not available in Python 3+"), | ||
|
r10281 | (r'^\s*\t', "don't use tabs"), | ||
|
r10412 | (r'\S;\s*\n', "semicolon"), | ||
|
r10281 | (r'\w,\w', "missing whitespace after ,"), | ||
(r'\w[+/*\-<>]\w', "missing whitespace in expression"), | ||||
(r'^\s+\w+=\w+[^,)]$', "missing whitespace in assignment"), | ||||
(r'.{85}', "line too long"), | ||||
|
r11672 | (r'.{81}', "warning: line over 80 characters"), | ||
|
r10281 | (r'[^\n]\Z', "no trailing newline"), | ||
# (r'^\s+[^_ ][^_. ]+_[^_]+\s*=', "don't use underbars in identifiers"), | ||||
# (r'\w*[a-z][A-Z]\w*\s*=', "don't use camelcase in identifiers"), | ||||
|
r10286 | (r'^\s*(if|while|def|class|except|try)\s[^[]*:\s*[^\]#\s]+', | ||
"linebreak after :"), | ||||
|
r10281 | (r'class\s[^(]:', "old-style class, use class foo(object)"), | ||
|
r10291 | (r'^\s+del\(', "del isn't a function"), | ||
|
r10281 | (r'^\s+except\(', "except isn't a function"), | ||
|
r10412 | (r',]', "unneeded trailing ',' in list"), | ||
|
r10281 | # (r'class\s[A-Z][^\(]*\((?!Exception)', | ||
# "don't capitalize non-exception classes"), | ||||
# (r'in range\(', "use xrange"), | ||||
# (r'^\s*print\s+', "avoid using print in core and extensions"), | ||||
(r'[\x80-\xff]', "non-ASCII character literal"), | ||||
(r'("\')\.format\(', "str.format() not available in Python 2.4"), | ||||
(r'^\s*with\s+', "with not available in Python 2.4"), | ||||
|
r11345 | (r'(?<!def)\s+(any|all|format)\(', | ||
"any/all/format not available in Python 2.4"), | ||||
|
r11522 | (r'(?<!def)\s+(callable)\(', | ||
"callable not available in Python 3, use hasattr(f, '__call__')"), | ||||
|
r10281 | (r'if\s.*\selse', "if ... else form not available in Python 2.4"), | ||
(r'([\(\[]\s\S)|(\S\s[\)\]])', "gratuitous whitespace in () or []"), | ||||
# (r'\s\s=', "gratuitous whitespace before ="), | ||||
|
r11345 | (r'[^>< ](\+=|-=|!=|<>|<=|>=|<<=|>>=)\S', | ||
"missing whitespace around operator"), | ||||
(r'[^>< ](\+=|-=|!=|<>|<=|>=|<<=|>>=)\s', | ||||
"missing whitespace around operator"), | ||||
(r'\s(\+=|-=|!=|<>|<=|>=|<<=|>>=)\S', | ||||
"missing whitespace around operator"), | ||||
(r'[^+=*!<>&| -](\s=|=\s)[^= ]', | ||||
"wrong whitespace around ="), | ||||
|
r10451 | (r'raise Exception', "don't raise generic exceptions"), | ||
|
r11599 | (r'ui\.(status|progress|write|note|warn)\([\'\"]x', | ||
|
r10895 | "warning: unwrapped ui message"), | ||
|
r10281 | ] | ||
pyfilters = [ | ||||
|
r10727 | (r"""(?msx)(?P<comment>\#.*?$)| | ||
((?P<quote>('''|\"\"\"|(?<!')'(?!')|(?<!")"(?!"))) | ||||
(?P<text>(([^\\]|\\.)*?)) | ||||
(?P=quote))""", reppython), | ||||
|
r10281 | ] | ||
cpats = [ | ||||
(r'//', "don't use //-style comments"), | ||||
(r'^ ', "don't use spaces to indent"), | ||||
(r'\S\t', "don't use tabs except for indent"), | ||||
(r'(\S\s+|^\s+)\n', "trailing whitespace"), | ||||
(r'.{85}', "line too long"), | ||||
(r'(while|if|do|for)\(', "use space after while/if/do/for"), | ||||
(r'return\(', "return is not a function"), | ||||
(r' ;', "no space before ;"), | ||||
(r'\w+\* \w+', "use int *foo, not int* foo"), | ||||
(r'\([^\)]+\) \w+', "use (int)foo, not (int) foo"), | ||||
(r'\S+ (\+\+|--)', "use foo++, not foo ++"), | ||||
(r'\w,\w', "missing whitespace after ,"), | ||||
(r'\w[+/*]\w', "missing whitespace in expression"), | ||||
(r'^#\s+\w', "use #foo, not # foo"), | ||||
(r'[^\n]\Z', "no trailing newline"), | ||||
] | ||||
cfilters = [ | ||||
(r'(/\*)(((\*(?!/))|[^*])*)\*/', repccomment), | ||||
|
r10722 | (r'''(?P<quote>(?<!")")(?P<text>([^"]|\\")+)"(?!")''', repquote), | ||
|
r10281 | (r'''(#\s*include\s+<)([^>]+)>''', repinclude), | ||
(r'(\()([^)]+\))', repcallspaces), | ||||
] | ||||
checks = [ | ||||
('python', r'.*\.(py|cgi)$', pyfilters, pypats), | ||||
('test script', r'(.*/)?test-[^.~]*$', testfilters, testpats), | ||||
('c', r'.*\.c$', cfilters, cpats), | ||||
] | ||||
|
r10719 | class norepeatlogger(object): | ||
def __init__(self): | ||||
self._lastseen = None | ||||
|
r11604 | def log(self, fname, lineno, line, msg, blame): | ||
|
r10719 | """print error related a to given line of a given file. | ||
The faulty line will also be printed but only once in the case | ||||
of multiple errors. | ||||
|
r10281 | |||
|
r10719 | :fname: filename | ||
:lineno: line number | ||||
:line: actual content of the line | ||||
:msg: error message | ||||
""" | ||||
msgid = fname, lineno, line | ||||
if msgid != self._lastseen: | ||||
|
r11604 | if blame: | ||
print "%s:%d (%s):" % (fname, lineno, blame) | ||||
else: | ||||
print "%s:%d:" % (fname, lineno) | ||||
|
r10719 | print " > %s" % line | ||
self._lastseen = msgid | ||||
print " " + msg | ||||
_defaultlogger = norepeatlogger() | ||||
|
r11604 | def getblame(f): | ||
lines = [] | ||||
for l in os.popen('hg annotate -un %s' % f): | ||||
start, line = l.split(':', 1) | ||||
user, rev = start.split() | ||||
lines.append((line[1:-1], user, rev)) | ||||
return lines | ||||
def checkfile(f, logfunc=_defaultlogger.log, maxerr=None, warnings=False, | ||||
blame=False): | ||||
|
r10719 | """checks style and portability of a given file | ||
:f: filepath | ||||
:logfunc: function used to report error | ||||
logfunc(filename, linenumber, linecontent, errormessage) | ||||
:maxerr: number of error to display before arborting. | ||||
Set to None (default) to report all errors | ||||
|
r10720 | |||
return True if no error is found, False otherwise. | ||||
|
r10719 | """ | ||
|
r11604 | blamecache = None | ||
|
r10720 | result = True | ||
|
r10281 | for name, match, filters, pats in checks: | ||
fc = 0 | ||||
if not re.match(match, f): | ||||
continue | ||||
pre = post = open(f).read() | ||||
|
r10287 | if "no-" + "check-code" in pre: | ||
break | ||||
|
r10281 | for p, r in filters: | ||
post = re.sub(p, r, post) | ||||
# print post # uncomment to show filtered version | ||||
z = enumerate(zip(pre.splitlines(), post.splitlines(True))) | ||||
for n, l in z: | ||||
|
r10287 | if "check-code" + "-ignore" in l[0]: | ||
continue | ||||
|
r10281 | for p, msg in pats: | ||
|
r10895 | if not warnings and msg.startswith("warning"): | ||
continue | ||||
|
r10281 | if re.search(p, l[1]): | ||
|
r11604 | bd = "" | ||
if blame: | ||||
bd = 'working directory' | ||||
if not blamecache: | ||||
blamecache = getblame(f) | ||||
if n < len(blamecache): | ||||
bl, bu, br = blamecache[n] | ||||
if bl == l[0]: | ||||
bd = '%s@%s' % (bu, br) | ||||
logfunc(f, n + 1, l[0], msg, bd) | ||||
|
r10281 | fc += 1 | ||
|
r10720 | result = False | ||
|
r10718 | if maxerr is not None and fc >= maxerr: | ||
|
r10281 | print " (too many errors, giving up)" | ||
break | ||||
break | ||||
|
r10720 | return result | ||
|
r10717 | |||
|
r10716 | if __name__ == "__main__": | ||
|
r10895 | parser = optparse.OptionParser("%prog [options] [files]") | ||
parser.add_option("-w", "--warnings", action="store_true", | ||||
help="include warning-level checks") | ||||
parser.add_option("-p", "--per-file", type="int", | ||||
help="max warnings per file") | ||||
|
r11604 | parser.add_option("-b", "--blame", action="store_true", | ||
help="use annotate to generate blame info") | ||||
|
r10895 | |||
|
r11604 | parser.set_defaults(per_file=15, warnings=False, blame=False) | ||
|
r10895 | (options, args) = parser.parse_args() | ||
if len(args) == 0: | ||||
|
r10716 | check = glob.glob("*") | ||
else: | ||||
|
r10895 | check = args | ||
|
r10281 | |||
|
r10716 | for f in check: | ||
|
r11604 | checkfile(f, maxerr=options.per_file, warnings=options.warnings, | ||
blame=options.blame) | ||||