#NEWLINE# def main( ) : #NEWLINE# #INDENT# parser = argparse . ArgumentParser ( description = 'An easier way to use cProfile.' , usage = '%(prog)s [--version] [-a ADDRESS] [-p PORT] scriptfile [arg] ...' , formatter_class = argparse . RawTextHelpFormatter ) #NEWLINE# parser . add_argument ( '--version' , action = 'version' , version = VERSION ) #NEWLINE# parser . add_argument ( '-a' , '--address' , type = str , default = '127.0.0.1' , help = 'The address to listen on. (defaults to 127.0.0.1).' ) #NEWLINE# parser . add_argument ( '-p' , '--port' , type = int , default = 4000 , help = 'The port to listen on. (defaults to 4000).' ) #NEWLINE# parser . add_argument ( '-f' , '--file' , type = str , help = 'cProfile output to view.\nIf specified, the scriptfile provided will be ignored.' ) #NEWLINE# parser . add_argument ( 'remainder' , nargs = argparse . REMAINDER , help = 'The python script file to run and profile.' , metavar = 'scriptfile' ) #NEWLINE# args = parser . parse_args ( ) #NEWLINE# if ( not sys . argv [ 1 : ] ) : #NEWLINE# #INDENT# parser . print_help ( ) #NEWLINE# sys . exit ( 2 ) #NEWLINE# #UNINDENT# info = ( '[cProfileV]: cProfile output available at http://%s:%s' % ( args . address , args . port ) ) #NEWLINE# if args . file : #NEWLINE# #INDENT# sys . stderr . write ( ( info + '\n' ) ) #NEWLINE# cprofilev = CProfileV ( args . file , title = args . file , address = args . address , port = args . port ) #NEWLINE# cprofilev . start ( ) #NEWLINE# return #NEWLINE# #UNINDENT# sys . argv [ : ] = args . remainder #NEWLINE# if ( len ( cprofilev . remainder ) < 0 ) : #NEWLINE# #INDENT# parser . print_help ( ) #NEWLINE# sys . exit ( 2 ) #NEWLINE# #UNINDENT# profile = cProfile . Profile ( ) #NEWLINE# progname = args . remainder [ 0 ] #NEWLINE# sys . path . insert ( 0 , os . path . dirname ( progname ) ) #NEWLINE# with open ( progname , 'rb' ) as fp : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# code = compile ( fp . read ( ) , progname , 'exec' ) #NEWLINE# #UNINDENT# except ( TypeError , SyntaxError ) as e : #NEWLINE# #INDENT# sys . stderr . write ( ( '[cProfileV]: there was a problem compiling your scriptfile.' + '\n\n' ) ) #NEWLINE# parser . print_help ( ) #NEWLINE# sys . exit ( 2 ) #NEWLINE# #UNINDENT# #UNINDENT# sys . stderr . write ( ( info + '\n' ) ) #NEWLINE# globs = { #NEWLINE# #INDENT# '__file__' : progname , #NEWLINE# '__name__' : '__main__' , #NEWLINE# '__package__' : None , #NEWLINE# #UNINDENT# } #NEWLINE# progthread = threading . Thread ( target = profile . runctx , args = ( code , globs , None ) ) #NEWLINE# progthread . setDaemon ( True ) #NEWLINE# progthread . start ( ) #NEWLINE# cprofilev = CProfileV ( profile , title = progname , address = args . address , port = args . port ) #NEWLINE# cprofilev . start ( )