<s> """<STR_LIT>""" <EOL> import os <EOL> from coverage . backward import iitems , pickle , sorted <EOL> from coverage . files import PathAliases <EOL> from coverage . misc import file_be_gone <EOL> class CoverageData ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , basename = None , collector = None , debug = None ) : <EOL> """<STR_LIT>""" <EOL> self . collector = collector or '<STR_LIT>' <EOL> self . debug = debug <EOL> self . use_file = True <EOL> self . filename = basename or "<STR_LIT>" <EOL> self . filename = os . path . abspath ( self . filename ) <EOL> self . lines = { } <EOL> self . arcs = { } <EOL> def usefile ( self , use_file = True ) : <EOL> """<STR_LIT>""" <EOL> self . use_file = use_file <EOL> def read ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . use_file : <EOL> self . lines , self . arcs = self . _read_file ( self . filename ) <EOL> else : <EOL> self . lines , self . arcs = { } , { } <EOL> def write ( self , suffix = None ) : <EOL> """<STR_LIT>""" <EOL> if self . use_file : <EOL> filename = self . filename <EOL> if suffix : <EOL> filename += "<STR_LIT:.>" + suffix <EOL> self . write_file ( filename ) <EOL> def erase ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . use_file : <EOL> if self . filename : <EOL> file_be_gone ( self . filename ) <EOL> self . lines = { } <EOL> self . arcs = { } <EOL> def line_data ( self ) : <EOL> """<STR_LIT>""" <EOL> return dict ( <EOL> [ ( f , sorted ( lmap . keys ( ) ) ) for f , lmap in iitems ( self . lines ) ] <EOL> ) <EOL> def arc_data ( self ) : <EOL> """<STR_LIT>""" <EOL> return dict ( <EOL> [ ( f , sorted ( amap . keys ( ) ) ) for f , amap in iitems ( self . arcs ) ] <EOL> ) <EOL> def write_file ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> data = { } <EOL> data [ '<STR_LIT>' ] = self . line_data ( ) <EOL> arcs = self . arc_data ( ) <EOL> if arcs : <EOL> data [ '<STR_LIT>' ] = arcs <EOL> if self . collector : <EOL> data [ '<STR_LIT>' ] = self . collector <EOL> if self . debug and self . debug . should ( '<STR_LIT>' ) : <EOL> self . debug . write ( "<STR_LIT>" % ( filename , ) ) <EOL> fdata = open ( filename , '<STR_LIT:wb>' ) <EOL> try : <EOL> pickle . dump ( data , fdata , <NUM_LIT:2> ) <EOL> finally : <EOL> fdata . close ( ) <EOL> def read_file ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> self . lines , self . arcs = self . _read_file ( filename ) <EOL> def raw_data ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> if self . debug and self . debug . should ( '<STR_LIT>' ) : <EOL> self . debug . write ( "<STR_LIT>" % ( filename , ) ) <EOL> fdata = open ( filename , '<STR_LIT:rb>' ) <EOL> try : <EOL> data = pickle . load ( fdata ) <EOL> finally : <EOL> fdata . close ( ) <EOL> return data <EOL> def _read_file ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> lines = { } <EOL> arcs = { } <EOL> try : <EOL> data = self . raw_data ( filename ) <EOL> if isinstance ( data , dict ) : <EOL> lines = dict ( [ <EOL> ( f , dict . fromkeys ( linenos , None ) ) <EOL> for f , linenos in iitems ( data . get ( '<STR_LIT>' , { } ) ) <EOL> ] ) <EOL> arcs = dict ( [ <EOL> ( f , dict . fromkeys ( arcpairs , None ) ) <EOL> for f , arcpairs in iitems ( data . get ( '<STR_LIT>' , { } ) ) <EOL> ] ) <EOL> except Exception : <EOL> pass <EOL> return lines , arcs <EOL> def combine_parallel_data ( self , aliases = None ) : <EOL> """<STR_LIT>""" <EOL> aliases = aliases or PathAliases ( ) <EOL> data_dir , local = os . path . split ( self . filename ) <EOL> localdot = local + '<STR_LIT:.>' <EOL> for f in os . listdir ( data_dir or '<STR_LIT:.>' ) : <EOL> if f . startswith ( localdot ) : <EOL> full_path = os . path . join ( data_dir , f ) <EOL> new_lines , new_arcs = self . _read_file ( full_path ) <EOL> for filename , file_data in iitems ( new_lines ) : <EOL> filename = aliases . map ( filename ) <EOL> self . lines . setdefault ( filename , { } ) . update ( file_data ) <EOL> for filename , file_data in iitems ( new_arcs ) : <EOL> filename = aliases . map ( filename ) <EOL> self . arcs . setdefault ( filename , { } ) . update ( file_data ) <EOL> if f != local : <EOL> os . remove ( full_path ) <EOL> def add_line_data ( self , line_data ) : <EOL> """<STR_LIT>""" <EOL> for filename , linenos in iitems ( line_data ) : <EOL> self . lines . setdefault ( filename , { } ) . update ( linenos ) <EOL> def add_arc_data ( self , arc_data ) : <EOL> """<STR_LIT>""" <EOL> for filename , arcs in iitems ( arc_data ) : <EOL> self . arcs . setdefault ( filename , { } ) . update ( arcs ) <EOL> def touch_file ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> self . lines . setdefault ( filename , { } ) <EOL> def measured_files ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( self . lines . keys ( ) ) <EOL> def executed_lines ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> return self . lines . get ( filename ) or { } <EOL> def executed_arcs ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> return self . arcs . get ( filename ) or { } <EOL> def add_to_hash ( self , filename , hasher ) : <EOL> """<STR_LIT>""" <EOL> hasher . update ( self . executed_lines ( filename ) ) <EOL> hasher . update ( self . executed_arcs ( filename ) ) <EOL> def summary ( self , fullpath = False ) : <EOL> """<STR_LIT>""" <EOL> summ = { } <EOL> if fullpath : <EOL> filename_fn = lambda f : f <EOL> else : <EOL> filename_fn = os . path . basename <EOL> for filename , lines in iitems ( self . lines ) : <EOL> summ [ filename_fn ( filename ) ] = len ( lines ) <EOL> return summ <EOL> def has_arcs ( self ) : <EOL> """<STR_LIT>""" <EOL> return bool ( self . arcs ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import pprint , sys <EOL> covdata = CoverageData ( ) <EOL> if sys . argv [ <NUM_LIT:1> : ] : <EOL> fname = sys . argv [ <NUM_LIT:1> ] <EOL> else : <EOL> fname = covdata . filename <EOL> pprint . pprint ( covdata . raw_data ( fname ) ) </s>
