mò
­fIc           @   sG  d  Z  d k Z d k Z d k Z d k Z d k Z d k Z d k Z d k Z d k	 Z	 y d k
 Z
 e
 Z Wn e j
 o d k Z n Xd „  Z d Z e i d ƒ Z d f  d „  ƒ  YZ d „  Z d „  Z d	 f  d
 „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z d f  d „  ƒ  YZ d „  Z d d „ Z e d j o e ƒ  n d S(   sœ  program/module to trace Python program or function execution

Sample use, command line:
  trace.py -c -f counts --ignore-dir '$prefix' spam.py eggs
  trace.py -t --ignore-dir '$prefix' spam.py eggs
  trace.py --trackcalls spam.py eggs

Sample use, programmatically
   # create a Trace object, telling it what to ignore, and whether to
   # do tracing or line-counting or both.
   trace = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], trace=0,
                       count=1)
   # run the new command using the given trace
   trace.run('main()')
   # make a report, telling it where you want output
   r = trace.results()
   r.write_results(show_missing=True)
Nc         C   s   |  i d t i d ƒ d  S(   NsN  Usage: %s [OPTIONS] <file> [ARGS]

Meta-options:
--help                Display this help then exit.
--version             Output version information then exit.

Otherwise, exactly one of the following three options must be given:
-t, --trace           Print each line to sys.stdout before it is executed.
-c, --count           Count the number of times each line is executed
                      and write the counts to <module>.cover for each
                      module executed, in the module's directory.
                      See also `--coverdir', `--file', `--no-report' below.
-l, --listfuncs       Keep track of which functions are executed at least
                      once and write the results to sys.stdout after the
                      program exits.
-T, --trackcalls      Keep track of caller/called pairs and write the
                      results to sys.stdout after the program exits.
-r, --report          Generate a report from a counts file; do not execute
                      any code.  `--file' must specify the results file to
                      read, which must have been created in a previous run
                      with `--count --file=FILE'.

Modifiers:
-f, --file=<file>     File to accumulate counts over several runs.
-R, --no-report       Do not generate the coverage report files.
                      Useful if you want to accumulate over several runs.
-C, --coverdir=<dir>  Directory where the report files.  The coverage
                      report for <package>.<module> is written to file
                      <dir>/<package>/<module>.cover.
-m, --missing         Annotate executable lines that were not executed
                      with '>>>>>> '.
-s, --summary         Write a brief summary on stdout for each file.
                      (Can only be used with --count or --report.)

Filters, may be repeated multiple times:
--ignore-module=<mod> Ignore the given module and its submodules
                      (if it is a package).
--ignore-dir=<dir>    Ignore files in the given directory (multiple
                      directories can be joined by os.pathsep).
i    (   t   outfilet   writet   syst   argv(   R    (    (    t"   /mit/python/lib/python2.4/trace.pyt   usageA   s    s   #pragma NO COVERs   ^\s*(#.*)?$t   Ignorec           B   s    t  Z d  d  d „ Z d „  Z RS(   Nc         C   sQ   | p g  |  _ | p g  |  _ t t i i |  i ƒ |  _ h  d d <|  _	 d  S(   Ns   <string>i   (
   t   modulest   selft   _modst   dirst   _dirst   mapt   ost   patht   normpatht   _ignore(   R   R   R
   (    (    R   t   __init__q   s    c         C   s  |  i i | ƒ o |  i | Sn xv |  i D]k } | | j o d |  i | <d Sn t | ƒ } | | |  j o& | | d j o d |  i | <d Sq, q, W| d  j o d |  i | <d Sn x= |  i
 D]2 } | i | t i ƒ o d |  i | <d SqÇ qÇ Wd |  i | <d S(   Ni   t   .i    (   R   R   t   has_keyt
   modulenameR	   t   modt   lent   nt   filenamet   NoneR   t   dt
   startswithR   t   sep(   R   R   R   R   R   R   (    (    R   t   namesx   s*    
 "
 (   t   __name__t
   __module__R   R   R   (    (    (    R   R   p   s   c         C   s.   t  i i |  ƒ } t  i i | ƒ \ } } | S(   s-   Return a plausible module name for the patch.N(   R   R   t   basenamet   baset   splitextR   t   ext(   R   R#   R!   R   (    (    R   t   modname¥   s     c         C   sç   d } xe t i D]Z } |  i | ƒ oD |  t | ƒ t i i j o' t | ƒ t | ƒ j o
 | } qj q q W| o |  t | ƒ d } n |  } | i	 t i d ƒ } t i
 o | i	 t i
 d ƒ } n t i i | ƒ \ } } | S(   s,   Return a plausible module name for the path.t    i   R   N(   t   longestR   R   t   dirR   R   R   R   R!   t   replacet   altsepR"   R   R#   (   R   R   R#   R!   R&   R'   (    (    R   t   fullmodname¬   s     
 -
t   CoverageResultsc           B   sD   t  Z d  d  d  d  d  d „ Z d „  Z e e d  d „ Z d „  Z RS(   Nc         C   sK  | |  _  |  i  d  j o h  |  _  n |  i  i ƒ  |  _ | |  _ |  i d  j o h  |  _ n |  i i ƒ  |  _ | |  _ |  i d  j o h  |  _ n |  i i ƒ  |  _ | |  _ | |  _ |  i oƒ yD t	 i
 t |  i d ƒ ƒ \ } } } |  i |  i | | | ƒ ƒ WqGt t t f j
 o# } t i d |  i | f IJqGXn d  S(   Nt   rbs   Skipping counts file %r: %s(   t   countsR   R   t   copyt   countert   calledfuncst   callerst   infileR    t   picklet   loadt   opent   updatet	   __class__t   IOErrort   EOFErrort
   ValueErrort   errR   t   stderr(   R   R-   R0   R2   R1   R    R;   (    (    R   R   Å   s(    					
$ c   	      C   s±   |  i } |  i } |  i } | i } | i } | i } x2 | i ƒ  D]$ } | i
 | d ƒ | | | | <qC Wx | i ƒ  D] } d | | <qx Wx | i ƒ  D] } d | | <q™ Wd S(   s.   Merge in the data from another CoverageResultsi    i   N(   R   R-   R0   R1   t   othert   other_countst   other_calledfuncst   other_callerst   keyst   keyt   get(	   R   R=   R>   R?   R@   R0   RB   R-   R1   (    (    R   R6   ß   s      						 "  c         C   sÃ  |  i oO Hd GH|  i i ƒ  } | i ƒ  x- | D]! \ } } } d | | | f GHq0 Wn |  i oÃ Hd GH|  i i ƒ  } | i ƒ  d } } x— | D]‹ \ \ } } }
 \ } } } | | j o Hd G| Gd GH| } d } n | | j o  | | j o d G| GH| } n d | |
 | | f GHq“ Wn h  }	 xN |  i i ƒ  D]= \ } } |	 i | h  ƒ } |	 | <|  i | | f | | <q<Wh  } xl|	 i ƒ  D]^\ } } | d j o qn | i d	 ƒ p | i d
 ƒ o | d  } n | d j o. t i i t i i | ƒ ƒ } t! | ƒ } n7 | } t i i" | ƒ p t i# | ƒ n t$ | ƒ } | o t& | ƒ } n h  } t( i) | ƒ } t i i+ | | d ƒ } |  i- | | | | ƒ \ } } | o5 | o. t1 d | | ƒ } | | | | f | | <qqW| oZ | oS | i ƒ  } | i ƒ  d GHx5 | D]) } | | \ } } } } d | | GHq"Wn |  i5 ob y5 t6 i7 |  i |  i |  i f t8 |  i5 d ƒ d ƒ Wq¿t9 j
 o } t; i< d | IJq¿Xn d S(   s!   
        @param coverdir
        s   functions called:s*   filename: %s, modulename: %s, funcname: %ss   calling relationships:R%   s   ***s     -->s       %s.%s -> %s.%ss   <string>s   .pycs   .pyoiÿÿÿÿs   .coverid   s   lines   cov%   module   (path)s   %5d   %3d%%   %s   (%s)t   wbi   s"   Can't save counts files because %sN(=   R   R0   RA   t   callst   sortR   R   t   funcnameR1   t   lastfilet	   lastcfilet   pfilet   pmodt   pfunct   cfilet   cmodt   cfunct   per_fileR-   t   linenoRC   t	   lines_hitt   sumst	   iteritemst   countt   endswitht   coverdirR   R   R   t   dirnamet   abspathR'   R$   t   existst   makedirsR*   t   show_missingt   find_executable_linenost   lnotabt	   linecachet   getlinest   sourcet   joint	   coverpatht   write_results_filet   n_hitst   n_linest   summaryt   intt   percentt   modst   mR    R3   t   dumpR5   R8   R;   R   R<   (   R   R\   Rg   RW   R   R;   RN   RR   RO   RP   RL   RK   Ri   RI   R   Ra   RQ   Rc   RM   RJ   Rf   RS   RU   Rj   Re   RE   Rk   RG   R'   R^   RH   (    (    R   t   write_resultsñ   sŠ     

 


 
	
   
 
c         C   sH  y t  | d ƒ }	 Wn0 t j
 o$ } t i d | | f IJd
 Sn Xd } d } xß t	 | ƒ D]Ñ \ } } | d }
 |
 | j o- |	 i d | |
 ƒ | d 7} | d 7} nh t i | ƒ o |	 i d ƒ nG |
 | j o, t | | j o |	 i d ƒ | d 7} n |	 i d ƒ |	 i | | i d ƒ ƒ q_ W|	 i ƒ  | | f S(   s'   Return a coverage results file in path.t   ws2   trace: Could not open %r for writing: %s- skippingi    i   s   %5d: s          s   >>>>>> i   N(   i    i    (   R5   R   R    R8   R;   R   R<   Rf   Re   t	   enumeratet   linest   it   lineRQ   RR   R   t   rx_blankt   matchR^   t   PRAGMA_NOCOVERt
   expandtabst   close(   R   R   Rp   R^   RR   Re   R;   Rq   Rf   R    RQ   Rr   (    (    R   Rd   J  s0     	 


(	   R   R   R   R   R6   t   Truet   FalseRm   Rd   (    (    (    R   R+   Ä   s   	Yc   
      C   s’   h  } g  } |  i d d d … D] } | t | ƒ q! ~ } t | ƒ }	 t	 } |  i } x3 | D]+ } | | 7} | | j o d | | <q_ q_ W| S(   s:   Return dict where keys are lines in the line number table.i   Ni   (   t   linenost   _[1]t   codet	   co_lnotabt   ct   ordt   line_incrementsR   t   table_lengthRy   t	   docstringt   co_firstlinenoRQ   t   lit   strs(
   R|   R…   R~   R‚   Rz   R„   R{   RQ   R€   R   (    (    R   t   find_lines_from_coden  s     7	 
c         C   sT   t  |  | ƒ } x> |  i D]3 } t | t i ƒ o | i	 t
 | | ƒ ƒ q q W| S(   s<   Return lineno dict for all code objects reachable from code.N(   R†   R|   R…   Rz   t	   co_constsR~   t
   isinstancet   typest   CodeTypeR6   t
   find_lines(   R|   R…   R~   Rz   (    (    R   R‹   ~  s     
 c         C   sÃ   h  } t i } t |  ƒ } x— t i | i	 ƒ D]ƒ \ }	 } } } }
 |	 t i j oX | t i j oD | \ } } | \ } } x) t | | d ƒ D] } d | | <q Wq« n |	 } q. W| i ƒ  | S(   sÄ   Return a dict of possible docstring positions.

    The dict maps line numbers to strings.  There is an entry for
    line that contains only a string or a part of a triple-quoted
    string.
    i   N(   R   t   tokent   INDENTt
   prev_ttypeR5   R   t   ft   tokenizet   generate_tokenst   readlinet   ttypet   tstrt   startt   endRr   t   STRINGt   slinet   scolt   elinet   ecolt   rangeRq   Rw   (   R   R–   R   R   RŽ   R›   Rš   R™   R•   R“   Rr   Rq   R”   R˜   (    (    R   t   find_stringsŠ  s      	  

c         C   sw   y t  |  d ƒ i ƒ  } Wn0 t j
 o$ } t i d |  | f IJh  Sn Xt | |  d ƒ } t
 |  ƒ } t | | ƒ S(   sA   Return dict where keys are line numbers in the line number table.t   rUs%   Not printing coverage data for %r: %st   execN(   R5   R   t   readt   progR8   R;   R   R<   t   compileR|   R   R…   R‹   (   R   R|   R;   R…   R¡   (    (    R   R]   ¡  s     	t   Tracec        	   B   s’   t  Z d d d d f  f  d  d  d „ Z d „  Z d  d  d „ Z d „  Z d „  Z d „  Z d	 „  Z	 d
 „  Z
 d „  Z d „  Z d „  Z d „  Z RS(   Ni   i    c	   	      C   s  | |  _  | |  _ t | | ƒ |  _ h  |  _ h  |  _ h  |  _	 d |  _
 | |  _ h  |  _ h  |  _ h  |  _ | o |  i |  _ n‘ | o |  i |  _ nz | o# | o |  i |  _ |  i |  _ nP | o |  i |  _ |  i |  _ n- | o |  i |  _ |  i |  _ n
 d |  _
 d S(   s9  
        @param count true iff it should count number of times each
                     line is executed
        @param trace true iff it should print out each line that is
                     being counted
        @param countfuncs true iff it should just output a list of
                     (filename, modulename, funcname,) for functions
                     that were called at least once;  This overrides
                     `count' and `trace'
        @param ignoremods a list of the names of modules to ignore
        @param ignoredirs a list of the names of directories to ignore
                     all of the (recursive) contents of
        @param infile file from which to read stored counts to be
                     added into the results
        @param outfile file in which to write the results
        i    i   N(   R2   R   R    R   t
   ignoremodst
   ignoredirst   ignoreR-   t   blabbedt   pathtobasenamet	   donothingt   tracet   _calledfuncst   _callerst   _caller_cachet   countcallerst   globaltrace_trackcallerst   globaltracet
   countfuncst   globaltrace_countfuncsRU   t   globaltrace_ltt   localtrace_trace_and_countt
   localtracet   localtrace_tracet   localtrace_count(	   R   RU   Rª   R±   R®   R¤   R¥   R2   R    (    (    R   R   ®  s4     										c         B   s~   d  k  } | i } |  i p$ e i |  i ƒ e i |  i ƒ n z | | | UWd  |  i p e i d  ƒ e i d  ƒ n Xd  S(   N(   t   __main__t   __dict__t   dictR   R©   R   t   settraceR°   t	   threadingt   cmdR   (   R   R½   Rº   R¸   (    (    R   t   runÜ  s    		
 
c         B   sš   | d  j o
 h  } n | d  j o
 h  } n |  i p$ e i |  i ƒ e i |  i ƒ n z | | | UWd  |  i p e i d  ƒ e i d  ƒ n Xd  S(   N(
   t   globalsR   t   localsR   R©   R   R»   R°   R¼   R½   (   R   R½   R¿   RÀ   (    (    R   t   runctxé  s     
 

 
c         O   sZ   d  } |  i p t i |  i ƒ n z | | | Ž  } Wd  |  i p t i d  ƒ n X| S(   N(
   R   t   resultR   R©   R   R»   R°   t   funct   argst   kw(   R   RÃ   RÄ   RÅ   RÂ   (    (    R   t   runfuncö  s    
 
c         C   sø  | i } | i } | o t | ƒ } n d  } | i } d  } | |  i j o) |  i | d  j	 o |  i | } q¨n2d  |  i | <g  } t i | ƒ D]! }
 t |
 d ƒ o | |
 q˜ q˜ ~ } t | ƒ d j oÓ g  } t i | d ƒ D]! } t | t ƒ o | | qí qí ~ }	 t |	 ƒ d j oz g  } t i |	 d ƒ D]! } t | d ƒ o | | qBqB~ } t | ƒ d j o! t | d ƒ } | |  i | <q¤q¨n | d  j	 o6 | i d ƒ d } d i | ƒ } d | | f } n | | | f S(   Nt   func_doci   i    t	   __bases__R   s   %s.%s(   t   framet   f_codeR|   t   co_filenameR   R$   R   R   t   co_nameRG   t   clsnameR   R­   R{   t   gct   get_referrersR   t   hasattrt   funcsR   R   Rˆ   Rº   t   dictsR~   t   classest   strt   splitRb   (   R   RÉ   RÑ   R|   RÍ   R   R   R~   R   RÒ   R   RG   R{   RÓ   (    (    R   t   file_module_function_of  s0    			>BBc         C   sI   | d j o8 |  i | ƒ } |  i | i ƒ } d |  i | | f <n d S(   sk   Handler for call events.

        Adds information about who called who to the self._callers dict.
        t   calli   N(   t   whyR   RÖ   RÉ   t	   this_funct   f_backt   parent_funcR¬   (   R   RÉ   RØ   t   argRÛ   RÙ   (    (    R   R¯   .  s
     c         C   s1   | d j o  |  i | ƒ } d |  i | <n d S(   so   Handler for call events.

        Adds (filename, modulename, funcname) to the self._calledfuncs dict.
        R×   i   N(   RØ   R   RÖ   RÉ   RÙ   R«   (   R   RÉ   RØ   RÜ   RÙ   (    (    R   R²   9  s     c         C   sš   | d j o‰ | i } | i } | oh t | ƒ } | d j	 oK |  i
 i | | ƒ } | p+ |  i o d | | i f GHn |  i SqŠ q’ q– d Sn d S(   s’   Handler for call events.

        If the code block being entered is to be ignored, returns `None',
        else returns self.localtrace.
        R×   s!    --- modulename: %s, funcname: %sN(   RØ   RÉ   RÊ   R|   RË   R   R$   R   R   R   R¦   R   t	   ignore_itRª   RÌ   Rµ   (   R   RÉ   RØ   RÜ   R|   RÝ   R   R   (    (    R   R³   B  s     		
c         C   sˆ   | d j ot | i i } | i } | | f } |  i	 i
 | d ƒ d |  i	 | <t i i | ƒ } d | | t i | | ƒ f Gn |  i S(   NRr   i    i   s
   %s(%d): %s(   RØ   RÉ   RÊ   RË   R   t   f_linenoRQ   RB   R   R-   RC   R   R   R    t   bnameR_   t   getlineRµ   (   R   RÉ   RØ   RÜ   R   Rß   RQ   RB   (    (    R   R´   Y  s    	 !c         C   s\   | d j oH | i i } | i } t i i	 | ƒ } d | | t i | | ƒ f Gn |  i S(   NRr   s
   %s(%d): %s(   RØ   RÉ   RÊ   RË   R   RÞ   RQ   R   R   R    Rß   R_   Rà   R   Rµ   (   R   RÉ   RØ   RÜ   R   Rß   RQ   (    (    R   R¶   f  s    	!c         C   sY   | d j oE | i i } | i } | | f } |  i	 i
 | d ƒ d |  i	 | <n |  i S(   NRr   i    i   (   RØ   RÉ   RÊ   RË   R   RÞ   RQ   RB   R   R-   RC   Rµ   (   R   RÉ   RØ   RÜ   R   RQ   RB   (    (    R   R·   q  s    	$c      
   C   s1   t  |  i d |  i d |  i d |  i d |  i ƒS(   NR2   R    R0   R1   (   R+   R   R-   R2   R    R«   R¬   (   R   (    (    R   t   resultsy  s    		(   R   R   R   R   R¾   RÁ   RÆ   RÖ   R¯   R²   R³   R´   R¶   R·   Rá   (    (    (    R   R£   ­  s   !.			-							c         C   s2   t  i i d t  i d |  f ƒ t  i d ƒ d  S(   Ns   %s: %s
i    i   (   R   R<   R   R   t   msgt   exit(   Râ   (    (    R   t	   _err_exit  s    !c         C   sì  d  k  } |  d  j o t i }  n yM | i  |  d d d d d d d d d	 d
 d d d d d d g ƒ \ } } Wna | i j
 oR } t i i	 d t i d | f ƒ t i i	 d t i d ƒ t i
 d ƒ n Xd }
 d } d } d } d  } d } g  } g  } d  } d } t } t } xë| D]ã\ }	 } |	 d j o t t i ƒ t i
 d ƒ n |	 d j o! t i i	 d ƒ t i
 d ƒ n |	 d j p |	 d j o t } q#n |	 d j p |	 d j o t } q#n |	 d j p |	 d j o d }
 q#n |	 d j p |	 d j o d } q#n |	 d j p |	 d  j o d } q#n |	 d! j p |	 d" j o d } q#n |	 d# j p |	 d$ j o | } q#n |	 d% j p |	 d& j o d } q#n |	 d' j p |	 d( j o | } q#n |	 d) j p |	 d* j o d } q#n |	 d+ j o | i | ƒ q#n |	 d, j o¹ x¯ | i t i  ƒ D]› } t i" i# | ƒ } | i$ d- t i" i% t i& d. d/ t i' d0  ƒ ƒ } | i$ d1 t i" i% t i( d. d/ t i' d0  ƒ ƒ } t i" i) | ƒ } | i | ƒ qOWq#n d p
 t* d2 ‚ q#W| o | p |
 o t+ d3 ƒ n | p |
 p | p | p | p t+ d4 ƒ n | o | o t+ d5 ƒ n | o | o t+ d6 ƒ n | o! t, | ƒ d j o t+ d7 ƒ n | o2 t- d8 | d9 | ƒ } | i/ | d	 | d: | ƒnñ | t _ | d } t i" i | ƒ d t i" d <t1 | |
 d; | d< | d= | d> | d8 | d9 | ƒ} y | i3 d? | f ƒ WnB t4 j
 o$ } t+ d@ t i d | f ƒ n t6 j
 o n X| i. ƒ  } | p | i/ | d	 | d: | ƒn d  S(A   Ni   s   tcrRf:d:msC:lTt   helpt   versionRª   RU   t   reports	   no-reportRg   s   file=t   missings   ignore-module=s   ignore-dir=s	   coverdir=t	   listfuncst
   trackcallss   %s: %s
i    s%   Try `%s --help' for more information
s   --helps	   --versions
   trace 2.0
s   -Ts   --trackcallss   -ls   --listfuncss   -ts   --traces   -cs   --counts   -rs   --reports   -Rs   --no-reports   -fs   --files   -ms	   --missings   -Cs
   --coverdirs   -ss	   --summarys   --ignore-modules   --ignore-dirs   $prefixt   libt   pythoni   s   $exec_prefixs   Should never get heres8   cannot specify both --listfuncs and (--trace or --count)sL   must specify one of --trace, --count, --report, --listfuncs, or --trackcallss,   cannot specify both --report and --no-reports   --report requires a --files   missing name of file to runR2   R    RW   R±   R®   R¤   R¥   s   execfile(%r)s   Cannot run file %r because: %s(7   t   getoptR   R   R   t   optst	   prog_argvt   errorRâ   R<   R   Rã   Rª   RU   Rç   t	   no_reportt   counts_fileRè   t   ignore_modulest   ignore_dirsRW   Rg   Ry   Ré   R®   t   optt   valR   t   stdoutRx   t   appendRÕ   R   t   pathsept   sR   t
   expandvarsR(   Rb   t   prefixRæ   t   exec_prefixR   t   AssertionErrorRä   R   R+   Rá   Rm   t   prognameR£   t   tR¾   R8   R;   t
   SystemExit(   R   R;   Rñ   Rï   Rö   R   Râ   Rí   Rô   Rõ   Rª   Rè   Rÿ   Rá   Rç   Ré   R®   RU   RW   Rò   Rg   Rú   Ró   Rî   (    (    R   t   mainƒ  sÔ    	=!  		#	
R¸   (   t   __doc__R_   R   t   reR   R¼   RŒ   R   R‰   RÎ   t   cPickleR3   t   ImportErrorR   Ru   R¢   Rs   R   R$   R*   R+   R†   R‹   R   R]   R£   Rä   R   R  R   (   Rä   RÎ   R_   R*   R]   R£   R‹   R†   R   R   R  R+   R  R   R   R  R‰   R   R$   R   R¼   RŒ   Ru   Rs   R3   (    (    R   t   ?/   s<   										
	*5		ª				Ò	