
]c           @   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
 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 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 d d l m  Z! d d l" m# Z# d d l" m$ Z$ d   Z  d Z% d   Z& d a( d   Z) d e* f d     YZ+ d   Z, d e* f d     YZ- d S(    s   
    werkzeug.debug
    ~~~~~~~~~~~~~~

    WSGI application traceback debugger.

    :copyright: 2007 Pallets
    :license: BSD-3-Clause
iN(   t   chain(   t   basename(   t   joini   (   t	   text_type(   t   _log(   t   parse_cookie(   t   gen_salt(   t   BaseRequest(   t   BaseResponsei   (   t   Console(   t
   debug_repr(   t   get_current_traceback(   t   render_console_htmlc          O   s/   d d  l  } | j d t d d t |  |   S(   Nis}   'debug_repr' has moved to 'werkzeug.debug.repr.debug_repr' as of version 0.7. This old import will be removed in version 1.0.t
   stackleveli   (   t   warningst   warnt   DeprecationWarningt   _debug_repr(   t   argst   kwargsR   (    (    s6   lib/python2.7/site-packages/werkzeug/debug/__init__.pyR
   %   s    i<   i   i   c         C   s?   t  |  t  r$ |  j d d  }  n  t j |  d  j   d  S(   Ns   utf-8t   replacet
   shittysalti   (   t
   isinstanceR   t   encodet   hashlibt   md5t	   hexdigest(   t   pin(    (    s6   lib/python2.7/site-packages/werkzeug/debug/__init__.pyt   hash_pin6   s    c          C   s0   t  }  |  d  k	 r |  Sd   } |   a  }  |  S(   Nc          S   s  y% t  d   }  |  j   } Wd  QXWn t k
 r8 n$ X| j   j d  d } | r\ | SxQ d D]I } y, t  | d   }  |  j   j   SWd  QXWqc t k
 r qc qc Xqc Wyp d d l m } m } | d	 d
 d d d g d | j   d } t	 j
 d |  } | d  k	 r| j d  SWn t t f k
 r9n Xd  } y d d  l } Wn5 t k
 ry d d  l } Wqt k
 rqXn X| d  k	 ryj | j | j d d | j | j B ? } | j | d  \ }	 }
 |
 | j k r|	 j d  S|	 SWd  QXWqt k
 rqXn  d  S(   Ns   /proc/self/cgroups   /docker/i   s   /etc/machine-ids   /proc/sys/kernel/random/boot_idt   rbi(   t   Popent   PIPEt   ioregs   -ct   IOPlatformExpertDevices   -dt   2t   stdouti    s   "serial-number" = <([^>]+)i   s   SOFTWARE\Microsoft\Cryptographyt   MachineGuids   utf-8(   s   /etc/machine-ids   /proc/sys/kernel/random/boot_id(   t   opent   readlinet   IOErrort   stript	   partitiont
   subprocessR   R   t   communicatet   ret   searcht   Nonet   groupt   OSErrort   ImportErrort   winregt   _winregt   OpenKeyt   HKEY_LOCAL_MACHINEt   KEY_READt   KEY_WOW64_64KEYt   QueryValueExt   REG_SZR   t   WindowsError(   t   ft   valuet   filenameR   R   t   dumpt   matcht   wrt   rkt   machineGuidt   wrType(    (    s6   lib/python2.7/site-packages/werkzeug/debug/__init__.pyt	   _generateE   s\    !(   t   _machine_idR.   (   t   rvRD   (    (    s6   lib/python2.7/site-packages/werkzeug/debug/__init__.pyt   get_machine_id?   s    	At   _ConsoleFramec           B   s   e  Z d  Z d   Z RS(   s]   Helper class so that we can reuse the frame console code for the
    standalone console.
    c         C   s   t  |  |  _ d |  _ d  S(   Ni    (   R	   t   consolet   id(   t   selft	   namespace(    (    s6   lib/python2.7/site-packages/werkzeug/debug/__init__.pyt   __init__   s    (   t   __name__t
   __module__t   __doc__RM   (    (    (    s6   lib/python2.7/site-packages/werkzeug/debug/__init__.pyRH      s   c            sM  t  j j d  } d } d  | d k r. d S| d k	 rp | j d d  j   rp d | k rg | } qp |  n  t |  d |  j j  } y t	 j
   } Wn t t f k
 r d } n Xt j j |  } | | t |  d |  j j  t | d d  g } t t j    t   g } t j   } xQ t | |  D]@ }	 |	 sCq1n  t |	 t  rd|	 j d  }	 n  | j |	  q1W| j d	  d
 | j   d  }
  d k r| j d  d t | j   d  d   n  | d k rCxe d D]T   t     d k rd j    f d   t d t      D  } PqqW } n  | |
 f S(   sQ  Given an application object this returns a semi-stable 9 digit pin
    code and a random key.  The hope is that this is stable between
    restarts to not make debugging particularly frustrating.  If the pin
    was forcefully disabled this returns `None`.

    Second item in the resulting tuple is the cookie name for remembering.
    t   WERKZEUG_DEBUG_PINt   offt   -t    RO   RN   t   __file__s   utf-8t
   cookiesaltt   __wzdi   t   pinsalts   %09di   i	   i   i   i   i    c         3   s,   |  ]" }  | |   !j    d   Vq d S(   t   0N(   t   rjust(   t   .0t   x(   t
   group_sizet   num(    s6   lib/python2.7/site-packages/werkzeug/debug/__init__.pys	   <genexpr>   s   N(   NN(   i   i   i   (    t   ost   environt   getR.   R   t   isdigitt   getattrt	   __class__RO   t   getpasst   getuserR1   t   KeyErrort   syst   modulesRN   t   strt   uuidt   getnodeRG   R   R   R    R   R   R   t   updateR   t   intt   lenR   t   range(   t   appR   RF   t   modnamet   usernamet   modt   probably_public_bitst   private_bitst   ht   bitt   cookie_name(    (   R]   R^   s6   lib/python2.7/site-packages/werkzeug/debug/__init__.pyt   get_pin_and_cookie_name   sR    $		
 	t   DebuggedApplicationc        	   B   s   e  Z d  Z e d d d e d e e d  Z d   Z d   Z e	 e e  Z
 [ [ e	 d    Z d   Z d   Z d	   Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   sQ  Enables debugging support for a given application::

        from werkzeug.debug import DebuggedApplication
        from myapp import app
        app = DebuggedApplication(app, evalex=True)

    The `evalex` keyword argument allows evaluating expressions in a
    traceback's frame context.

    .. versionadded:: 0.9
       The `lodgeit_url` parameter was deprecated.

    :param app: the WSGI application to run debugged.
    :param evalex: enable exception evaluation feature (interactive
                   debugging).  This requires a non-forking server.
    :param request_key: The key that points to the request object in ths
                        environment.  This parameter is ignored in current
                        versions.
    :param console_path: the URL for a general purpose console.
    :param console_init_func: the function that is executed before starting
                              the general purpose console.  The return value
                              is used as initial namespace.
    :param show_hidden_frames: by default hidden traceback frames are skipped.
                               You can show them by setting this parameter
                               to `True`.
    :param pin_security: can be used to disable the pin based security system.
    :param pin_logging: enables the logging of the pin system.
    s   werkzeug.requests   /consolec
         C   s!  | d  k	 r2 d d l m }
 |
 d t d d n  | sA d  } n  | |  _ | |  _ i  |  _ i  |  _ | |  _ | |  _	 | |  _
 | |  _ t d  |  _ d |  _ |	 |  _ | rt j j d  d	 k r|	 rt d
 d  |  j d  k r t d
 d  qt d d |  j  qn	 d  |  _ d  S(   Ni(   R   s   'lodgeit_url' is no longer used as of version 0.9 and will be removed in version 1.0. Werkzeug uses https://gist.github.com/ instead.R   i   i   i    t   WERKZEUG_RUN_MAINt   truet   warnings    * Debugger is active!s-    * Debugger PIN disabled. DEBUGGER UNSECURED!t   infos    * Debugger PIN: %s(   R.   R   R   R   Rq   t   evalext   framest
   tracebackst   request_keyt   console_patht   console_init_funct   show_hidden_framesR   t   secrett   _failed_pin_autht   pin_loggingR_   R`   Ra   R   R   (   RK   Rq   R   R   R   R   R   t   lodgeit_urlt   pin_securityR   R   (    (    s6   lib/python2.7/site-packages/werkzeug/debug/__init__.pyRM     s4    
											c         C   s4   t  |  d  s- t |  j  \ |  _ |  _ n  |  j S(   Nt   _pin(   t   hasattrRz   Rq   R   t   _pin_cookie(   RK   (    (    s6   lib/python2.7/site-packages/werkzeug/debug/__init__.pyt   _get_pin4  s    c         C   s   | |  _  d  S(   N(   R   (   RK   R<   (    (    s6   lib/python2.7/site-packages/werkzeug/debug/__init__.pyt   _set_pin9  s    c         C   s4   t  |  d  s- t |  j  \ |  _ |  _ n  |  j S(   s   The name of the pin cookie.R   (   R   Rz   Rq   R   R   (   RK   (    (    s6   lib/python2.7/site-packages/werkzeug/debug/__init__.pyt   pin_cookie_name?  s    c         c   se  d } yH |  j | |  } x | D] } | Vq" Wt | d  rM | j   n  Wnt k
 r`t | d  rz | j   n  t d d d |  j d t  } x! | j D] } | |  j | j	 <q W| |  j
 | j	 <y | d d d g  Wn" t k
 r| d j d  nE Xt |  j |   } | j d |  j d | d |  j  j d d  V| j | d  n Xd S(   s6   Run the application and conserve the traceback frames.t   closet   skipi   R   t   ignore_system_exceptionss   500 INTERNAL SERVER ERRORs   Content-Types   text/html; charset=utf-8s   X-XSS-ProtectionRY   s   wsgi.errorssp   Debugging middleware caught exception in streamed response at a point where response headers were already sent.
R   t   evalex_trustedR   s   utf-8R   N(   s   Content-Types   text/html; charset=utf-8(   s   X-XSS-ProtectionRY   (   R.   Rq   R   R   t	   ExceptionR   R   t   TrueR   RJ   R   t   writet   boolt   check_pin_trustt   render_fullR   R   R   t   log(   RK   R`   t   start_responset   app_itert   itemt	   tracebackt   framet
   is_trusted(    (    s6   lib/python2.7/site-packages/werkzeug/debug/__init__.pyt   debug_applicationF  s<    			
	c         C   s   t  | j j |  d d S(   s   Execute a command in a console.t   mimetypes	   text/html(   t   ResponseRI   t   eval(   RK   t   requestt   commandR   (    (    s6   lib/python2.7/site-packages/werkzeug/debug/__init__.pyt   execute_commandw  s    c         C   s   d |  j  k rb |  j d k r' i  } n t |  j    } | j d |  j  t |  |  j  d <n  t |  j | j	   } t
 t d |  j d |  d d S(   s   Display a standalone shell.i    Rq   R   R   R   s	   text/htmlN(   R   R   R.   t   dictt
   setdefaultRq   RH   R   R   R`   R   R   R   (   RK   R   t   nsR   (    (    s6   lib/python2.7/site-packages/werkzeug/debug/__init__.pyt   display_console{  s    	c         C   s%   | j    } t t j |  d d S(   s/   Paste the traceback and return a JSON response.R   s   application/json(   t   pasteR   t   jsont   dumps(   RK   R   R   RF   (    (    s6   lib/python2.7/site-packages/werkzeug/debug/__init__.pyt   paste_traceback  s    c         C   s   t  d t |   } y t j t |  } Wn t k
 rD d } n X| d k	 rz t j |  d pg d } t	 | d | St	 d d d S(	   s0   Return a static resource from the shared folder.t   sharedi    s   application/octet-streamR   s	   Not Foundt   statusi  N(
   R   R   t   pkgutilt   get_datat   __package__R0   R.   t	   mimetypest
   guess_typeR   (   RK   R   R=   t   dataR   (    (    s6   lib/python2.7/site-packages/werkzeug/debug/__init__.pyt   get_resource  s    
c         C   s   |  j  d k r t St |  j |  j  } | s> d | k rB t S| j d d  \ } } | j   sj t S| t	 |  j   k r d St
 j
   t t |  k  S(   s!  Checks if the request passed the pin test.  This returns `True` if the
        request is trusted on a pin/cookie basis and returns `False` if not.
        Additionally if the cookie's stored pin hash is wrong it will return
        `None` so that appropriate action can be taken.
        t   |i   N(   R   R.   R   R   Ra   R   t   Falset   splitRb   R   t   timet   PIN_TIMERn   (   RK   R`   t   valt   tst   pin_hash(    (    s6   lib/python2.7/site-packages/werkzeug/debug/__init__.pyR     s    c         C   s5   t  j |  j d k r d n d  |  j d 7_ d  S(   Ni   g      @g      ?i   (   R   t   sleepR   (   RK   (    (    s6   lib/python2.7/site-packages/werkzeug/debug/__init__.pyt   _fail_pin_auth  s    "c         C   sL  t  } t  } |  j | j  } t  } | d k rC |  j   t } n | rR t } ns |  j d k rj t } n[ | j j d  } | j	   j
 d d  |  j j
 d d  k r d |  _ t } n
 |  j   t t j i | d 6| d 6 d d	 } | r/| j |  j d
 t t j    t |  j  f d t n | rH| j |  j  n  | S(   s   Authenticates with the pin.i
   R   RS   RT   i    t   autht	   exhaustedR   s   application/jsons   %s|%st   httponlyN(   R   R   R`   R.   R   R   R   R   Ra   R(   R   R   R   R   R   t
   set_cookieR   Rn   R   R   t   delete_cookie(   RK   R   R   R   t   trustt
   bad_cookiet   entered_pinRF   (    (    s6   lib/python2.7/site-packages/werkzeug/debug/__init__.pyt   pin_auth  s6    
			-		
	%
c         C   sF   |  j  r< |  j d k	 r< t d d  t d d |  j  n  t d  S(   s   Log the pin if needed.R   s=    * To enable the debugger you need to enter the security pin:s    * Debugger pin code: %sRT   N(   R   R   R.   R   R   (   RK   (    (    s6   lib/python2.7/site-packages/werkzeug/debug/__init__.pyt   log_pin_request  s
    
c   
      C   s  t  |  } |  j } | j j d  d k r| j j d  } | j j d  } | j j d  } |  j j | j j d d t  } |  j j | j j d d t  }	 | d	 k r | r |  j | |  } q| d
 k r| d k	 r| |  j	 k r|  j
 | |  } q| d k r;| |  j	 k r;|  j |  } q| d k re| |  j	 k re|  j   } q|  j r| d k	 r|	 d k	 r|  j	 | k r|  j |  r|  j | | |	  } qn< |  j r|  j d k	 r| j |  j k r|  j |  } n  | | |  S(   s   Dispatch the requests.t   __debugger__t   yest   cmdR;   t   st   tbt   typet   frmt   resourceR   t   pinautht   printpinN(   t   RequestR   R   Ra   R   Rn   R   R   R.   R   R   R   R   R   R   R   R   t   pathR   (
   RK   R`   R   R   t   responseR   t   argR   R   R   (    (    s6   lib/python2.7/site-packages/werkzeug/debug/__init__.pyt   __call__  s6    	$$'		N(   RN   RO   RP   R   R.   R   RM   R   R   t   propertyR   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s6   lib/python2.7/site-packages/werkzeug/debug/__init__.pyR{      s0   %			1							/		i  iQ i:	 (.   RP   Re   R   R   R   R_   R   R,   Rh   R   Rk   t	   itertoolsR    t   os.pathR   R   t   _compatR   t	   _internalR   t   httpR   t   securityR   t   wrappersR   R   R   R   RI   R	   t   reprR
   R   t   tbtoolsR   R   R   R   R.   RE   RG   t   objectRH   Rz   R{   (    (    (    s6   lib/python2.7/site-packages/werkzeug/debug/__init__.pyt   <module>
   s>   			K
	S