ó
 ‘hOc           @   sæ   d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l 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 k râ e j 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__(    (    (    sX   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/pyprof2calltree.pyR   &   s   t   Entryc           B   s   e  Z RS(    (   R   R   (    (    (    sX   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/pyprof2calltree.pyR   )   s   c         C   s>  t  ƒ  } t  ƒ  } x³ |  j j ƒ  D]¢ \ } } t ƒ  } | \ | _ | _ | _ | \ } } } }	 }
 t ƒ  } | | _ | | _	 | | | _
 | | _ |	 | _ t ƒ  | _ | | | <|
 j ƒ  | | <q" Wxi | j ƒ  D][ } t j | j ƒ } | j | g  ƒ } x. | D]& \ } } | | j j | | f ƒ qWqÕ W| j ƒ  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(    (    sX   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/pyprof2calltree.pyt   pstats2entries,   s*    								
"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   sd   t  | t ƒ r* t t j | ƒ ƒ |  _ n- t  | t j ƒ rN t | ƒ |  _ n	 | |  _ d  |  _ d  S(   N(   t
   isinstancet
   basestringR(   t   pstatst   StatsR   t   Nonet   out_file(   t   selft   profiling_data(    (    sX   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/pyprof2calltree.pyt   __init__X   s    	c         C   sB   | |  _  | d IJ|  j ƒ  x |  j D] } |  j | ƒ q' Wd S(   s'   Write the converted entries to out_files   events: TicksN(   R.   t   _print_summaryR   t   _entry(   R/   R.   R$   (    (    sX   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/pyprof2calltree.pyt   outputd   s
    	

c         C   s¡   |  j  d k rL t j d d ƒ \ } } t | d ƒ } |  j | ƒ t } n t } z t j	 |  j
 |  j  j ƒ Wd | rœ | j ƒ  t j | ƒ 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(    (    sX   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/pyprof2calltree.pyR   l   s    	
c         C   sT   d } x3 |  j  D]( } t | j d ƒ } t | | ƒ } q W|  j d | f IJd  S(   Ni    iè  s   summary: %d(   R   t   intR   t   maxR.   (   R/   t   max_costR$   R   (    (    sX   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/pyprof2calltree.pyR2   ƒ   s
    c         C   s  |  j  } | j } t j | ƒ \ } } } | d | f IJ| d | | | f IJt | j d ƒ } t | t ƒ r‡ | d I| IJn | d | j | f IJ| j	 r³ | j	 } n g  } t | t ƒ rÑ d }	 n	 | j }	 x' | D] \ }
 } |  j
 |	 |
 | ƒ 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   (    (    sX   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/pyprof2calltree.pyR3   Š   s&    					c   
      C   s–   |  j  } | j } t j | ƒ \ } } } | 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   (    (    sX   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/pyprof2calltree.pyRJ   ª   s    		(
   R   R   t   __doc__R>   R1   R4   R   R2   R3   RJ   (    (    (    sX   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/pyprof2calltree.pyR   S   s   					 c       	   C   sQ  d }  t  j d |  t j d ƒ } t | _ | j d d d d d d	 d
 d ƒ| j d d d d d d d
 d ƒ| j d d d d d d d
 d ƒ| j d d d d d d d d ƒ| j ƒ  \ } } | j	 } | j
 d k	 ru| j
 g | t j (| j	 sd t j j | j
 ƒ } n  t j ƒ  } z9 y! | j d t j d f ƒ } Wn t k
 rZn XWd t | j ƒ  ƒ } Xn‚ | j d k	 rà| j	 s©d t j j | j ƒ } n  | j | k rÅ| d 7} n  t t j | j ƒ ƒ } n | j ƒ  t j d ƒ | j	 d k	 s| j r2d | GH| j t | d ƒ ƒ n  | j rMd  GH| j ƒ  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(    (    sX   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/pyprof2calltree.pyt   main·   sP    			!	
		c         C   s   t  |  ƒ } | j ƒ  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(    (    sX   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/pyprof2calltree.pyR   ÷   s    c         C   s]   t  |  ƒ } t | t ƒ rL t | d ƒ } z | j | ƒ Wd | j ƒ  Xn | j | ƒ 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   (    (    sX   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/pyprof2calltree.pyR      s    t   __main__(   RM   R   R+   RW   R<   RY   R7   t   __all__t   objectR   R   R(   R   Rk   R   R    R   Re   (    (    (    sX   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/pyprof2calltree.pyt   <module>   s    	'd	@		