Show More
@@ -1,160 +1,164 | |||||
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 | import sys, re, glob |
|
10 | import sys, re, glob | |
11 |
|
11 | |||
12 | def repquote(m): |
|
12 | def repquote(m): | |
13 | t = re.sub(r"\S", "x", m.group(2)) |
|
13 | t = re.sub(r"\S", "x", m.group(2)) | |
14 | return m.group(1) + t + m.group(1) |
|
14 | return m.group(1) + t + m.group(1) | |
15 |
|
15 | |||
16 | def repcomment(m): |
|
16 | def repcomment(m): | |
17 | return m.group(1) + "#" * len(m.group(2)) |
|
17 | return m.group(1) + "#" * len(m.group(2)) | |
18 |
|
18 | |||
19 | def repccomment(m): |
|
19 | def repccomment(m): | |
20 | t = re.sub(r"((?<=\n) )|\S", "x", m.group(2)) |
|
20 | t = re.sub(r"((?<=\n) )|\S", "x", m.group(2)) | |
21 | return m.group(1) + t + "*/" |
|
21 | return m.group(1) + t + "*/" | |
22 |
|
22 | |||
23 | def repcallspaces(m): |
|
23 | def repcallspaces(m): | |
24 | t = re.sub(r"\n\s+", "\n", m.group(2)) |
|
24 | t = re.sub(r"\n\s+", "\n", m.group(2)) | |
25 | return m.group(1) + t |
|
25 | return m.group(1) + t | |
26 |
|
26 | |||
27 | def repinclude(m): |
|
27 | def repinclude(m): | |
28 | return m.group(1) + "<foo>" |
|
28 | return m.group(1) + "<foo>" | |
29 |
|
29 | |||
30 | def rephere(m): |
|
30 | def rephere(m): | |
31 | t = re.sub(r"\S", "x", m.group(2)) |
|
31 | t = re.sub(r"\S", "x", m.group(2)) | |
32 | return m.group(1) + t |
|
32 | return m.group(1) + t | |
33 |
|
33 | |||
34 |
|
34 | |||
35 | testpats = [ |
|
35 | testpats = [ | |
36 | (r'(pushd|popd)', "don't use 'pushd' or 'popd', use 'cd'"), |
|
36 | (r'(pushd|popd)', "don't use 'pushd' or 'popd', use 'cd'"), | |
37 | (r'\W\$?\(\([^\)]*\)\)', "don't use (()) or $(()), use 'expr'"), |
|
37 | (r'\W\$?\(\([^\)]*\)\)', "don't use (()) or $(()), use 'expr'"), | |
38 | (r'^function', "don't use 'function', use old style"), |
|
38 | (r'^function', "don't use 'function', use old style"), | |
39 | (r'grep.*-q', "don't use 'grep -q', redirect to /dev/null"), |
|
39 | (r'grep.*-q', "don't use 'grep -q', redirect to /dev/null"), | |
40 | (r'echo.*\\n', "don't use 'echo \\n', use printf"), |
|
40 | (r'echo.*\\n', "don't use 'echo \\n', use printf"), | |
41 | (r'^diff.*-\w*N', "don't use 'diff -N'"), |
|
41 | (r'^diff.*-\w*N', "don't use 'diff -N'"), | |
42 | (r'(^| )wc[^|]*$', "filter wc output"), |
|
42 | (r'(^| )wc[^|]*$', "filter wc output"), | |
43 | (r'head -c', "don't use 'head -c', use 'dd'"), |
|
43 | (r'head -c', "don't use 'head -c', use 'dd'"), | |
44 | (r'ls.*-\w*R', "don't use 'ls -R', use 'find'"), |
|
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"), |
|
45 | (r'printf.*\\\d\d\d', "don't use 'printf \NNN', use Python"), | |
46 | (r'printf.*\\x', "don't use printf \\x, use Python"), |
|
46 | (r'printf.*\\x', "don't use printf \\x, use Python"), | |
47 | (r'\$\(.*\)', "don't use $(expr), use `expr`"), |
|
47 | (r'\$\(.*\)', "don't use $(expr), use `expr`"), | |
48 | (r'rm -rf \*', "don't use naked rm -rf, target a directory"), |
|
48 | (r'rm -rf \*', "don't use naked rm -rf, target a directory"), | |
49 | (r'(^|\|\s*)grep (-\w\s+)*[^|]*[(|]\w', |
|
49 | (r'(^|\|\s*)grep (-\w\s+)*[^|]*[(|]\w', | |
50 | "use egrep for extended grep syntax"), |
|
50 | "use egrep for extended grep syntax"), | |
51 | (r'/bin/', "don't use explicit paths for tools"), |
|
51 | (r'/bin/', "don't use explicit paths for tools"), | |
52 | (r'\$PWD', "don't use $PWD, use `pwd`"), |
|
52 | (r'\$PWD', "don't use $PWD, use `pwd`"), | |
53 | (r'[^\n]\Z', "no trailing newline"), |
|
53 | (r'[^\n]\Z', "no trailing newline"), | |
54 | ] |
|
54 | ] | |
55 |
|
55 | |||
56 | testfilters = [ |
|
56 | testfilters = [ | |
57 | (r"( *)(#([^\n]*\S)?)", repcomment), |
|
57 | (r"( *)(#([^\n]*\S)?)", repcomment), | |
58 | (r"<<(\S+)((.|\n)*?\n\1)", rephere), |
|
58 | (r"<<(\S+)((.|\n)*?\n\1)", rephere), | |
59 | ] |
|
59 | ] | |
60 |
|
60 | |||
61 | pypats = [ |
|
61 | pypats = [ | |
62 | (r'^\s*\t', "don't use tabs"), |
|
62 | (r'^\s*\t', "don't use tabs"), | |
63 | (r'(\S\s+|^\s+)\n', "trailing whitespace"), |
|
63 | (r'\S;\s*\n', "semicolon"), | |
64 | (r'\w,\w', "missing whitespace after ,"), |
|
64 | (r'\w,\w', "missing whitespace after ,"), | |
65 | (r'\w[+/*\-<>]\w', "missing whitespace in expression"), |
|
65 | (r'\w[+/*\-<>]\w', "missing whitespace in expression"), | |
66 | (r'^\s+\w+=\w+[^,)]$', "missing whitespace in assignment"), |
|
66 | (r'^\s+\w+=\w+[^,)]$', "missing whitespace in assignment"), | |
67 | (r'.{85}', "line too long"), |
|
67 | (r'.{85}', "line too long"), | |
68 | (r'[^\n]\Z', "no trailing newline"), |
|
68 | (r'[^\n]\Z', "no trailing newline"), | |
69 | # (r'^\s+[^_ ][^_. ]+_[^_]+\s*=', "don't use underbars in identifiers"), |
|
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"), |
|
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]+', |
|
71 | (r'^\s*(if|while|def|class|except|try)\s[^[]*:\s*[^\]#\s]+', | |
72 | "linebreak after :"), |
|
72 | "linebreak after :"), | |
73 | (r'class\s[^(]:', "old-style class, use class foo(object)"), |
|
73 | (r'class\s[^(]:', "old-style class, use class foo(object)"), | |
74 | (r'^\s+del\(', "del isn't a function"), |
|
74 | (r'^\s+del\(', "del isn't a function"), | |
75 | (r'^\s+except\(', "except isn't a function"), |
|
75 | (r'^\s+except\(', "except isn't a function"), | |
|
76 | (r',]', "unneeded trailing ',' in list"), | |||
76 | # (r'class\s[A-Z][^\(]*\((?!Exception)', |
|
77 | # (r'class\s[A-Z][^\(]*\((?!Exception)', | |
77 | # "don't capitalize non-exception classes"), |
|
78 | # "don't capitalize non-exception classes"), | |
78 | # (r'in range\(', "use xrange"), |
|
79 | # (r'in range\(', "use xrange"), | |
79 | # (r'^\s*print\s+', "avoid using print in core and extensions"), |
|
80 | # (r'^\s*print\s+', "avoid using print in core and extensions"), | |
80 | (r'[\x80-\xff]', "non-ASCII character literal"), |
|
81 | (r'[\x80-\xff]', "non-ASCII character literal"), | |
81 | (r'("\')\.format\(', "str.format() not available in Python 2.4"), |
|
82 | (r'("\')\.format\(', "str.format() not available in Python 2.4"), | |
82 | (r'^\s*with\s+', "with not available in Python 2.4"), |
|
83 | (r'^\s*with\s+', "with not available in Python 2.4"), | |
83 | (r'if\s.*\selse', "if ... else form not available in Python 2.4"), |
|
84 | (r'if\s.*\selse', "if ... else form not available in Python 2.4"), | |
84 | (r'([\(\[]\s\S)|(\S\s[\)\]])', "gratuitous whitespace in () or []"), |
|
85 | (r'([\(\[]\s\S)|(\S\s[\)\]])', "gratuitous whitespace in () or []"), | |
85 | # (r'\s\s=', "gratuitous whitespace before ="), |
|
86 | # (r'\s\s=', "gratuitous whitespace before ="), | |
86 |
(r' |
|
87 | (r'[^>< ](\+=|-=|!=|<>|<=|>=|<<=|>>=)\S', "missing whitespace around operator"), | |
|
88 | (r'[^>< ](\+=|-=|!=|<>|<=|>=|<<=|>>=)\s', "missing whitespace around operator"), | |||
|
89 | (r'\s(\+=|-=|!=|<>|<=|>=|<<=|>>=)\S', "missing whitespace around operator"), | |||
|
90 | (r'[^+=*!<>&| -](\s=|=\s)[^= ]', "wrong whitespace around ="), | |||
87 | ] |
|
91 | ] | |
88 |
|
92 | |||
89 | pyfilters = [ |
|
93 | pyfilters = [ | |
90 | (r"""(''')(([^']|\\'|'{1,2}(?!'))*)'''""", repquote), |
|
94 | (r"""(''')(([^']|\\'|'{1,2}(?!'))*)'''""", repquote), | |
91 | (r'''(""")(([^"]|\\"|"{1,2}(?!"))*)"""''', repquote), |
|
95 | (r'''(""")(([^"]|\\"|"{1,2}(?!"))*)"""''', repquote), | |
92 | (r'''(?<!")(")(([^"\n]|\\")+)"(?!")''', repquote), |
|
96 | (r'''(?<!")(")(([^"\n]|\\")+)"(?!")''', repquote), | |
93 | (r"""(?<!')(')(([^'\n]|\\')+)'(?!')""", repquote), |
|
97 | (r"""(?<!')(')(([^'\n]|\\')+)'(?!')""", repquote), | |
94 | (r"( *)(#([^\n]*\S)?)", repcomment), |
|
98 | (r"( *)(#([^\n]*\S)?)", repcomment), | |
95 | ] |
|
99 | ] | |
96 |
|
100 | |||
97 | cpats = [ |
|
101 | cpats = [ | |
98 | (r'//', "don't use //-style comments"), |
|
102 | (r'//', "don't use //-style comments"), | |
99 | (r'^ ', "don't use spaces to indent"), |
|
103 | (r'^ ', "don't use spaces to indent"), | |
100 | (r'\S\t', "don't use tabs except for indent"), |
|
104 | (r'\S\t', "don't use tabs except for indent"), | |
101 | (r'(\S\s+|^\s+)\n', "trailing whitespace"), |
|
105 | (r'(\S\s+|^\s+)\n', "trailing whitespace"), | |
102 | (r'.{85}', "line too long"), |
|
106 | (r'.{85}', "line too long"), | |
103 | (r'(while|if|do|for)\(', "use space after while/if/do/for"), |
|
107 | (r'(while|if|do|for)\(', "use space after while/if/do/for"), | |
104 | (r'return\(', "return is not a function"), |
|
108 | (r'return\(', "return is not a function"), | |
105 | (r' ;', "no space before ;"), |
|
109 | (r' ;', "no space before ;"), | |
106 | (r'\w+\* \w+', "use int *foo, not int* foo"), |
|
110 | (r'\w+\* \w+', "use int *foo, not int* foo"), | |
107 | (r'\([^\)]+\) \w+', "use (int)foo, not (int) foo"), |
|
111 | (r'\([^\)]+\) \w+', "use (int)foo, not (int) foo"), | |
108 | (r'\S+ (\+\+|--)', "use foo++, not foo ++"), |
|
112 | (r'\S+ (\+\+|--)', "use foo++, not foo ++"), | |
109 | (r'\w,\w', "missing whitespace after ,"), |
|
113 | (r'\w,\w', "missing whitespace after ,"), | |
110 | (r'\w[+/*]\w', "missing whitespace in expression"), |
|
114 | (r'\w[+/*]\w', "missing whitespace in expression"), | |
111 | (r'^#\s+\w', "use #foo, not # foo"), |
|
115 | (r'^#\s+\w', "use #foo, not # foo"), | |
112 | (r'[^\n]\Z', "no trailing newline"), |
|
116 | (r'[^\n]\Z', "no trailing newline"), | |
113 | ] |
|
117 | ] | |
114 |
|
118 | |||
115 | cfilters = [ |
|
119 | cfilters = [ | |
116 | (r'(/\*)(((\*(?!/))|[^*])*)\*/', repccomment), |
|
120 | (r'(/\*)(((\*(?!/))|[^*])*)\*/', repccomment), | |
117 | (r'''(?<!")(")(([^"]|\\")+"(?!"))''', repquote), |
|
121 | (r'''(?<!")(")(([^"]|\\")+"(?!"))''', repquote), | |
118 | (r'''(#\s*include\s+<)([^>]+)>''', repinclude), |
|
122 | (r'''(#\s*include\s+<)([^>]+)>''', repinclude), | |
119 | (r'(\()([^)]+\))', repcallspaces), |
|
123 | (r'(\()([^)]+\))', repcallspaces), | |
120 | ] |
|
124 | ] | |
121 |
|
125 | |||
122 | checks = [ |
|
126 | checks = [ | |
123 | ('python', r'.*\.(py|cgi)$', pyfilters, pypats), |
|
127 | ('python', r'.*\.(py|cgi)$', pyfilters, pypats), | |
124 | ('test script', r'(.*/)?test-[^.~]*$', testfilters, testpats), |
|
128 | ('test script', r'(.*/)?test-[^.~]*$', testfilters, testpats), | |
125 | ('c', r'.*\.c$', cfilters, cpats), |
|
129 | ('c', r'.*\.c$', cfilters, cpats), | |
126 | ] |
|
130 | ] | |
127 |
|
131 | |||
128 | if len(sys.argv) == 1: |
|
132 | if len(sys.argv) == 1: | |
129 | check = glob.glob("*") |
|
133 | check = glob.glob("*") | |
130 | else: |
|
134 | else: | |
131 | check = sys.argv[1:] |
|
135 | check = sys.argv[1:] | |
132 |
|
136 | |||
133 | for f in check: |
|
137 | for f in check: | |
134 | for name, match, filters, pats in checks: |
|
138 | for name, match, filters, pats in checks: | |
135 | fc = 0 |
|
139 | fc = 0 | |
136 | if not re.match(match, f): |
|
140 | if not re.match(match, f): | |
137 | continue |
|
141 | continue | |
138 | pre = post = open(f).read() |
|
142 | pre = post = open(f).read() | |
139 | if "no-" + "check-code" in pre: |
|
143 | if "no-" + "check-code" in pre: | |
140 | break |
|
144 | break | |
141 | for p, r in filters: |
|
145 | for p, r in filters: | |
142 | post = re.sub(p, r, post) |
|
146 | post = re.sub(p, r, post) | |
143 | # print post # uncomment to show filtered version |
|
147 | # print post # uncomment to show filtered version | |
144 | z = enumerate(zip(pre.splitlines(), post.splitlines(True))) |
|
148 | z = enumerate(zip(pre.splitlines(), post.splitlines(True))) | |
145 | for n, l in z: |
|
149 | for n, l in z: | |
146 | if "check-code" + "-ignore" in l[0]: |
|
150 | if "check-code" + "-ignore" in l[0]: | |
147 | continue |
|
151 | continue | |
148 | lc = 0 |
|
152 | lc = 0 | |
149 | for p, msg in pats: |
|
153 | for p, msg in pats: | |
150 | if re.search(p, l[1]): |
|
154 | if re.search(p, l[1]): | |
151 | if not lc: |
|
155 | if not lc: | |
152 | print "%s:%d:" % (f, n + 1) |
|
156 | print "%s:%d:" % (f, n + 1) | |
153 | print " > %s" % l[0] |
|
157 | print " > %s" % l[0] | |
154 | print " %s" % msg |
|
158 | print " %s" % msg | |
155 | lc += 1 |
|
159 | lc += 1 | |
156 | fc += 1 |
|
160 | fc += 1 | |
157 | if fc == 15: |
|
161 | if fc == 15: | |
158 | print " (too many errors, giving up)" |
|
162 | print " (too many errors, giving up)" | |
159 | break |
|
163 | break | |
160 | break |
|
164 | break |
General Comments 0
You need to be logged in to leave comments.
Login now