Mercurial > public > mercurial-scm > hg
comparison contrib/check-code.py @ 10717:b1f4fcef99b3
check-code: Add a ``checkfile`` function
The part of the code actually checking each file is moved in the
``checkfile`` function to allow external reuses.
author | Pierre-Yves David <pierre-yves.david@logilab.fr> |
---|---|
date | Tue, 16 Mar 2010 19:52:57 +0100 |
parents | 5f92bde72eef |
children | f18c37fd624f |
comparison
equal
deleted
inserted
replaced
10716:5f92bde72eef | 10717:b1f4fcef99b3 |
---|---|
131 checks = [ | 131 checks = [ |
132 ('python', r'.*\.(py|cgi)$', pyfilters, pypats), | 132 ('python', r'.*\.(py|cgi)$', pyfilters, pypats), |
133 ('test script', r'(.*/)?test-[^.~]*$', testfilters, testpats), | 133 ('test script', r'(.*/)?test-[^.~]*$', testfilters, testpats), |
134 ('c', r'.*\.c$', cfilters, cpats), | 134 ('c', r'.*\.c$', cfilters, cpats), |
135 ] | 135 ] |
136 def checkfile(f): | |
137 """checks style and portability of a given file""" | |
138 for name, match, filters, pats in checks: | |
139 fc = 0 | |
140 if not re.match(match, f): | |
141 continue | |
142 pre = post = open(f).read() | |
143 if "no-" + "check-code" in pre: | |
144 break | |
145 for p, r in filters: | |
146 post = re.sub(p, r, post) | |
147 # print post # uncomment to show filtered version | |
148 z = enumerate(zip(pre.splitlines(), post.splitlines(True))) | |
149 for n, l in z: | |
150 if "check-code" + "-ignore" in l[0]: | |
151 continue | |
152 lc = 0 | |
153 for p, msg in pats: | |
154 if re.search(p, l[1]): | |
155 if not lc: | |
156 print "%s:%d:" % (f, n + 1) | |
157 print " > %s" % l[0] | |
158 print " %s" % msg | |
159 lc += 1 | |
160 fc += 1 | |
161 if fc == 15: | |
162 print " (too many errors, giving up)" | |
163 break | |
164 break | |
165 | |
136 | 166 |
137 if __name__ == "__main__": | 167 if __name__ == "__main__": |
138 if len(sys.argv) == 1: | 168 if len(sys.argv) == 1: |
139 check = glob.glob("*") | 169 check = glob.glob("*") |
140 else: | 170 else: |
141 check = sys.argv[1:] | 171 check = sys.argv[1:] |
142 | 172 |
143 for f in check: | 173 for f in check: |
144 for name, match, filters, pats in checks: | 174 checkfile(f) |
145 fc = 0 | |
146 if not re.match(match, f): | |
147 continue | |
148 pre = post = open(f).read() | |
149 if "no-" + "check-code" in pre: | |
150 break | |
151 for p, r in filters: | |
152 post = re.sub(p, r, post) | |
153 # print post # uncomment to show filtered version | |
154 z = enumerate(zip(pre.splitlines(), post.splitlines(True))) | |
155 for n, l in z: | |
156 if "check-code" + "-ignore" in l[0]: | |
157 continue | |
158 lc = 0 | |
159 for p, msg in pats: | |
160 if re.search(p, l[1]): | |
161 if not lc: | |
162 print "%s:%d:" % (f, n + 1) | |
163 print " > %s" % l[0] | |
164 print " %s" % msg | |
165 lc += 1 | |
166 fc += 1 | |
167 if fc == 15: | |
168 print " (too many errors, giving up)" | |
169 break | |
170 break |