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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
|
#!/usr/bin/env python
#
# check-code - a style and portability checker for Mercurial
#
# Copyright 2010 Matt Mackall <mpm@selenic.com>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
import re, glob
import optparse
def repquote(m):
t = re.sub(r"\w", "x", m.group('text'))
t = re.sub(r"[^\sx]", "o", t)
return m.group('quote') + t + m.group('quote')
def reppython(m):
comment = m.group('comment')
if comment:
return "#" * len(comment)
return repquote(m)
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 = [
(r'(pushd|popd)', "don't use 'pushd' or 'popd', use 'cd'"),
(r'\W\$?\(\([^\)]*\)\)', "don't use (()) or $(()), use 'expr'"),
(r'^function', "don't use 'function', use old style"),
(r'grep.*-q', "don't use 'grep -q', redirect to /dev/null"),
(r'echo.*\\n', "don't use 'echo \\n', use printf"),
(r'echo -n', "don't use 'echo -n', use printf"),
(r'^diff.*-\w*N', "don't use 'diff -N'"),
(r'(^| )wc[^|]*$', "filter wc output"),
(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"),
(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"),
(r'export.*=', "don't export and assign at once"),
('^([^"\']|("[^"]*")|(\'[^\']*\'))*\\^', "^ must be quoted"),
(r'^source\b', "don't use 'source', use '.'"),
]
testfilters = [
(r"( *)(#([^\n]*\S)?)", repcomment),
(r"<<(\S+)((.|\n)*?\n\1)", rephere),
]
pypats = [
(r'^\s*\t', "don't use tabs"),
(r'\S;\s*\n', "semicolon"),
(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"),
(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"),
(r'^\s*(if|while|def|class|except|try)\s[^[]*:\s*[^\]#\s]+',
"linebreak after :"),
(r'class\s[^(]:', "old-style class, use class foo(object)"),
(r'^\s+del\(', "del isn't a function"),
(r'^\s+except\(', "except isn't a function"),
(r',]', "unneeded trailing ',' in list"),
# (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"),
(r'(?<!def)\s+(any|all|format)\(',
"any/all/format not available in Python 2.4"),
(r'(?<!def)\s+(callable)\(',
"callable not available in Python 3, use hasattr(f, '__call__')"),
(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 ="),
(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 ="),
(r'raise Exception', "don't raise generic exceptions"),
(r'ui\.(status|progress|write|note|warn)\([\'\"]x',
"warning: unwrapped ui message"),
]
pyfilters = [
(r"""(?msx)(?P<comment>\#.*?$)|
((?P<quote>('''|\"\"\"|(?<!')'(?!')|(?<!")"(?!")))
(?P<text>(([^\\]|\\.)*?))
(?P=quote))""", reppython),
]
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),
(r'''(?P<quote>(?<!")")(?P<text>([^"]|\\")+)"(?!")''', repquote),
(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),
]
class norepeatlogger(object):
def __init__(self):
self._lastseen = None
def log(self, fname, lineno, line, msg):
"""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.
:fname: filename
:lineno: line number
:line: actual content of the line
:msg: error message
"""
msgid = fname, lineno, line
if msgid != self._lastseen:
print "%s:%d:" % (fname, lineno)
print " > %s" % line
self._lastseen = msgid
print " " + msg
_defaultlogger = norepeatlogger()
def checkfile(f, logfunc=_defaultlogger.log, maxerr=None, warnings=False):
"""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
return True if no error is found, False otherwise.
"""
result = True
for name, match, filters, pats in checks:
fc = 0
if not re.match(match, f):
continue
pre = post = open(f).read()
if "no-" + "check-code" in pre:
break
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:
if "check-code" + "-ignore" in l[0]:
continue
for p, msg in pats:
if not warnings and msg.startswith("warning"):
continue
if re.search(p, l[1]):
logfunc(f, n + 1, l[0], msg)
fc += 1
result = False
if maxerr is not None and fc >= maxerr:
print " (too many errors, giving up)"
break
break
return result
if __name__ == "__main__":
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")
parser.set_defaults(per_file=15, warnings=False)
(options, args) = parser.parse_args()
if len(args) == 0:
check = glob.glob("*")
else:
check = args
for f in check:
checkfile(f, maxerr=options.per_file, warnings=options.warnings)
|