##// END OF EJS Templates
contrib: ban $RANDOM using check-code...
Augie Fackler -
r36182:c38e9248 default
parent child Browse files
Show More
@@ -1,737 +1,738 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 #
2 #
3 # check-code - a style and portability checker for Mercurial
3 # check-code - a style and portability checker for Mercurial
4 #
4 #
5 # Copyright 2010 Matt Mackall <mpm@selenic.com>
5 # Copyright 2010 Matt Mackall <mpm@selenic.com>
6 #
6 #
7 # This software may be used and distributed according to the terms of the
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.
8 # GNU General Public License version 2 or any later version.
9
9
10 """style and portability checker for Mercurial
10 """style and portability checker for Mercurial
11
11
12 when a rule triggers wrong, do one of the following (prefer one from top):
12 when a rule triggers wrong, do one of the following (prefer one from top):
13 * do the work-around the rule suggests
13 * do the work-around the rule suggests
14 * doublecheck that it is a false match
14 * doublecheck that it is a false match
15 * improve the rule pattern
15 * improve the rule pattern
16 * add an ignore pattern to the rule (3rd arg) which matches your good line
16 * add an ignore pattern to the rule (3rd arg) which matches your good line
17 (you can append a short comment and match this, like: #re-raises)
17 (you can append a short comment and match this, like: #re-raises)
18 * change the pattern to a warning and list the exception in test-check-code-hg
18 * change the pattern to a warning and list the exception in test-check-code-hg
19 * ONLY use no--check-code for skipping entire files from external sources
19 * ONLY use no--check-code for skipping entire files from external sources
20 """
20 """
21
21
22 from __future__ import absolute_import, print_function
22 from __future__ import absolute_import, print_function
23 import glob
23 import glob
24 import keyword
24 import keyword
25 import optparse
25 import optparse
26 import os
26 import os
27 import re
27 import re
28 import sys
28 import sys
29 if sys.version_info[0] < 3:
29 if sys.version_info[0] < 3:
30 opentext = open
30 opentext = open
31 else:
31 else:
32 def opentext(f):
32 def opentext(f):
33 return open(f, encoding='ascii')
33 return open(f, encoding='ascii')
34 try:
34 try:
35 xrange
35 xrange
36 except NameError:
36 except NameError:
37 xrange = range
37 xrange = range
38 try:
38 try:
39 import re2
39 import re2
40 except ImportError:
40 except ImportError:
41 re2 = None
41 re2 = None
42
42
43 def compilere(pat, multiline=False):
43 def compilere(pat, multiline=False):
44 if multiline:
44 if multiline:
45 pat = '(?m)' + pat
45 pat = '(?m)' + pat
46 if re2:
46 if re2:
47 try:
47 try:
48 return re2.compile(pat)
48 return re2.compile(pat)
49 except re2.error:
49 except re2.error:
50 pass
50 pass
51 return re.compile(pat)
51 return re.compile(pat)
52
52
53 # check "rules depending on implementation of repquote()" in each
53 # check "rules depending on implementation of repquote()" in each
54 # patterns (especially pypats), before changing around repquote()
54 # patterns (especially pypats), before changing around repquote()
55 _repquotefixedmap = {' ': ' ', '\n': '\n', '.': 'p', ':': 'q',
55 _repquotefixedmap = {' ': ' ', '\n': '\n', '.': 'p', ':': 'q',
56 '%': '%', '\\': 'b', '*': 'A', '+': 'P', '-': 'M'}
56 '%': '%', '\\': 'b', '*': 'A', '+': 'P', '-': 'M'}
57 def _repquoteencodechr(i):
57 def _repquoteencodechr(i):
58 if i > 255:
58 if i > 255:
59 return 'u'
59 return 'u'
60 c = chr(i)
60 c = chr(i)
61 if c in _repquotefixedmap:
61 if c in _repquotefixedmap:
62 return _repquotefixedmap[c]
62 return _repquotefixedmap[c]
63 if c.isalpha():
63 if c.isalpha():
64 return 'x'
64 return 'x'
65 if c.isdigit():
65 if c.isdigit():
66 return 'n'
66 return 'n'
67 return 'o'
67 return 'o'
68 _repquotett = ''.join(_repquoteencodechr(i) for i in xrange(256))
68 _repquotett = ''.join(_repquoteencodechr(i) for i in xrange(256))
69
69
70 def repquote(m):
70 def repquote(m):
71 t = m.group('text')
71 t = m.group('text')
72 t = t.translate(_repquotett)
72 t = t.translate(_repquotett)
73 return m.group('quote') + t + m.group('quote')
73 return m.group('quote') + t + m.group('quote')
74
74
75 def reppython(m):
75 def reppython(m):
76 comment = m.group('comment')
76 comment = m.group('comment')
77 if comment:
77 if comment:
78 l = len(comment.rstrip())
78 l = len(comment.rstrip())
79 return "#" * l + comment[l:]
79 return "#" * l + comment[l:]
80 return repquote(m)
80 return repquote(m)
81
81
82 def repcomment(m):
82 def repcomment(m):
83 return m.group(1) + "#" * len(m.group(2))
83 return m.group(1) + "#" * len(m.group(2))
84
84
85 def repccomment(m):
85 def repccomment(m):
86 t = re.sub(r"((?<=\n) )|\S", "x", m.group(2))
86 t = re.sub(r"((?<=\n) )|\S", "x", m.group(2))
87 return m.group(1) + t + "*/"
87 return m.group(1) + t + "*/"
88
88
89 def repcallspaces(m):
89 def repcallspaces(m):
90 t = re.sub(r"\n\s+", "\n", m.group(2))
90 t = re.sub(r"\n\s+", "\n", m.group(2))
91 return m.group(1) + t
91 return m.group(1) + t
92
92
93 def repinclude(m):
93 def repinclude(m):
94 return m.group(1) + "<foo>"
94 return m.group(1) + "<foo>"
95
95
96 def rephere(m):
96 def rephere(m):
97 t = re.sub(r"\S", "x", m.group(2))
97 t = re.sub(r"\S", "x", m.group(2))
98 return m.group(1) + t
98 return m.group(1) + t
99
99
100
100
101 testpats = [
101 testpats = [
102 [
102 [
103 (r'\b(push|pop)d\b', "don't use 'pushd' or 'popd', use 'cd'"),
103 (r'\b(push|pop)d\b', "don't use 'pushd' or 'popd', use 'cd'"),
104 (r'\W\$?\(\([^\)\n]*\)\)', "don't use (()) or $(()), use 'expr'"),
104 (r'\W\$?\(\([^\)\n]*\)\)', "don't use (()) or $(()), use 'expr'"),
105 (r'grep.*-q', "don't use 'grep -q', redirect to /dev/null"),
105 (r'grep.*-q', "don't use 'grep -q', redirect to /dev/null"),
106 (r'(?<!hg )grep.* -a', "don't use 'grep -a', use in-line python"),
106 (r'(?<!hg )grep.* -a', "don't use 'grep -a', use in-line python"),
107 (r'sed.*-i', "don't use 'sed -i', use a temporary file"),
107 (r'sed.*-i', "don't use 'sed -i', use a temporary file"),
108 (r'\becho\b.*\\n', "don't use 'echo \\n', use printf"),
108 (r'\becho\b.*\\n', "don't use 'echo \\n', use printf"),
109 (r'echo -n', "don't use 'echo -n', use printf"),
109 (r'echo -n', "don't use 'echo -n', use printf"),
110 (r'(^|\|\s*)\bwc\b[^|]*$\n(?!.*\(re\))', "filter wc output"),
110 (r'(^|\|\s*)\bwc\b[^|]*$\n(?!.*\(re\))', "filter wc output"),
111 (r'head -c', "don't use 'head -c', use 'dd'"),
111 (r'head -c', "don't use 'head -c', use 'dd'"),
112 (r'tail -n', "don't use the '-n' option to tail, just use '-<num>'"),
112 (r'tail -n', "don't use the '-n' option to tail, just use '-<num>'"),
113 (r'sha1sum', "don't use sha1sum, use $TESTDIR/md5sum.py"),
113 (r'sha1sum', "don't use sha1sum, use $TESTDIR/md5sum.py"),
114 (r'ls.*-\w*R', "don't use 'ls -R', use 'find'"),
114 (r'ls.*-\w*R', "don't use 'ls -R', use 'find'"),
115 (r'printf.*[^\\]\\([1-9]|0\d)', r"don't use 'printf \NNN', use Python"),
115 (r'printf.*[^\\]\\([1-9]|0\d)', r"don't use 'printf \NNN', use Python"),
116 (r'printf.*[^\\]\\x', "don't use printf \\x, use Python"),
116 (r'printf.*[^\\]\\x', "don't use printf \\x, use Python"),
117 (r'\$\(.*\)', "don't use $(expr), use `expr`"),
117 (r'\$\(.*\)', "don't use $(expr), use `expr`"),
118 (r'rm -rf \*', "don't use naked rm -rf, target a directory"),
118 (r'rm -rf \*', "don't use naked rm -rf, target a directory"),
119 (r'\[[^\]]+==', '[ foo == bar ] is a bashism, use [ foo = bar ] instead'),
119 (r'\[[^\]]+==', '[ foo == bar ] is a bashism, use [ foo = bar ] instead'),
120 (r'(^|\|\s*)grep (-\w\s+)*[^|]*[(|]\w',
120 (r'(^|\|\s*)grep (-\w\s+)*[^|]*[(|]\w',
121 "use egrep for extended grep syntax"),
121 "use egrep for extended grep syntax"),
122 (r'(^|\|\s*)e?grep .*\\S', "don't use \\S in regular expression"),
122 (r'(^|\|\s*)e?grep .*\\S', "don't use \\S in regular expression"),
123 (r'(?<!!)/bin/', "don't use explicit paths for tools"),
123 (r'(?<!!)/bin/', "don't use explicit paths for tools"),
124 (r'#!.*/bash', "don't use bash in shebang, use sh"),
124 (r'#!.*/bash', "don't use bash in shebang, use sh"),
125 (r'[^\n]\Z', "no trailing newline"),
125 (r'[^\n]\Z', "no trailing newline"),
126 (r'export .*=', "don't export and assign at once"),
126 (r'export .*=', "don't export and assign at once"),
127 (r'^source\b', "don't use 'source', use '.'"),
127 (r'^source\b', "don't use 'source', use '.'"),
128 (r'touch -d', "don't use 'touch -d', use 'touch -t' instead"),
128 (r'touch -d', "don't use 'touch -d', use 'touch -t' instead"),
129 (r'\bls +[^|\n-]+ +-', "options to 'ls' must come before filenames"),
129 (r'\bls +[^|\n-]+ +-', "options to 'ls' must come before filenames"),
130 (r'[^>\n]>\s*\$HGRCPATH', "don't overwrite $HGRCPATH, append to it"),
130 (r'[^>\n]>\s*\$HGRCPATH', "don't overwrite $HGRCPATH, append to it"),
131 (r'^stop\(\)', "don't use 'stop' as a shell function name"),
131 (r'^stop\(\)', "don't use 'stop' as a shell function name"),
132 (r'(\[|\btest\b).*-e ', "don't use 'test -e', use 'test -f'"),
132 (r'(\[|\btest\b).*-e ', "don't use 'test -e', use 'test -f'"),
133 (r'\[\[\s+[^\]]*\]\]', "don't use '[[ ]]', use '[ ]'"),
133 (r'\[\[\s+[^\]]*\]\]', "don't use '[[ ]]', use '[ ]'"),
134 (r'^alias\b.*=', "don't use alias, use a function"),
134 (r'^alias\b.*=', "don't use alias, use a function"),
135 (r'if\s*!', "don't use '!' to negate exit status"),
135 (r'if\s*!', "don't use '!' to negate exit status"),
136 (r'/dev/u?random', "don't use entropy, use /dev/zero"),
136 (r'/dev/u?random', "don't use entropy, use /dev/zero"),
137 (r'do\s*true;\s*done', "don't use true as loop body, use sleep 0"),
137 (r'do\s*true;\s*done', "don't use true as loop body, use sleep 0"),
138 (r'sed (-e )?\'(\d+|/[^/]*/)i(?!\\\n)',
138 (r'sed (-e )?\'(\d+|/[^/]*/)i(?!\\\n)',
139 "put a backslash-escaped newline after sed 'i' command"),
139 "put a backslash-escaped newline after sed 'i' command"),
140 (r'^diff *-\w*[uU].*$\n(^ \$ |^$)', "prefix diff -u/-U with cmp"),
140 (r'^diff *-\w*[uU].*$\n(^ \$ |^$)', "prefix diff -u/-U with cmp"),
141 (r'^\s+(if)? diff *-\w*[uU]', "prefix diff -u/-U with cmp"),
141 (r'^\s+(if)? diff *-\w*[uU]', "prefix diff -u/-U with cmp"),
142 (r'[\s="`\']python\s(?!bindings)', "don't use 'python', use '$PYTHON'"),
142 (r'[\s="`\']python\s(?!bindings)', "don't use 'python', use '$PYTHON'"),
143 (r'seq ', "don't use 'seq', use $TESTDIR/seq.py"),
143 (r'seq ', "don't use 'seq', use $TESTDIR/seq.py"),
144 (r'\butil\.Abort\b', "directly use error.Abort"),
144 (r'\butil\.Abort\b', "directly use error.Abort"),
145 (r'\|&', "don't use |&, use 2>&1"),
145 (r'\|&', "don't use |&, use 2>&1"),
146 (r'\w = +\w', "only one space after = allowed"),
146 (r'\w = +\w', "only one space after = allowed"),
147 (r'\bsed\b.*[^\\]\\n', "don't use 'sed ... \\n', use a \\ and a newline"),
147 (r'\bsed\b.*[^\\]\\n', "don't use 'sed ... \\n', use a \\ and a newline"),
148 (r'env.*-u', "don't use 'env -u VAR', use 'unset VAR'"),
148 (r'env.*-u', "don't use 'env -u VAR', use 'unset VAR'"),
149 (r'cp.* -r ', "don't use 'cp -r', use 'cp -R'"),
149 (r'cp.* -r ', "don't use 'cp -r', use 'cp -R'"),
150 (r'grep.* -[ABC]', "don't use grep's context flags"),
150 (r'grep.* -[ABC]', "don't use grep's context flags"),
151 (r'find.*-printf',
151 (r'find.*-printf',
152 "don't use 'find -printf', it doesn't exist on BSD find(1)"),
152 "don't use 'find -printf', it doesn't exist on BSD find(1)"),
153 (r'\$RANDOM ', "don't use bash-only $RANDOM to generate random values"),
153 ],
154 ],
154 # warnings
155 # warnings
155 [
156 [
156 (r'^function', "don't use 'function', use old style"),
157 (r'^function', "don't use 'function', use old style"),
157 (r'^diff.*-\w*N', "don't use 'diff -N'"),
158 (r'^diff.*-\w*N', "don't use 'diff -N'"),
158 (r'\$PWD|\${PWD}', "don't use $PWD, use `pwd`"),
159 (r'\$PWD|\${PWD}', "don't use $PWD, use `pwd`"),
159 (r'^([^"\'\n]|("[^"\n]*")|(\'[^\'\n]*\'))*\^', "^ must be quoted"),
160 (r'^([^"\'\n]|("[^"\n]*")|(\'[^\'\n]*\'))*\^', "^ must be quoted"),
160 (r'kill (`|\$\()', "don't use kill, use killdaemons.py")
161 (r'kill (`|\$\()', "don't use kill, use killdaemons.py")
161 ]
162 ]
162 ]
163 ]
163
164
164 testfilters = [
165 testfilters = [
165 (r"( *)(#([^!][^\n]*\S)?)", repcomment),
166 (r"( *)(#([^!][^\n]*\S)?)", repcomment),
166 (r"<<(\S+)((.|\n)*?\n\1)", rephere),
167 (r"<<(\S+)((.|\n)*?\n\1)", rephere),
167 ]
168 ]
168
169
169 uprefix = r"^ \$ "
170 uprefix = r"^ \$ "
170 utestpats = [
171 utestpats = [
171 [
172 [
172 (r'^(\S.*|| [$>] \S.*)[ \t]\n', "trailing whitespace on non-output"),
173 (r'^(\S.*|| [$>] \S.*)[ \t]\n', "trailing whitespace on non-output"),
173 (uprefix + r'.*\|\s*sed[^|>\n]*\n',
174 (uprefix + r'.*\|\s*sed[^|>\n]*\n',
174 "use regex test output patterns instead of sed"),
175 "use regex test output patterns instead of sed"),
175 (uprefix + r'(true|exit 0)', "explicit zero exit unnecessary"),
176 (uprefix + r'(true|exit 0)', "explicit zero exit unnecessary"),
176 (uprefix + r'.*(?<!\[)\$\?', "explicit exit code checks unnecessary"),
177 (uprefix + r'.*(?<!\[)\$\?', "explicit exit code checks unnecessary"),
177 (uprefix + r'.*\|\| echo.*(fail|error)',
178 (uprefix + r'.*\|\| echo.*(fail|error)',
178 "explicit exit code checks unnecessary"),
179 "explicit exit code checks unnecessary"),
179 (uprefix + r'set -e', "don't use set -e"),
180 (uprefix + r'set -e', "don't use set -e"),
180 (uprefix + r'(\s|fi\b|done\b)', "use > for continued lines"),
181 (uprefix + r'(\s|fi\b|done\b)', "use > for continued lines"),
181 (uprefix + r'.*:\.\S*/', "x:.y in a path does not work on msys, rewrite "
182 (uprefix + r'.*:\.\S*/', "x:.y in a path does not work on msys, rewrite "
182 "as x://.y, or see `hg log -k msys` for alternatives", r'-\S+:\.|' #-Rxxx
183 "as x://.y, or see `hg log -k msys` for alternatives", r'-\S+:\.|' #-Rxxx
183 '# no-msys'), # in test-pull.t which is skipped on windows
184 '# no-msys'), # in test-pull.t which is skipped on windows
184 (r'^ [^$>].*27\.0\.0\.1',
185 (r'^ [^$>].*27\.0\.0\.1',
185 'use $LOCALIP not an explicit loopback address'),
186 'use $LOCALIP not an explicit loopback address'),
186 (r'^ (?![>$] ).*\$LOCALIP.*[^)]$',
187 (r'^ (?![>$] ).*\$LOCALIP.*[^)]$',
187 'mark $LOCALIP output lines with (glob) to help tests in BSD jails'),
188 'mark $LOCALIP output lines with (glob) to help tests in BSD jails'),
188 (r'^ (cat|find): .*: \$ENOENT\$',
189 (r'^ (cat|find): .*: \$ENOENT\$',
189 'use test -f to test for file existence'),
190 'use test -f to test for file existence'),
190 (r'^ diff -[^ -]*p',
191 (r'^ diff -[^ -]*p',
191 "don't use (external) diff with -p for portability"),
192 "don't use (external) diff with -p for portability"),
192 (r' readlink ', 'use readlink.py instead of readlink'),
193 (r' readlink ', 'use readlink.py instead of readlink'),
193 (r'^ [-+][-+][-+] .* [-+]0000 \(glob\)',
194 (r'^ [-+][-+][-+] .* [-+]0000 \(glob\)',
194 "glob timezone field in diff output for portability"),
195 "glob timezone field in diff output for portability"),
195 (r'^ @@ -[0-9]+ [+][0-9]+,[0-9]+ @@',
196 (r'^ @@ -[0-9]+ [+][0-9]+,[0-9]+ @@',
196 "use '@@ -N* +N,n @@ (glob)' style chunk header for portability"),
197 "use '@@ -N* +N,n @@ (glob)' style chunk header for portability"),
197 (r'^ @@ -[0-9]+,[0-9]+ [+][0-9]+ @@',
198 (r'^ @@ -[0-9]+,[0-9]+ [+][0-9]+ @@',
198 "use '@@ -N,n +N* @@ (glob)' style chunk header for portability"),
199 "use '@@ -N,n +N* @@ (glob)' style chunk header for portability"),
199 (r'^ @@ -[0-9]+ [+][0-9]+ @@',
200 (r'^ @@ -[0-9]+ [+][0-9]+ @@',
200 "use '@@ -N* +N* @@ (glob)' style chunk header for portability"),
201 "use '@@ -N* +N* @@ (glob)' style chunk header for portability"),
201 (uprefix + r'hg( +-[^ ]+( +[^ ]+)?)* +extdiff'
202 (uprefix + r'hg( +-[^ ]+( +[^ ]+)?)* +extdiff'
202 r'( +(-[^ po-]+|--(?!program|option)[^ ]+|[^-][^ ]*))*$',
203 r'( +(-[^ po-]+|--(?!program|option)[^ ]+|[^-][^ ]*))*$',
203 "use $RUNTESTDIR/pdiff via extdiff (or -o/-p for false-positives)"),
204 "use $RUNTESTDIR/pdiff via extdiff (or -o/-p for false-positives)"),
204 ],
205 ],
205 # warnings
206 # warnings
206 [
207 [
207 (r'^ (?!.*\$LOCALIP)[^*?/\n]* \(glob\)$',
208 (r'^ (?!.*\$LOCALIP)[^*?/\n]* \(glob\)$',
208 "glob match with no glob string (?, *, /, and $LOCALIP)"),
209 "glob match with no glob string (?, *, /, and $LOCALIP)"),
209 ]
210 ]
210 ]
211 ]
211
212
212 # transform plain test rules to unified test's
213 # transform plain test rules to unified test's
213 for i in [0, 1]:
214 for i in [0, 1]:
214 for tp in testpats[i]:
215 for tp in testpats[i]:
215 p = tp[0]
216 p = tp[0]
216 m = tp[1]
217 m = tp[1]
217 if p.startswith(r'^'):
218 if p.startswith(r'^'):
218 p = r"^ [$>] (%s)" % p[1:]
219 p = r"^ [$>] (%s)" % p[1:]
219 else:
220 else:
220 p = r"^ [$>] .*(%s)" % p
221 p = r"^ [$>] .*(%s)" % p
221 utestpats[i].append((p, m) + tp[2:])
222 utestpats[i].append((p, m) + tp[2:])
222
223
223 # don't transform the following rules:
224 # don't transform the following rules:
224 # " > \t" and " \t" should be allowed in unified tests
225 # " > \t" and " \t" should be allowed in unified tests
225 testpats[0].append((r'^( *)\t', "don't use tabs to indent"))
226 testpats[0].append((r'^( *)\t', "don't use tabs to indent"))
226 utestpats[0].append((r'^( ?)\t', "don't use tabs to indent"))
227 utestpats[0].append((r'^( ?)\t', "don't use tabs to indent"))
227
228
228 utestfilters = [
229 utestfilters = [
229 (r"<<(\S+)((.|\n)*?\n > \1)", rephere),
230 (r"<<(\S+)((.|\n)*?\n > \1)", rephere),
230 (r"( +)(#([^!][^\n]*\S)?)", repcomment),
231 (r"( +)(#([^!][^\n]*\S)?)", repcomment),
231 ]
232 ]
232
233
233 pypats = [
234 pypats = [
234 [
235 [
235 (r'^\s*def\s*\w+\s*\(.*,\s*\(',
236 (r'^\s*def\s*\w+\s*\(.*,\s*\(',
236 "tuple parameter unpacking not available in Python 3+"),
237 "tuple parameter unpacking not available in Python 3+"),
237 (r'lambda\s*\(.*,.*\)',
238 (r'lambda\s*\(.*,.*\)',
238 "tuple parameter unpacking not available in Python 3+"),
239 "tuple parameter unpacking not available in Python 3+"),
239 (r'(?<!def)\s+(cmp)\(', "cmp is not available in Python 3+"),
240 (r'(?<!def)\s+(cmp)\(', "cmp is not available in Python 3+"),
240 (r'(?<!\.)\breduce\s*\(.*', "reduce is not available in Python 3+"),
241 (r'(?<!\.)\breduce\s*\(.*', "reduce is not available in Python 3+"),
241 (r'\bdict\(.*=', 'dict() is different in Py2 and 3 and is slower than {}',
242 (r'\bdict\(.*=', 'dict() is different in Py2 and 3 and is slower than {}',
242 'dict-from-generator'),
243 'dict-from-generator'),
243 (r'\.has_key\b', "dict.has_key is not available in Python 3+"),
244 (r'\.has_key\b', "dict.has_key is not available in Python 3+"),
244 (r'\s<>\s', '<> operator is not available in Python 3+, use !='),
245 (r'\s<>\s', '<> operator is not available in Python 3+, use !='),
245 (r'^\s*\t', "don't use tabs"),
246 (r'^\s*\t', "don't use tabs"),
246 (r'\S;\s*\n', "semicolon"),
247 (r'\S;\s*\n', "semicolon"),
247 (r'[^_]_\([ \t\n]*(?:"[^"]+"[ \t\n+]*)+%', "don't use % inside _()"),
248 (r'[^_]_\([ \t\n]*(?:"[^"]+"[ \t\n+]*)+%', "don't use % inside _()"),
248 (r"[^_]_\([ \t\n]*(?:'[^']+'[ \t\n+]*)+%", "don't use % inside _()"),
249 (r"[^_]_\([ \t\n]*(?:'[^']+'[ \t\n+]*)+%", "don't use % inside _()"),
249 (r'(\w|\)),\w', "missing whitespace after ,"),
250 (r'(\w|\)),\w', "missing whitespace after ,"),
250 (r'(\w|\))[+/*\-<>]\w', "missing whitespace in expression"),
251 (r'(\w|\))[+/*\-<>]\w', "missing whitespace in expression"),
251 (r'^\s+(\w|\.)+=\w[^,()\n]*$', "missing whitespace in assignment"),
252 (r'^\s+(\w|\.)+=\w[^,()\n]*$', "missing whitespace in assignment"),
252 (r'\w\s=\s\s+\w', "gratuitous whitespace after ="),
253 (r'\w\s=\s\s+\w', "gratuitous whitespace after ="),
253 ((
254 ((
254 # a line ending with a colon, potentially with trailing comments
255 # a line ending with a colon, potentially with trailing comments
255 r':([ \t]*#[^\n]*)?\n'
256 r':([ \t]*#[^\n]*)?\n'
256 # one that is not a pass and not only a comment
257 # one that is not a pass and not only a comment
257 r'(?P<indent>[ \t]+)[^#][^\n]+\n'
258 r'(?P<indent>[ \t]+)[^#][^\n]+\n'
258 # more lines at the same indent level
259 # more lines at the same indent level
259 r'((?P=indent)[^\n]+\n)*'
260 r'((?P=indent)[^\n]+\n)*'
260 # a pass at the same indent level, which is bogus
261 # a pass at the same indent level, which is bogus
261 r'(?P=indent)pass[ \t\n#]'
262 r'(?P=indent)pass[ \t\n#]'
262 ), 'omit superfluous pass'),
263 ), 'omit superfluous pass'),
263 (r'.{81}', "line too long"),
264 (r'.{81}', "line too long"),
264 (r'[^\n]\Z', "no trailing newline"),
265 (r'[^\n]\Z', "no trailing newline"),
265 (r'(\S[ \t]+|^[ \t]+)\n', "trailing whitespace"),
266 (r'(\S[ \t]+|^[ \t]+)\n', "trailing whitespace"),
266 # (r'^\s+[^_ \n][^_. \n]+_[^_\n]+\s*=',
267 # (r'^\s+[^_ \n][^_. \n]+_[^_\n]+\s*=',
267 # "don't use underbars in identifiers"),
268 # "don't use underbars in identifiers"),
268 (r'^\s+(self\.)?[A-Za-z][a-z0-9]+[A-Z]\w* = ',
269 (r'^\s+(self\.)?[A-Za-z][a-z0-9]+[A-Z]\w* = ',
269 "don't use camelcase in identifiers", r'#.*camelcase-required'),
270 "don't use camelcase in identifiers", r'#.*camelcase-required'),
270 (r'^\s*(if|while|def|class|except|try)\s[^[\n]*:\s*[^\\n]#\s]+',
271 (r'^\s*(if|while|def|class|except|try)\s[^[\n]*:\s*[^\\n]#\s]+',
271 "linebreak after :"),
272 "linebreak after :"),
272 (r'class\s[^( \n]+:', "old-style class, use class foo(object)",
273 (r'class\s[^( \n]+:', "old-style class, use class foo(object)",
273 r'#.*old-style'),
274 r'#.*old-style'),
274 (r'class\s[^( \n]+\(\):',
275 (r'class\s[^( \n]+\(\):',
275 "class foo() creates old style object, use class foo(object)",
276 "class foo() creates old style object, use class foo(object)",
276 r'#.*old-style'),
277 r'#.*old-style'),
277 (r'\b(%s)\(' % '|'.join(k for k in keyword.kwlist
278 (r'\b(%s)\(' % '|'.join(k for k in keyword.kwlist
278 if k not in ('print', 'exec')),
279 if k not in ('print', 'exec')),
279 "Python keyword is not a function"),
280 "Python keyword is not a function"),
280 (r',]', "unneeded trailing ',' in list"),
281 (r',]', "unneeded trailing ',' in list"),
281 # (r'class\s[A-Z][^\(]*\((?!Exception)',
282 # (r'class\s[A-Z][^\(]*\((?!Exception)',
282 # "don't capitalize non-exception classes"),
283 # "don't capitalize non-exception classes"),
283 # (r'in range\(', "use xrange"),
284 # (r'in range\(', "use xrange"),
284 # (r'^\s*print\s+', "avoid using print in core and extensions"),
285 # (r'^\s*print\s+', "avoid using print in core and extensions"),
285 (r'[\x80-\xff]', "non-ASCII character literal"),
286 (r'[\x80-\xff]', "non-ASCII character literal"),
286 (r'("\')\.format\(', "str.format() has no bytes counterpart, use %"),
287 (r'("\')\.format\(', "str.format() has no bytes counterpart, use %"),
287 (r'^\s*(%s)\s\s' % '|'.join(keyword.kwlist),
288 (r'^\s*(%s)\s\s' % '|'.join(keyword.kwlist),
288 "gratuitous whitespace after Python keyword"),
289 "gratuitous whitespace after Python keyword"),
289 (r'([\(\[][ \t]\S)|(\S[ \t][\)\]])', "gratuitous whitespace in () or []"),
290 (r'([\(\[][ \t]\S)|(\S[ \t][\)\]])', "gratuitous whitespace in () or []"),
290 # (r'\s\s=', "gratuitous whitespace before ="),
291 # (r'\s\s=', "gratuitous whitespace before ="),
291 (r'[^>< ](\+=|-=|!=|<>|<=|>=|<<=|>>=|%=)\S',
292 (r'[^>< ](\+=|-=|!=|<>|<=|>=|<<=|>>=|%=)\S',
292 "missing whitespace around operator"),
293 "missing whitespace around operator"),
293 (r'[^>< ](\+=|-=|!=|<>|<=|>=|<<=|>>=|%=)\s',
294 (r'[^>< ](\+=|-=|!=|<>|<=|>=|<<=|>>=|%=)\s',
294 "missing whitespace around operator"),
295 "missing whitespace around operator"),
295 (r'\s(\+=|-=|!=|<>|<=|>=|<<=|>>=|%=)\S',
296 (r'\s(\+=|-=|!=|<>|<=|>=|<<=|>>=|%=)\S',
296 "missing whitespace around operator"),
297 "missing whitespace around operator"),
297 (r'[^^+=*/!<>&| %-](\s=|=\s)[^= ]',
298 (r'[^^+=*/!<>&| %-](\s=|=\s)[^= ]',
298 "wrong whitespace around ="),
299 "wrong whitespace around ="),
299 (r'\([^()]*( =[^=]|[^<>!=]= )',
300 (r'\([^()]*( =[^=]|[^<>!=]= )',
300 "no whitespace around = for named parameters"),
301 "no whitespace around = for named parameters"),
301 (r'raise Exception', "don't raise generic exceptions"),
302 (r'raise Exception', "don't raise generic exceptions"),
302 (r'raise [^,(]+, (\([^\)]+\)|[^,\(\)]+)$',
303 (r'raise [^,(]+, (\([^\)]+\)|[^,\(\)]+)$',
303 "don't use old-style two-argument raise, use Exception(message)"),
304 "don't use old-style two-argument raise, use Exception(message)"),
304 (r' is\s+(not\s+)?["\'0-9-]', "object comparison with literal"),
305 (r' is\s+(not\s+)?["\'0-9-]', "object comparison with literal"),
305 (r' [=!]=\s+(True|False|None)',
306 (r' [=!]=\s+(True|False|None)',
306 "comparison with singleton, use 'is' or 'is not' instead"),
307 "comparison with singleton, use 'is' or 'is not' instead"),
307 (r'^\s*(while|if) [01]:',
308 (r'^\s*(while|if) [01]:',
308 "use True/False for constant Boolean expression"),
309 "use True/False for constant Boolean expression"),
309 (r'^\s*if False(:| +and)', 'Remove code instead of using `if False`'),
310 (r'^\s*if False(:| +and)', 'Remove code instead of using `if False`'),
310 (r'(?:(?<!def)\s+|\()hasattr\(',
311 (r'(?:(?<!def)\s+|\()hasattr\(',
311 'hasattr(foo, bar) is broken on py2, use util.safehasattr(foo, bar) '
312 'hasattr(foo, bar) is broken on py2, use util.safehasattr(foo, bar) '
312 'instead', r'#.*hasattr-py3-only'),
313 'instead', r'#.*hasattr-py3-only'),
313 (r'opener\([^)]*\).read\(',
314 (r'opener\([^)]*\).read\(',
314 "use opener.read() instead"),
315 "use opener.read() instead"),
315 (r'opener\([^)]*\).write\(',
316 (r'opener\([^)]*\).write\(',
316 "use opener.write() instead"),
317 "use opener.write() instead"),
317 (r'[\s\(](open|file)\([^)]*\)\.read\(',
318 (r'[\s\(](open|file)\([^)]*\)\.read\(',
318 "use util.readfile() instead"),
319 "use util.readfile() instead"),
319 (r'[\s\(](open|file)\([^)]*\)\.write\(',
320 (r'[\s\(](open|file)\([^)]*\)\.write\(',
320 "use util.writefile() instead"),
321 "use util.writefile() instead"),
321 (r'^[\s\(]*(open(er)?|file)\([^)]*\)',
322 (r'^[\s\(]*(open(er)?|file)\([^)]*\)',
322 "always assign an opened file to a variable, and close it afterwards"),
323 "always assign an opened file to a variable, and close it afterwards"),
323 (r'[\s\(](open|file)\([^)]*\)\.',
324 (r'[\s\(](open|file)\([^)]*\)\.',
324 "always assign an opened file to a variable, and close it afterwards"),
325 "always assign an opened file to a variable, and close it afterwards"),
325 (r'(?i)descend[e]nt', "the proper spelling is descendAnt"),
326 (r'(?i)descend[e]nt', "the proper spelling is descendAnt"),
326 (r'\.debug\(\_', "don't mark debug messages for translation"),
327 (r'\.debug\(\_', "don't mark debug messages for translation"),
327 (r'\.strip\(\)\.split\(\)', "no need to strip before splitting"),
328 (r'\.strip\(\)\.split\(\)', "no need to strip before splitting"),
328 (r'^\s*except\s*:', "naked except clause", r'#.*re-raises'),
329 (r'^\s*except\s*:', "naked except clause", r'#.*re-raises'),
329 (r'^\s*except\s([^\(,]+|\([^\)]+\))\s*,',
330 (r'^\s*except\s([^\(,]+|\([^\)]+\))\s*,',
330 'legacy exception syntax; use "as" instead of ","'),
331 'legacy exception syntax; use "as" instead of ","'),
331 (r':\n( )*( ){1,3}[^ ]', "must indent 4 spaces"),
332 (r':\n( )*( ){1,3}[^ ]', "must indent 4 spaces"),
332 (r'release\(.*wlock, .*lock\)', "wrong lock release order"),
333 (r'release\(.*wlock, .*lock\)', "wrong lock release order"),
333 (r'\bdef\s+__bool__\b', "__bool__ should be __nonzero__ in Python 2"),
334 (r'\bdef\s+__bool__\b', "__bool__ should be __nonzero__ in Python 2"),
334 (r'os\.path\.join\(.*, *(""|\'\')\)',
335 (r'os\.path\.join\(.*, *(""|\'\')\)',
335 "use pathutil.normasprefix(path) instead of os.path.join(path, '')"),
336 "use pathutil.normasprefix(path) instead of os.path.join(path, '')"),
336 (r'\s0[0-7]+\b', 'legacy octal syntax; use "0o" prefix instead of "0"'),
337 (r'\s0[0-7]+\b', 'legacy octal syntax; use "0o" prefix instead of "0"'),
337 # XXX only catch mutable arguments on the first line of the definition
338 # XXX only catch mutable arguments on the first line of the definition
338 (r'def.*[( ]\w+=\{\}', "don't use mutable default arguments"),
339 (r'def.*[( ]\w+=\{\}', "don't use mutable default arguments"),
339 (r'\butil\.Abort\b', "directly use error.Abort"),
340 (r'\butil\.Abort\b', "directly use error.Abort"),
340 (r'^@(\w*\.)?cachefunc', "module-level @cachefunc is risky, please avoid"),
341 (r'^@(\w*\.)?cachefunc', "module-level @cachefunc is risky, please avoid"),
341 (r'^import atexit', "don't use atexit, use ui.atexit"),
342 (r'^import atexit', "don't use atexit, use ui.atexit"),
342 (r'^import Queue', "don't use Queue, use util.queue + util.empty"),
343 (r'^import Queue', "don't use Queue, use util.queue + util.empty"),
343 (r'^import cStringIO', "don't use cStringIO.StringIO, use util.stringio"),
344 (r'^import cStringIO', "don't use cStringIO.StringIO, use util.stringio"),
344 (r'^import urllib', "don't use urllib, use util.urlreq/util.urlerr"),
345 (r'^import urllib', "don't use urllib, use util.urlreq/util.urlerr"),
345 (r'^import SocketServer', "don't use SockerServer, use util.socketserver"),
346 (r'^import SocketServer', "don't use SockerServer, use util.socketserver"),
346 (r'^import urlparse', "don't use urlparse, use util.urlreq"),
347 (r'^import urlparse', "don't use urlparse, use util.urlreq"),
347 (r'^import xmlrpclib', "don't use xmlrpclib, use util.xmlrpclib"),
348 (r'^import xmlrpclib', "don't use xmlrpclib, use util.xmlrpclib"),
348 (r'^import cPickle', "don't use cPickle, use util.pickle"),
349 (r'^import cPickle', "don't use cPickle, use util.pickle"),
349 (r'^import pickle', "don't use pickle, use util.pickle"),
350 (r'^import pickle', "don't use pickle, use util.pickle"),
350 (r'^import httplib', "don't use httplib, use util.httplib"),
351 (r'^import httplib', "don't use httplib, use util.httplib"),
351 (r'^import BaseHTTPServer', "use util.httpserver instead"),
352 (r'^import BaseHTTPServer', "use util.httpserver instead"),
352 (r'^(from|import) mercurial\.(cext|pure|cffi)',
353 (r'^(from|import) mercurial\.(cext|pure|cffi)',
353 "use mercurial.policy.importmod instead"),
354 "use mercurial.policy.importmod instead"),
354 (r'\.next\(\)', "don't use .next(), use next(...)"),
355 (r'\.next\(\)', "don't use .next(), use next(...)"),
355 (r'([a-z]*).revision\(\1\.node\(',
356 (r'([a-z]*).revision\(\1\.node\(',
356 "don't convert rev to node before passing to revision(nodeorrev)"),
357 "don't convert rev to node before passing to revision(nodeorrev)"),
357 (r'platform\.system\(\)', "don't use platform.system(), use pycompat"),
358 (r'platform\.system\(\)', "don't use platform.system(), use pycompat"),
358
359
359 # rules depending on implementation of repquote()
360 # rules depending on implementation of repquote()
360 (r' x+[xpqo%APM][\'"]\n\s+[\'"]x',
361 (r' x+[xpqo%APM][\'"]\n\s+[\'"]x',
361 'string join across lines with no space'),
362 'string join across lines with no space'),
362 (r'''(?x)ui\.(status|progress|write|note|warn)\(
363 (r'''(?x)ui\.(status|progress|write|note|warn)\(
363 [ \t\n#]*
364 [ \t\n#]*
364 (?# any strings/comments might precede a string, which
365 (?# any strings/comments might precede a string, which
365 # contains translatable message)
366 # contains translatable message)
366 ((['"]|\'\'\'|""")[ \npq%bAPMxno]*(['"]|\'\'\'|""")[ \t\n#]+)*
367 ((['"]|\'\'\'|""")[ \npq%bAPMxno]*(['"]|\'\'\'|""")[ \t\n#]+)*
367 (?# sequence consisting of below might precede translatable message
368 (?# sequence consisting of below might precede translatable message
368 # - formatting string: "% 10s", "%05d", "% -3.2f", "%*s", "%%" ...
369 # - formatting string: "% 10s", "%05d", "% -3.2f", "%*s", "%%" ...
369 # - escaped character: "\\", "\n", "\0" ...
370 # - escaped character: "\\", "\n", "\0" ...
370 # - character other than '%', 'b' as '\', and 'x' as alphabet)
371 # - character other than '%', 'b' as '\', and 'x' as alphabet)
371 (['"]|\'\'\'|""")
372 (['"]|\'\'\'|""")
372 ((%([ n]?[PM]?([np]+|A))?x)|%%|b[bnx]|[ \nnpqAPMo])*x
373 ((%([ n]?[PM]?([np]+|A))?x)|%%|b[bnx]|[ \nnpqAPMo])*x
373 (?# this regexp can't use [^...] style,
374 (?# this regexp can't use [^...] style,
374 # because _preparepats forcibly adds "\n" into [^...],
375 # because _preparepats forcibly adds "\n" into [^...],
375 # even though this regexp wants match it against "\n")''',
376 # even though this regexp wants match it against "\n")''',
376 "missing _() in ui message (use () to hide false-positives)"),
377 "missing _() in ui message (use () to hide false-positives)"),
377 ],
378 ],
378 # warnings
379 # warnings
379 [
380 [
380 # rules depending on implementation of repquote()
381 # rules depending on implementation of repquote()
381 (r'(^| )pp +xxxxqq[ \n][^\n]', "add two newlines after '.. note::'"),
382 (r'(^| )pp +xxxxqq[ \n][^\n]', "add two newlines after '.. note::'"),
382 ]
383 ]
383 ]
384 ]
384
385
385 pyfilters = [
386 pyfilters = [
386 (r"""(?msx)(?P<comment>\#.*?$)|
387 (r"""(?msx)(?P<comment>\#.*?$)|
387 ((?P<quote>('''|\"\"\"|(?<!')'(?!')|(?<!")"(?!")))
388 ((?P<quote>('''|\"\"\"|(?<!')'(?!')|(?<!")"(?!")))
388 (?P<text>(([^\\]|\\.)*?))
389 (?P<text>(([^\\]|\\.)*?))
389 (?P=quote))""", reppython),
390 (?P=quote))""", reppython),
390 ]
391 ]
391
392
392 # non-filter patterns
393 # non-filter patterns
393 pynfpats = [
394 pynfpats = [
394 [
395 [
395 (r'pycompat\.osname\s*[=!]=\s*[\'"]nt[\'"]', "use pycompat.iswindows"),
396 (r'pycompat\.osname\s*[=!]=\s*[\'"]nt[\'"]', "use pycompat.iswindows"),
396 (r'pycompat\.osname\s*[=!]=\s*[\'"]posix[\'"]', "use pycompat.isposix"),
397 (r'pycompat\.osname\s*[=!]=\s*[\'"]posix[\'"]', "use pycompat.isposix"),
397 (r'pycompat\.sysplatform\s*[!=]=\s*[\'"]darwin[\'"]',
398 (r'pycompat\.sysplatform\s*[!=]=\s*[\'"]darwin[\'"]',
398 "use pycompat.isdarwin"),
399 "use pycompat.isdarwin"),
399 ],
400 ],
400 # warnings
401 # warnings
401 [],
402 [],
402 ]
403 ]
403
404
404 # extension non-filter patterns
405 # extension non-filter patterns
405 pyextnfpats = [
406 pyextnfpats = [
406 [(r'^"""\n?[A-Z]', "don't capitalize docstring title")],
407 [(r'^"""\n?[A-Z]', "don't capitalize docstring title")],
407 # warnings
408 # warnings
408 [],
409 [],
409 ]
410 ]
410
411
411 txtfilters = []
412 txtfilters = []
412
413
413 txtpats = [
414 txtpats = [
414 [
415 [
415 ('\s$', 'trailing whitespace'),
416 ('\s$', 'trailing whitespace'),
416 ('.. note::[ \n][^\n]', 'add two newlines after note::')
417 ('.. note::[ \n][^\n]', 'add two newlines after note::')
417 ],
418 ],
418 []
419 []
419 ]
420 ]
420
421
421 cpats = [
422 cpats = [
422 [
423 [
423 (r'//', "don't use //-style comments"),
424 (r'//', "don't use //-style comments"),
424 (r'\S\t', "don't use tabs except for indent"),
425 (r'\S\t', "don't use tabs except for indent"),
425 (r'(\S[ \t]+|^[ \t]+)\n', "trailing whitespace"),
426 (r'(\S[ \t]+|^[ \t]+)\n', "trailing whitespace"),
426 (r'.{81}', "line too long"),
427 (r'.{81}', "line too long"),
427 (r'(while|if|do|for)\(', "use space after while/if/do/for"),
428 (r'(while|if|do|for)\(', "use space after while/if/do/for"),
428 (r'return\(', "return is not a function"),
429 (r'return\(', "return is not a function"),
429 (r' ;', "no space before ;"),
430 (r' ;', "no space before ;"),
430 (r'[^;] \)', "no space before )"),
431 (r'[^;] \)', "no space before )"),
431 (r'[)][{]', "space between ) and {"),
432 (r'[)][{]', "space between ) and {"),
432 (r'\w+\* \w+', "use int *foo, not int* foo"),
433 (r'\w+\* \w+', "use int *foo, not int* foo"),
433 (r'\W\([^\)]+\) \w+', "use (int)foo, not (int) foo"),
434 (r'\W\([^\)]+\) \w+', "use (int)foo, not (int) foo"),
434 (r'\w+ (\+\+|--)', "use foo++, not foo ++"),
435 (r'\w+ (\+\+|--)', "use foo++, not foo ++"),
435 (r'\w,\w', "missing whitespace after ,"),
436 (r'\w,\w', "missing whitespace after ,"),
436 (r'^[^#]\w[+/*]\w', "missing whitespace in expression"),
437 (r'^[^#]\w[+/*]\w', "missing whitespace in expression"),
437 (r'\w\s=\s\s+\w', "gratuitous whitespace after ="),
438 (r'\w\s=\s\s+\w', "gratuitous whitespace after ="),
438 (r'^#\s+\w', "use #foo, not # foo"),
439 (r'^#\s+\w', "use #foo, not # foo"),
439 (r'[^\n]\Z', "no trailing newline"),
440 (r'[^\n]\Z', "no trailing newline"),
440 (r'^\s*#import\b', "use only #include in standard C code"),
441 (r'^\s*#import\b', "use only #include in standard C code"),
441 (r'strcpy\(', "don't use strcpy, use strlcpy or memcpy"),
442 (r'strcpy\(', "don't use strcpy, use strlcpy or memcpy"),
442 (r'strcat\(', "don't use strcat"),
443 (r'strcat\(', "don't use strcat"),
443
444
444 # rules depending on implementation of repquote()
445 # rules depending on implementation of repquote()
445 ],
446 ],
446 # warnings
447 # warnings
447 [
448 [
448 # rules depending on implementation of repquote()
449 # rules depending on implementation of repquote()
449 ]
450 ]
450 ]
451 ]
451
452
452 cfilters = [
453 cfilters = [
453 (r'(/\*)(((\*(?!/))|[^*])*)\*/', repccomment),
454 (r'(/\*)(((\*(?!/))|[^*])*)\*/', repccomment),
454 (r'''(?P<quote>(?<!")")(?P<text>([^"]|\\")+)"(?!")''', repquote),
455 (r'''(?P<quote>(?<!")")(?P<text>([^"]|\\")+)"(?!")''', repquote),
455 (r'''(#\s*include\s+<)([^>]+)>''', repinclude),
456 (r'''(#\s*include\s+<)([^>]+)>''', repinclude),
456 (r'(\()([^)]+\))', repcallspaces),
457 (r'(\()([^)]+\))', repcallspaces),
457 ]
458 ]
458
459
459 inutilpats = [
460 inutilpats = [
460 [
461 [
461 (r'\bui\.', "don't use ui in util"),
462 (r'\bui\.', "don't use ui in util"),
462 ],
463 ],
463 # warnings
464 # warnings
464 []
465 []
465 ]
466 ]
466
467
467 inrevlogpats = [
468 inrevlogpats = [
468 [
469 [
469 (r'\brepo\.', "don't use repo in revlog"),
470 (r'\brepo\.', "don't use repo in revlog"),
470 ],
471 ],
471 # warnings
472 # warnings
472 []
473 []
473 ]
474 ]
474
475
475 webtemplatefilters = []
476 webtemplatefilters = []
476
477
477 webtemplatepats = [
478 webtemplatepats = [
478 [],
479 [],
479 [
480 [
480 (r'{desc(\|(?!websub|firstline)[^\|]*)+}',
481 (r'{desc(\|(?!websub|firstline)[^\|]*)+}',
481 'follow desc keyword with either firstline or websub'),
482 'follow desc keyword with either firstline or websub'),
482 ]
483 ]
483 ]
484 ]
484
485
485 allfilesfilters = []
486 allfilesfilters = []
486
487
487 allfilespats = [
488 allfilespats = [
488 [
489 [
489 (r'(http|https)://[a-zA-Z0-9./]*selenic.com/',
490 (r'(http|https)://[a-zA-Z0-9./]*selenic.com/',
490 'use mercurial-scm.org domain URL'),
491 'use mercurial-scm.org domain URL'),
491 (r'mercurial@selenic\.com',
492 (r'mercurial@selenic\.com',
492 'use mercurial-scm.org domain for mercurial ML address'),
493 'use mercurial-scm.org domain for mercurial ML address'),
493 (r'mercurial-devel@selenic\.com',
494 (r'mercurial-devel@selenic\.com',
494 'use mercurial-scm.org domain for mercurial-devel ML address'),
495 'use mercurial-scm.org domain for mercurial-devel ML address'),
495 ],
496 ],
496 # warnings
497 # warnings
497 [],
498 [],
498 ]
499 ]
499
500
500 py3pats = [
501 py3pats = [
501 [
502 [
502 (r'os\.environ', "use encoding.environ instead (py3)", r'#.*re-exports'),
503 (r'os\.environ', "use encoding.environ instead (py3)", r'#.*re-exports'),
503 (r'os\.name', "use pycompat.osname instead (py3)"),
504 (r'os\.name', "use pycompat.osname instead (py3)"),
504 (r'os\.getcwd', "use pycompat.getcwd instead (py3)"),
505 (r'os\.getcwd', "use pycompat.getcwd instead (py3)"),
505 (r'os\.sep', "use pycompat.ossep instead (py3)"),
506 (r'os\.sep', "use pycompat.ossep instead (py3)"),
506 (r'os\.pathsep', "use pycompat.ospathsep instead (py3)"),
507 (r'os\.pathsep', "use pycompat.ospathsep instead (py3)"),
507 (r'os\.altsep', "use pycompat.osaltsep instead (py3)"),
508 (r'os\.altsep', "use pycompat.osaltsep instead (py3)"),
508 (r'sys\.platform', "use pycompat.sysplatform instead (py3)"),
509 (r'sys\.platform', "use pycompat.sysplatform instead (py3)"),
509 (r'getopt\.getopt', "use pycompat.getoptb instead (py3)"),
510 (r'getopt\.getopt', "use pycompat.getoptb instead (py3)"),
510 (r'os\.getenv', "use encoding.environ.get instead"),
511 (r'os\.getenv', "use encoding.environ.get instead"),
511 (r'os\.setenv', "modifying the environ dict is not preferred"),
512 (r'os\.setenv', "modifying the environ dict is not preferred"),
512 ],
513 ],
513 # warnings
514 # warnings
514 [],
515 [],
515 ]
516 ]
516
517
517 checks = [
518 checks = [
518 ('python', r'.*\.(py|cgi)$', r'^#!.*python', pyfilters, pypats),
519 ('python', r'.*\.(py|cgi)$', r'^#!.*python', pyfilters, pypats),
519 ('python', r'.*\.(py|cgi)$', r'^#!.*python', [], pynfpats),
520 ('python', r'.*\.(py|cgi)$', r'^#!.*python', [], pynfpats),
520 ('python', r'.*hgext.*\.py$', '', [], pyextnfpats),
521 ('python', r'.*hgext.*\.py$', '', [], pyextnfpats),
521 ('python 3', r'.*(hgext|mercurial)/(?!demandimport|policy|pycompat).*\.py',
522 ('python 3', r'.*(hgext|mercurial)/(?!demandimport|policy|pycompat).*\.py',
522 '', pyfilters, py3pats),
523 '', pyfilters, py3pats),
523 ('test script', r'(.*/)?test-[^.~]*$', '', testfilters, testpats),
524 ('test script', r'(.*/)?test-[^.~]*$', '', testfilters, testpats),
524 ('c', r'.*\.[ch]$', '', cfilters, cpats),
525 ('c', r'.*\.[ch]$', '', cfilters, cpats),
525 ('unified test', r'.*\.t$', '', utestfilters, utestpats),
526 ('unified test', r'.*\.t$', '', utestfilters, utestpats),
526 ('layering violation repo in revlog', r'mercurial/revlog\.py', '',
527 ('layering violation repo in revlog', r'mercurial/revlog\.py', '',
527 pyfilters, inrevlogpats),
528 pyfilters, inrevlogpats),
528 ('layering violation ui in util', r'mercurial/util\.py', '', pyfilters,
529 ('layering violation ui in util', r'mercurial/util\.py', '', pyfilters,
529 inutilpats),
530 inutilpats),
530 ('txt', r'.*\.txt$', '', txtfilters, txtpats),
531 ('txt', r'.*\.txt$', '', txtfilters, txtpats),
531 ('web template', r'mercurial/templates/.*\.tmpl', '',
532 ('web template', r'mercurial/templates/.*\.tmpl', '',
532 webtemplatefilters, webtemplatepats),
533 webtemplatefilters, webtemplatepats),
533 ('all except for .po', r'.*(?<!\.po)$', '',
534 ('all except for .po', r'.*(?<!\.po)$', '',
534 allfilesfilters, allfilespats),
535 allfilesfilters, allfilespats),
535 ]
536 ]
536
537
537 def _preparepats():
538 def _preparepats():
538 for c in checks:
539 for c in checks:
539 failandwarn = c[-1]
540 failandwarn = c[-1]
540 for pats in failandwarn:
541 for pats in failandwarn:
541 for i, pseq in enumerate(pats):
542 for i, pseq in enumerate(pats):
542 # fix-up regexes for multi-line searches
543 # fix-up regexes for multi-line searches
543 p = pseq[0]
544 p = pseq[0]
544 # \s doesn't match \n
545 # \s doesn't match \n
545 p = re.sub(r'(?<!\\)\\s', r'[ \\t]', p)
546 p = re.sub(r'(?<!\\)\\s', r'[ \\t]', p)
546 # [^...] doesn't match newline
547 # [^...] doesn't match newline
547 p = re.sub(r'(?<!\\)\[\^', r'[^\\n', p)
548 p = re.sub(r'(?<!\\)\[\^', r'[^\\n', p)
548
549
549 pats[i] = (re.compile(p, re.MULTILINE),) + pseq[1:]
550 pats[i] = (re.compile(p, re.MULTILINE),) + pseq[1:]
550 filters = c[3]
551 filters = c[3]
551 for i, flt in enumerate(filters):
552 for i, flt in enumerate(filters):
552 filters[i] = re.compile(flt[0]), flt[1]
553 filters[i] = re.compile(flt[0]), flt[1]
553
554
554 class norepeatlogger(object):
555 class norepeatlogger(object):
555 def __init__(self):
556 def __init__(self):
556 self._lastseen = None
557 self._lastseen = None
557
558
558 def log(self, fname, lineno, line, msg, blame):
559 def log(self, fname, lineno, line, msg, blame):
559 """print error related a to given line of a given file.
560 """print error related a to given line of a given file.
560
561
561 The faulty line will also be printed but only once in the case
562 The faulty line will also be printed but only once in the case
562 of multiple errors.
563 of multiple errors.
563
564
564 :fname: filename
565 :fname: filename
565 :lineno: line number
566 :lineno: line number
566 :line: actual content of the line
567 :line: actual content of the line
567 :msg: error message
568 :msg: error message
568 """
569 """
569 msgid = fname, lineno, line
570 msgid = fname, lineno, line
570 if msgid != self._lastseen:
571 if msgid != self._lastseen:
571 if blame:
572 if blame:
572 print("%s:%d (%s):" % (fname, lineno, blame))
573 print("%s:%d (%s):" % (fname, lineno, blame))
573 else:
574 else:
574 print("%s:%d:" % (fname, lineno))
575 print("%s:%d:" % (fname, lineno))
575 print(" > %s" % line)
576 print(" > %s" % line)
576 self._lastseen = msgid
577 self._lastseen = msgid
577 print(" " + msg)
578 print(" " + msg)
578
579
579 _defaultlogger = norepeatlogger()
580 _defaultlogger = norepeatlogger()
580
581
581 def getblame(f):
582 def getblame(f):
582 lines = []
583 lines = []
583 for l in os.popen('hg annotate -un %s' % f):
584 for l in os.popen('hg annotate -un %s' % f):
584 start, line = l.split(':', 1)
585 start, line = l.split(':', 1)
585 user, rev = start.split()
586 user, rev = start.split()
586 lines.append((line[1:-1], user, rev))
587 lines.append((line[1:-1], user, rev))
587 return lines
588 return lines
588
589
589 def checkfile(f, logfunc=_defaultlogger.log, maxerr=None, warnings=False,
590 def checkfile(f, logfunc=_defaultlogger.log, maxerr=None, warnings=False,
590 blame=False, debug=False, lineno=True):
591 blame=False, debug=False, lineno=True):
591 """checks style and portability of a given file
592 """checks style and portability of a given file
592
593
593 :f: filepath
594 :f: filepath
594 :logfunc: function used to report error
595 :logfunc: function used to report error
595 logfunc(filename, linenumber, linecontent, errormessage)
596 logfunc(filename, linenumber, linecontent, errormessage)
596 :maxerr: number of error to display before aborting.
597 :maxerr: number of error to display before aborting.
597 Set to false (default) to report all errors
598 Set to false (default) to report all errors
598
599
599 return True if no error is found, False otherwise.
600 return True if no error is found, False otherwise.
600 """
601 """
601 blamecache = None
602 blamecache = None
602 result = True
603 result = True
603
604
604 try:
605 try:
605 with opentext(f) as fp:
606 with opentext(f) as fp:
606 try:
607 try:
607 pre = post = fp.read()
608 pre = post = fp.read()
608 except UnicodeDecodeError as e:
609 except UnicodeDecodeError as e:
609 print("%s while reading %s" % (e, f))
610 print("%s while reading %s" % (e, f))
610 return result
611 return result
611 except IOError as e:
612 except IOError as e:
612 print("Skipping %s, %s" % (f, str(e).split(':', 1)[0]))
613 print("Skipping %s, %s" % (f, str(e).split(':', 1)[0]))
613 return result
614 return result
614
615
615 for name, match, magic, filters, pats in checks:
616 for name, match, magic, filters, pats in checks:
616 post = pre # discard filtering result of previous check
617 post = pre # discard filtering result of previous check
617 if debug:
618 if debug:
618 print(name, f)
619 print(name, f)
619 fc = 0
620 fc = 0
620 if not (re.match(match, f) or (magic and re.search(magic, pre))):
621 if not (re.match(match, f) or (magic and re.search(magic, pre))):
621 if debug:
622 if debug:
622 print("Skipping %s for %s it doesn't match %s" % (
623 print("Skipping %s for %s it doesn't match %s" % (
623 name, match, f))
624 name, match, f))
624 continue
625 continue
625 if "no-" "check-code" in pre:
626 if "no-" "check-code" in pre:
626 # If you're looking at this line, it's because a file has:
627 # If you're looking at this line, it's because a file has:
627 # no- check- code
628 # no- check- code
628 # but the reason to output skipping is to make life for
629 # but the reason to output skipping is to make life for
629 # tests easier. So, instead of writing it with a normal
630 # tests easier. So, instead of writing it with a normal
630 # spelling, we write it with the expected spelling from
631 # spelling, we write it with the expected spelling from
631 # tests/test-check-code.t
632 # tests/test-check-code.t
632 print("Skipping %s it has no-che?k-code (glob)" % f)
633 print("Skipping %s it has no-che?k-code (glob)" % f)
633 return "Skip" # skip checking this file
634 return "Skip" # skip checking this file
634 for p, r in filters:
635 for p, r in filters:
635 post = re.sub(p, r, post)
636 post = re.sub(p, r, post)
636 nerrs = len(pats[0]) # nerr elements are errors
637 nerrs = len(pats[0]) # nerr elements are errors
637 if warnings:
638 if warnings:
638 pats = pats[0] + pats[1]
639 pats = pats[0] + pats[1]
639 else:
640 else:
640 pats = pats[0]
641 pats = pats[0]
641 # print post # uncomment to show filtered version
642 # print post # uncomment to show filtered version
642
643
643 if debug:
644 if debug:
644 print("Checking %s for %s" % (name, f))
645 print("Checking %s for %s" % (name, f))
645
646
646 prelines = None
647 prelines = None
647 errors = []
648 errors = []
648 for i, pat in enumerate(pats):
649 for i, pat in enumerate(pats):
649 if len(pat) == 3:
650 if len(pat) == 3:
650 p, msg, ignore = pat
651 p, msg, ignore = pat
651 else:
652 else:
652 p, msg = pat
653 p, msg = pat
653 ignore = None
654 ignore = None
654 if i >= nerrs:
655 if i >= nerrs:
655 msg = "warning: " + msg
656 msg = "warning: " + msg
656
657
657 pos = 0
658 pos = 0
658 n = 0
659 n = 0
659 for m in p.finditer(post):
660 for m in p.finditer(post):
660 if prelines is None:
661 if prelines is None:
661 prelines = pre.splitlines()
662 prelines = pre.splitlines()
662 postlines = post.splitlines(True)
663 postlines = post.splitlines(True)
663
664
664 start = m.start()
665 start = m.start()
665 while n < len(postlines):
666 while n < len(postlines):
666 step = len(postlines[n])
667 step = len(postlines[n])
667 if pos + step > start:
668 if pos + step > start:
668 break
669 break
669 pos += step
670 pos += step
670 n += 1
671 n += 1
671 l = prelines[n]
672 l = prelines[n]
672
673
673 if ignore and re.search(ignore, l, re.MULTILINE):
674 if ignore and re.search(ignore, l, re.MULTILINE):
674 if debug:
675 if debug:
675 print("Skipping %s for %s:%s (ignore pattern)" % (
676 print("Skipping %s for %s:%s (ignore pattern)" % (
676 name, f, n))
677 name, f, n))
677 continue
678 continue
678 bd = ""
679 bd = ""
679 if blame:
680 if blame:
680 bd = 'working directory'
681 bd = 'working directory'
681 if not blamecache:
682 if not blamecache:
682 blamecache = getblame(f)
683 blamecache = getblame(f)
683 if n < len(blamecache):
684 if n < len(blamecache):
684 bl, bu, br = blamecache[n]
685 bl, bu, br = blamecache[n]
685 if bl == l:
686 if bl == l:
686 bd = '%s@%s' % (bu, br)
687 bd = '%s@%s' % (bu, br)
687
688
688 errors.append((f, lineno and n + 1, l, msg, bd))
689 errors.append((f, lineno and n + 1, l, msg, bd))
689 result = False
690 result = False
690
691
691 errors.sort()
692 errors.sort()
692 for e in errors:
693 for e in errors:
693 logfunc(*e)
694 logfunc(*e)
694 fc += 1
695 fc += 1
695 if maxerr and fc >= maxerr:
696 if maxerr and fc >= maxerr:
696 print(" (too many errors, giving up)")
697 print(" (too many errors, giving up)")
697 break
698 break
698
699
699 return result
700 return result
700
701
701 def main():
702 def main():
702 parser = optparse.OptionParser("%prog [options] [files | -]")
703 parser = optparse.OptionParser("%prog [options] [files | -]")
703 parser.add_option("-w", "--warnings", action="store_true",
704 parser.add_option("-w", "--warnings", action="store_true",
704 help="include warning-level checks")
705 help="include warning-level checks")
705 parser.add_option("-p", "--per-file", type="int",
706 parser.add_option("-p", "--per-file", type="int",
706 help="max warnings per file")
707 help="max warnings per file")
707 parser.add_option("-b", "--blame", action="store_true",
708 parser.add_option("-b", "--blame", action="store_true",
708 help="use annotate to generate blame info")
709 help="use annotate to generate blame info")
709 parser.add_option("", "--debug", action="store_true",
710 parser.add_option("", "--debug", action="store_true",
710 help="show debug information")
711 help="show debug information")
711 parser.add_option("", "--nolineno", action="store_false",
712 parser.add_option("", "--nolineno", action="store_false",
712 dest='lineno', help="don't show line numbers")
713 dest='lineno', help="don't show line numbers")
713
714
714 parser.set_defaults(per_file=15, warnings=False, blame=False, debug=False,
715 parser.set_defaults(per_file=15, warnings=False, blame=False, debug=False,
715 lineno=True)
716 lineno=True)
716 (options, args) = parser.parse_args()
717 (options, args) = parser.parse_args()
717
718
718 if len(args) == 0:
719 if len(args) == 0:
719 check = glob.glob("*")
720 check = glob.glob("*")
720 elif args == ['-']:
721 elif args == ['-']:
721 # read file list from stdin
722 # read file list from stdin
722 check = sys.stdin.read().splitlines()
723 check = sys.stdin.read().splitlines()
723 else:
724 else:
724 check = args
725 check = args
725
726
726 _preparepats()
727 _preparepats()
727
728
728 ret = 0
729 ret = 0
729 for f in check:
730 for f in check:
730 if not checkfile(f, maxerr=options.per_file, warnings=options.warnings,
731 if not checkfile(f, maxerr=options.per_file, warnings=options.warnings,
731 blame=options.blame, debug=options.debug,
732 blame=options.blame, debug=options.debug,
732 lineno=options.lineno):
733 lineno=options.lineno):
733 ret = 1
734 ret = 1
734 return ret
735 return ret
735
736
736 if __name__ == "__main__":
737 if __name__ == "__main__":
737 sys.exit(main())
738 sys.exit(main())
General Comments 0
You need to be logged in to leave comments. Login now