##// END OF EJS Templates
check-code: support multiline matches like try/except/finally...
Matt Mackall -
r15281:aeeb2afc stable
parent child Browse files
Show More
@@ -13,7 +13,7 b' import optparse'
13 13
14 14 def repquote(m):
15 15 t = re.sub(r"\w", "x", m.group('text'))
16 t = re.sub(r"[^\sx]", "o", t)
16 t = re.sub(r"[^\s\nx]", "o", t)
17 17 return m.group('quote') + t + m.group('quote')
18 18
19 19 def reppython(m):
@@ -44,30 +44,30 b' def rephere(m):'
44 44 testpats = [
45 45 [
46 46 (r'(pushd|popd)', "don't use 'pushd' or 'popd', use 'cd'"),
47 (r'\W\$?\(\([^\)]*\)\)', "don't use (()) or $(()), use 'expr'"),
47 (r'\W\$?\(\([^\)\n]*\)\)', "don't use (()) or $(()), use 'expr'"),
48 48 (r'^function', "don't use 'function', use old style"),
49 49 (r'grep.*-q', "don't use 'grep -q', redirect to /dev/null"),
50 50 (r'echo.*\\n', "don't use 'echo \\n', use printf"),
51 51 (r'echo -n', "don't use 'echo -n', use printf"),
52 52 (r'^diff.*-\w*N', "don't use 'diff -N'"),
53 (r'(^| )wc[^|]*$', "filter wc output"),
53 (r'(^| )wc[^|\n]*$', "filter wc output"),
54 54 (r'head -c', "don't use 'head -c', use 'dd'"),
55 55 (r'ls.*-\w*R', "don't use 'ls -R', use 'find'"),
56 56 (r'printf.*\\\d\d\d', "don't use 'printf \NNN', use Python"),
57 57 (r'printf.*\\x', "don't use printf \\x, use Python"),
58 58 (r'\$\(.*\)', "don't use $(expr), use `expr`"),
59 59 (r'rm -rf \*', "don't use naked rm -rf, target a directory"),
60 (r'(^|\|\s*)grep (-\w\s+)*[^|]*[(|]\w',
60 (r'(^|\|\s*)grep (-\w\s+)*[^|\n]*[(|]\w',
61 61 "use egrep for extended grep syntax"),
62 62 (r'/bin/', "don't use explicit paths for tools"),
63 63 (r'\$PWD', "don't use $PWD, use `pwd`"),
64 64 (r'[^\n]\Z', "no trailing newline"),
65 65 (r'export.*=', "don't export and assign at once"),
66 ('^([^"\']|("[^"]*")|(\'[^\']*\'))*\\^', "^ must be quoted"),
66 ('^([^"\'\n]|("[^"\n]*")|(\'[^\'\n]*\'))*\\^', "^ must be quoted"),
67 67 (r'^source\b', "don't use 'source', use '.'"),
68 68 (r'touch -d', "don't use 'touch -d', use 'touch -t' instead"),
69 (r'ls\s+[^|-]+\s+-', "options to 'ls' must come before filenames"),
70 (r'[^>]>\s*\$HGRCPATH', "don't overwrite $HGRCPATH, append to it"),
69 (r'ls\s+[^|\n-]+\s+-', "options to 'ls' must come before filenames"),
70 (r'[^>\n]>\s*\$HGRCPATH', "don't overwrite $HGRCPATH, append to it"),
71 71 (r'stop\(\)', "don't use 'stop' as a shell function name"),
72 72 ],
73 73 # warnings
@@ -83,7 +83,7 b' uprefix = r"^ \\$ "'
83 83 uprefixc = r"^ > "
84 84 utestpats = [
85 85 [
86 (r'^(\S| $ ).*(\S\s+|^\s+)\n', "trailing whitespace on non-output"),
86 (r'^(\S| $ ).*(\S[ \t]+|^[ \t]+)\n', "trailing whitespace on non-output"),
87 87 (uprefix + r'.*\|\s*sed', "use regex test output patterns instead of sed"),
88 88 (uprefix + r'(true|exit 0)', "explicit zero exit unnecessary"),
89 89 (uprefix + r'.*\$\?', "explicit exit code checks unnecessary"),
@@ -121,16 +121,18 b' pypats = ['
121 121 (r'\S;\s*\n', "semicolon"),
122 122 (r'\w,\w', "missing whitespace after ,"),
123 123 (r'\w[+/*\-<>]\w', "missing whitespace in expression"),
124 (r'^\s+\w+=\w+[^,)]$', "missing whitespace in assignment"),
124 (r'^\s+\w+=\w+[^,)\n]$', "missing whitespace in assignment"),
125 (r'(?m)(\s+)try:\n((?:\n|\1\s.*\n)+?)\1except.*?:\n'
126 r'((?:\n|\1\s.*\n)+?)\1finally:', 'no try/except/finally in Py2.4'),
125 127 (r'.{85}', "line too long"),
126 128 (r'[^\n]\Z', "no trailing newline"),
127 (r'(\S\s+|^\s+)\n', "trailing whitespace"),
128 # (r'^\s+[^_ ][^_. ]+_[^_]+\s*=', "don't use underbars in identifiers"),
129 (r'(\S[ \t]+|^[ \t]+)\n', "trailing whitespace"),
130 # (r'^\s+[^_ \n][^_. \n]+_[^_\n]+\s*=', "don't use underbars in identifiers"),
129 131 # (r'\w*[a-z][A-Z]\w*\s*=', "don't use camelcase in identifiers"),
130 (r'^\s*(if|while|def|class|except|try)\s[^[]*:\s*[^\]#\s]+',
132 (r'^\s*(if|while|def|class|except|try)\s[^[\n]*:\s*[^\\n]#\s]+',
131 133 "linebreak after :"),
132 (r'class\s[^( ]+:', "old-style class, use class foo(object)"),
133 (r'class\s[^( ]+\(\):',
134 (r'class\s[^( \n]+:', "old-style class, use class foo(object)"),
135 (r'class\s[^( \n]+\(\):',
134 136 "class foo() not available in Python 2.4, use class foo(object)"),
135 137 (r'\b(%s)\(' % '|'.join(keyword.kwlist),
136 138 "Python keyword is not a function"),
@@ -152,7 +154,7 b' pypats = ['
152 154 (r'if\s.*\selse', "if ... else form not available in Python 2.4"),
153 155 (r'^\s*(%s)\s\s' % '|'.join(keyword.kwlist),
154 156 "gratuitous whitespace after Python keyword"),
155 (r'([\(\[]\s\S)|(\S\s[\)\]])', "gratuitous whitespace in () or []"),
157 (r'([\(\[][ \t]\S)|(\S[ \t][\)\]])', "gratuitous whitespace in () or []"),
156 158 # (r'\s\s=', "gratuitous whitespace before ="),
157 159 (r'[^>< ](\+=|-=|!=|<>|<=|>=|<<=|>>=)\S',
158 160 "missing whitespace around operator"),
@@ -206,7 +208,7 b' cpats = ['
206 208 (r'//', "don't use //-style comments"),
207 209 (r'^ ', "don't use spaces to indent"),
208 210 (r'\S\t', "don't use tabs except for indent"),
209 (r'(\S\s+|^\s+)\n', "trailing whitespace"),
211 (r'(\S[ \t]+|^[ \t]+)\n', "trailing whitespace"),
210 212 (r'.{85}', "line too long"),
211 213 (r'(while|if|do|for)\(', "use space after while/if/do/for"),
212 214 (r'return\(', "return is not a function"),
@@ -331,32 +333,54 b' def checkfile(f, logfunc=_defaultlogger.'
331 333 else:
332 334 pats = pats[0]
333 335 # print post # uncomment to show filtered version
334 z = enumerate(zip(pre.splitlines(), post.splitlines(True)))
336
335 337 if debug:
336 338 print "Checking %s for %s" % (name, f)
337 for n, l in z:
338 if "check-code" + "-ignore" in l[0]:
339 if debug:
340 print "Skipping %s for %s:%s (check-code -ignore)" % (
341 name, f, n)
342 continue
343 for p, msg in pats:
344 if re.search(p, l[1]):
345 bd = ""
346 if blame:
347 bd = 'working directory'
348 if not blamecache:
349 blamecache = getblame(f)
350 if n < len(blamecache):
351 bl, bu, br = blamecache[n]
352 if bl == l[0]:
353 bd = '%s@%s' % (bu, br)
354 logfunc(f, n + 1, l[0], msg, bd)
355 fc += 1
356 result = False
339
340 prelines = None
341 errors = []
342 for p, msg in pats:
343 pos = 0
344 n = 0
345 for m in re.finditer(p, post):
346 if prelines is None:
347 prelines = pre.splitlines()
348 postlines = post.splitlines(True)
349
350 start = m.start()
351 while n < len(postlines):
352 step = len(postlines[n])
353 if pos + step > start:
354 break
355 pos += step
356 n += 1
357 l = prelines[n]
358
359 if "check-code" + "-ignore" in l:
360 if debug:
361 print "Skipping %s for %s:%s (check-code -ignore)" % (
362 name, f, n)
363 continue
364 bd = ""
365 if blame:
366 bd = 'working directory'
367 if not blamecache:
368 blamecache = getblame(f)
369 if n < len(blamecache):
370 bl, bu, br = blamecache[n]
371 if bl == l:
372 bd = '%s@%s' % (bu, br)
373 errors.append((f, n + 1, l, msg, bd))
374 result = False
375
376 errors.sort()
377 for e in errors:
378 logfunc(*e)
379 fc += 1
357 380 if maxerr is not None and fc >= maxerr:
358 381 print " (too many errors, giving up)"
359 382 break
383
360 384 return result
361 385
362 386 if __name__ == "__main__":
@@ -50,8 +50,8 b''
50 50 Python keyword is not a function
51 51 ./wrong.py:3:
52 52 > return ( 5+6, 9)
53 gratuitous whitespace in () or []
53 54 missing whitespace in expression
54 gratuitous whitespace in () or []
55 55 ./quote.py:5:
56 56 > '"""', 42+1, """and
57 57 missing whitespace in expression
General Comments 0
You need to be logged in to leave comments. Login now