Changeset 109

Show
Ignore:
Timestamp:
07/24/06 14:12:35 (2 years ago)
Author:
mk
Message:

Make pylint index work for long file lists.

Files:

Legend:

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

    r106 r109  
    307307 
    308308    return files, directories 
     309 
     310def length(L): 
     311    """Overall length of all strings in list. 
     312 
     313    >>> length(['a', 'bc', 'd', '', 'efg']) 
     314    7 
     315    """ 
     316    return sum(map(lambda x: len(x), L)) 
     317 
     318def generate_arguments(arguments, max_length): 
     319    """Pass list of strings in chunks of size not greater than max_length. 
     320 
     321    >>> for x in generate_arguments(['abc', 'def'], 4): 
     322    ...     print x 
     323    ['abc'] 
     324    ['def'] 
     325 
     326    >>> for x in generate_arguments(['a', 'bc', 'd', 'e', 'f'], 2): 
     327    ...     print x 
     328    ['a'] 
     329    ['bc'] 
     330    ['d', 'e'] 
     331    ['f'] 
     332 
     333    If a single argument is larger than max_length, ValueError is raised. 
     334        >>> L = [] 
     335        >>> for x in generate_arguments(['abc', 'de', 'fghijk', 'l'], 4): 
     336        ...     L.append(x) 
     337        Traceback (most recent call last): 
     338          ... 
     339        ValueError: Argument 'fghijk' larger than 4. 
     340        >>> L 
     341        [['abc'], ['de']] 
     342    """ 
     343    L = [] 
     344    i = 0 
     345 
     346    # We have to look ahead, so C-style loop here. 
     347    while arguments: 
     348        if L == [] and len(arguments[i]) > max_length: 
     349            raise ValueError("Argument '%s' larger than %d." % (arguments[i], max_length)) 
     350 
     351        L.append(arguments[i]) 
     352 
     353        # End of arguments: yield then terminate. 
     354        if i == len(arguments) - 1: 
     355            yield L 
     356            break 
     357 
     358        # Adding next argument would exceed max_length, so yield now. 
     359        if length(L) + len(arguments[i+1]) > max_length: 
     360            yield L 
     361            L = [] 
     362 
     363        i += 1 
    309364 
    310365################################################################################ 
     
    9571012 
    9581013    def compute(self, files_list, package_dir): 
     1014        # Maximum length of arguments (not very precise). 
     1015        max_arguments_length = 65536 
     1016 
    9591017        # Exclude __init__.py files from score as they cause pylint 
    9601018        #     to fail with ImportError "Unable to find module for %s in %s". 
     
    9731031        os.chdir(package_dir) 
    9741032 
    975         self.cheesecake.log.debug("Running pylint on files: %s." % ' '.join(files_to_lint)) 
    976  
    977         rc, output = run_cmd("pylint %s %s" % (' '.join(files_to_lint), self.pylint_args)) 
    978         if rc: 
    979             self.cheesecake.log.debug("encountered an error (%d):\n***\n%s\n***\n" % (rc, output)) 
     1033        pylint_score = 0 
     1034        count = 0 
     1035        error_count = 0 
     1036 
     1037        for filenames in generate_arguments(files_to_lint, max_arguments_length - len(self.pylint_args)): 
     1038            filenames =  ' '.join(filenames) 
     1039            self.cheesecake.log.debug("Running pylint on files: %s." % filenames) 
     1040 
     1041            rc, output = run_cmd("pylint %s %s" % (filenames, self.pylint_args)) 
     1042            if rc: 
     1043                self.cheesecake.log.debug("encountered an error (%d):\n***\n%s\n***\n" % (rc, output)) 
     1044                error_count += 1 
     1045            else: 
     1046                # Extract score from pylint output. 
     1047                score_line = output.split("\n")[-3] 
     1048                s = re.search(r" (-?\d+\.\d+)/10", score_line) 
     1049                if s: 
     1050                    pylint_score += float(s.group(1)) 
     1051                    count += 1 
    9801052 
    9811053        # Switching back to the original cwd. 
    9821054        os.chdir(original_cwd) 
    9831055 
    984         # Extract score from pylint output. 
    985         score_line = output.split("\n")[-3] 
    986         s = re.search(r" (\d+\.\d+)/10", score_line) 
    987  
    988         # Scores below 0 are treated as 0. 
    989         pylint_score = 0 
    990         if s: 
    991             pylint_score = float(s.group(1)) 
    992  
     1056        if count: 
     1057            pylint_score = float(pylint_score)/float(count) 
     1058            self.details = "pylint score was %.2f out of 10" % pylint_score 
     1059        elif error_count: 
     1060            self.details = "encountered an error during pylint execution" 
     1061        else: 
     1062            self.details = "no files to check found" 
     1063 
     1064        # Assume scores below zero as zero for means of index value computation. 
     1065        if pylint_score < 0: 
     1066            pylint_score = 0 
    9931067        self.value = int(ceil(pylint_score/10.0 * self.max_value)) 
    994         self.details = "pylint score was %.2f out of 10" % pylint_score 
    9951068 
    9961069        return self.value 
  • branches/mk/tests/unit/test_index_pylint.py

    r71 r109  
     1import os 
     2import shutil 
     3import tempfile 
    14 
    25import _path_cheesecake 
    3 from _helper_cheesecake import DATA_PATH, Glutton 
     6from _helper_cheesecake import DATA_PATH, Glutton, create_empty_file 
    47from cheesecake.cheesecake_index import IndexPyLint 
     8from cheesecake import logger 
    59 
    610 
     
    1923        print index.value 
    2024        assert index.value == index.max_value 
     25 
     26    def test_long_file_list(self): 
     27        "Test if pylint index works for long files lists." 
     28        _package_dir = tempfile.mkdtemp() 
     29        _files_list = map(lambda x: 'long_module_with_a_number_%d.py' % x, xrange(4000)) 
     30 
     31        for filename in _files_list: 
     32            create_empty_file(os.path.join(_package_dir, filename)) 
     33 
     34        logger.setconsumer('console', logger.STDOUT) 
     35        console_log = logger.MultipleProducer('cheesecake console') 
     36 
     37        class CheesecakeMockup(object): 
     38            package_dir = _package_dir 
     39            files_list = _files_list 
     40            log = console_log 
     41 
     42        index = IndexPyLint() 
     43        cheesecake = CheesecakeMockup() 
     44 
     45        index.compute_with(cheesecake) 
     46        assert index.details != "encountered an error during pylint execution" 
     47 
     48        # Clean up. 
     49        shutil.rmtree(_package_dir)