Changeset 102

Show
Ignore:
Timestamp:
07/23/06 08:28:34 (2 years ago)
Author:
mk
Message:

Run pylint once (as opposed to running it separate on each file) gaining major speed up.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/mk/cheesecake/cheesecake_index.py

    r101 r102  
    944944 
    945945class IndexPyLint(Index): 
    946     """Compute pylint index as average of positive pylint scores obtained for 
    947     the Python files identified in the package. 
     946    """Compute pylint index of the whole package. 
    948947    """ 
    949948    name = "pylint" 
     
    964963            return self.value 
    965964 
    966         pylint_value = 0 
    967         cnt = 0 
    968  
    969         # wbg: switching cwd so that pylint works correctly regarding 
    970         # running it on individual modules 
     965        # Exclude __init__.py files from score as they cause pylint 
     966        #     to fail with ImportError "Unable to find module for %s in %s". 
     967        files_to_lint = filter(lambda name: not name.endswith('__init__.py'), 
     968                               get_files_of_type(files_list, 'module')) 
     969 
     970        # Switching cwd so that pylint works correctly regarding 
     971        #     running it on individual modules. 
    971972        original_cwd = os.getcwd() 
    972         # change dir to top of the package tree 
    973         # package_dir may be a file if the archive contains a single file..
    974         # if this is the case, change dir to the parent dir of that file 
     973 
     974        # Note: package_dir may be a file if the archive contains a single file
     975        # If this is the case, change dir to the parent dir of that file. 
    975976        if os.path.isfile(package_dir): 
    976977            package_dir = os.path.dirname(package_dir) 
     978 
    977979        os.chdir(package_dir) 
    978980 
    979         for pyfile in get_files_of_type(files_list, 'module'): 
    980             # wbg: because we change the working dir, we can use relative 
    981             # paths which pylint is happier about. 
    982             # fullpath = os.path.join(package_dir, pyfile) 
    983             fullpath = pyfile 
    984             path, filename = os.path.split(fullpath) 
    985             module, ext = os.path.splitext(filename) 
    986  
    987             self.cheesecake.log.debug("Running pylint on file " + fullpath) 
    988             rc, output = run_cmd("pylint %s %s" % (self.pylint_args, fullpath)) 
    989             if rc: 
    990                 self.cheesecake.log.debug("encountered an error (%d)." % rc) 
    991                 continue 
    992  
    993             score_line = output.split("\n")[-3] 
    994             s = re.search(r" (\d+\.\d+)/10", score_line) 
    995  
    996             # We only take positive scores into account 
    997             if s: 
    998                 score = s.group(1) 
    999                 if score == "0.00": 
    1000                     self.cheesecake.log.debug("ignoring 0.00 score.") 
    1001                     continue 
    1002                 else: 
    1003                     self.cheesecake.log.debug("pylint score for module %s: %s" % (module, score)) 
    1004                 pylint_value += float(score) 
    1005                 cnt += 1 
    1006  
    1007         # wbg: switching back to the original cwd in case that's important 
     981        self.cheesecake.log.debug("Running pylint on files: %s." % ' '.join(files_to_lint)) 
     982 
     983        rc, output = run_cmd("pylint %s %s" % (' '.join(files_to_lint), self.pylint_args)) 
     984        if rc: 
     985            self.cheesecake.log.debug("encountered an error (%d):\n***\n%s\n***\n" % (rc, output)) 
     986 
     987        # Switching back to the original cwd. 
    1008988        os.chdir(original_cwd) 
    1009989 
    1010         avg_score = 0 
    1011         if cnt: 
    1012             avg_score = float(pylint_value)/float(cnt) 
    1013  
    1014         self.value = int(ceil(avg_score/10.0 * self.max_value)) 
    1015         self.details = "average pylint score is %.2f out of 10" % avg_score 
     990        # Extract score from pylint output. 
     991        score_line = output.split("\n")[-3] 
     992        s = re.search(r" (\d+\.\d+)/10", score_line) 
     993 
     994        # Scores below 0 are treated as 0. 
     995        pylint_score = 0 
     996        if s: 
     997            pylint_score = float(s.group(1)) 
     998 
     999        self.value = int(ceil(pylint_score/10.0 * self.max_value)) 
     1000        self.details = "pylint score was %.2f out of 10" % pylint_score 
    10161001 
    10171002        return self.value