Package commons :: Module trace
[hide private]
[frames] | no frames]

Source Code for Module commons.trace

 1  #!/usr/bin/env python 
 2  # -*- mode: python; tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4; -*- 
 3  # vim:ft=python:et:sw=4:ts=4 
 4   
 5  """ 
 6  Standalone program for tracing a Python program. Simply specify the module whose C{main} function we want to run. 
 7   
 8  @todo: Document this more completely. 
 9  """ 
10   
11  from __future__ import absolute_import 
12  from .startup import run_main 
13  import imp, linecache, sys 
14   
15 -class Tracer( object ):
16 - def __init__( self ):
17 self.do_trace = False
18
19 - def trace( self, frame, event, arg ):
20 c = frame.f_code 21 g = frame.f_globals 22 # events: line, call 23 module_name = g["__name__"] 24 function_name = c.co_name 25 if event == 'line' and ( self.do_trace and function_name.startswith( 'forms' ) or function_name.startswith( 'select_form' ) ): 26 self.do_trace = True 27 lineno = frame.f_lineno 28 filename = '<unknown file>' 29 line = '<line unavailable>' 30 try: 31 filename = g["__file__"] 32 if (filename.endswith(".pyc") or 33 filename.endswith(".pyo")): 34 filename = filename[:-1] 35 line = linecache.getline( filename, lineno ) 36 if line is None or line.strip() == '': 37 line = '<line unavailable>' 38 except KeyError: 39 pass 40 print '%s:%d:\t%s:%s(%s):\t%s' % ( filename, 41 lineno, 42 module_name, 43 function_name, 44 ', '.join( c.co_varnames[ : c.co_argcount ] ), 45 line.rstrip() ) 46 return self.trace
47
48 -def main( argv ):
49 module_name = argv[ 1 ] 50 args = argv[ 1 : ] 51 52 module = imp.load_module( module_name, 53 *imp.find_module( module_name ) ) 54 55 tracer = Tracer() 56 sys.settrace( tracer.trace ) 57 58 module.main( args )
59 60 run_main() 61