
bc           @   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 l Z d d l Z d d l	 Z	 d d l
 Z
 y d d l Z e Z Wn e k
 r d d l Z n Xy d d l Z Wn# e k
 r e j Z d   Z n Xd   Z d   Z d   Z d Z e j 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 k r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")
iNc           C   s   t  j d   d  S(   N(   t   syst   settracet   None(    (    (    s   /usr/lib/python2.7/trace.pyt   _unsettraceH   s    c         C   s   t  j |   t j |   d  S(   N(   t	   threadingR   R    (   t   func(    (    s   /usr/lib/python2.7/trace.pyt	   _settraceK   s    c           C   s   t  j d   t j d   d  S(   N(   R    R   R   R   (    (    (    s   /usr/lib/python2.7/trace.pyR   O   s    c         C   s   |  j  d t j 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   writeR    t   argv(   t   outfile(    (    s   /usr/lib/python2.7/trace.pyt   usageS   s    *s   #pragma NO COVERs   ^\s*(#.*)?$t   Ignorec           B   s    e  Z d d d   Z d   Z RS(   c         C   sM   | p	 g  |  _  | p g  |  _ t t j j |  j  |  _ i d d 6|  _ d  S(   Ni   s   <string>(   t   _modst   _dirst   mapt   ost   patht   normpatht   _ignore(   t   selft   modulest   dirs(    (    s   /usr/lib/python2.7/trace.pyt   __init__   s    c         C   s   | |  j  k r |  j  | Sxk |  j D]` } | | k rG d |  j  | <d St |  } | | |  k r$ | | d k r$ d |  j  | <d Sq$ W| d  k r d |  j  | <d Sx8 |  j D]- } | j | t j  r d |  j  | <d Sq Wd |  j  | <d S(   Ni   t   .i    (   R   R   t   lenR   R   t
   startswithR   t   sep(   R   t   filenamet
   modulenamet   modt   nt   d(    (    s   /usr/lib/python2.7/trace.pyt   names   s&     N(   t   __name__t
   __module__R   R   R    (    (    (    s   /usr/lib/python2.7/trace.pyR      s   c         C   s.   t  j j |   } t  j j |  \ } } | S(   s-   Return a plausible module name for the patch.(   R   R   t   basenamet   splitext(   R   t   baseR   t   ext(    (    s   /usr/lib/python2.7/trace.pyt   modname   s    c         C   s   t  j j |   } d } xo t j D]d } t  j j |  } | j |  r" | t |  t  j k r" t |  t |  k r | } q q" q" W| r |  t |  d } n |  } t  j j |  \ } } | j t  j d  } t  j	 r | j t  j	 d  } n  t  j j
 |  \ } } | j d  S(   s,   Return a plausible module name for the path.t    i   R   (   R   R   t   normcaseR    R   R   R   t
   splitdrivet   replacet   altsepR$   t   lstrip(   R   t   comparepatht   longestt   dirR%   t   driveR   R&   (    (    s   /usr/lib/python2.7/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   sA  | |  _  |  j  d  k r$ i  |  _  n  |  j  j   |  _ | |  _ |  j d  k rZ i  |  _ n  |  j j   |  _ | |  _ |  j d  k r i  |  _ n  |  j j   |  _ | |  _ | |  _ |  j r=yD t j	 t
 |  j d   \ } } } |  j |  j | | |   Wq=t t t f k
 r9} t j d |  j | f IJq=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   R5   R8   R:   R9   R	   t   err(    (    s   /usr/lib/python2.7/trace.pyR      s*    						$ 
c   	      C   s   |  j  } |  j } |  j } | j  } | j } | j } x2 | j   D]$ } | j | d  | | | | <qC Wx | j   D] } d | | <qx Wx | j   D] } d | | <q Wd S(   s.   Merge in the data from another CoverageResultsi    i   N(   R5   R8   R9   t   keyst   get(	   R   t   otherR5   R8   R9   t   other_countst   other_calledfuncst   other_callerst   key(    (    s   /usr/lib/python2.7/trace.pyR>      s    						"c         C   s  |  j  rW Hd GH|  j  j   } | j   x, | D]! \ } } } d | | | f GHq/ Wn  |  j rHd GH|  j j   } | j   d } }	 x | D] \ \ }
 } } \ } } } |
 | k r Hd G|
 Gd GH|
 } d }	 n  | |
 k r|	 | k rd G| GH| }	 n  d | | | | f GHq Wn  i  } xN |  j j   D]= \ } } | j | i   } | | <|  j | | f | | <q3Wi  } x^| j   D]P\ } } | d k rqn  | j d	  rqn  | j d  r| d  } n  | d k rt
 j j t
 j j |   } t |  } n4 | } t
 j j |  s7t
 j |  n  t |  } | rXt |  } n i  } t j |  } t
 j j | | d  } |  j | | | |  \ } } | r| rd | | } | | | | f | | <qqW| r9| r9| j   } | j   d GHx4 | D]) } | | \ } } } } d | | GHq	Wn  |  j ry5 t j |  j |  j  |  j f t |  j d  d  Wqt k
 r} t j 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   .pyois   .coverid   s   lines   cov%   module   (path)s   %5d   %3d%%   %s   (%s)t   wbi   s"   Can't save counts files because %sN(   s   .pycs   .pyo(   R8   RE   t   sortR9   R5   RF   t	   iteritemsR   t   endswithR   R   R   t   dirnamet   abspathR'   t   existst   makedirsR2   t   find_executable_linenost	   linecachet   getlinest   joint   write_results_fileR	   R;   t   dumpR=   R@   R    RC   (   R   t   show_missingt   summaryt   coverdirt   callsR   R   t   funcnamet   lastfilet	   lastcfilet   pfilet   pmodt   pfunct   cfilet   cmodt   cfunct   per_filet   linenot	   lines_hitt   sumst   countR0   t   lnotabt   sourcet	   coverpatht   n_hitst   n_linest   percentt   modst   mRD   (    (    s   /usr/lib/python2.7/trace.pyt   write_results
  s    	
	

