Mercurial > public > mercurial-scm > hg-stable
comparison contrib/check-code.py @ 10281:e7d3b509af8b
Introduce check-code.py
check-code is a simple regex-based framework for checking our code and
tests for common style and portability errors. Currently, it knows a
fair amount about our Python and C style, and a little about common
shell script portability problems.
author | Matt Mackall <mpm@selenic.com> |
---|---|
date | Mon, 25 Jan 2010 00:05:22 -0600 |
parents | |
children | cc0340ef47f7 |
comparison
equal
deleted
inserted
replaced
10280:21b3346ce3c7 | 10281:e7d3b509af8b |
---|---|
1 #!/usr/bin/env python | |
2 # | |
3 # check-code - a style and portability checker for Mercurial | |
4 # | |
5 # Copyright 2005-2007 Matt Mackall <mpm@selenic.com> | |
6 # | |
7 # This software may be used and distributed according to the terms of the | |
8 # GNU General Public License version 2 or any later version. | |
9 | |
10 import sys, re, glob | |
11 | |
12 def repquote(m): | |
13 t = re.sub(r"\S", "x", m.group(2)) | |
14 return m.group(1) + t + m.group(1) | |
15 | |
16 def repcomment(m): | |
17 return m.group(1) + "#" * len(m.group(2)) | |
18 | |
19 def repccomment(m): | |
20 t = re.sub(r"((?<=\n) )|\S", "x", m.group(2)) | |
21 return m.group(1) + t + "*/" | |
22 | |
23 def repcallspaces(m): | |
24 t = re.sub(r"\n\s+", "\n", m.group(2)) | |
25 return m.group(1) + t | |
26 | |
27 def repinclude(m): | |
28 return m.group(1) + "<foo>" | |
29 | |
30 def rephere(m): | |
31 t = re.sub(r"\S", "x", m.group(2)) | |
32 return m.group(1) + t | |
33 | |
34 | |
35 testpats = [ | |
36 (r'(pushd|popd)', "don't use pushd|popd, use cd"), | |
37 (r'\W\$?\(\([^\)]*\)\)', "don't use (()) or $(()), use expr"), | |
38 (r'^function', "don't use 'function', use old style"), | |
39 (r'grep.*-q', "don't use grep -q, redirect to /dev/null"), | |
40 (r'echo.*\\n', "don't use 'echo \n', use printf"), | |
41 (r'^diff.*-\w*N', "don't use diff -N"), | |
42 (r'(^| )wc[^|]*$', "filter wc output"), | |
43 (r'head -c', "don't use head -c, use dd"), | |
44 (r'ls.*-\w*R', "don't use ls -R, use find"), | |
45 (r'printf.*\\\d\d\d', "don't use printf \NNN, use python"), | |
46 (r'printf.*\\x', "don't use printf \\x, use python"), | |
47 (r'\$\(.*\)', "don't use $(expr), use `expr`"), | |
48 (r'rm -rf \*', "don't use naked rm -rf, target a directory"), | |
49 (r'(^|\|\s*)grep (-\w\s+)*[^|]*[(|]\w', | |
50 "use egrep for extended grep syntax"), | |
51 (r'/bin/', "don't use explicit paths for tools"), | |
52 (r'\$PWD', "don't use $PWD, use `pwd`"), | |
53 (r'[^\n]\Z', "no trailing newline"), | |
54 ] | |
55 | |
56 testfilters = [ | |
57 (r"( *)(#([^\n]*\S)?)", repcomment), | |
58 (r"<<(\S+)((.|\n)*?\n\1)", rephere), | |
59 ] | |
60 | |
61 pypats = [ | |
62 (r'^\s*\t', "don't use tabs"), | |
63 (r'(\S\s+|^\s+)\n', "trailing whitespace"), | |
64 (r'\w,\w', "missing whitespace after ,"), | |
65 (r'\w[+/*\-<>]\w', "missing whitespace in expression"), | |
66 (r'^\s+\w+=\w+[^,)]$', "missing whitespace in assignment"), | |
67 (r'.{85}', "line too long"), | |
68 (r'[^\n]\Z', "no trailing newline"), | |
69 # (r'^\s+[^_ ][^_. ]+_[^_]+\s*=', "don't use underbars in identifiers"), | |
70 # (r'\w*[a-z][A-Z]\w*\s*=', "don't use camelcase in identifiers"), | |
71 (r'^\s*(if|while|def|class|except|try)\s[^[]*:\s*[^\]#\s]+', "linebreak after :"), | |
72 (r'class\s[^(]:', "old-style class, use class foo(object)"), | |
73 (r'^\s+except\(', "except isn't a function"), | |
74 # (r'class\s[A-Z][^\(]*\((?!Exception)', | |
75 # "don't capitalize non-exception classes"), | |
76 # (r'in range\(', "use xrange"), | |
77 # (r'^\s*print\s+', "avoid using print in core and extensions"), | |
78 (r'[\x80-\xff]', "non-ASCII character literal"), | |
79 (r'("\')\.format\(', "str.format() not available in Python 2.4"), | |
80 (r'^\s*with\s+', "with not available in Python 2.4"), | |
81 (r'if\s.*\selse', "if ... else form not available in Python 2.4"), | |
82 (r'([\(\[]\s\S)|(\S\s[\)\]])', "gratuitous whitespace in () or []"), | |
83 # (r'\s\s=', "gratuitous whitespace before ="), | |
84 (r'\S(\+=|-=|!=|<>|<=|>=|<<=|>>=)\S', "missing whitespace around operator") | |
85 ] | |
86 | |
87 pyfilters = [ | |
88 (r"""(''')(([^']|\\'|'{1,2}(?!'))*)'''""", repquote), | |
89 (r'''(""")(([^"]|\\"|"{1,2}(?!"))*)"""''', repquote), | |
90 (r'''(?<!")(")(([^"\n]|\\")+)"(?!")''', repquote), | |
91 (r"""(?<!')(')(([^'\n]|\\')+)'(?!')""", repquote), | |
92 (r"( *)(#([^\n]*\S)?)", repcomment), | |
93 ] | |
94 | |
95 cpats = [ | |
96 (r'//', "don't use //-style comments"), | |
97 (r'^ ', "don't use spaces to indent"), | |
98 (r'\S\t', "don't use tabs except for indent"), | |
99 (r'(\S\s+|^\s+)\n', "trailing whitespace"), | |
100 (r'.{85}', "line too long"), | |
101 (r'(while|if|do|for)\(', "use space after while/if/do/for"), | |
102 (r'return\(', "return is not a function"), | |
103 (r' ;', "no space before ;"), | |
104 (r'\w+\* \w+', "use int *foo, not int* foo"), | |
105 (r'\([^\)]+\) \w+', "use (int)foo, not (int) foo"), | |
106 (r'\S+ (\+\+|--)', "use foo++, not foo ++"), | |
107 (r'\w,\w', "missing whitespace after ,"), | |
108 (r'\w[+/*]\w', "missing whitespace in expression"), | |
109 (r'^#\s+\w', "use #foo, not # foo"), | |
110 (r'[^\n]\Z', "no trailing newline"), | |
111 ] | |
112 | |
113 cfilters = [ | |
114 (r'(/\*)(((\*(?!/))|[^*])*)\*/', repccomment), | |
115 (r'''(?<!")(")(([^"]|\\")+"(?!"))''', repquote), | |
116 (r'''(#\s*include\s+<)([^>]+)>''', repinclude), | |
117 (r'(\()([^)]+\))', repcallspaces), | |
118 ] | |
119 | |
120 checks = [ | |
121 ('python', r'.*\.(py|cgi)$', pyfilters, pypats), | |
122 ('test script', r'(.*/)?test-[^.~]*$', testfilters, testpats), | |
123 ('c', r'.*\.c$', cfilters, cpats), | |
124 ] | |
125 | |
126 if len(sys.argv) == 1: | |
127 check = glob.glob("*") | |
128 else: | |
129 check = sys.argv[1:] | |
130 | |
131 for f in check: | |
132 for name, match, filters, pats in checks: | |
133 fc = 0 | |
134 if not re.match(match, f): | |
135 continue | |
136 pre = post = open(f).read() | |
137 for p, r in filters: | |
138 post = re.sub(p, r, post) | |
139 # print post # uncomment to show filtered version | |
140 z = enumerate(zip(pre.splitlines(), post.splitlines(True))) | |
141 for n, l in z: | |
142 lc = 0 | |
143 for p, msg in pats: | |
144 if re.search(p, l[1]): | |
145 if not lc: | |
146 print "%s:%d:" % (f, n + 1) | |
147 print " > %s" % l[0] | |
148 print " %s" % msg | |
149 lc += 1 | |
150 fc += 1 | |
151 if fc == 15: | |
152 print " (too many errors, giving up)" | |
153 break | |
154 break |