Mercurial > hg
comparison contrib/check-code.py @ 14135:673abd432104
check-code: adding debug flag
author | timeless <timeless@mozdev.org> |
---|---|
date | Sun, 01 May 2011 15:55:00 +0200 |
parents | 64de9ca66511 |
children | eebf196a8bbe |
comparison
equal
deleted
inserted
replaced
14134:8468ec1109d1 | 14135:673abd432104 |
---|---|
249 user, rev = start.split() | 249 user, rev = start.split() |
250 lines.append((line[1:-1], user, rev)) | 250 lines.append((line[1:-1], user, rev)) |
251 return lines | 251 return lines |
252 | 252 |
253 def checkfile(f, logfunc=_defaultlogger.log, maxerr=None, warnings=False, | 253 def checkfile(f, logfunc=_defaultlogger.log, maxerr=None, warnings=False, |
254 blame=False): | 254 blame=False, debug=False): |
255 """checks style and portability of a given file | 255 """checks style and portability of a given file |
256 | 256 |
257 :f: filepath | 257 :f: filepath |
258 :logfunc: function used to report error | 258 :logfunc: function used to report error |
259 logfunc(filename, linenumber, linecontent, errormessage) | 259 logfunc(filename, linenumber, linecontent, errormessage) |
263 return True if no error is found, False otherwise. | 263 return True if no error is found, False otherwise. |
264 """ | 264 """ |
265 blamecache = None | 265 blamecache = None |
266 result = True | 266 result = True |
267 for name, match, filters, pats in checks: | 267 for name, match, filters, pats in checks: |
268 if debug: | |
269 print name, f | |
268 fc = 0 | 270 fc = 0 |
269 if not re.match(match, f): | 271 if not re.match(match, f): |
272 if debug: | |
273 print "Skipping %s for %s it doesn't match %s" % ( | |
274 name, match, f) | |
270 continue | 275 continue |
271 fp = open(f) | 276 fp = open(f) |
272 pre = post = fp.read() | 277 pre = post = fp.read() |
273 fp.close() | 278 fp.close() |
274 if "no-" + "check-code" in pre: | 279 if "no-" + "check-code" in pre: |
280 if debug: | |
281 print "Skipping %s for %s it has no- and check-code" % ( | |
282 name, f) | |
275 break | 283 break |
276 for p, r in filters: | 284 for p, r in filters: |
277 post = re.sub(p, r, post) | 285 post = re.sub(p, r, post) |
278 if warnings: | 286 if warnings: |
279 pats = pats[0] + pats[1] | 287 pats = pats[0] + pats[1] |
280 else: | 288 else: |
281 pats = pats[0] | 289 pats = pats[0] |
282 # print post # uncomment to show filtered version | 290 # print post # uncomment to show filtered version |
283 z = enumerate(zip(pre.splitlines(), post.splitlines(True))) | 291 z = enumerate(zip(pre.splitlines(), post.splitlines(True))) |
292 if debug: | |
293 print "Checking %s for %s" % (name, f) | |
284 for n, l in z: | 294 for n, l in z: |
285 if "check-code" + "-ignore" in l[0]: | 295 if "check-code" + "-ignore" in l[0]: |
296 if debug: | |
297 print "Skipping %s for %s:%s (check-code -ignore)" % ( | |
298 name, f, n) | |
286 continue | 299 continue |
287 for p, msg in pats: | 300 for p, msg in pats: |
288 if re.search(p, l[1]): | 301 if re.search(p, l[1]): |
289 bd = "" | 302 bd = "" |
290 if blame: | 303 if blame: |
310 help="include warning-level checks") | 323 help="include warning-level checks") |
311 parser.add_option("-p", "--per-file", type="int", | 324 parser.add_option("-p", "--per-file", type="int", |
312 help="max warnings per file") | 325 help="max warnings per file") |
313 parser.add_option("-b", "--blame", action="store_true", | 326 parser.add_option("-b", "--blame", action="store_true", |
314 help="use annotate to generate blame info") | 327 help="use annotate to generate blame info") |
315 | 328 parser.add_option("", "--debug", action="store_true", |
316 parser.set_defaults(per_file=15, warnings=False, blame=False) | 329 help="show debug information") |
330 | |
331 parser.set_defaults(per_file=15, warnings=False, blame=False, debug=False) | |
317 (options, args) = parser.parse_args() | 332 (options, args) = parser.parse_args() |
318 | 333 |
319 if len(args) == 0: | 334 if len(args) == 0: |
320 check = glob.glob("*") | 335 check = glob.glob("*") |
321 else: | 336 else: |
322 check = args | 337 check = args |
323 | 338 |
324 for f in check: | 339 for f in check: |
325 ret = 0 | 340 ret = 0 |
326 if not checkfile(f, maxerr=options.per_file, warnings=options.warnings, | 341 if not checkfile(f, maxerr=options.per_file, warnings=options.warnings, |
327 blame=options.blame): | 342 blame=options.blame, debug=options.debug): |
328 ret = 1 | 343 ret = 1 |
329 sys.exit(ret) | 344 sys.exit(ret) |