Ñò
3mÈKc           @   st  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 k Z d d k Z d d k	 Z	 d d k
 Z
 y d d k Z e Z Wn e j
 o d d k Z n Xd „  Z d Z e i d ƒ Z d d d „  ƒ  YZ d „  Z d	 „  Z d
 d d „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z d d 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
  import sys

  # create a Trace object, telling it what to ignore, and whether to
  # do tracing or line-counting or both.
  tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], trace=0,
                    count=1)
  # run the new command using the given tracer
  tracer.run('main()')
  # make a report, placing output in /tmp
  r = tracer.results()
  r.write_results(show_missing=True, coverdir="/tmp")
iÿÿÿÿNc         C   s   |  i  d t i d ƒ d  S(   Ns	  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.)
-g, --timing          Prefix each line with the time since the program started.
                      Only used while tracing.

Filters, may be repeated multiple times:
--ignore-module=<mod> Ignore the given module(s) and its submodules
                      (if it is a package).  Accepts comma separated
                      list of module names
--ignore-dir=<dir>    Ignore files in the given directory (multiple
                      directories can be joined by os.pathsep).
i    (   t   writet   syst   argv(   t   outfile(    (    s   /usr/lib/python2.6/trace.pyt   usageD   s    *s   #pragma NO COVERs   ^\s*(#.*)?$t   Ignorec           B   s    e  Z d d d  „ Z d „  Z RS(   c         C   sO   | p g  |  _  | p g  |  _ t t i i |  i ƒ |  _ h d d 6|  _ d  S(   Ni   s   <string>(   t   _modst   _dirst   mapt   ost   patht   normpatht   _ignore(   t   selft   modulest   dirs(    (    s   /usr/lib/python2.6/trace.pyt   __init__w   s    c         C   sÿ   |  i  i | ƒ o |  i  | Sxp |  i D]e } | | j o d |  i  | <d St | ƒ } | | |  j o# | | d j o d |  i  | <d Sq) W| d  j o d |  i  | <d Sx: |  i D]/ } | i | t i ƒ o d |  i  | <d Sq» Wd |  i  | <d S(   Ni   t   .i    (	   R   t   has_keyR   t   lent   NoneR   t
   startswithR	   t   sep(   R   t   filenamet
   modulenamet   modt   nt   d(    (    s   /usr/lib/python2.6/trace.pyt   names~   s*    
 "	
 	N(   t   __name__t
   __module__R   R   R   (    (    (    s   /usr/lib/python2.6/trace.pyR   v   s   c         C   s.   t  i i |  ƒ } t  i i | ƒ \ } } | S(   s-   Return a plausible module name for the patch.(   R	   R
   t   basenamet   splitext(   R
   t   baseR   t   ext(    (    s   /usr/lib/python2.6/trace.pyt   modname«   s    c         C   s  t  i i |  ƒ } d } xt t i D]i } t  i i | ƒ } | i | ƒ oA | t | ƒ t  i j o' t | ƒ t | ƒ j o
 | } q‹ 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   (
   R	   R
   t   normcaseR   R   R   R   t   replacet   altsepR    (   R
   t   comparepatht   longestt   dirR!   R   R"   (    (    s   /usr/lib/python2.6/trace.pyt   fullmodname²   s     
 *
t   CoverageResultsc           B   sD   e  Z d d d d d d  „ Z d „  Z e e d d „ Z d „  Z RS(   c         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   t   copyt   countert   calledfuncst   callerst   infileR   t   picklet   loadt   opent   updatet	   __class__t   IOErrort   EOFErrort
   ValueErrorR   t   stderr(   R   R.   R1   R3   R2   R   t   err(    (    s   /usr/lib/python2.6/trace.pyR   Í   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.   R1   R2   t   keyst   get(	   R   t   otherR.   R1   R2   t   other_countst   other_calledfuncst   other_callerst   key(    (    s   /usr/lib/python2.6/trace.pyR7   ç   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  } xs| i ƒ  D]e\ } } | d j o qn | i d	 ƒ o qn | 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. t d | | ƒ } | | | | f | | <qqW| oZ | oS | i ƒ  } | i ƒ  d GHx5 | D]) } | | \ } } } } d | | GHq)Wn |  i ob y5 t i |  i |  i  |  i f t |  i d ƒ d ƒ WqÆt 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	   <doctest 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(   s   .pycs   .pyo(   R1   R>   t   sortR2   R.   R?   t	   iteritemsR   t   endswithR   R	   R
   t   dirnamet   abspathR#   t   existst   makedirsR+   t   find_executable_linenost	   linecachet   getlinest   joint   write_results_filet   intR   R4   t   dumpR6   R9   R   R<   (   R   t   show_missingt   summaryt   coverdirt   callsR   R   t   funcnamet   lastfilet	   lastcfilet   pfilet   pmodt   pfunct   cfilet   cmodt   cfunct   per_filet   linenot	   lines_hitt   sumst   countR*   t   lnotabt   sourcet	   coverpatht   n_hitst   n_linest   percentt   modst   mR=   (    (    s   /usr/lib/python2.6/trace.pyt   write_resultsù   sŽ    

 


 
	
  
 
c         C   sE  y t  | d ƒ } Wn- t j
 o! } t i d | | f IJd	 S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   (   i    i    (   R6   R9   R   R<   t	   enumerateR    t   rx_blankt   matcht   PRAGMA_NOCOVERt
   expandtabst   close(   R   R
   t   linesRf   Rc   R   R=   Rj   Ri   t   it   lineRb   (    (    s   /usr/lib/python2.6/trace.pyRQ   T  s0    
 


N(	   R   R   R   R   R7   t   Truet   FalseRn   RQ   (    (    (    s   /usr/lib/python2.6/trace.pyR,   Ì   s
   		[c   
      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	   co_lnotabt   ordR   Rz   t   co_firstlineno(
   t   codet   strst   linenost   _[1]t   ct   line_incrementst   table_lengtht	   docstringRb   t   li(    (    s   /usr/lib/python2.6/trace.pyt   find_lines_from_codex  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.(   R‡   t	   co_constst
   isinstancet   typest   CodeTypeR7   t
   find_lines(   R~   R   R€   R‚   (    (    s   /usr/lib/python2.6/trace.pyRŒ   ˆ  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   (	   t   tokent   INDENTR6   t   tokenizet   generate_tokenst   readlinet   STRINGt   rangeRu   (   R   R   t
   prev_ttypet   ft   ttypet   tstrt   startt   endRx   t   slinet   scolt   elinet   ecolRw   (    (    s   /usr/lib/python2.6/trace.pyt   find_strings”  s    	  

c         C   st   y t  |  d ƒ i ƒ  } Wn- t j
 o! } t i d |  | f IJh  S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   exec(   R6   t   readR9   R   R<   t   compileRž   RŒ   (   R   t   progR=   R~   R   (    (    s   /usr/lib/python2.6/trace.pyRM   «  s    
t   Tracec        
   B   s•   e  Z d  d  d d d d d d e 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(   i   i    c
   
      C   s:  | |  _  | |  _ t | | ƒ |  _ h  |  _ h  |  _ h  |  _ d |  _ | |  _ h  |  _	 h  |  _
 h  |  _ d |  _ |	 o t i ƒ  |  _ n | o |  i |  _ n‘ | o |  i |  _ nz | o# | o |  i |  _ |  i |  _ nP | o |  i |  _ |  i |  _ n- | o |  i |  _ |  i |  _ n
 d |  _ d S(   sx  
        @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
        @param timing true iff timing information be displayed
        i    i   N(   R3   R   R   t   ignoreR.   t   blabbedt   pathtobasenamet	   donothingt   tracet   _calledfuncst   _callerst   _caller_cacheR   t
   start_timet   timet   globaltrace_trackcallerst   globaltracet   globaltrace_countfuncst   globaltrace_ltt   localtrace_trace_and_countt
   localtracet   localtrace_tracet   localtrace_count(
   R   Re   R©   t
   countfuncst   countcallerst
   ignoremodst
   ignoredirsR3   R   t   timing(    (    s   /usr/lib/python2.6/trace.pyR   ¸  s8    											c         B   s   d 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(   Niÿÿÿÿ(   t   __main__t   __dict__R¨   R   t   settraceR°   t	   threadingR   (   R   t   cmdR¼   t   dict(    (    s   /usr/lib/python2.6/trace.pyt   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(   R   R¨   R   R¾   R°   R¿   (   R   RÀ   t   globalst   locals(    (    s   /usr/lib/python2.6/trace.pyt   runctxø  s     
 


c         O   sZ   d  } |  i p t i |  i ƒ n z | | | Ž  } Wd  |  i p t i d  ƒ n X| S(   N(   R   R¨   R   R¾   R°   (   R   t   funct   argst   kwt   result(    (    s   /usr/lib/python2.6/trace.pyt   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   f_codet   co_filenameR#   R   t   co_nameR¬   t   gct   get_referrerst   hasattrR   R‰   RÁ   t   strt   splitRP   (   R   t   frameR~   R   R   RX   t   clsnameR   R•   t   funcst   _[2]R   t   dictst   _[3]R‚   t   classes(    (    s   /usr/lib/python2.6/trace.pyt   file_module_function_of  s6    			$$$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(   RÜ   t   f_backR«   (   R   RÕ   t   whyt   argt	   this_funct   parent_func(    (    s   /usr/lib/python2.6/trace.pyR¯   =  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á   (    (    s   /usr/lib/python2.6/trace.pyR±   H  s    c         C   s£   | d j o’ | i  } | i i d d ƒ } | oe t | ƒ } | d j	 oH |  i i | | ƒ } | p( |  i o d | | i f GHn |  i	 S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Ý   t   __file__s!    --- modulename: %s, funcname: %sN(
   RÍ   t	   f_globalsR?   R   R#   R¥   R   R©   RÏ   R´   (   R   RÕ   Rß   Rà   R~   R   R   t	   ignore_it(    (    s   /usr/lib/python2.6/trace.pyR²   Q  s    	
c         C   s«   | d j o— | i  i } | i } | | f } |  i i | d ƒ d |  i | <|  i o d t i ƒ  |  i Gn t i i	 | ƒ } d | | t
 i | | ƒ f Gn |  i S(   NRx   i    i   s   %.2fs
   %s(%d): %s(   RÍ   RÎ   t   f_linenoR.   R?   R­   R®   R	   R
   R   RN   t   getlineR´   (   R   RÕ   Rß   Rà   R   Rb   RD   t   bname(    (    s   /usr/lib/python2.6/trace.pyR³   h  s    	 
	c         C   s   | d j ok | i  i } | i } |  i o d t i ƒ  |  i Gn t i i | ƒ } d | | t i	 | | ƒ f Gn |  i
 S(   NRx   s   %.2fs
   %s(%d): %s(   RÍ   RÎ   Ræ   R­   R®   R	   R
   R   RN   Rç   R´   (   R   RÕ   Rß   Rà   R   Rb   Rè   (    (    s   /usr/lib/python2.6/trace.pyRµ   w  s    	
	c         C   sY   | d j oE | i  i } | i } | | f } |  i i | d ƒ d |  i | <n |  i S(   NRx   i    i   (   RÍ   RÎ   Ræ   R.   R?   R´   (   R   RÕ   Rß   Rà   R   Rb   RD   (    (    s   /usr/lib/python2.6/trace.pyR¶   „  s    	$c      
   C   s1   t  |  i d |  i d |  i d |  i d |  i ƒS(   NR3   R   R1   R2   (   R,   R.   R3   R   Rª   R«   (   R   (    (    s   /usr/lib/python2.6/trace.pyt   resultsŒ  s    		(    (    N(   R   R   R   Rz   R   RÂ   RÅ   RÊ   RÜ   R¯   R±   R²   R³   Rµ   R¶   Ré   (    (    (    s   /usr/lib/python2.6/trace.pyR¤   ·  s   1			-							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   exit(   t   msg(    (    s   /usr/lib/python2.6/trace.pyt	   _err_exit’  s    !c         C   sE  d d  k  } |  d  j o t i }  n yP | i  |  d 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 } t } x2| 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 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 o4 x* | i d0 ƒ D] } | i | i ƒ  ƒ q^Wq/n | d1 j o¹ x¯ | i t i ƒ D]› } t i i | ƒ } | i d2 t i i t i d3 d4 t i d5  ƒ ƒ } | i d6 t i i t i d3 d4 t i d5  ƒ ƒ } t i i | ƒ } | i | ƒ q¢Wq/n d p
 t d7 ‚ q/W| o | p | o t d8 ƒ n | p | p | p | p | p t d9 ƒ n | o | o t d: ƒ n | o |	 o t d; ƒ n | o! t | ƒ d j o t d< ƒ n | o2 t d= |	 d> |	 ƒ } | i |
 d
 | d? | ƒn÷ | t _ | d } t i i | ƒ d t i d <t | | d@ | dA | dB | dC | d= |	 d> |	 d | ƒ} y | i dD | f ƒ WnB t  j
 o$ } t dE t i d | f ƒ n t! j
 o n X| i" ƒ  } | p | i |
 d
 | d? | ƒn d  S(F   Niÿÿÿÿi   s   tcrRf:d:msC:lTgt   helpt   versionR©   Re   t   reports	   no-reportRU   s   file=t   missings   ignore-module=s   ignore-dir=s	   coverdir=t	   listfuncst
   trackcallsR»   s   %s: %s
i    s%   Try `%s --help' for more information
s   --helps	   --versions
   trace 2.0
s   -Ts   --trackcallss   -ls   --listfuncss   -gs   --timings   -ts   --traces   -cs   --counts   -rs   --reports   -Rs   --no-reports   -fs   --files   -ms	   --missings   -Cs
   --coverdirs   -ss	   --summarys   --ignore-modulet   ,s   --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 runR3   R   RV   R·   R¸   R¹   Rº   s   execfile(%r)s   Cannot run file %r because: %s(#   t   getoptR   R   R   t   errorR<   R    Rê   Rz   R   t   stdoutRy   RÔ   t   appendt   stripR	   t   pathsepR
   t
   expandvarsR&   RP   t   prefixRî   t   exec_prefixR   t   AssertionErrorRì   R   R,   Rn   R¤   RÂ   R9   t
   SystemExitRé   (   R   Rö   t   optst	   prog_argvRë   R©   Re   Rï   t	   no_reportt   counts_fileRð   t   ignore_modulest   ignore_dirsRV   RU   Rñ   R¸   R»   t   optt   valR   t   sRé   t   prognamet   tR=   (    (    s   /usr/lib/python2.6/trace.pyt   main–  sì    	!   		#	
R¼   (    (    (    (   t   __doc__RN   R	   t   reR   R¿   R®   R   R   RŠ   RÐ   t   cPickleR4   t   ImportErrorR   Rs   R¢   Rq   R   R#   R+   R,   R‡   RŒ   Rž   RM   R¤   Rì   R   R  R   (    (    (    s   /usr/lib/python2.6/trace.pyt   <module>1   s>   
	-5		¬				Û	–