<s> import imp <EOL> import os <EOL> import sys <EOL> import re <EOL> import subprocess <EOL> conaryPath = os . environ . get ( '<STR_LIT>' , None ) <EOL> if not conaryPath : <EOL> print >> sys . stderr , '<STR_LIT>' <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if conaryPath not in sys . path : <EOL> sys . path . insert ( <NUM_LIT:0> , conaryPath ) <EOL> from conary . lib import util <EOL> class CoverageWrapper ( object ) : <EOL> def __init__ ( self , coveragePath , dataPath , annotatePath , baseDirs ) : <EOL> self . _coveragePath = coveragePath <EOL> self . _executable = coveragePath + '<STR_LIT>' <EOL> self . _dataPath = dataPath <EOL> self . _annotatePath = annotatePath <EOL> self . _baseDirs = baseDirs <EOL> os . environ [ '<STR_LIT>' ] = dataPath <EOL> util . mkdirChain ( dataPath ) <EOL> self . environ = dict ( COVERAGE_DIR = dataPath , PATH = '<STR_LIT>' , <EOL> COVERAGE_PATH = coveragePath ) <EOL> if '<STR_LIT>' in os . environ : <EOL> self . environ [ '<STR_LIT>' ] = os . environ [ '<STR_LIT>' ] <EOL> self . environ [ '<STR_LIT>' ] = os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . coverage = None <EOL> def reset ( self ) : <EOL> if os . path . exists ( self . _dataPath ) : <EOL> util . rmtree ( self . _dataPath ) <EOL> if os . path . exists ( self . _annotatePath ) : <EOL> util . rmtree ( self . _annotatePath ) <EOL> def execute ( self , argv ) : <EOL> interp = "<STR_LIT>" <EOL> if isinstance ( argv , str ) : <EOL> cmd = '<STR_LIT>' % ( interp , argv ) <EOL> else : <EOL> cmd = [ interp ] + argv <EOL> retval = subprocess . call ( cmd , env = os . environ ) <EOL> return retval <EOL> def getCoverage ( self ) : <EOL> if self . coverage : <EOL> return self . coverage <EOL> coverage = imp . load_source ( '<STR_LIT>' , self . _executable ) <EOL> coverage = coverage . the_coverage <EOL> coverage . cacheDir = self . _dataPath <EOL> coverage . get_ready ( restoreDir = True ) <EOL> return coverage <EOL> def compress ( self ) : <EOL> coverage = self . getCoverage ( ) <EOL> if os . path . exists ( self . _dataPath ) : <EOL> util . rmtree ( self . _dataPath ) <EOL> util . mkdirChain ( self . _dataPath ) <EOL> coverage . save ( ) <EOL> def displayReport ( self , files , displayMissingLines = False ) : <EOL> assert ( not displayMissingLines ) <EOL> missingSortKey = lambda x : ( len ( x [ <NUM_LIT:2> ] ) , len ( x [ <NUM_LIT:1> ] ) ) <EOL> sortFn = lambda a , b : cmp ( missingSortKey ( a ) , missingSortKey ( b ) ) <EOL> coverage = self . getCoverage ( ) <EOL> coverage . report ( files , show_missing = False , sortFn = sortFn , <EOL> baseDirs = self . _baseDirs ) <EOL> def writeAnnotatedFiles ( self , files ) : <EOL> if os . path . exists ( self . _annotatePath ) : <EOL> util . rmtree ( self . _annotatePath ) <EOL> annotatePath = self . _annotatePath <EOL> coverage = self . getCoverage ( ) <EOL> coverage . annotate ( files , self . _annotatePath , baseDirs = self . _baseDirs ) <EOL> if annotatePath . startswith ( os . getcwd ( ) + '<STR_LIT:/>' ) : <EOL> annotatePath = '<STR_LIT:.>' + annotatePath [ len ( os . getcwd ( ) ) : ] <EOL> print <EOL> print '<STR_LIT>' % ( len ( files ) , annotatePath ) <EOL> def iterAnalysis ( self , paths ) : <EOL> coverage = imp . load_source ( '<STR_LIT>' , self . _executable ) <EOL> coverage = coverage . the_coverage <EOL> coverage . cache = self . _dataPath <EOL> coverage . restore ( ) <EOL> for path in paths : <EOL> _ , statements , excluded , missing , missingFmted = coverage . analysis2 ( path ) <EOL> total = len ( statements ) <EOL> covered = total - len ( missing ) <EOL> if covered < total : <EOL> percentCovered = <NUM_LIT> * covered / total <EOL> else : <EOL> percentCovered = <NUM_LIT> <EOL> yield path , total , covered , percentCovered , missingFmted <EOL> def getTotals ( self , paths ) : <EOL> totalLines = <NUM_LIT:0> <EOL> totalCovered = <NUM_LIT:0> <EOL> for _ , pathLines , coveredLines , _ , _ in self . iterAnalysis ( paths ) : <EOL> totalLines += pathLines <EOL> totalCovered += coveredLines <EOL> if totalCovered != totalLines : <EOL> percentCovered = <NUM_LIT:1.0> - <NUM_LIT> * totalCovered / totalLines <EOL> else : <EOL> percentCovered = <NUM_LIT> <EOL> return totalLines , totalCovered , percentCovered <EOL> def getEnviron ( ) : <EOL> conaryPath = os . environ . get ( '<STR_LIT>' , None ) <EOL> coveragePath = os . environ . get ( '<STR_LIT>' , None ) <EOL> policyPath = os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not coveragePath : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> elif not policyPath : <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> return { '<STR_LIT>' : conaryPath , <EOL> '<STR_LIT>' : policyPath , <EOL> '<STR_LIT>' : coveragePath } <EOL> def _isPythonFile ( fullPath , posFilters = [ r'<STR_LIT>' ] , negFilters = [ '<STR_LIT>' ] ) : <EOL> found = False <EOL> for posFilter in posFilters : <EOL> if re . search ( posFilter , fullPath ) : <EOL> found = True <EOL> break <EOL> if not found : <EOL> return False <EOL> foundNeg = False <EOL> for negFilter in negFilters : <EOL> if re . search ( negFilter , fullPath ) : <EOL> foundNeg = True <EOL> break <EOL> if foundNeg : <EOL> return False <EOL> return True <EOL> def getFilesToAnnotate ( baseDirs = [ ] , filesToFind = [ ] , exclude = [ ] ) : <EOL> notExists = set ( filesToFind ) <EOL> addAll = not filesToFind <EOL> allFiles = set ( x for x in filesToFind if os . path . exists ( x ) ) <EOL> filesToFind = [ x for x in filesToFind if x not in allFiles ] <EOL> if not isinstance ( exclude , list ) : <EOL> exclude = [ exclude ] <EOL> negFilters = [ '<STR_LIT>' ] + exclude <EOL> baseDirs = [ os . path . realpath ( x ) for x in baseDirs ] <EOL> for baseDir in baseDirs : <EOL> for root , dirs , pathList in os . walk ( baseDir ) : <EOL> for path in filesToFind : <EOL> if os . path . exists ( os . path . join ( root , path ) ) : <EOL> allFiles . add ( os . path . join ( root , path ) ) <EOL> notExists . discard ( path ) <EOL> if addAll : <EOL> for path in pathList : <EOL> fullPath = '<STR_LIT:/>' . join ( ( root , path ) ) <EOL> if _isPythonFile ( fullPath , negFilters = negFilters ) : <EOL> allFiles . add ( fullPath ) <EOL> return list ( allFiles ) , notExists <EOL> def getFilesToAnnotateByPatch ( baseDir , exclude , patchInput , filesDict = None ) : <EOL> if filesDict is None : <EOL> files = { } <EOL> else : <EOL> files = filesDict <EOL> cwd = os . getcwd ( ) <EOL> try : <EOL> os . chdir ( baseDir ) <EOL> patchReaderPath = os . environ [ '<STR_LIT>' ] + '<STR_LIT>' <EOL> stdin , output = os . popen2 ( '<STR_LIT>' % ( patchReaderPath , ) , '<STR_LIT:w>' ) <EOL> stdin . write ( patchInput . read ( ) ) <EOL> stdin . flush ( ) <EOL> stdin . close ( ) <EOL> finally : <EOL> os . chdir ( cwd ) <EOL> while True : <EOL> fileName = output . readline ( ) . strip ( ) <EOL> if not fileName : <EOL> break <EOL> lines = [ int ( x ) for x in output . readline ( ) . split ( ) ] <EOL> files [ fileName ] = lines <EOL> for fileName in files . keys ( ) : <EOL> for excludePattern in exclude : <EOL> if re . search ( excludePattern , fileName ) : <EOL> del files [ fileName ] <EOL> def getFilesToAnnotateFromPatchFile ( path , baseDirs = [ ] , exclude = [ ] ) : <EOL> return _getFilesToAnnotateFromFn ( baseDirs , exclude , open , path ) <EOL> def getFilesToAnnotateFromHg ( baseDirs = [ ] , exclude = [ ] ) : <EOL> return _getFilesToAnnotateFromFn ( baseDirs , exclude , os . popen , "<STR_LIT>" ) <EOL> def getFilesToAnnotateFromHgOut ( baseDirs = [ ] , exclude = [ ] ) : <EOL> return _getFilesToAnnotateFromFn ( baseDirs , exclude , os . popen , "<STR_LIT>" ) <EOL> def _getFilesToAnnotateFromFn ( baseDirs , exclude , fn , * args , ** kw ) : <EOL> files = { } <EOL> curDir = os . getcwd ( ) <EOL> try : <EOL> for baseDir in baseDirs : <EOL> if not os . path . isdir ( os . path . join ( baseDir , '<STR_LIT>' ) ) and not os . path . isdir ( os . path . join ( baseDir , '<STR_LIT>' ) ) : <EOL> continue <EOL> os . chdir ( baseDir ) <EOL> output = fn ( * args , ** kw ) <EOL> getFilesToAnnotateByPatch ( baseDir , exclude , output , files ) <EOL> finally : <EOL> os . chdir ( curDir ) <EOL> for fullPath in files . keys ( ) : <EOL> if not _isPythonFile ( fullPath ) : <EOL> del files [ fullPath ] <EOL> return files , [ ] </s>
