<s> from __future__ import print_function <EOL> import optparse <EOL> import os <EOL> import sys <EOL> try : <EOL> import cProfile <EOL> except ImportError : <EOL> raise SystemExit ( "<STR_LIT>" ) <EOL> def label ( code ) : <EOL> if isinstance ( code , str ) : <EOL> return ( '<STR_LIT>' , <NUM_LIT:0> , code ) <EOL> else : <EOL> return '<STR_LIT>' % ( code . co_name , <EOL> code . co_filename , <EOL> code . co_firstlineno ) <EOL> class KCacheGrind ( object ) : <EOL> def __init__ ( self , profiler ) : <EOL> self . data = profiler . getstats ( ) <EOL> self . out_file = None <EOL> def output ( self , out_file ) : <EOL> self . out_file = out_file <EOL> print ( '<STR_LIT>' , file = out_file ) <EOL> self . _print_summary ( ) <EOL> for entry in self . data : <EOL> self . _entry ( entry ) <EOL> def _print_summary ( self ) : <EOL> max_cost = <NUM_LIT:0> <EOL> for entry in self . data : <EOL> totaltime = int ( entry . totaltime * <NUM_LIT:1000> ) <EOL> max_cost = max ( max_cost , totaltime ) <EOL> print ( '<STR_LIT>' % ( max_cost , ) , file = self . out_file ) <EOL> def _entry ( self , entry ) : <EOL> out_file = self . out_file <EOL> code = entry . code <EOL> if isinstance ( code , str ) : <EOL> print ( '<STR_LIT>' , file = out_file ) <EOL> else : <EOL> print ( '<STR_LIT>' % ( code . co_filename , ) , file = out_file ) <EOL> print ( '<STR_LIT>' % ( label ( code ) , ) , file = out_file ) <EOL> inlinetime = int ( entry . inlinetime * <NUM_LIT:1000> ) <EOL> if isinstance ( code , str ) : <EOL> print ( '<STR_LIT>' , inlinetime , file = out_file ) <EOL> else : <EOL> print ( '<STR_LIT>' % ( code . co_firstlineno , inlinetime ) , file = out_file ) <EOL> if entry . calls : <EOL> calls = entry . calls <EOL> else : <EOL> calls = [ ] <EOL> if isinstance ( code , str ) : <EOL> lineno = <NUM_LIT:0> <EOL> else : <EOL> lineno = code . co_firstlineno <EOL> for subentry in calls : <EOL> self . _subentry ( lineno , subentry ) <EOL> print ( file = out_file ) <EOL> def _subentry ( self , lineno , subentry ) : <EOL> out_file = self . out_file <EOL> code = subentry . code <EOL> print ( '<STR_LIT>' % ( label ( code ) , ) , file = out_file ) <EOL> if isinstance ( code , str ) : <EOL> print ( '<STR_LIT>' , file = out_file ) <EOL> print ( '<STR_LIT>' % ( subentry . callcount , ) , file = out_file ) <EOL> else : <EOL> print ( '<STR_LIT>' % ( code . co_filename , ) , file = out_file ) <EOL> print ( '<STR_LIT>' % ( <EOL> subentry . callcount , code . co_firstlineno ) , file = out_file ) <EOL> totaltime = int ( subentry . totaltime * <NUM_LIT:1000> ) <EOL> print ( '<STR_LIT>' % ( lineno , totaltime ) , file = out_file ) <EOL> def main ( args ) : <EOL> usage = "<STR_LIT>" <EOL> parser = optparse . OptionParser ( usage = usage % sys . argv [ <NUM_LIT:0> ] ) <EOL> parser . allow_interspersed_args = False <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" , default = None ) <EOL> if not sys . argv [ <NUM_LIT:1> : ] : <EOL> parser . print_usage ( ) <EOL> sys . exit ( <NUM_LIT:2> ) <EOL> options , args = parser . parse_args ( ) <EOL> if not options . outfile : <EOL> options . outfile = '<STR_LIT>' % os . path . basename ( args [ <NUM_LIT:0> ] ) <EOL> sys . argv [ : ] = args <EOL> prof = cProfile . Profile ( ) <EOL> try : <EOL> try : <EOL> prof = prof . run ( '<STR_LIT>' % ( sys . argv [ <NUM_LIT:0> ] , ) ) <EOL> except SystemExit : <EOL> pass <EOL> finally : <EOL> kg = KCacheGrind ( prof ) <EOL> kg . output ( file ( options . outfile , '<STR_LIT:w>' ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( main ( sys . argv ) ) </s>
