Changeset 173

Show
Ignore:
Timestamp:
02/04/07 13:17:30 (6 years ago)
Author:
mk
Message:

Stop pylint when it exceeds 60 seconds of running time (closes ticket #48).

Files:

Legend:

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

    r172 r173  
    432432        """Iterate over each subindex and yield their values. 
    433433        """ 
    434         for index in self.subindices: 
    435             # Pass Cheesecake instance to other indices. 
    436             yield index.compute_with(self.cheesecake) 
    437             # Print index info after computing. 
    438             if not self.cheesecake.quiet: 
    439                 index.print_info() 
     434        for index in self.subindices[:]: 
     435            try: 
     436                # Pass Cheesecake instance to a subindex. 
     437                yield index.compute_with(self.cheesecake) 
     438                # Print index info after computing. 
     439                if not self.cheesecake.quiet: 
     440                    index.print_info() 
     441            except: 
     442                # When exception is thrown, silence it 
     443                #   and remove this subindex from the list. 
     444                self.subindices.remove(index) 
    440445 
    441446    def compute_with(self, cheesecake): 
     
    10491054    pylint_args = ' '.join(map(lambda x: '--disable-msg=%s' % x, disabled_messages)) 
    10501055 
     1056    max_execution_time = 60 
     1057 
    10511058    def compute(self, files_list, package_dir): 
    10521059        # See if pylint script location is set via environment variable 
     
    10801087            self.cheesecake.log.debug("Running pylint on files: %s." % filenames) 
    10811088 
    1082             rc, output = run_cmd("%s %s --persistent=n %s" % (pylint_location, filenames, self.pylint_args)) 
     1089            # Run pylint, but don't allow it to work longer than one minute. 
     1090            rc, output = run_cmd("%s %s --persistent=n %s" % (pylint_location, filenames, self.pylint_args), 
     1091                                 max_timeout=self.max_execution_time) 
    10831092            if rc: 
     1093                if output == 'Time exceeded': 
     1094                    # Raise and exception what will cause PyLint to be removed from list of indices 
     1095                    #   and thus won't affect the score. 
     1096                    self.cheesecake.log.debug("pylint exceeded maximum execution time of %d seconds and was terminated." % \ 
     1097                                              self.max_execution_time) 
     1098                    raise OSError 
    10841099                self.cheesecake.log.debug("encountered an error (%d):\n***\n%s\n***\n" % (rc, output)) 
    10851100                error_count += 1 
     
    17351750        specific indexes. 
    17361751        """ 
    1737         # Recursively compute all indices. 
    1738         max_cheesecake_index = self.index.max_value 
    1739  
    17401752        # Pass Cheesecake instance to the main Index object. 
    17411753        cheesecake_index = self.index.compute_with(self) 
     1754 
     1755        # Get max value *after* computing indices, because computing 
     1756        #   process can remove some indices from the list. 
     1757        max_cheesecake_index = self.index.max_value 
     1758 
    17421759        percentage = (cheesecake_index * 100) / max_cheesecake_index 
    17431760 
  • trunk/cheesecake/util.py

    r150 r173  
    44import os 
    55import shutil 
     6import signal 
    67import sys 
    78import tarfile 
     
    1415PAD_VALUE = 4 
    1516 
    16 def run_cmd(cmd, env=None): 
     17def run_cmd(cmd, env=None, max_timeout=None): 
    1718    """Run command and return its return code and its output. 
    1819 
    1920    >>> run_cmd('/bin/true') 
    2021    (0, '') 
     22 
     23    >>> run_cmd('/bin/cat', max_timeout=0.2) 
     24    (1, 'Time exceeded') 
    2125    """ 
    2226    arglist = cmd.split() 
    2327    try: 
    2428        p = Popen(arglist, stdout=PIPE, stderr=STDOUT, env=env) 
    25     except ProcessError, e: 
     29    except Exception, e: 
    2630        return 1, e 
     31 
     32    # Wait only max_timeout seconds. 
     33    if max_timeout: 
     34        start = time.time() 
     35        while p.poll() is None: 
     36            time.sleep(0.1) 
     37            if time.time() - start > max_timeout: 
     38                os.kill(p.pid, signal.SIGINT) 
     39                p.wait() 
     40                return 1, "Time exceeded" 
     41 
    2742    output = p.communicate()[0] 
    2843    return p.returncode, output 
  • trunk/tests/unit/test_index_class.py

    r159 r173  
    33    >>> import _path_cheesecake 
    44    >>> from cheesecake.cheesecake_index import Index 
     5    >>> from _helper_cheesecake import Glutton 
    56 
    67***** 
     
    8485    >>> new.requirements 
    8586    ['one', 'two', 'three', 'four'] 
     87 
     88***** 
     89 
     90Index which throws an Exception during computation will 
     91get removed from the list of subindices. 
     92    >>> class BadIndex(Index): 
     93    ...     max_value = 10 
     94    ...     def compute(self): 
     95    ...         raise Exception("No reason.") 
     96    >>> bad_index = BadIndex() 
     97    >>> index = Index(bad_index) 
     98    >>> bad_index in index.subindices 
     99    True 
     100    >>> index.compute_with(Glutton()) 
     101    0 
     102    >>> bad_index in index.subindices 
     103    False 
     104    >>> index.max_value 
     105    0 
    86106"""