comparison contrib/check-code.py @ 21222:4840abc83970

check-code: look at shebang to identify Python scripts
author Matt Mackall <mpm@selenic.com>
date Mon, 05 May 2014 13:37:59 -0500
parents e8ef59b351c3
children c26464ce0781
comparison
equal deleted inserted replaced
21221:e3ca21e4d05f 21222:4840abc83970
366 # warnings 366 # warnings
367 [] 367 []
368 ] 368 ]
369 369
370 checks = [ 370 checks = [
371 ('python', r'.*\.(py|cgi)$', pyfilters, pypats), 371 ('python', r'.*\.(py|cgi)$', r'^#!.*python', pyfilters, pypats),
372 ('test script', r'(.*/)?test-[^.~]*$', testfilters, testpats), 372 ('test script', r'(.*/)?test-[^.~]*$', '', testfilters, testpats),
373 ('c', r'.*\.[ch]$', cfilters, cpats), 373 ('c', r'.*\.[ch]$', '', cfilters, cpats),
374 ('unified test', r'.*\.t$', utestfilters, utestpats), 374 ('unified test', r'.*\.t$', '', utestfilters, utestpats),
375 ('layering violation repo in revlog', r'mercurial/revlog\.py', pyfilters, 375 ('layering violation repo in revlog', r'mercurial/revlog\.py', '',
376 inrevlogpats), 376 pyfilters, inrevlogpats),
377 ('layering violation ui in util', r'mercurial/util\.py', pyfilters, 377 ('layering violation ui in util', r'mercurial/util\.py', '', pyfilters,
378 inutilpats), 378 inutilpats),
379 ('txt', r'.*\.txt$', txtfilters, txtpats), 379 ('txt', r'.*\.txt$', '', txtfilters, txtpats),
380 ] 380 ]
381 381
382 def _preparepats(): 382 def _preparepats():
383 for c in checks: 383 for c in checks:
384 failandwarn = c[-1] 384 failandwarn = c[-1]
390 p = re.sub(r'(?<!\\)\\s', r'[ \\t]', p) 390 p = re.sub(r'(?<!\\)\\s', r'[ \\t]', p)
391 # [^...] doesn't match newline 391 # [^...] doesn't match newline
392 p = re.sub(r'(?<!\\)\[\^', r'[^\\n', p) 392 p = re.sub(r'(?<!\\)\[\^', r'[^\\n', p)
393 393
394 pats[i] = (re.compile(p, re.MULTILINE),) + pseq[1:] 394 pats[i] = (re.compile(p, re.MULTILINE),) + pseq[1:]
395 filters = c[2] 395 filters = c[3]
396 for i, flt in enumerate(filters): 396 for i, flt in enumerate(filters):
397 filters[i] = re.compile(flt[0]), flt[1] 397 filters[i] = re.compile(flt[0]), flt[1]
398 _preparepats() 398 _preparepats()
399 399
400 class norepeatlogger(object): 400 class norepeatlogger(object):
444 444
445 return True if no error is found, False otherwise. 445 return True if no error is found, False otherwise.
446 """ 446 """
447 blamecache = None 447 blamecache = None
448 result = True 448 result = True
449 for name, match, filters, pats in checks: 449
450 try:
451 fp = open(f)
452 except IOError, e:
453 print "Skipping %s, %s" % (f, str(e).split(':', 1)[0])
454 return result
455 pre = post = fp.read()
456 fp.close()
457
458 for name, match, magic, filters, pats in checks:
450 if debug: 459 if debug:
451 print name, f 460 print name, f
452 fc = 0 461 fc = 0
453 if not re.match(match, f): 462 if not (re.match(match, f) or (magic and re.search(magic, f))):
454 if debug: 463 if debug:
455 print "Skipping %s for %s it doesn't match %s" % ( 464 print "Skipping %s for %s it doesn't match %s" % (
456 name, match, f) 465 name, match, f)
457 continue 466 continue
458 try:
459 fp = open(f)
460 except IOError, e:
461 print "Skipping %s, %s" % (f, str(e).split(':', 1)[0])
462 continue
463 pre = post = fp.read()
464 fp.close()
465 if "no-" "check-code" in pre: 467 if "no-" "check-code" in pre:
466 print "Skipping %s it has no-" "check-code" % f 468 print "Skipping %s it has no-" "check-code" % f
467 return "Skip" # skip checking this file 469 return "Skip" # skip checking this file
468 for p, r in filters: 470 for p, r in filters:
469 post = re.sub(p, r, post) 471 post = re.sub(p, r, post)