Ñò
Ù4Nc        	   @   sè   d  Z  d d k Z d d k Z d d k Z d d k Z d d k Z d d k Z d d d g Z d e f d „  ƒ  YZ	 d e f d	 „  ƒ  YZ
 d
 „  Z d e f d „  ƒ  YZ d „  Z d „  Z d „  Z e d j o e i e ƒ  ƒ n d S(   sÎ   pyprof2calltree: profiling output which is readable by kcachegrind

This script can either take raw cProfile.Profile.getstats() log entries or
take a previously recorded instance of the pstats.Stats class.
iÿÿÿÿNt   convertt	   visualizet   CalltreeConvertert   Codec           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    sn   /afs/sipb.mit.edu/project/bazki/lib/python2.6/site-packages/pyprof2calltree-1.1.0-py2.6.egg/pyprof2calltree.pyR   &   s   t   Entryc           B   s   e  Z RS(    (   R   R   (    (    (    sn   /afs/sipb.mit.edu/project/bazki/lib/python2.6/site-packages/pyprof2calltree-1.1.0-py2.6.egg/pyprof2calltree.pyR   )   s   c         C   s>  t  ƒ  } t  ƒ  } x³ |  i i ƒ  D]¢ \ } } t ƒ  } | \ | _ | _ | _ | \ } } } }	 }
 t ƒ  } | | _ | | _	 | | | _
 | | _ |	 | _ t ƒ  | _ | | | <|
 i ƒ  | | <q" Wxi | i ƒ  D][ } t i | i ƒ } | i | g  ƒ } x. | D]& \ } } | | i i | | f ƒ qWqÕ W| i ƒ  S(   s„   Helper to convert serialized pstats back to a list of raw entries

    Converse opperation of cProfile.Profile.snapshot_stats()
    (   t   dictt   statst   itemsR   t   co_filenamet   co_firstlinenot   co_nameR   t   codet	   callcountt   reccallcountt
   inlinetimet	   totaltimet   listt   callst
   itervaluest   cProfilet   labelt   gett   appendt   values(   t   datat   entriest
   allcallerst	   code_infot	   call_infoR   t   cct   nct   ttt   ctt   callerst   entryt   entry_labelt   entry_callerst   entry_caller(    (    sn   /afs/sipb.mit.edu/project/bazki/lib/python2.6/site-packages/pyprof2calltree-1.1.0-py2.6.egg/pyprof2calltree.pyt   pstats2entries,   s0    		 						
  "c           B   sJ   e  Z d  Z d Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   s:   Convert raw cProfile or pstats data to the calltree formats   kcachegrind %sc         C   sh   t  | t ƒ o t t i | ƒ ƒ |  _ n0 t  | t i ƒ o t | ƒ |  _ n
 | |  _ d  |  _ d  S(   N(   t
   isinstancet
   basestringR(   t   pstatst   StatsR   t   Nonet   out_file(   t   selft   profiling_data(    (    sn   /afs/sipb.mit.edu/project/bazki/lib/python2.6/site-packages/pyprof2calltree-1.1.0-py2.6.egg/pyprof2calltree.pyt   __init__X   s    	c         C   sB   | |  _  | d IJ|  i ƒ  x |  i D] } |  i | ƒ q' Wd S(   s'   Write the converted entries to out_files   events: TicksN(   R.   t   _print_summaryR   t   _entry(   R/   R.   R$   (    (    sn   /afs/sipb.mit.edu/project/bazki/lib/python2.6/site-packages/pyprof2calltree-1.1.0-py2.6.egg/pyprof2calltree.pyt   outputd   s    	


 c         C   s¥   |  i  d j o> t i d d ƒ \ } } t | d ƒ } |  i | ƒ t } n t } z t i	 |  i
 |  i  i ƒ Wd | o$ | i ƒ  t i | ƒ d |  _  n Xd S(   sl   Launch kcachegrind on the converted entries

        kcachegrind must be present in the system path
        s   .logt   pyprof2calltreet   wbN(   R.   R-   t   tempfilet   mkstempt   fileR4   t   Truet   Falset   ost   systemt   kcachegrind_commandt   namet   closet   remove(   R/   t   _t   outfilet   ft   use_temp_file(    (    sn   /afs/sipb.mit.edu/project/bazki/lib/python2.6/site-packages/pyprof2calltree-1.1.0-py2.6.egg/pyprof2calltree.pyR   l   s    

c         C   sT   d } x3 |  i  D]( } t | i d ƒ } t | | ƒ } q W|  i d | f IJd  S(   Ni    iè  s   summary: %d(   R   t   intR   t   maxR.   (   R/   t   max_costR$   R   (    (    sn   /afs/sipb.mit.edu/project/bazki/lib/python2.6/site-packages/pyprof2calltree-1.1.0-py2.6.egg/pyprof2calltree.pyR2   ƒ   s    
 c         C   s  |  i  } | i } t i | ƒ \ } } } | d | f IJ| d | | | f IJt | i d ƒ } t | t ƒ o | d I| IJn | d | i | f IJ| i	 o | i	 } n g  } t | t ƒ o
 d }	 n
 | i }	 x' | D] \ }
 } |  i
 |	 |
 | ƒ qç W| Jd  S(   Ns   fi=%ss   fn=%s %s:%diè  s   0 s   %d %di    (   R.   R   R   R   RF   R   R)   t   strR   R   t	   _subentry(   R/   R$   R.   R   R
   R   R   R   R   t   linenot   subentryR   (    (    sn   /afs/sipb.mit.edu/project/bazki/lib/python2.6/site-packages/pyprof2calltree-1.1.0-py2.6.egg/pyprof2calltree.pyR3   Š   s(    		

	 c   
      C   s–   |  i  } | i } t i | ƒ \ } } } | d | | | f IJ| d | f IJ| d | d | f IJt | d d ƒ }	 | d | |	 f IJd  S(   Ns   cfn=%s %s:%ds   cfi=%ss   calls=%d %di    i   iè  s   %d %d(   R.   R   R   R   RF   (
   R/   RK   RL   R   R.   R   R
   R   R   R   (    (    sn   /afs/sipb.mit.edu/project/bazki/lib/python2.6/site-packages/pyprof2calltree-1.1.0-py2.6.egg/pyprof2calltree.pyRJ   ª   s    		(
   R   R   t   __doc__R>   R1   R4   R   R2   R3   RJ   (    (    (    sn   /afs/sipb.mit.edu/project/bazki/lib/python2.6/site-packages/pyprof2calltree-1.1.0-py2.6.egg/pyprof2calltree.pyR   S   s   					 c       
   C   sb  d }  t  i d |  t i d ƒ } t | _ | i d d d d d d	 d
 d ƒ| i d d d d d d d
 d ƒ| i d d d d d d d
 d ƒ| i d d d d d d d d ƒ| i ƒ  \ } } | i	 } | i
 d j	 oœ | i
 g | t i (| i	 p d t i i | i
 ƒ } n t i ƒ  } z; y! | i d t i d f ƒ } Wn t j
 o n XWd t | i ƒ  ƒ } Xn‰ | i d j	 oa | i	 p d t i i | i ƒ } n | i | j o | d 7} n t t i | i ƒ ƒ } n | i ƒ  t i d ƒ | i	 d j	 p | i o# d | GH| i t | d ƒ ƒ n | i o d  GH| i ƒ  n d S(!   sC   Execute the converter using parameters provided on the command linesI   %s [-k] [-o output_file_path] [-i input_file_path] [-r scriptfile [args]]t   usagei    s   -os	   --outfilet   destRC   t   helps    Save calltree stats to <outfile>t   defaults   -is   --infilet   infiles   Read python stats from <infile>s   -rs   --run-scriptt   scripts:   Name of the python script to run to collect profiling datas   -ks   --kcachegrindt   kcachegrinds.   Run the kcachegrind tool on the converted datat   actiont
   store_trues   %s.logs   execfile(%r)Ns   .logi   s   writing converted data to: R6   s   launching kcachegrind(   t   optparset   OptionParsert   syst   argvR;   t   allow_interspersed_argst
   add_optionR-   t
   parse_argsRC   RS   R<   t   patht   basenameR   t   Profilet   runt
   SystemExitR   t   getstatsRR   R+   R,   t   print_usaget   exitRT   R4   R9   R   (   RN   t   parsert   optionst   argsRC   t   proft   kg(    (    sn   /afs/sipb.mit.edu/project/bazki/lib/python2.6/site-packages/pyprof2calltree-1.1.0-py2.6.egg/pyprof2calltree.pyt   main·   sP    		
!	

	
c         C   s   t  |  ƒ } | i ƒ  d S(   sâ   launch the kcachegrind on `profiling_data`

    `profiling_data` can either be:
        - a pstats.Stats instance
        - the filename of a pstats.Stats dump
        - the result of a call to cProfile.Profile.getstats()
    N(   R   R   (   R0   t	   converter(    (    sn   /afs/sipb.mit.edu/project/bazki/lib/python2.6/site-packages/pyprof2calltree-1.1.0-py2.6.egg/pyprof2calltree.pyR   ÷   s    c         C   s_   t  |  ƒ } t | t ƒ o2 t | d ƒ } z | i | ƒ Wd | i ƒ  Xn | i | ƒ d S(   sd  convert `profiling_data` to calltree format and dump it to `outputfile`

    `profiling_data` can either be:
        - a pstats.Stats instance
        - the filename of a pstats.Stats dump
        - the result of a call to cProfile.Profile.getstats()

    `outputfile` can either be:
        - a file() instance open in write mode
        - a filename
    R6   N(   R   R)   R*   R9   R4   R@   (   R0   t
   outputfileRl   RD   (    (    sn   /afs/sipb.mit.edu/project/bazki/lib/python2.6/site-packages/pyprof2calltree-1.1.0-py2.6.egg/pyprof2calltree.pyR      s    t   __main__(   RM   R   R+   RW   R<   RY   R7   t   __all__t   objectR   R   R(   R   Rk   R   R    R   Re   (    (    (    sn   /afs/sipb.mit.edu/project/bazki/lib/python2.6/site-packages/pyprof2calltree-1.1.0-py2.6.egg/pyprof2calltree.pyt   <module>   s    	'd	@		