#NEWLINE# def output_profile( pr , stats_path = '/tmp/stats' , stop = False , id_ = None ) : #NEWLINE# #INDENT# if ( ( pr is not None ) and HAS_CPROFILE ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# pr . disable ( ) #NEWLINE# if ( not os . path . isdir ( stats_path ) ) : #NEWLINE# #INDENT# os . makedirs ( stats_path ) #NEWLINE# #UNINDENT# date = datetime . datetime . now ( ) . isoformat ( ) #NEWLINE# if ( id_ is None ) : #NEWLINE# #INDENT# id_ = rand_str ( size = 32 ) #NEWLINE# #UNINDENT# ficp = os . path . join ( stats_path , '{0}.{1}.pstats' . format ( id_ , date ) ) #NEWLINE# fico = os . path . join ( stats_path , '{0}.{1}.dot' . format ( id_ , date ) ) #NEWLINE# ficn = os . path . join ( stats_path , '{0}.{1}.stats' . format ( id_ , date ) ) #NEWLINE# if ( not os . path . exists ( ficp ) ) : #NEWLINE# #INDENT# pr . dump_stats ( ficp ) #NEWLINE# with open ( ficn , 'w' ) as fic : #NEWLINE# #INDENT# pstats . Stats ( pr , stream = fic ) . sort_stats ( 'cumulative' ) #NEWLINE# #UNINDENT# #UNINDENT# log . info ( 'PROFILING: {0} generated' . format ( ficp ) ) #NEWLINE# log . info ( 'PROFILING (cumulative): {0} generated' . format ( ficn ) ) #NEWLINE# pyprof = which ( 'pyprof2calltree' ) #NEWLINE# cmd = [ pyprof , '-i' , ficp , '-o' , fico ] #NEWLINE# if pyprof : #NEWLINE# #INDENT# failed = False #NEWLINE# try : #NEWLINE# #INDENT# pro = subprocess . Popen ( cmd , shell = False , stdout = subprocess . PIPE , stderr = subprocess . PIPE ) #NEWLINE# #UNINDENT# except OSError : #NEWLINE# #INDENT# failed = True #NEWLINE# #UNINDENT# if pro . returncode : #NEWLINE# #INDENT# failed = True #NEWLINE# #UNINDENT# if failed : #NEWLINE# #INDENT# log . error ( 'PROFILING (dot problem' ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# log . info ( 'PROFILING (dot): {0} generated' . format ( fico ) ) #NEWLINE# #UNINDENT# log . trace ( 'pyprof2calltree output:' ) #NEWLINE# log . trace ( ( to_str ( pro . stdout . read ( ) ) . strip ( ) + to_str ( pro . stderr . read ( ) ) . strip ( ) ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# log . info ( 'You can run {0} for additional stats.' . format ( cmd ) ) #NEWLINE# #UNINDENT# #UNINDENT# finally : #NEWLINE# #INDENT# if ( not stop ) : #NEWLINE# #INDENT# pr . enable ( ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return pr