comparison contrib/check-code.py @ 41828:7a139fc60eb0

contrib: factor out actual error check for file data of check-code.py This is a part of preparation to apply checking with check-code.py on code fragments embedded in *.t test scripts. Newly added _checkfiledata() will be useful to apply checks on code fragments embedded in *.t test scripts.
author FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
date Fri, 01 Mar 2019 02:53:09 +0900
parents 55ae5cd31f76
children 519b2faea261
comparison
equal deleted inserted replaced
41827:55ae5cd31f76 41828:7a139fc60eb0
633 :maxerr: number of error to display before aborting. 633 :maxerr: number of error to display before aborting.
634 Set to false (default) to report all errors 634 Set to false (default) to report all errors
635 635
636 return True if no error is found, False otherwise. 636 return True if no error is found, False otherwise.
637 """ 637 """
638 blamecache = None
639 result = True 638 result = True
640 639
641 try: 640 try:
642 with opentext(f) as fp: 641 with opentext(f) as fp:
643 try: 642 try:
647 return result 646 return result
648 except IOError as e: 647 except IOError as e:
649 print("Skipping %s, %s" % (f, str(e).split(':', 1)[0])) 648 print("Skipping %s, %s" % (f, str(e).split(':', 1)[0]))
650 return result 649 return result
651 650
651 # context information shared while single checkfile() invocation
652 context = {'blamecache': None}
653
652 for name, match, magic, filters, pats in checks: 654 for name, match, magic, filters, pats in checks:
653 post = pre # discard filtering result of previous check
654 if debug: 655 if debug:
655 print(name, f) 656 print(name, f)
656 fc = 0
657 if not (re.match(match, f) or (magic and re.search(magic, pre))): 657 if not (re.match(match, f) or (magic and re.search(magic, pre))):
658 if debug: 658 if debug:
659 print("Skipping %s for %s it doesn't match %s" % ( 659 print("Skipping %s for %s it doesn't match %s" % (
660 name, match, f)) 660 name, match, f))
661 continue 661 continue
666 # tests easier. So, instead of writing it with a normal 666 # tests easier. So, instead of writing it with a normal
667 # spelling, we write it with the expected spelling from 667 # spelling, we write it with the expected spelling from
668 # tests/test-check-code.t 668 # tests/test-check-code.t
669 print("Skipping %s it has no-che?k-code (glob)" % f) 669 print("Skipping %s it has no-che?k-code (glob)" % f)
670 return "Skip" # skip checking this file 670 return "Skip" # skip checking this file
671
672 if not _checkfiledata(name, f, pre, filters, pats, context,
673 logfunc, maxerr, warnings, blame, debug, lineno):
674 result = False
675
676 return result
677
678 def _checkfiledata(name, f, filedata, filters, pats, context,
679 logfunc, maxerr, warnings, blame, debug, lineno):
680 """Execute actual error check for file data
681
682 :name: of the checking category
683 :f: filepath
684 :filedata: content of a file
685 :filters: to be applied before checking
686 :pats: to detect errors
687 :context: a dict of information shared while single checkfile() invocation
688 Valid keys: 'blamecache'.
689 :logfunc: function used to report error
690 logfunc(filename, linenumber, linecontent, errormessage)
691 :maxerr: number of error to display before aborting, or False to
692 report all errors
693 :warnings: whether warning level checks should be applied
694 :blame: whether blame information should be displayed at error reporting
695 :debug: whether debug information should be displayed
696 :lineno: whether lineno should be displayed at error reporting
697
698 return True if no error is found, False otherwise.
699 """
700 blamecache = context['blamecache']
701
702 fc = 0
703 pre = post = filedata
704 result = True
705
706 if True: # TODO: get rid of this redundant 'if' block
671 for p, r in filters: 707 for p, r in filters:
672 post = re.sub(p, r, post) 708 post = re.sub(p, r, post)
673 nerrs = len(pats[0]) # nerr elements are errors 709 nerrs = len(pats[0]) # nerr elements are errors
674 if warnings: 710 if warnings:
675 pats = pats[0] + pats[1] 711 pats = pats[0] + pats[1]
713 name, f, n)) 749 name, f, n))
714 continue 750 continue
715 bd = "" 751 bd = ""
716 if blame: 752 if blame:
717 bd = 'working directory' 753 bd = 'working directory'
718 if not blamecache: 754 if blamecache is None:
719 blamecache = getblame(f) 755 blamecache = getblame(f)
756 context['blamecache'] = blamecache
720 if n < len(blamecache): 757 if n < len(blamecache):
721 bl, bu, br = blamecache[n] 758 bl, bu, br = blamecache[n]
722 if bl == l: 759 if bl == l:
723 bd = '%s@%s' % (bu, br) 760 bd = '%s@%s' % (bu, br)
724 761