%			
	c         C   s<  y t  | d  } Wn+ t k
 r@ } t j d | | f IJd	 SXd } d } x t |  D] \ }	 }
 |	 d } | | k r | j d | |  | d 7} | d 7} nb t j |
  r | j d  nC | | k r t | |	 k r | j d  | d 7} n | j d  | j | |	 j	 d   qZ W| j
   | | f S(
   s'   Return a coverage results file in path.t   ws3   trace: Could not open %r for writing: %s - skippingi    i   s   %5d: s          s   >>>>>> i   (   i    i    (   R=   R@   R    RC   t	   enumerateR   t   rx_blankt   matcht   PRAGMA_NOCOVERt
   expandtabst   close(   R   R   t   linesRl   Ri   R	   RD   Rp   Ro   t   it   lineRh   (    (    s   /usr/lib/python2.7/trace.pyRX   e  s.    



N(	   R!   R"   R   R   R>   t   Truet   FalseRt   RX   (    (    (    s   /usr/lib/python2.7/trace.pyR3      s
   		[c         C   sC   i  } x6 t  j |   D]% \ } } | | k r d | | <q q W| S(   s:   Return dict where keys are lines in the line number table.i   (   t   dist   findlinestarts(   t   codet   strst   linenost   _Rh   (    (    s   /usr/lib/python2.7/trace.pyt   find_lines_from_code  s
    c         C   sO   t  |  |  } x9 |  j D]. } t j |  r | j t | |   q q W| S(   s<   Return lineno dict for all code objects reachable from code.(   R   t	   co_constst   inspectt   iscodeR>   t
   find_lines(   R   R   R   t   c(    (    s   /usr/lib/python2.7/trace.pyR     s
    c         C   s   i  } t  j } t |   } x t j | j  D] \ } } } } } | t  j k r | t  j k r | \ }	 }
 | \ } } x( t |	 | d  D] } d | | <q Wq n  | } q. W| j   | 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   INDENTR=   t   tokenizet   generate_tokenst   readlinet   STRINGt   rangeR{   (   R   R   t
   prev_ttypet   ft   ttypet   tstrt   startt   endR~   t   slinet   scolt   elinet   ecolR}   (    (    s   /usr/lib/python2.7/trace.pyt   find_strings  s    	(

c         C   sr   y t  |  d  j   } Wn+ t k
 rF } t j d |  | f IJi  SXt | |  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(   R=   t   readR@   R    RC   t   compileR   R   (   R   t   progRD   R   R   (    (    s   /usr/lib/python2.7/trace.pyRT     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 | |  |  _ i  |  _ i  |  _ i  |  _ d |  _ | |  _ i  |  _	 i  |  _
 i  |  _ d |  _ |	 r t j   |  _ n  | r |  j |  _ n | r |  j |  _ nr | r | r |  j |  _ |  j |  _ nK | r |  j |  _ |  j |  _ n* | r |  j |  _ |  j |  _ 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(   R:   R	   R   t   ignoreR5   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   Rk   R   t
   countfuncst   countcallerst
   ignoremodst
   ignoredirsR:   R	   t   timing(    (    s   /usr/lib/python2.7/trace.pyR     s8    											c         C   s,   d d  l  } | j } |  j | | |  d  S(   Ni(   t   __main__t   __dict__t   runctx(   R   t   cmdR   t   dict(    (    s   /usr/lib/python2.7/trace.pyt   run  s    	c         B   sl   | d  k r i  } n  | d  k r* i  } n  |  j sC e |  j  n  z | | | UWd  |  j sg e   n  Xd  S(   N(   R   R   R   R   R   (   R   R   t   globalst   locals(    (    s   /usr/lib/python2.7/trace.pyR     s     	 			c         O   sV   d  } |  j s" t j |  j  n  z | | |   } Wd  |  j sQ t j d   n  X| S(   N(   R   R   R    R   R   (   R   R   t   argst   kwt   result(    (    s   /usr/lib/python2.7/trace.pyt   runfunc  s    		c         C   s  | j  } | j } | r' t |  } n d  } | j } d  } | |  j k rq |  j | d  k	 rr|  j | } qrnd  |  j | <g  t j |  D] } t j	 |  r | ^ q } t
 |  d k rrg  t j | d  D] }	 t |	 t  r |	 ^ q }
 t
 |
  d k rrg  t j |
 d  D] } t | d  r| ^ q} t
 |  d k ro| d j } | |  j | <qoqrn  | d  k	 rd | | f } n  | | | f S(   Ni   i    t	   __bases__s   %s.%s(   t   f_codet   co_filenameR'   R   t   co_nameR   t   gct   get_referrersR   t
   isfunctionR   t
   isinstanceR   t   hasattrR!   (   R   t   frameR   R   R   R^   t   clsnameR   t   funcsR   t   dictsR   t   classes(    (    s   /usr/lib/python2.7/trace.pyt   file_module_function_of  s2    			c         C   sG   | d k rC |  j  |  } |  j  | j  } d |  j | | 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.7/trace.pyR   :  s    c         C   s/   | d k r+ |  j  |  } d |  j | <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.7/trace.pyR   E  s    c         C   s   | d k r | j  } | j j d d  } | r t |  } | d k	 r |  j j | |  } | s |  j r d | | j f GHn  |  j	 Sq 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_globalsRF   R   R'   R   R    R   R   R   (   R   R   R   R   R   R   R   t	   ignore_it(    (    s   /usr/lib/python2.7/trace.pyR   N  s    		c         C   s   | d k r | j  j } | j } | | f } |  j j | d  d |  j | <|  j rn d t j   |  j Gn  t j j	 |  } d | | t
 j | |  f Gn  |  j S(   NR~   i    i   s   %.2fs
   %s(%d): %s(   R   R   t   f_linenoR5   RF   R   R   R   R   R#   RU   t   getlineR   (   R   R   R   R   R   Rh   RK   t   bname(    (    s   /usr/lib/python2.7/trace.pyR   e  s    	 		c         C   s{   | d k rt | j  j } | j } |  j rB d t j   |  j Gn  t j j |  } d | | t j	 | |  f Gn  |  j
 S(   NR~   s   %.2fs
   %s(%d): %s(   R   R   R   R   R   R   R   R#   RU   R   R   (   R   R   R   R   R   Rh   R   (    (    s   /usr/lib/python2.7/trace.pyR   t  s    			c         C   sW   | d k rP | j  j } | j } | | f } |  j j | d  d |  j | <n  |  j S(   NR~   i    i   (   R   R   R   R5   RF   R   (   R   R   R   R   R   Rh   RK   (    (    s   /usr/lib/python2.7/trace.pyR     s    	#c      
   C   s1   t  |  j d |  j d |  j d |  j d |  j S(   NR:   R	   R8   R9   (   R3   R5   R:   R	   R   R   (   R   (    (    s   /usr/lib/python2.7/trace.pyt   results  s    		(    (    N(   R!   R"   R   R   R   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s   /usr/lib/python2.7/trace.pyR     s   1			)							c         C   s2   t  j j d t  j d |  f  t  j d  d  S(   Ns   %s: %s
i    i   (   R    RC   R   R   t   exit(   t   msg(    (    s   /usr/lib/python2.7/trace.pyt	   _err_exit  s    !c         C   sL  d d  l  } |  d  k r$ t j }  n  yP | j  |  d d d d d d d d	 d
 d d d d d d d d g  \ } } Wn_ | j k
 r } t j j d t j d | f  t j j d t j d  t j d  n Xd } d } d } d } d  }	 d }
 g  } g  } d  } d } t } t } t } x
| D]\ } } | d k r`t	 t j
  t j d  n  | d k rt j
 j d  t j d  n  | d k s| d k rt } q+n  | d k s| d k rt } q+n  | d k s| d k rt } q+n  | d k s| d  k rd } q+n  | d! k s4| d" k r@d } q+n  | d# k sX| d$ k rdd } q+n  | d% k s|| d& k rd } q+n  | d' k s| d( k r| }	 q+n  | d) k s| d* k rd }
 q+n  | d+ k s| d, k r| } q+n  | d- k s| d. k rd } q+n  | d/ k rWx* | j d0  D] } | j | j    q4Wq+n  | d1 k rx | j t j  D] } t j j |  } | j d2 t j j t j d3 d4 t j d5    } | j d6 t j j t j d3 d4 t j d5    } t j j |  } | j |  qvWq+n  d s+t d7   q+W| rP| sC| rPt d8  n  | pk| pk| pk| pk| s{t d9  n  | r| rt d:  n  | r|	 rt d;  n  | rt |  d k rt d<  n  | r
t d= |	 d> |	  } | j |
 d
 | d? | n>| t _ | d } t j j |  d t j d <t | | d@ | dA | dB | dC | d= |	 d> |	 d | } yf t |   } t  | j!   | dD  } Wd  QXi | dE 6dF dG 6d  dH 6d  dI 6} | j" | | |  Wn> t# k
 r	} t dJ t j d | f  n t$ k
 rn X| j%   } | sH| j |
 d
 | d? | n  d  S(K   Nii   s   tcrRf:d:msC:lTgt   helpt   versionR   Rk   t   reports	   no-reportR[   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 runR:   R	   R\   R   R   R   R   R   R   R   R!   t   __package__t
   __cached__s   Cannot run file %r because: %s(&   t   getoptR   R    R   t   errorRC   R   R   R   R
   t   stdoutR   t   splitt   appendt   stripR   t   pathsepR   t
   expandvarsR+   RW   t   prefixR   t   exec_prefixR   t   AssertionErrorR   R   R3   Rt   R   R=   R   R   R   R@   t
   SystemExitR   (   R   R   t   optst	   prog_argvR   R   Rk   R   t	   no_reportt   counts_fileR   t   ignore_modulest   ignore_dirsR\   R[   R   R   R   t   optt   valR   t   sR   t   prognamet   tt   fpR   t   globsRD   (    (    s   /usr/lib/python2.7/trace.pyt   main  s    	!			

R   (    (    (    (#   t   __doc__RU   R   t   reR    R   R   R   R   R   R   t   cPickleR;   t   ImportErrorR   R   R   R   R
   Ry   R   Rw   R   R'   R2   R3   R   R   R   RT   R   R   R   R  R!   (    (    (    s   /usr/lib/python2.7/trace.pyt   <module>1   sL   
				-5			
				