check-code.py
432 lines
| 15.0 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. | ||||
|
r11816 | import re, glob, os, sys | ||
|
r13074 | import keyword | ||
|
r10895 | import optparse | ||
|
r10281 | |||
def repquote(m): | ||||
|
r10722 | t = re.sub(r"\w", "x", m.group('text')) | ||
|
r15281 | t = re.sub(r"[^\s\nx]", "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 = [ | ||||
|
r14009 | [ | ||
|
r10374 | (r'(pushd|popd)', "don't use 'pushd' or 'popd', use 'cd'"), | ||
|
r15281 | (r'\W\$?\(\([^\)\n]*\)\)', "don't use (()) or $(()), use 'expr'"), | ||
|
r15372 | (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"), | ||
|
r11884 | (r'echo -n', "don't use 'echo -n', use printf"), | ||
|
r15372 | (r'^diff.*-\w*N', "don't use 'diff -N'"), | ||
(r'(^| )wc[^|]*$\n(?!.*\(re\))', "filter wc output"), | ||||
|
r10374 | (r'head -c', "don't use 'head -c', use 'dd'"), | ||
|
r15389 | (r'sha1sum', "don't use sha1sum, use $TESTDIR/md5sum.py"), | ||
|
r10374 | (r'ls.*-\w*R', "don't use 'ls -R', use 'find'"), | ||
|
r16098 | (r'printf.*\\\d{1,3}', "don't use 'printf \NNN', use Python"), | ||
|
r10374 | (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"), | ||||
|
r15372 | (r'(^|\|\s*)grep (-\w\s+)*[^|]*[(|]\w', | ||
|
r10281 | "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"), | ||
|
r15372 | (r'^([^"\'\n]|("[^"\n]*")|(\'[^\'\n]*\'))*\\^', "^ must be quoted"), | ||
(r'^source\b', "don't use 'source', use '.'"), | ||||
|
r12367 | (r'touch -d', "don't use 'touch -d', use 'touch -t' instead"), | ||
|
r15364 | (r'ls +[^|\n-]+ +-', "options to 'ls' must come before filenames"), | ||
|
r15281 | (r'[^>\n]>\s*\$HGRCPATH', "don't overwrite $HGRCPATH, append to it"), | ||
|
r15372 | (r'^stop\(\)', "don't use 'stop' as a shell function name"), | ||
|
r15282 | (r'(\[|\btest\b).*-e ', "don't use 'test -e', use 'test -f'"), | ||
|
r16013 | (r'^alias\b.*=', "don't use alias, use a function"), | ||
|
r14009 | ], | ||
# warnings | ||||
[] | ||||
|
r10281 | ] | ||
testfilters = [ | ||||
(r"( *)(#([^\n]*\S)?)", repcomment), | ||||
(r"<<(\S+)((.|\n)*?\n\1)", rephere), | ||||
] | ||||
|
r15372 | uprefix = r"^ \$ " | ||
uprefixc = r"^ > " | ||||
|
r12364 | utestpats = [ | ||
|
r14009 | [ | ||
|
r15372 | (r'^(\S| $ ).*(\S[ \t]+|^[ \t]+)\n', "trailing whitespace on non-output"), | ||
|
r12366 | (uprefix + r'.*\|\s*sed', "use regex test output patterns instead of sed"), | ||
|
r12364 | (uprefix + r'(true|exit 0)', "explicit zero exit unnecessary"), | ||
|
r15607 | (uprefix + r'.*(?<!\[)\$\?', "explicit exit code checks unnecessary"), | ||
|
r12364 | (uprefix + r'.*\|\| echo.*(fail|error)', | ||
"explicit exit code checks unnecessary"), | ||||
(uprefix + r'set -e', "don't use set -e"), | ||||
|
r12743 | (uprefixc + r'( *)\t', "don't use tabs to indent"), | ||
|
r16364 | (uprefixc + r'.*do\s*true;\s*done', | ||
"don't use true as loop body, use sleep 0"), | ||||
|
r14009 | ], | ||
# warnings | ||||
[] | ||||
|
r12364 | ] | ||
|
r14203 | for i in [0, 1]: | ||
for p, m in testpats[i]: | ||||
|
r15372 | if p.startswith(r'^'): | ||
p = uprefix + p[1:] | ||||
|
r14203 | else: | ||
|
r15372 | p = uprefix + ".*" + p | ||
|
r14203 | utestpats[i].append((p, m)) | ||
|
r12364 | |||
utestfilters = [ | ||||
(r"( *)(#([^\n]*\S)?)", repcomment), | ||||
] | ||||
|
r10281 | pypats = [ | ||
|
r14009 | [ | ||
|
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+"), | ||||
|
r11764 | (r'(?<!def)\s+(cmp)\(', "cmp is 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"), | ||||
|
r15281 | (r'^\s+\w+=\w+[^,)\n]$', "missing whitespace in assignment"), | ||
|
r15372 | (r'(\s+)try:\n((?:\n|\1\s.*\n)+?)\1except.*?:\n' | ||
|
r15281 | r'((?:\n|\1\s.*\n)+?)\1finally:', 'no try/except/finally in Py2.4'), | ||
|
r10281 | (r'.{85}', "line too long"), | ||
|
r15372 | (r' x+[xo][\'"]\n\s+[\'"]x', 'string join across lines with no space'), | ||
|
r10281 | (r'[^\n]\Z', "no trailing newline"), | ||
|
r15281 | (r'(\S[ \t]+|^[ \t]+)\n', "trailing whitespace"), | ||
# (r'^\s+[^_ \n][^_. \n]+_[^_\n]+\s*=', "don't use underbars in identifiers"), | ||||
|
r15457 | (r'^\s+(self\.)?[A-za-z][a-z0-9]+[A-Z]\w* = ', | ||
"don't use camelcase in identifiers"), | ||||
|
r15281 | (r'^\s*(if|while|def|class|except|try)\s[^[\n]*:\s*[^\\n]#\s]+', | ||
|
r10286 | "linebreak after :"), | ||
|
r15281 | (r'class\s[^( \n]+:', "old-style class, use class foo(object)"), | ||
(r'class\s[^( \n]+\(\):', | ||||
|
r14763 | "class foo() not available in Python 2.4, use class foo(object)"), | ||
|
r13076 | (r'\b(%s)\(' % '|'.join(keyword.kwlist), | ||
"Python keyword is not 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"), | ||||
|
r14267 | (r'\.isdisjoint\(', "set.isdisjoint not available in Python 2.4"), | ||
|
r13160 | (r'^\s*except.* as .*:', "except as not available in Python 2.4"), | ||
|
r13161 | (r'^\s*os\.path\.relpath', "relpath 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)\(', | ||
|
r14978 | "callable not available in Python 3, use getattr(f, '__call__', None)"), | ||
|
r10281 | (r'if\s.*\selse', "if ... else form not available in Python 2.4"), | ||
|
r13074 | (r'^\s*(%s)\s\s' % '|'.join(keyword.kwlist), | ||
"gratuitous whitespace after Python keyword"), | ||||
|
r15281 | (r'([\(\[][ \t]\S)|(\S[ \t][\)\]])', "gratuitous whitespace in () or []"), | ||
|
r10281 | # (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"), | ||||
|
r14303 | (r'[^+=*/!<>&| -](\s=|=\s)[^= ]', | ||
|
r11345 | "wrong whitespace around ="), | ||
|
r10451 | (r'raise Exception', "don't raise generic exceptions"), | ||
|
r14009 | (r' is\s+(not\s+)?["\'0-9-]', "object comparison with literal"), | ||
(r' [=!]=\s+(True|False|None)', | ||||
"comparison with singleton, use 'is' or 'is not' instead"), | ||||
|
r14494 | (r'^\s*(while|if) [01]:', | ||
"use True/False for constant Boolean expression"), | ||||
|
r16416 | (r'(?:(?<!def)\s+|\()hasattr', | ||
|
r14978 | 'hasattr(foo, bar) is broken, use util.safehasattr(foo, bar) instead'), | ||
|
r14169 | (r'opener\([^)]*\).read\(', | ||
"use opener.read() instead"), | ||||
|
r15334 | (r'BaseException', 'not in Py2.4, use Exception'), | ||
(r'os\.path\.relpath', 'os.path.relpath is not in Py2.5'), | ||||
|
r14169 | (r'opener\([^)]*\).write\(', | ||
"use opener.write() instead"), | ||||
(r'[\s\(](open|file)\([^)]*\)\.read\(', | ||||
"use util.readfile() instead"), | ||||
(r'[\s\(](open|file)\([^)]*\)\.write\(', | ||||
"use util.readfile() instead"), | ||||
(r'^[\s\(]*(open(er)?|file)\([^)]*\)', | ||||
"always assign an opened file to a variable, and close it afterwards"), | ||||
(r'[\s\(](open|file)\([^)]*\)\.', | ||||
"always assign an opened file to a variable, and close it afterwards"), | ||||
|
r14549 | (r'(?i)descendent', "the proper spelling is descendAnt"), | ||
|
r14709 | (r'\.debug\(\_', "don't mark debug messages for translation"), | ||
|
r14009 | ], | ||
# warnings | ||||
[ | ||||
(r'.{81}', "warning: line over 80 characters"), | ||||
|
r14005 | (r'^\s*except:$', "warning: naked except clause"), | ||
|
r11599 | (r'ui\.(status|progress|write|note|warn)\([\'\"]x', | ||
|
r10895 | "warning: unwrapped ui message"), | ||
|
r14009 | ] | ||
|
r10281 | ] | ||
pyfilters = [ | ||||
|
r10727 | (r"""(?msx)(?P<comment>\#.*?$)| | ||
((?P<quote>('''|\"\"\"|(?<!')'(?!')|(?<!")"(?!"))) | ||||
(?P<text>(([^\\]|\\.)*?)) | ||||
(?P=quote))""", reppython), | ||||
|
r10281 | ] | ||
cpats = [ | ||||
|
r14009 | [ | ||
|
r10281 | (r'//', "don't use //-style comments"), | ||
(r'^ ', "don't use spaces to indent"), | ||||
(r'\S\t', "don't use tabs except for indent"), | ||||
|
r15281 | (r'(\S[ \t]+|^[ \t]+)\n', "trailing whitespace"), | ||
|
r10281 | (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 ,"), | ||||
|
r13736 | (r'^[^#]\w[+/*]\w', "missing whitespace in expression"), | ||
|
r10281 | (r'^#\s+\w', "use #foo, not # foo"), | ||
(r'[^\n]\Z', "no trailing newline"), | ||||
|
r13748 | (r'^\s*#import\b', "use only #include in standard C code"), | ||
|
r14009 | ], | ||
# warnings | ||||
[] | ||||
|
r10281 | ] | ||
cfilters = [ | ||||
(r'(/\*)(((\*(?!/))|[^*])*)\*/', repccomment), | ||||
|
r10722 | (r'''(?P<quote>(?<!")")(?P<text>([^"]|\\")+)"(?!")''', repquote), | ||
|
r10281 | (r'''(#\s*include\s+<)([^>]+)>''', repinclude), | ||
(r'(\()([^)]+\))', repcallspaces), | ||||
] | ||||
|
r14137 | inutilpats = [ | ||
[ | ||||
(r'\bui\.', "don't use ui in util"), | ||||
], | ||||
# warnings | ||||
[] | ||||
] | ||||
inrevlogpats = [ | ||||
[ | ||||
(r'\brepo\.', "don't use repo in revlog"), | ||||
], | ||||
# warnings | ||||
[] | ||||
] | ||||
|
r10281 | checks = [ | ||
('python', r'.*\.(py|cgi)$', pyfilters, pypats), | ||||
('test script', r'(.*/)?test-[^.~]*$', testfilters, testpats), | ||||
('c', r'.*\.c$', cfilters, cpats), | ||||
|
r12364 | ('unified test', r'.*\.t$', utestfilters, utestpats), | ||
|
r14137 | ('layering violation repo in revlog', r'mercurial/revlog\.py', pyfilters, | ||
inrevlogpats), | ||||
('layering violation ui in util', r'mercurial/util\.py', pyfilters, | ||||
inutilpats), | ||||
|
r10281 | ] | ||
|
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, | ||||
|
r15502 | blame=False, debug=False, lineno=True): | ||
|
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. | ||||
|
r15873 | Set to false (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: | ||
|
r14135 | if debug: | ||
print name, f | ||||
|
r10281 | fc = 0 | ||
if not re.match(match, f): | ||||
|
r14135 | if debug: | ||
print "Skipping %s for %s it doesn't match %s" % ( | ||||
name, match, f) | ||||
|
r10281 | continue | ||
|
r13400 | fp = open(f) | ||
pre = post = fp.read() | ||||
fp.close() | ||||
|
r10287 | if "no-" + "check-code" in pre: | ||
|
r14135 | if debug: | ||
print "Skipping %s for %s it has no- and check-code" % ( | ||||
name, f) | ||||
|
r10287 | break | ||
|
r10281 | for p, r in filters: | ||
post = re.sub(p, r, post) | ||||
|
r14009 | if warnings: | ||
pats = pats[0] + pats[1] | ||||
else: | ||||
pats = pats[0] | ||||
|
r10281 | # print post # uncomment to show filtered version | ||
|
r15281 | |||
|
r14135 | if debug: | ||
print "Checking %s for %s" % (name, f) | ||||
|
r15281 | |||
prelines = None | ||||
errors = [] | ||||
for p, msg in pats: | ||||
|
r15372 | # fix-up regexes for multiline searches | ||
po = p | ||||
# \s doesn't match \n | ||||
p = re.sub(r'(?<!\\)\\s', r'[ \\t]', p) | ||||
# [^...] doesn't match newline | ||||
p = re.sub(r'(?<!\\)\[\^', r'[^\\n', p) | ||||
#print po, '=>', p | ||||
|
r15281 | pos = 0 | ||
n = 0 | ||||
|
r15372 | for m in re.finditer(p, post, re.MULTILINE): | ||
|
r15281 | if prelines is None: | ||
prelines = pre.splitlines() | ||||
postlines = post.splitlines(True) | ||||
start = m.start() | ||||
while n < len(postlines): | ||||
step = len(postlines[n]) | ||||
if pos + step > start: | ||||
break | ||||
pos += step | ||||
n += 1 | ||||
l = prelines[n] | ||||
if "check-code" + "-ignore" in l: | ||||
if debug: | ||||
print "Skipping %s for %s:%s (check-code -ignore)" % ( | ||||
name, f, n) | ||||
continue | ||||
bd = "" | ||||
if blame: | ||||
bd = 'working directory' | ||||
if not blamecache: | ||||
blamecache = getblame(f) | ||||
if n < len(blamecache): | ||||
bl, bu, br = blamecache[n] | ||||
if bl == l: | ||||
bd = '%s@%s' % (bu, br) | ||||
|
r15502 | errors.append((f, lineno and n + 1, l, msg, bd)) | ||
|
r15281 | result = False | ||
errors.sort() | ||||
for e in errors: | ||||
logfunc(*e) | ||||
fc += 1 | ||||
|
r15873 | if maxerr and fc >= maxerr: | ||
|
r10281 | print " (too many errors, giving up)" | ||
break | ||||
|
r15281 | |||
|
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") | ||||
|
r14135 | parser.add_option("", "--debug", action="store_true", | ||
help="show debug information") | ||||
|
r15502 | parser.add_option("", "--nolineno", action="store_false", | ||
dest='lineno', help="don't show line numbers") | ||||
|
r10895 | |||
|
r15502 | parser.set_defaults(per_file=15, warnings=False, blame=False, debug=False, | ||
lineno=True) | ||||
|
r10895 | (options, args) = parser.parse_args() | ||
if len(args) == 0: | ||||
|
r10716 | check = glob.glob("*") | ||
else: | ||||
|
r10895 | check = args | ||
|
r10281 | |||
|
r15544 | ret = 0 | ||
|
r10716 | for f in check: | ||
|
r11816 | if not checkfile(f, maxerr=options.per_file, warnings=options.warnings, | ||
|
r15502 | blame=options.blame, debug=options.debug, | ||
lineno=options.lineno): | ||||
|
r11816 | ret = 1 | ||
sys.exit(ret) | ||||