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