ó
‘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 l Z d d l m	 Z	 d d l m
 Z
 d d l m Z d d l m Z d d l m Z e j d d	 k Z e Z y d d l Z Wn e k
 rì e Z n Xe j j e j j e ƒ ƒ Z d
 Z d Z d e f d „  ƒ  YZ d „  Z d Z d Z  d Z! d Z" d Z# d Z$ d Z% d Z& d Z' d e f d „  ƒ  YZ( e Z) e d d d d d „ Z+ d S(   s    Middleware that profiles all requests, accumulating timings.

o Insprired by the paste.debug.profile version, which profiles single requests.
iÿÿÿÿN(   t   bytes_(   t   parse_qs(   t   profile(   t   StringIO(   t	   url_quotei    i   s	   wsgi.profs  
app_iter = self.app(environ, start_response)
if isinstance(app_iter, types.GeneratorType):
    # unwind the generator; it may call start_response
    app_iter_ = list(app_iter)
    if hasattr(app_iter, 'close'):
        app_iter.close()
else:
    app_iter_ = app_iter
t   ProfileMiddlewarec           B   sJ   e  Z e j Z d e d e e d  d „ Z d d „ Z d „  Z	 d „  Z
 RS(   s   /__profile__c         C   ss   t  j j |  _ t  j |  _ | |  _ t j ƒ  |  _ | |  _ | |  _	 | |  _
 t j ƒ  |  _ | |  _ | |  _ d  S(   N(   t   ost   patht   existst   removet   appR   t   Profilet   profilert   log_filenamet   cachegrind_filenamet   first_requestt	   threadingt   Lockt   lockt   flush_at_shutdown(   t   selfR
   t   global_confR   R   t   discard_first_requestR   R   (    (    s`   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/repoze/profile/profiler.pyt   __init__/   s    					c         C   s.  | j  ƒ  } t | j d d ƒ ƒ } | j d d ƒ } | j d d  ƒ } | j d d ƒ j ƒ  } t | j d d	 ƒ ƒ } | j d
 d ƒ }	 | d  k r¢ t ƒ  } n  | j ƒ  }
 t j j	 |  j
 ƒ } | r÷ | r÷ t j |  j
 ƒ t j ƒ  |  _ t } n  | r£|  j |  j
 ƒ } | s"| j ƒ  n  | j | ƒ t | d ƒ rJ| | _ n  zI t j } | t _ t | d |	 ƒ } | rˆ| | | ƒ n
 | | ƒ Wd  | t _ Xn  | j ƒ  } t } |
 } d } | pÊd } | rðd } d | } d | } t j | | ƒ } d | } d | } t j | | ƒ } d |	 } d |	 } t j | | ƒ }	 d } d } | rfd } n  t j | | ƒ } d } d | } t j | | ƒ } | t  j! t" ƒ } | j# i | d 6| d 6| d 6| d 6| d 6|	 d
 6| d 6ƒ } n  t  j! t$ ƒ } | j# i | d 6| d 6| d 6| d 6ƒ } | S(   Nt   fulldirsi    t   sortt   timet   cleart   filenamet    t   limitid   t   modet   statst   streams   print_%ss(  
            Profiling information is generated using the standard Python 
            profiler. To learn how to interpret the profiler statistics, 
            see the <a
            href="http://www.python.org/doc/current/lib/module-profile.html">
            Python profiler documentation</a>.s   <option value="%s">s   <option value="%s" selected>s   />s	   checked/>s   value=""s
   value="%s"t   descriptiont   actiont   formelementst   profiledata(%   t
   get_paramst   intt   gett   Nonet   stripR   t   get_urlR   R   R   R   R	   R   R   R   t   Falset   Statst
   strip_dirst
   sort_statst   hasattrR!   t   syst   stdoutt   getattrt   getvaluet   empty_descriptiont	   sort_tmplt   replacet
   limit_tmplt	   mode_tmplt   fulldirs_tmplt   filename_tmplt   stringt   Templatet   formelements_tmplt
   substitutet
   index_tmpl(   R   t   requestt   outputt	   querydataR   R   R   R   R   R   t   urlt
   log_existsR    t   orig_stdoutt   print_fnR%   R"   R#   R$   t	   sort_replt   sort_selectedt
   limit_replt   limit_selectedt	   mode_replt   mode_selectedt   fulldirs_checkedt   fulldirs_replt   filename_replt   filename_selectedt   index(    (    s`   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/repoze/profile/profiler.pyRR   B   sŽ    			





	


c         C   s2   |  j  r. |  j |  j ƒ r. |  j |  j ƒ n  d  S(   N(   R   R   R   R	   (   R   (    (    s`   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/repoze/profile/profiler.pyt   __del__š   s    c   
      C   s  t  | ƒ } | j |  j k r |  j j ƒ  z |  j | ƒ } Wd  |  j j ƒ  X| d d d t t | ƒ ƒ f g ƒ t	 | ƒ g S|  j j ƒ  zê t
 ƒ  } |  j j t t ƒ  | ƒ |  j r× t j ƒ  |  _ t |  _ n’ |  j j |  j ƒ t ri|  j d  k	 rit j |  j ƒ } t j | ƒ } d  } z# t |  j d ƒ } | j | ƒ Wd  | d  k	 re| j ƒ  n  Xn  | d }	 |	 SWd  |  j j ƒ  Xd  S(   Ns   200 OKs   content-types   text/html; charset="UTF-8"s   content-lengtht   wbt	   app_iter_(   s   content-types   text/html; charset="UTF-8"(   t   MiniRequestt	   path_infoR   R   t   acquireRR   t   releaset   strt   lenR    t   localsR   t   runctxt   PROFILE_EXECt   globalsR   R   R   R,   t
   dump_statsR   t	   HAS_PP2CTR   R)   t   pstatsR-   t   pyprof2calltreet   CalltreeConvertert   fileRB   t   close(
   R   t   environt   start_responseRA   t   textt   _localsR    t   convt   grindt   app_iter(    (    s`   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/repoze/profile/profiler.pyt   __call__ž   s<    		
N(   t   __name__t
   __module__Rb   R-   R)   t   DEFAULT_PROFILE_LOGt   TrueR   RR   RS   Rn   (    (    (    s`   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/repoze/profile/profiler.pyR   ,   s   	X	c         C   s`   |  t  k r t  S|  j ƒ  }  |  j d ƒ sX |  j d ƒ sX |  j d ƒ sX |  j d ƒ r\ t  St S(   Nt   tt   yt   1t   on(   Rr   t   lowert
   startswithR,   (   t   s(    (    s`   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/repoze/profile/profiler.pyt   booleanÇ   s    sv   
        There is not yet any profiling data to report.
        <input type="submit" name="refresh" value="Refresh"/>
sT  
              <select name="sort">
                <option value="time">time</option>
                <option value="cumulative">cumulative</option>
                <option value="calls">calls</option>
                <option value="pcalls">pcalls</option>
                <option value="name">name</option>
                <option value="file">file</option>
                <option value="module">module</option>
                <option value="line">line</option>
                <option value="nfl">nfl</option>
                <option value="stdname">stdname</option>
              </select>
s2  
              <select name="limit">
                <option value="100">100</option>
                <option value="200">200</option>
                <option value="300">300</option>
                <option value="400">400</option>
                <option value="500">500</option>
              </select>
sB   
              <input type="checkbox" name="fulldirs" value="1"/>
sã   
              <select name="mode">
                <option value="stats">stats</option>
                <option value="callees">callees</option>
                <option value="callers">callers</option>
              </select>
sh   
              <input type="text" name="filename"
              value="" placeholder="filename part" />
s  
      <div>
        <table>
          <tr>
            <td>
              <strong>Sort</strong>:
               ${sort}
            </td>
            <td>
              <strong>Limit</strong>:
               ${limit}
            </td>
            <td>
              <strong>Full Dirs</strong>:
              ${fulldirs}
            </td>
            <td>
              <strong>Mode</strong>:
              ${mode}
            </td>
            <td>
              <strong>Filter</strong>:
              ${filename}
            </td>
            <td>
              <input type="submit" name="submit" value="Update"/>
            </td>
            <td>
              <input type="submit" name="clear" value="Clear"/>
            </td>
          </tr>
        </table>
      </div>
s'  
<html>
  <head>
    <title>repoze.profile results</title>
  </head>
  <body>
    
    <form action="${action}" method="POST">

      <div class="form-text">
        ${description}
      </div>

      ${formelements}
    
    </form>
    <pre>
       ${profiledata}
    </pre>
  </body>
</html>
s   /:@&+$,RV   c           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s   | |  _  | d |  _ d  S(   Nt	   PATH_INFO(   Rg   RW   (   R   Rg   (    (    s`   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/repoze/profile/profiler.pyR   <  s    	c         C   s]  |  j  } | d d } | j d ƒ r` | d } d | k rW | j d d ƒ \ } } qt d  } n | d } | d } |  j  d d k rŸ | d	 k rÊ d  } qÊ n+ |  j  d d
 k rÊ | d k rÊ d  } qÊ n  | | 7} | rë | d | 7} n  | t |  j  j d d ƒ t ƒ 7} | t |  j  j d d ƒ t ƒ 7} |  j  j d ƒ rY| d |  j  d 7} n  | S(   Ns   wsgi.url_schemes   ://t	   HTTP_HOSTt   :i   t   SERVER_NAMEt   SERVER_PORTt   httpst   443t   httpt   80s   :%st   SCRIPT_NAMER   R{   t   QUERY_STRINGt   ?(   Rg   R(   t   splitR)   R   t	   PATH_SAFE(   R   t   eRD   t   hostt   port(    (    s`   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/repoze/profile/profiler.pyR+   @  s2    	
	


c         C   sœ   i  } t  j d |  j d d |  j d t ƒ } x3 | j p= d D]" } | j } | j } | | | <q> Wt |  j j d d ƒ d t d t	 ƒ} | j
 | ƒ | S(	   Nt   fps
   wsgi.inputRg   t   keep_blank_valuesR…   R   t   strict_parsing(    (   t   cgit   FieldStorageRg   Rr   t   listt   namet   valueR   R(   R,   t   update(   R   t   paramst   fst   fieldR’   R“   R&   (    (    s`   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/repoze/profile/profiler.pyR&   ^  s    						(   Ro   Rp   R   R+   R&   (    (    (    s`   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/repoze/profile/profiler.pyRV   ;  s   		t   trues   /__profile__c         C   s@   t  | ƒ } t  | ƒ } t |  d | d | d | d | d | ƒS(   st  Wrap the application in a component that will profile each
    request, appending data from each request to an aggregate
    file.

    Nota bene
    ---------

    o This middleware serializes all requests (i.e., removing concurrency).

    o The Python profiler is seriously SLOW (maybe an order of magnitude!).

    o Ergo, NEVER USE THIS MIDDLEWARE IN PRODUCTION.
    R   R   R   R   R   (   Rz   R   (   R
   R   R   R   R   R   R   (    (    s`   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/repoze/profile/profiler.pyt   make_profile_middlewarep  s    (,   t   __doc__R   R   Rb   R<   R1   R   t   typest   repoze.profile.compatR    R   R   R   R   t   version_infot   PY3Rr   Ra   Rc   t   ImportErrorR,   R   t   abspatht   dirnamet   __file__t   _HERERq   R^   t   objectR   Rz   R5   R6   R8   R:   R9   R;   R>   R@   Rˆ   RV   t   AccumulatingProfileMiddlewareR)   R™   (    (    (    s`   /afs/athena.mit.edu/user/x/a/xavid/.local/lib/python2.7/site-packages/repoze/profile/profiler.pyt   <module>   sN   
›	
#3