<s> """<STR_LIT>""" <EOL> def new_func_strip_path ( func_name ) : <EOL> """<STR_LIT>""" <EOL> filename , line , name = func_name <EOL> if filename . endswith ( "<STR_LIT>" ) : <EOL> return os . path . basename ( filename [ : - <NUM_LIT:12> ] ) + filename [ - <NUM_LIT:12> : ] , line , name <EOL> return os . path . basename ( filename ) , line , name <EOL> try : <EOL> import profile <EOL> import pstats <EOL> pstats . func_strip_path = new_func_strip_path <EOL> except ImportError : <EOL> profile = None <EOL> pstats = None <EOL> import os , os . path <EOL> import sys <EOL> import warnings <EOL> from cherrypy . _cpcompat import BytesIO <EOL> _count = <NUM_LIT:0> <EOL> class Profiler ( object ) : <EOL> def __init__ ( self , path = None ) : <EOL> if not path : <EOL> path = os . path . join ( os . path . dirname ( __file__ ) , "<STR_LIT>" ) <EOL> self . path = path <EOL> if not os . path . exists ( path ) : <EOL> os . makedirs ( path ) <EOL> def run ( self , func , * args , ** params ) : <EOL> """<STR_LIT>""" <EOL> global _count <EOL> c = _count = _count + <NUM_LIT:1> <EOL> path = os . path . join ( self . path , "<STR_LIT>" % c ) <EOL> prof = profile . Profile ( ) <EOL> result = prof . runcall ( func , * args , ** params ) <EOL> prof . dump_stats ( path ) <EOL> return result <EOL> def statfiles ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ f for f in os . listdir ( self . path ) <EOL> if f . startswith ( "<STR_LIT>" ) and f . endswith ( "<STR_LIT>" ) ] <EOL> def stats ( self , filename , sortby = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> sio = BytesIO ( ) <EOL> if sys . version_info >= ( <NUM_LIT:2> , <NUM_LIT:5> ) : <EOL> s = pstats . Stats ( os . path . join ( self . path , filename ) , stream = sio ) <EOL> s . strip_dirs ( ) <EOL> s . sort_stats ( sortby ) <EOL> s . print_stats ( ) <EOL> else : <EOL> s = pstats . Stats ( os . path . join ( self . path , filename ) ) <EOL> s . strip_dirs ( ) <EOL> s . sort_stats ( sortby ) <EOL> oldout = sys . stdout <EOL> try : <EOL> sys . stdout = sio <EOL> s . print_stats ( ) <EOL> finally : <EOL> sys . stdout = oldout <EOL> response = sio . getvalue ( ) <EOL> sio . close ( ) <EOL> return response <EOL> def index ( self ) : <EOL> return """<STR_LIT>""" <EOL> index . exposed = True <EOL> def menu ( self ) : <EOL> yield "<STR_LIT>" <EOL> yield "<STR_LIT>" <EOL> runs = self . statfiles ( ) <EOL> runs . sort ( ) <EOL> for i in runs : <EOL> yield "<STR_LIT>" % ( i , i ) <EOL> menu . exposed = True <EOL> def report ( self , filename ) : <EOL> import cherrypy <EOL> cherrypy . response . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> return self . stats ( filename ) <EOL> report . exposed = True <EOL> class ProfileAggregator ( Profiler ) : <EOL> def __init__ ( self , path = None ) : <EOL> Profiler . __init__ ( self , path ) <EOL> global _count <EOL> self . count = _count = _count + <NUM_LIT:1> <EOL> self . profiler = profile . Profile ( ) <EOL> def run ( self , func , * args ) : <EOL> path = os . path . join ( self . path , "<STR_LIT>" % self . count ) <EOL> result = self . profiler . runcall ( func , * args ) <EOL> self . profiler . dump_stats ( path ) <EOL> return result <EOL> class make_app : <EOL> def __init__ ( self , nextapp , path = None , aggregate = False ) : <EOL> """<STR_LIT>""" <EOL> if profile is None or pstats is None : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> warnings . warn ( msg ) <EOL> self . nextapp = nextapp <EOL> self . aggregate = aggregate <EOL> if aggregate : <EOL> self . profiler = ProfileAggregator ( path ) <EOL> else : <EOL> self . profiler = Profiler ( path ) <EOL> def __call__ ( self , environ , start_response ) : <EOL> def gather ( ) : <EOL> result = [ ] <EOL> for line in self . nextapp ( environ , start_response ) : <EOL> result . append ( line ) <EOL> return result <EOL> return self . profiler . run ( gather ) <EOL> def serve ( path = None , port = <NUM_LIT> ) : <EOL> if profile is None or pstats is None : <EOL> msg = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> warnings . warn ( msg ) <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 ( Profiler ( path ) ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> serve ( * tuple ( sys . argv [ <NUM_LIT:1> : ] ) ) </s>
