<s> r"""<STR_LIT>""" <EOL> __version__ = "<STR_LIT>" <EOL> import compiler <EOL> import compiler . visitor <EOL> import glob <EOL> import os <EOL> import re <EOL> import string <EOL> import symbol <EOL> import sys <EOL> import threading <EOL> import token <EOL> import types <EOL> import email <EOL> from socket import gethostname <EOL> try : <EOL> strclass = basestring <EOL> except : <EOL> strclass = str <EOL> class StatementFindingAstVisitor ( compiler . visitor . ASTVisitor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , statements , excluded , suite_spots ) : <EOL> compiler . visitor . ASTVisitor . __init__ ( self ) <EOL> self . statements = statements <EOL> self . excluded = excluded <EOL> self . suite_spots = suite_spots <EOL> self . excluding_suite = <NUM_LIT:0> <EOL> def doRecursive ( self , node ) : <EOL> for n in node . getChildNodes ( ) : <EOL> self . dispatch ( n ) <EOL> visitStmt = visitModule = doRecursive <EOL> def doCode ( self , node ) : <EOL> if hasattr ( node , '<STR_LIT>' ) and node . decorators : <EOL> self . dispatch ( node . decorators ) <EOL> self . recordAndDispatch ( node . code ) <EOL> else : <EOL> self . doSuite ( node , node . code ) <EOL> visitFunction = visitClass = doCode <EOL> def getFirstLine ( self , node ) : <EOL> lineno = node . lineno <EOL> for n in node . getChildNodes ( ) : <EOL> f = self . getFirstLine ( n ) <EOL> if lineno and f : <EOL> lineno = min ( lineno , f ) <EOL> else : <EOL> lineno = lineno or f <EOL> return lineno <EOL> def getLastLine ( self , node ) : <EOL> lineno = node . lineno <EOL> for n in node . getChildNodes ( ) : <EOL> lineno = max ( lineno , self . getLastLine ( n ) ) <EOL> return lineno <EOL> def doStatement ( self , node ) : <EOL> self . recordLine ( self . getFirstLine ( node ) ) <EOL> visitAssert = visitAssign = visitAssTuple = visitPrint = visitPrintnl = visitRaise = visitSubscript = visitDecorators = doStatement <EOL> def visitPass ( self , node ) : <EOL> l = node . lineno <EOL> if l : <EOL> lines = self . suite_spots . get ( l , [ l , l ] ) <EOL> self . statements [ lines [ <NUM_LIT:1> ] ] = <NUM_LIT:1> <EOL> def visitDiscard ( self , node ) : <EOL> if node . expr . __class__ . __name__ != '<STR_LIT>' : <EOL> self . doStatement ( node ) <EOL> def recordNodeLine ( self , node ) : <EOL> if node . __class__ . __name__ != '<STR_LIT>' : <EOL> return self . recordLine ( self . getFirstLine ( node ) ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> def recordLine ( self , lineno ) : <EOL> if lineno : <EOL> if lineno in self . suite_spots : <EOL> lineno = self . suite_spots [ lineno ] [ <NUM_LIT:0> ] <EOL> if self . excluding_suite : <EOL> self . excluded [ lineno ] = <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> elif self . excluded . has_key ( lineno ) or self . suite_spots . has_key ( lineno ) and self . excluded . has_key ( self . suite_spots [ lineno ] [ <NUM_LIT:1> ] ) : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> self . statements [ lineno ] = <NUM_LIT:1> <EOL> return <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> default = recordNodeLine <EOL> def recordAndDispatch ( self , node ) : <EOL> self . recordNodeLine ( node ) <EOL> self . dispatch ( node ) <EOL> def doSuite ( self , intro , body , exclude = <NUM_LIT:0> ) : <EOL> exsuite = self . excluding_suite <EOL> if exclude or ( intro and not self . recordNodeLine ( intro ) ) : <EOL> self . excluding_suite = <NUM_LIT:1> <EOL> self . recordAndDispatch ( body ) <EOL> self . excluding_suite = exsuite <EOL> def doPlainWordSuite ( self , prevsuite , suite ) : <EOL> lastprev = self . getLastLine ( prevsuite ) <EOL> firstelse = self . getFirstLine ( suite ) <EOL> for l in range ( lastprev + <NUM_LIT:1> , firstelse ) : <EOL> if self . suite_spots . has_key ( l ) : <EOL> self . doSuite ( None , suite , exclude = self . excluded . has_key ( l ) ) <EOL> break <EOL> else : <EOL> self . doSuite ( None , suite ) <EOL> def doElse ( self , prevsuite , node ) : <EOL> if node . else_ : <EOL> self . doPlainWordSuite ( prevsuite , node . else_ ) <EOL> def visitFor ( self , node ) : <EOL> self . doSuite ( node , node . body ) <EOL> self . doElse ( node . body , node ) <EOL> visitWhile = visitFor <EOL> def visitIf ( self , node ) : <EOL> self . doSuite ( node , node . tests [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) <EOL> for t , n in node . tests [ <NUM_LIT:1> : ] : <EOL> self . doSuite ( t , n ) <EOL> self . doElse ( node . tests [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] , node ) <EOL> def visitTryExcept ( self , node ) : <EOL> self . doSuite ( node , node . body ) <EOL> for i in range ( len ( node . handlers ) ) : <EOL> a , b , h = node . handlers [ i ] <EOL> if not a : <EOL> if i > <NUM_LIT:0> : <EOL> prev = node . handlers [ i - <NUM_LIT:1> ] [ <NUM_LIT:2> ] <EOL> else : <EOL> prev = node . body <EOL> self . doPlainWordSuite ( prev , h ) <EOL> else : <EOL> self . doSuite ( a , h ) <EOL> self . doElse ( node . handlers [ - <NUM_LIT:1> ] [ <NUM_LIT:2> ] , node ) <EOL> def visitTryFinally ( self , node ) : <EOL> self . doSuite ( node , node . body ) <EOL> self . doPlainWordSuite ( node . body , node . final ) <EOL> def visitWith ( self , node ) : <EOL> self . doSuite ( node , node . body ) <EOL> def visitGlobal ( self , node ) : <EOL> pass <EOL> def getCoverageLoc ( ) : <EOL> global cache_location <EOL> return cache_location <EOL> class CoverageException ( Exception ) : pass <EOL> class coverage : <EOL> cache_default = "<STR_LIT>" <EOL> cache_env = "<STR_LIT>" <EOL> c = { } <EOL> cexecuted = { } <EOL> analysis_cache = { } <EOL> canonical_filename_cache = { } <EOL> def __init__ ( self ) : <EOL> self . usecache = <NUM_LIT:1> <EOL> self . cache = None <EOL> self . parallel_mode = False <EOL> self . exclude_re = '<STR_LIT>' <EOL> self . nesting = <NUM_LIT:0> <EOL> self . cstack = [ ] <EOL> self . xstack = [ ] <EOL> self . relative_dir = os . path . normcase ( os . path . abspath ( os . curdir ) + os . sep ) <EOL> self . exclude ( '<STR_LIT>' ) <EOL> def t ( self , f , w , unused ) : <EOL> if w == '<STR_LIT>' : <EOL> self . c [ ( f . f_code . co_filename , f . f_lineno ) ] = <NUM_LIT:1> <EOL> for c in self . cstack : <EOL> c [ ( f . f_code . co_filename , f . f_lineno ) ] = <NUM_LIT:1> <EOL> return self . t <EOL> def help ( self , error = None ) : <EOL> if error : <EOL> print error <EOL> print <EOL> print __doc__ <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> def command_line ( self , argv , help_fn = None ) : <EOL> import getopt <EOL> help_fn = help_fn or self . help <EOL> settings = { } <EOL> optmap = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:-c>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> short_opts = string . join ( map ( lambda o : o [ <NUM_LIT:1> : ] , optmap . keys ( ) ) , '<STR_LIT>' ) <EOL> long_opts = optmap . values ( ) <EOL> options , args = getopt . getopt ( argv , short_opts , long_opts ) <EOL> for o , a in options : <EOL> if optmap . has_key ( o ) : <EOL> settings [ optmap [ o ] ] = <NUM_LIT:1> <EOL> elif optmap . has_key ( o + '<STR_LIT::>' ) : <EOL> settings [ optmap [ o + '<STR_LIT::>' ] ] = a <EOL> elif o [ <NUM_LIT:2> : ] in long_opts : <EOL> settings [ o [ <NUM_LIT:2> : ] ] = <NUM_LIT:1> <EOL> elif o [ <NUM_LIT:2> : ] + '<STR_LIT:=>' in long_opts : <EOL> settings [ o [ <NUM_LIT:2> : ] + '<STR_LIT:=>' ] = a <EOL> else : <EOL> pass <EOL> if settings . get ( '<STR_LIT>' ) : <EOL> help_fn ( ) <EOL> for i in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> for j in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if settings . get ( i ) and settings . get ( j ) : <EOL> help_fn ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( i , j ) ) <EOL> args_needed = ( settings . get ( '<STR_LIT>' ) <EOL> or settings . get ( '<STR_LIT>' ) <EOL> or settings . get ( '<STR_LIT>' ) ) <EOL> action = ( settings . get ( '<STR_LIT>' ) <EOL> or settings . get ( '<STR_LIT>' ) <EOL> or args_needed ) <EOL> if not action : <EOL> help_fn ( "<STR_LIT>" ) <EOL> if not args_needed and args : <EOL> help_fn ( "<STR_LIT>" % "<STR_LIT:U+0020>" . join ( args ) ) <EOL> self . parallel_mode = settings . get ( '<STR_LIT>' ) <EOL> self . get_ready ( ) <EOL> if settings . get ( '<STR_LIT>' ) : <EOL> self . erase ( ) <EOL> if settings . get ( '<STR_LIT>' ) : <EOL> if not args : <EOL> help_fn ( "<STR_LIT>" ) <EOL> sys . argv = args <EOL> self . start ( ) <EOL> import __main__ <EOL> sys . path [ <NUM_LIT:0> ] = os . path . dirname ( sys . argv [ <NUM_LIT:0> ] ) <EOL> execfile ( sys . argv [ <NUM_LIT:0> ] , __main__ . __dict__ ) <EOL> if settings . get ( '<STR_LIT>' ) : <EOL> self . collect ( ) <EOL> if not args : <EOL> args = self . cexecuted . keys ( ) <EOL> ignore_errors = settings . get ( '<STR_LIT>' ) <EOL> show_missing = settings . get ( '<STR_LIT>' ) <EOL> directory = settings . get ( '<STR_LIT>' ) <EOL> omit = settings . get ( '<STR_LIT>' ) <EOL> if omit is not None : <EOL> omit = omit . split ( '<STR_LIT:U+002C>' ) <EOL> else : <EOL> omit = [ ] <EOL> if settings . get ( '<STR_LIT>' ) : <EOL> self . report ( args , show_missing , ignore_errors , omit_prefixes = omit ) <EOL> if settings . get ( '<STR_LIT>' ) : <EOL> self . annotate ( args , directory , ignore_errors , omit_prefixes = omit ) <EOL> def use_cache ( self , usecache , cache_file = None ) : <EOL> self . usecache = usecache <EOL> if cache_file and not self . cache : <EOL> self . cache_default = cache_file <EOL> def get_ready ( self , parallel_mode = False ) : <EOL> if self . usecache and not self . cache : <EOL> self . cache = getCoverageLoc ( ) <EOL> if self . parallel_mode : <EOL> self . cache += "<STR_LIT:.>" + gethostname ( ) + "<STR_LIT:.>" + str ( os . getpid ( ) ) <EOL> self . restore ( ) <EOL> self . analysis_cache = { } <EOL> def start ( self , parallel_mode = False ) : <EOL> self . get_ready ( ) <EOL> if self . nesting == <NUM_LIT:0> : <EOL> sys . settrace ( self . t ) <EOL> if hasattr ( threading , '<STR_LIT>' ) : <EOL> threading . settrace ( self . t ) <EOL> self . nesting += <NUM_LIT:1> <EOL> def stop ( self ) : <EOL> self . nesting -= <NUM_LIT:1> <EOL> if self . nesting == <NUM_LIT:0> : <EOL> sys . settrace ( None ) <EOL> if hasattr ( threading , '<STR_LIT>' ) : <EOL> threading . settrace ( None ) <EOL> def erase ( self ) : <EOL> self . get_ready ( ) <EOL> self . c = { } <EOL> self . analysis_cache = { } <EOL> self . cexecuted = { } <EOL> if self . cache and os . path . exists ( self . cache ) : <EOL> os . remove ( self . cache ) <EOL> def exclude ( self , re ) : <EOL> if self . exclude_re : <EOL> self . exclude_re += "<STR_LIT:|>" <EOL> self . exclude_re += "<STR_LIT:(>" + re + "<STR_LIT:)>" <EOL> def begin_recursive ( self ) : <EOL> self . cstack . append ( self . c ) <EOL> self . xstack . append ( self . exclude_re ) <EOL> def end_recursive ( self ) : <EOL> self . c = self . cstack . pop ( ) <EOL> self . exclude_re = self . xstack . pop ( ) <EOL> def save ( self ) : <EOL> if self . usecache and self . cache : <EOL> self . canonicalize_filenames ( ) <EOL> cache = open ( self . cache , '<STR_LIT:wb>' ) <EOL> import marshal <EOL> marshal . dump ( self . cexecuted , cache ) <EOL> cache . close ( ) <EOL> def restore ( self ) : <EOL> self . c = { } <EOL> self . cexecuted = { } <EOL> assert self . usecache <EOL> if os . path . exists ( self . cache ) : <EOL> self . cexecuted = self . restore_file ( self . cache ) <EOL> def restore_file ( self , file_name ) : <EOL> try : <EOL> cache = open ( file_name , '<STR_LIT:rb>' ) <EOL> import marshal <EOL> cexecuted = marshal . load ( cache ) <EOL> cache . close ( ) <EOL> if isinstance ( cexecuted , types . DictType ) : <EOL> return cexecuted <EOL> else : <EOL> return { } <EOL> except : <EOL> return { } <EOL> def collect ( self ) : <EOL> cache_dir , local = os . path . split ( self . cache ) <EOL> for f in os . listdir ( cache_dir or '<STR_LIT:.>' ) : <EOL> if not f . startswith ( local ) : <EOL> continue <EOL> full_path = os . path . join ( cache_dir , f ) <EOL> cexecuted = self . restore_file ( full_path ) <EOL> self . merge_data ( cexecuted ) <EOL> def merge_data ( self , new_data ) : <EOL> for file_name , file_data in new_data . items ( ) : <EOL> if self . cexecuted . has_key ( file_name ) : <EOL> self . merge_file_data ( self . cexecuted [ file_name ] , file_data ) <EOL> else : <EOL> self . cexecuted [ file_name ] = file_data <EOL> def merge_file_data ( self , cache_data , new_data ) : <EOL> for line_number in new_data . keys ( ) : <EOL> if not cache_data . has_key ( line_number ) : <EOL> cache_data [ line_number ] = new_data [ line_number ] <EOL> def canonical_filename ( self , filename ) : <EOL> if not self . canonical_filename_cache . has_key ( filename ) : <EOL> f = filename <EOL> if os . path . isabs ( f ) and not os . path . exists ( f ) : <EOL> f = os . path . basename ( f ) <EOL> if not os . path . isabs ( f ) : <EOL> for path in [ os . curdir ] + sys . path : <EOL> g = os . path . join ( path , f ) <EOL> if os . path . exists ( g ) : <EOL> f = g <EOL> break <EOL> cf = os . path . normcase ( os . path . abspath ( f ) ) <EOL> self . canonical_filename_cache [ filename ] = cf <EOL> return self . canonical_filename_cache [ filename ] <EOL> def canonicalize_filenames ( self ) : <EOL> for filename , lineno in self . c . keys ( ) : <EOL> if filename == '<STR_LIT>' : <EOL> continue <EOL> f = self . canonical_filename ( filename ) <EOL> if not self . cexecuted . has_key ( f ) : <EOL> self . cexecuted [ f ] = { } <EOL> self . cexecuted [ f ] [ lineno ] = <NUM_LIT:1> <EOL> self . c = { } <EOL> def morf_filename ( self , morf ) : <EOL> if isinstance ( morf , types . ModuleType ) or ( hasattr ( email , '<STR_LIT>' ) and isinstance ( morf , email . LazyImporter ) ) : <EOL> if not hasattr ( morf , '<STR_LIT>' ) : <EOL> raise CoverageException ( "<STR_LIT>" ) <EOL> f = morf . __file__ <EOL> else : <EOL> f = morf <EOL> return self . canonical_filename ( f ) <EOL> def analyze_morf ( self , morf ) : <EOL> if self . analysis_cache . has_key ( morf ) : <EOL> return self . analysis_cache [ morf ] <EOL> filename = self . morf_filename ( morf ) <EOL> ext = os . path . splitext ( filename ) [ <NUM_LIT:1> ] <EOL> if ext == '<STR_LIT>' : <EOL> if not os . path . exists ( filename [ : - <NUM_LIT:1> ] ) : <EOL> raise CoverageException ( <EOL> "<STR_LIT>" % filename <EOL> ) <EOL> filename = filename [ : - <NUM_LIT:1> ] <EOL> source = open ( filename , '<STR_LIT:r>' ) <EOL> try : <EOL> lines , excluded_lines , line_map = self . find_executable_statements ( <EOL> source . read ( ) , exclude = self . exclude_re <EOL> ) <EOL> except SyntaxError , synerr : <EOL> raise CoverageException ( <EOL> "<STR_LIT>" % <EOL> ( filename , synerr . msg , synerr . lineno ) <EOL> ) <EOL> source . close ( ) <EOL> result = filename , lines , excluded_lines , line_map <EOL> self . analysis_cache [ morf ] = result <EOL> return result <EOL> def first_line_of_tree ( self , tree ) : <EOL> while True : <EOL> if len ( tree ) == <NUM_LIT:3> and type ( tree [ <NUM_LIT:2> ] ) == type ( <NUM_LIT:1> ) : <EOL> return tree [ <NUM_LIT:2> ] <EOL> tree = tree [ <NUM_LIT:1> ] <EOL> def last_line_of_tree ( self , tree ) : <EOL> while True : <EOL> if len ( tree ) == <NUM_LIT:3> and type ( tree [ <NUM_LIT:2> ] ) == type ( <NUM_LIT:1> ) : <EOL> return tree [ <NUM_LIT:2> ] <EOL> tree = tree [ - <NUM_LIT:1> ] <EOL> def find_docstring_pass_pair ( self , tree , spots ) : <EOL> for i in range ( <NUM_LIT:1> , len ( tree ) ) : <EOL> if self . is_string_constant ( tree [ i ] ) and self . is_pass_stmt ( tree [ i + <NUM_LIT:1> ] ) : <EOL> first_line = self . first_line_of_tree ( tree [ i ] ) <EOL> last_line = self . last_line_of_tree ( tree [ i + <NUM_LIT:1> ] ) <EOL> self . record_multiline ( spots , first_line , last_line ) <EOL> def is_string_constant ( self , tree ) : <EOL> try : <EOL> return tree [ <NUM_LIT:0> ] == symbol . stmt and tree [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] == symbol . expr_stmt <EOL> except : <EOL> return False <EOL> def is_pass_stmt ( self , tree ) : <EOL> try : <EOL> return tree [ <NUM_LIT:0> ] == symbol . stmt and tree [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] == symbol . pass_stmt <EOL> except : <EOL> return False <EOL> def record_multiline ( self , spots , i , j ) : <EOL> for l in range ( i , j + <NUM_LIT:1> ) : <EOL> spots [ l ] = ( i , j ) <EOL> def get_suite_spots ( self , tree , spots ) : <EOL> """<STR_LIT>""" <EOL> for i in range ( <NUM_LIT:1> , len ( tree ) ) : <EOL> if type ( tree [ i ] ) == type ( ( ) ) : <EOL> if tree [ i ] [ <NUM_LIT:0> ] == symbol . suite : <EOL> lineno_colon = lineno_word = None <EOL> for j in range ( i - <NUM_LIT:1> , <NUM_LIT:0> , - <NUM_LIT:1> ) : <EOL> if tree [ j ] [ <NUM_LIT:0> ] == token . COLON : <EOL> lineno_colon = self . last_line_of_tree ( tree [ j - <NUM_LIT:1> ] ) <EOL> elif tree [ j ] [ <NUM_LIT:0> ] == token . NAME : <EOL> if tree [ j ] [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> t = tree [ j + <NUM_LIT:1> ] <EOL> while t and token . ISNONTERMINAL ( t [ <NUM_LIT:0> ] ) : <EOL> t = t [ <NUM_LIT:1> ] <EOL> if t : <EOL> lineno_word = t [ <NUM_LIT:2> ] <EOL> else : <EOL> lineno_word = tree [ j ] [ <NUM_LIT:2> ] <EOL> break <EOL> elif tree [ j ] [ <NUM_LIT:0> ] == symbol . except_clause : <EOL> if tree [ j ] [ <NUM_LIT:1> ] [ <NUM_LIT:0> ] == token . NAME : <EOL> lineno_word = tree [ j ] [ <NUM_LIT:1> ] [ <NUM_LIT:2> ] <EOL> break <EOL> if lineno_colon and lineno_word : <EOL> self . record_multiline ( spots , lineno_word , lineno_colon ) <EOL> self . find_docstring_pass_pair ( tree [ i ] , spots ) <EOL> elif tree [ i ] [ <NUM_LIT:0> ] == symbol . simple_stmt : <EOL> first_line = self . first_line_of_tree ( tree [ i ] ) <EOL> last_line = self . last_line_of_tree ( tree [ i ] ) <EOL> if first_line != last_line : <EOL> self . record_multiline ( spots , first_line , last_line ) <EOL> self . get_suite_spots ( tree [ i ] , spots ) <EOL> def find_executable_statements ( self , text , exclude = None ) : <EOL> excluded = { } <EOL> suite_spots = { } <EOL> if exclude : <EOL> reExclude = re . compile ( exclude ) <EOL> lines = text . split ( '<STR_LIT:\n>' ) <EOL> for i in range ( len ( lines ) ) : <EOL> if reExclude . search ( lines [ i ] ) : <EOL> excluded [ i + <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> import parser <EOL> tree = parser . suite ( text + '<STR_LIT>' ) . totuple ( <NUM_LIT:1> ) <EOL> self . get_suite_spots ( tree , suite_spots ) <EOL> statements = { } <EOL> ast = compiler . parse ( text + '<STR_LIT>' ) <EOL> visitor = StatementFindingAstVisitor ( statements , excluded , suite_spots ) <EOL> compiler . walk ( ast , visitor , walker = visitor ) <EOL> lines = statements . keys ( ) <EOL> lines . sort ( ) <EOL> excluded_lines = excluded . keys ( ) <EOL> excluded_lines . sort ( ) <EOL> return lines , excluded_lines , suite_spots <EOL> def format_lines ( self , statements , lines ) : <EOL> pairs = [ ] <EOL> i = <NUM_LIT:0> <EOL> j = <NUM_LIT:0> <EOL> start = None <EOL> pairs = [ ] <EOL> while i < len ( statements ) and j < len ( lines ) : <EOL> if statements [ i ] == lines [ j ] : <EOL> if start == None : <EOL> start = lines [ j ] <EOL> end = lines [ j ] <EOL> j = j + <NUM_LIT:1> <EOL> elif start : <EOL> pairs . append ( ( start , end ) ) <EOL> start = None <EOL> i = i + <NUM_LIT:1> <EOL> if start : <EOL> pairs . append ( ( start , end ) ) <EOL> def stringify ( pair ) : <EOL> start , end = pair <EOL> if start == end : <EOL> return "<STR_LIT>" % start <EOL> else : <EOL> return "<STR_LIT>" % ( start , end ) <EOL> ret = string . join ( map ( stringify , pairs ) , "<STR_LIT:U+002C>" ) <EOL> return ret <EOL> def analysis ( self , morf ) : <EOL> f , s , _ , m , mf = self . analysis2 ( morf ) <EOL> return f , s , m , mf <EOL> def analysis2 ( self , morf ) : <EOL> filename , statements , excluded , line_map = self . analyze_morf ( morf ) <EOL> self . canonicalize_filenames ( ) <EOL> if not self . cexecuted . has_key ( filename ) : <EOL> self . cexecuted [ filename ] = { } <EOL> missing = [ ] <EOL> for line in statements : <EOL> lines = line_map . get ( line , [ line , line ] ) <EOL> for l in range ( lines [ <NUM_LIT:0> ] , lines [ <NUM_LIT:1> ] + <NUM_LIT:1> ) : <EOL> if self . cexecuted [ filename ] . has_key ( l ) : <EOL> break <EOL> else : <EOL> missing . append ( line ) <EOL> return ( filename , statements , excluded , missing , <EOL> self . format_lines ( statements , missing ) ) <EOL> def relative_filename ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> return filename . replace ( self . relative_dir , "<STR_LIT>" ) <EOL> def morf_name ( self , morf ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( morf , types . ModuleType ) or ( hasattr ( email , '<STR_LIT>' ) and isinstance ( morf , email . LazyImporter ) ) : <EOL> return morf . __name__ <EOL> else : <EOL> return self . relative_filename ( os . path . splitext ( morf ) [ <NUM_LIT:0> ] ) <EOL> def filter_by_prefix ( self , morfs , omit_prefixes ) : <EOL> """<STR_LIT>""" <EOL> filtered_morfs = [ ] <EOL> for morf in morfs : <EOL> for prefix in omit_prefixes : <EOL> if self . morf_name ( morf ) . startswith ( prefix ) : <EOL> break <EOL> else : <EOL> filtered_morfs . append ( morf ) <EOL> return filtered_morfs <EOL> def morf_name_compare ( self , x , y ) : <EOL> return cmp ( self . morf_name ( x ) , self . morf_name ( y ) ) <EOL> def report ( self , morfs , show_missing = <NUM_LIT:1> , ignore_errors = <NUM_LIT:0> , file = None , omit_prefixes = [ ] ) : <EOL> '''<STR_LIT>''' <EOL> if not isinstance ( morfs , types . ListType ) : <EOL> morfs = [ morfs ] <EOL> globbed = [ ] <EOL> for morf in morfs : <EOL> if isinstance ( morf , strclass ) : <EOL> globbed . extend ( glob . glob ( morf ) ) <EOL> else : <EOL> globbed . append ( morf ) <EOL> morfs = globbed <EOL> morfs = self . filter_by_prefix ( morfs , omit_prefixes ) <EOL> morfs . sort ( self . morf_name_compare ) <EOL> max_name = max ( [ <NUM_LIT:5> , ] + map ( len , map ( self . morf_name , morfs ) ) ) <EOL> fmt_name = "<STR_LIT>" % max_name <EOL> fmt_err = fmt_name + "<STR_LIT>" <EOL> header = fmt_name % "<STR_LIT:Name>" + "<STR_LIT>" <EOL> fmt_coverage = fmt_name + "<STR_LIT>" <EOL> if show_missing : <EOL> header = header + "<STR_LIT>" <EOL> fmt_coverage = fmt_coverage + "<STR_LIT>" <EOL> if not file : <EOL> file = sys . stdout <EOL> print >> file , header <EOL> print >> file , "<STR_LIT:->" * len ( header ) <EOL> total_statements = <NUM_LIT:0> <EOL> total_executed = <NUM_LIT:0> <EOL> for morf in morfs : <EOL> name = self . morf_name ( morf ) <EOL> try : <EOL> _ , statements , _ , missing , readable = self . analysis2 ( morf ) <EOL> n = len ( statements ) <EOL> m = n - len ( missing ) <EOL> if n > <NUM_LIT:0> : <EOL> pc = <NUM_LIT> * m / n <EOL> else : <EOL> pc = <NUM_LIT> <EOL> args = ( morf , n , m , pc ) <EOL> if show_missing : <EOL> args = args + ( readable , ) <EOL> print >> file , fmt_coverage % args <EOL> total_statements = total_statements + n <EOL> total_executed = total_executed + m <EOL> except KeyboardInterrupt : <EOL> raise <EOL> except : <EOL> if not ignore_errors : <EOL> typ , msg = sys . exc_info ( ) [ : <NUM_LIT:2> ] <EOL> print >> file , fmt_err % ( morf , typ , msg ) <EOL> if len ( morfs ) > <NUM_LIT:1> : <EOL> print >> file , "<STR_LIT:->" * len ( header ) <EOL> if total_statements > <NUM_LIT:0> : <EOL> pc = <NUM_LIT> * total_executed / total_statements <EOL> else : <EOL> pc = <NUM_LIT> <EOL> args = ( "<STR_LIT>" , total_statements , total_executed , pc ) <EOL> if show_missing : <EOL> args = args + ( "<STR_LIT>" , ) <EOL> print >> file , fmt_coverage % args <EOL> blank_re = re . compile ( r"<STR_LIT>" ) <EOL> else_re = re . compile ( r"<STR_LIT>" ) <EOL> def annotate ( self , morfs , directory = None , ignore_errors = <NUM_LIT:0> , omit_prefixes = [ ] ) : <EOL> morfs = self . filter_by_prefix ( morfs , omit_prefixes ) <EOL> for morf in morfs : <EOL> try : <EOL> filename , statements , excluded , missing , _ = self . analysis2 ( morf ) <EOL> self . annotate_file ( filename , statements , excluded , missing , directory ) <EOL> except KeyboardInterrupt : <EOL> raise <EOL> except : <EOL> if not ignore_errors : <EOL> raise <EOL> def annotate_file ( self , filename , statements , excluded , missing , directory = None ) : <EOL> source = open ( filename , '<STR_LIT:r>' ) <EOL> if directory : <EOL> dest_file = os . path . join ( directory , <EOL> os . path . basename ( filename ) <EOL> + '<STR_LIT>' ) <EOL> else : <EOL> dest_file = filename + '<STR_LIT>' <EOL> dest = open ( dest_file , '<STR_LIT:w>' ) <EOL> lineno = <NUM_LIT:0> <EOL> i = <NUM_LIT:0> <EOL> j = <NUM_LIT:0> <EOL> covered = <NUM_LIT:1> <EOL> while <NUM_LIT:1> : <EOL> line = source . readline ( ) <EOL> if line == '<STR_LIT>' : <EOL> break <EOL> lineno = lineno + <NUM_LIT:1> <EOL> while i < len ( statements ) and statements [ i ] < lineno : <EOL> i = i + <NUM_LIT:1> <EOL> while j < len ( missing ) and missing [ j ] < lineno : <EOL> j = j + <NUM_LIT:1> <EOL> if i < len ( statements ) and statements [ i ] == lineno : <EOL> covered = j >= len ( missing ) or missing [ j ] > lineno <EOL> if self . blank_re . match ( line ) : <EOL> dest . write ( '<STR_LIT:U+0020>' ) <EOL> elif self . else_re . match ( line ) : <EOL> if i >= len ( statements ) and j >= len ( missing ) : <EOL> dest . write ( '<STR_LIT>' ) <EOL> elif i >= len ( statements ) or j >= len ( missing ) : <EOL> dest . write ( '<STR_LIT>' ) <EOL> elif statements [ i ] == missing [ j ] : <EOL> dest . write ( '<STR_LIT>' ) <EOL> else : <EOL> dest . write ( '<STR_LIT>' ) <EOL> elif lineno in excluded : <EOL> dest . write ( '<STR_LIT>' ) <EOL> elif covered : <EOL> dest . write ( '<STR_LIT>' ) <EOL> else : <EOL> dest . write ( '<STR_LIT>' ) <EOL> dest . write ( line ) <EOL> source . close ( ) <EOL> dest . close ( ) <EOL> def use_cache ( * args , ** kw ) : <EOL> return the_coverage . use_cache ( * args , ** kw ) <EOL> def start ( * args , ** kw ) : <EOL> return the_coverage . start ( * args , ** kw ) <EOL> def stop ( * args , ** kw ) : <EOL> return the_coverage . stop ( * args , ** kw ) <EOL> def erase ( * args , ** kw ) : <EOL> return the_coverage . erase ( * args , ** kw ) <EOL> def begin_recursive ( * args , ** kw ) : <EOL> return the_coverage . begin_recursive ( * args , ** kw ) <EOL> def end_recursive ( * args , ** kw ) : <EOL> return the_coverage . end_recursive ( * args , ** kw ) <EOL> def exclude ( * args , ** kw ) : <EOL> return the_coverage . exclude ( * args , ** kw ) <EOL> def analysis ( * args , ** kw ) : <EOL> return the_coverage . analysis ( * args , ** kw ) <EOL> def analysis2 ( * args , ** kw ) : <EOL> return the_coverage . analysis2 ( * args , ** kw ) <EOL> def report ( * args , ** kw ) : <EOL> return the_coverage . report ( * args , ** kw ) <EOL> def annotate ( * args , ** kw ) : <EOL> return the_coverage . annotate ( * args , ** kw ) <EOL> def annotate_file ( * args , ** kw ) : <EOL> return the_coverage . annotate_file ( * args , ** kw ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> global cache_location <EOL> cache_location = sys . argv [ <NUM_LIT:1> ] <EOL> sys . argv . remove ( cache_location ) <EOL> print cache_location <EOL> global the_coverage <EOL> the_coverage = coverage ( ) <EOL> if len ( sys . argv ) == <NUM_LIT:2> : <EOL> if '<STR_LIT>' == sys . argv [ <NUM_LIT:1> ] : <EOL> sys . argv . remove ( '<STR_LIT>' ) <EOL> sys . argv . append ( '<STR_LIT>' ) <EOL> sys . argv . append ( '<STR_LIT>' ) <EOL> s = raw_input ( ) <EOL> s = s . replace ( '<STR_LIT:\r>' , '<STR_LIT>' ) <EOL> s = s . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> files = s . split ( '<STR_LIT:|>' ) <EOL> files = [ v for v in files if len ( v ) > <NUM_LIT:0> ] <EOL> sys . argv += files <EOL> if '<STR_LIT>' in sys . argv : <EOL> try : <EOL> import atexit <EOL> atexit . register ( the_coverage . save ) <EOL> except ImportError : <EOL> sys . exitfunc = the_coverage . save <EOL> the_coverage . command_line ( sys . argv [ <NUM_LIT:1> : ] ) </s>
