<s> """<STR_LIT>""" <EOL> import re <EOL> import sys <EOL> import cgi <EOL> from cherrypy . _cpcompat import quote_plus <EOL> import os , os . path <EOL> localFile = os . path . join ( os . path . dirname ( __file__ ) , "<STR_LIT>" ) <EOL> the_coverage = None <EOL> try : <EOL> from coverage import coverage <EOL> the_coverage = coverage ( data_file = localFile ) <EOL> def start ( ) : <EOL> the_coverage . start ( ) <EOL> except ImportError : <EOL> the_coverage = None <EOL> import warnings <EOL> warnings . warn ( "<STR_LIT>" ) <EOL> def start ( ) : <EOL> pass <EOL> start . priority = <NUM_LIT:20> <EOL> TEMPLATE_MENU = """<STR_LIT>""" <EOL> TEMPLATE_FORM = """<STR_LIT>""" <EOL> TEMPLATE_FRAMESET = """<STR_LIT>""" <EOL> TEMPLATE_COVERAGE = """<STR_LIT>""" <EOL> TEMPLATE_LOC_COVERED = """<STR_LIT>""" <EOL> TEMPLATE_LOC_NOT_COVERED = """<STR_LIT>""" <EOL> TEMPLATE_LOC_EXCLUDED = """<STR_LIT>""" <EOL> TEMPLATE_ITEM = "<STR_LIT>" <EOL> def _percent ( statements , missing ) : <EOL> s = len ( statements ) <EOL> e = s - len ( missing ) <EOL> if s > <NUM_LIT:0> : <EOL> return int ( round ( <NUM_LIT> * e / s ) ) <EOL> return <NUM_LIT:0> <EOL> def _show_branch ( root , base , path , pct = <NUM_LIT:0> , showpct = False , exclude = "<STR_LIT>" , <EOL> coverage = the_coverage ) : <EOL> dirs = [ k for k , v in root . items ( ) if v ] <EOL> dirs . sort ( ) <EOL> for name in dirs : <EOL> newpath = os . path . join ( path , name ) <EOL> if newpath . lower ( ) . startswith ( base ) : <EOL> relpath = newpath [ len ( base ) : ] <EOL> yield "<STR_LIT>" * relpath . count ( os . sep ) <EOL> yield "<STR_LIT>" % ( newpath , quote_plus ( exclude ) , name ) <EOL> for chunk in _show_branch ( root [ name ] , base , newpath , pct , showpct , exclude , coverage = coverage ) : <EOL> yield chunk <EOL> if path . lower ( ) . startswith ( base ) : <EOL> relpath = path [ len ( base ) : ] <EOL> files = [ k for k , v in root . items ( ) if not v ] <EOL> files . sort ( ) <EOL> for name in files : <EOL> newpath = os . path . join ( path , name ) <EOL> pc_str = "<STR_LIT>" <EOL> if showpct : <EOL> try : <EOL> _ , statements , _ , missing , _ = coverage . analysis2 ( newpath ) <EOL> except : <EOL> pass <EOL> else : <EOL> pc = _percent ( statements , missing ) <EOL> pc_str = ( "<STR_LIT>" % pc ) . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) <EOL> if pc < float ( pct ) or pc == - <NUM_LIT:1> : <EOL> pc_str = "<STR_LIT>" % pc_str <EOL> else : <EOL> pc_str = "<STR_LIT>" % pc_str <EOL> yield TEMPLATE_ITEM % ( "<STR_LIT>" * ( relpath . count ( os . sep ) + <NUM_LIT:1> ) , <EOL> pc_str , newpath , name ) <EOL> def _skip_file ( path , exclude ) : <EOL> if exclude : <EOL> return bool ( re . search ( exclude , path ) ) <EOL> def _graft ( path , tree ) : <EOL> d = tree <EOL> p = path <EOL> atoms = [ ] <EOL> while True : <EOL> p , tail = os . path . split ( p ) <EOL> if not tail : <EOL> break <EOL> atoms . append ( tail ) <EOL> atoms . append ( p ) <EOL> if p != "<STR_LIT:/>" : <EOL> atoms . append ( "<STR_LIT:/>" ) <EOL> atoms . reverse ( ) <EOL> for node in atoms : <EOL> if node : <EOL> d = d . setdefault ( node , { } ) <EOL> def get_tree ( base , exclude , coverage = the_coverage ) : <EOL> """<STR_LIT>""" <EOL> tree = { } <EOL> runs = coverage . data . executed_files ( ) <EOL> for path in runs : <EOL> if not _skip_file ( path , exclude ) and not os . path . isdir ( path ) : <EOL> _graft ( path , tree ) <EOL> return tree <EOL> class CoverStats ( object ) : <EOL> def __init__ ( self , coverage , root = None ) : <EOL> self . coverage = coverage <EOL> if root is None : <EOL> import cherrypy <EOL> root = os . path . dirname ( cherrypy . __file__ ) <EOL> self . root = root <EOL> def index ( self ) : <EOL> return TEMPLATE_FRAMESET % self . root . lower ( ) <EOL> index . exposed = True <EOL> def menu ( self , base = "<STR_LIT:/>" , pct = "<STR_LIT>" , showpct = "<STR_LIT>" , <EOL> exclude = r'<STR_LIT>' ) : <EOL> base = base . lower ( ) . rstrip ( os . sep ) <EOL> yield TEMPLATE_MENU <EOL> yield TEMPLATE_FORM % locals ( ) <EOL> yield "<STR_LIT>" <EOL> path = "<STR_LIT>" <EOL> atoms = base . split ( os . sep ) <EOL> atoms . pop ( ) <EOL> for atom in atoms : <EOL> path += atom + os . sep <EOL> yield ( "<STR_LIT>" <EOL> % ( path , quote_plus ( exclude ) , atom , os . sep ) ) <EOL> yield "<STR_LIT>" <EOL> yield "<STR_LIT>" <EOL> tree = get_tree ( base , exclude , self . coverage ) <EOL> if not tree : <EOL> yield "<STR_LIT>" <EOL> else : <EOL> for chunk in _show_branch ( tree , base , "<STR_LIT:/>" , pct , <EOL> showpct == '<STR_LIT>' , exclude , coverage = self . coverage ) : <EOL> yield chunk <EOL> yield "<STR_LIT>" <EOL> yield "<STR_LIT>" <EOL> menu . exposed = True <EOL> def annotated_file ( self , filename , statements , excluded , missing ) : <EOL> source = open ( filename , '<STR_LIT:r>' ) <EOL> buffer = [ ] <EOL> for lineno , line in enumerate ( source . readlines ( ) ) : <EOL> lineno += <NUM_LIT:1> <EOL> line = line . strip ( "<STR_LIT>" ) <EOL> empty_the_buffer = True <EOL> if lineno in excluded : <EOL> template = TEMPLATE_LOC_EXCLUDED <EOL> elif lineno in missing : <EOL> template = TEMPLATE_LOC_NOT_COVERED <EOL> elif lineno in statements : <EOL> template = TEMPLATE_LOC_COVERED <EOL> else : <EOL> empty_the_buffer = False <EOL> buffer . append ( ( lineno , line ) ) <EOL> if empty_the_buffer : <EOL> for lno , pastline in buffer : <EOL> yield template % ( lno , cgi . escape ( pastline ) ) <EOL> buffer = [ ] <EOL> yield template % ( lineno , cgi . escape ( line ) ) <EOL> def report ( self , name ) : <EOL> filename , statements , excluded , missing , _ = self . coverage . analysis2 ( name ) <EOL> pc = _percent ( statements , missing ) <EOL> yield TEMPLATE_COVERAGE % dict ( name = os . path . basename ( name ) , <EOL> fullpath = name , <EOL> pc = pc ) <EOL> yield '<STR_LIT>' <EOL> for line in self . annotated_file ( filename , statements , excluded , <EOL> missing ) : <EOL> yield line <EOL> yield '<STR_LIT>' <EOL> yield '<STR_LIT>' <EOL> yield '<STR_LIT>' <EOL> report . exposed = True <EOL> def serve ( path = localFile , port = <NUM_LIT> , root = None ) : <EOL> if coverage is None : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> from coverage import coverage <EOL> cov = coverage ( data_file = path ) <EOL> cov . load ( ) <EOL> import cherrypy <EOL> cherrypy . config . update ( { '<STR_LIT>' : int ( port ) , <EOL> '<STR_LIT>' : <NUM_LIT:10> , <EOL> '<STR_LIT>' : "<STR_LIT>" , <EOL> } ) <EOL> cherrypy . quickstart ( CoverStats ( cov , root ) ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> serve ( * tuple ( sys . argv [ <NUM_LIT:1> : ] ) ) </s>
