σ
P'^c           @@ s/  d  Z  d d l m Z m 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 d	 d
 d d g Z d Z e j j d  p― d Z e e j j d  pΚ e  Z d g Z e e d  Z d Z d Z d Z d Z d
 e	 f d     YZ d   Z d d  Z d S(   sτ  
celery.contrib.rdb
==================

Remote debugger for Celery tasks running in multiprocessing pool workers.
Inspired by http://snippets.dzone.com/posts/show/7248

**Usage**

.. code-block:: python

    from celery.contrib import rdb
    from celery import task

    @task()
    def add(x, y):
        result = x + y
        rdb.set_trace()
        return result


**Environment Variables**

.. envvar:: CELERY_RDB_HOST

    Hostname to bind to.  Default is '127.0.01', which means the socket
    will only be accessible from the local host.

.. envvar:: CELERY_RDB_PORT

    Base port to bind to.  Default is 6899.
    The debugger will try to find an available port starting from the
    base port.  The selected port will be logged by the worker.

i    (   t   absolute_importt   print_functionN(   t   Pdb(   t   current_process(   t   ranget   CELERY_RDB_HOSTt   CELERY_RDB_PORTt   default_portt   Rdbt   debuggert	   set_traceiσ  s	   127.0.0.1t	   _getframess   {self.ident}: Couldn't find an available port.

Please specify one using the CELERY_RDB_PORT environment variable.
s   {self.ident}: Please telnet into {self.host} {self.port}.

Type `exit` in session to continue.

{self.ident}: Waiting for client...
s5   {self.ident}: Now in session with {self.remote_addr}.s4   {self.ident}: Session with {self.remote_addr} ended.c           B@ s   e  Z d  Z d Z d Z e e d d 
e j	 d  Z
 d d 
d  Z d   Z d   Z d   Z d   Z d	   Z e Z Z d
   Z e Z Z d   Z RS(   s   Remote Debuggerid   i    c         C@ sT  t  |  _ | |  _ t j t j f |  _ |  j | | | |  \ |  _ } |  j j	 d  |  j j
 d  d j |  j |  |  _ | |  _ | |  _ |  j t j d |    |  j j   \ |  _ } |  j j	 d  d j d   | D  |  _ |  j t j d |    |  j j d  |  _ t _ t _ t j |  d d d	 |  j d
 |  j d  S(   Ni   s   {0}:{1}t   selft   :c         s@ s   |  ] } t  |  Vq d  S(   N(   t   str(   t   .0t   v(    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/contrib/rdb.pys	   <genexpr>i   s    t   rwt   completekeyt   tabt   stdint   stdout(   t   Truet   activet   outt   sysR   R   t   _prev_handlest   get_avail_portt   _sockt   setblockingt   listent   formatt   met   identt   hostt   portt   sayt   BANNERt   acceptt   _clientt   joint   remote_addrt   SESSION_STARTEDt   makefilet   _handleR   t   __init__(   R   R"   R#   t   port_search_limitt	   port_skewR   t	   this_portt   address(    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/contrib/rdb.pyR-   V   s$    				#c   
      C@ sό   y+ t    j j d  \ } } t |  } Wn t k
 r> n Xd  } x° t |  D] } t j t j t j	  } | | | } y | j
 | | f  Wn: t j k
 rΡ }	 |	 j t j t j g k rΛ qR n    qR X| | f SqR Wt t j d |     d  S(   Nt   -R   (   R   t   namet   splitt   intt
   ValueErrort   NoneR   t   sockett   AF_INETt   SOCK_STREAMt   bindt   errort   errnot
   EADDRINUSEt   EINVALt	   Exceptiont   NO_AVAILABLE_PORTR   (
   R   R"   R#   t   search_limitt   skewt   _R0   t   iR   t   exc(    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/contrib/rdb.pyR   o   s"    c         C@ s   t  | d |  j d  S(   Nt   file(   t   printR   (   R   t   m(    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/contrib/rdb.pyR$      s    c         C@ s   |  S(   N(    (   R   (    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/contrib/rdb.pyt	   __enter__   s    c         G@ s   |  j    d  S(   N(   t   _close_session(   R   t   exc_info(    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/contrib/rdb.pyt   __exit__   s    c         C@ s΄   |  j  \ |  _ |  _ \ t _ t _ |  j r° |  j d  k	 rM |  j j   n  |  j d  k	 rl |  j j   n  |  j	 d  k	 r |  j	 j   n  t
 |  _ |  j t j d |    n  d  S(   NR   (   R   R   R   R   R   R,   R7   t   closeR'   R   t   FalseR$   t   SESSION_ENDEDR   (   R   (    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/contrib/rdb.pyRK      s    %		c         C@ s   |  j    |  j   d S(   Ni   (   RK   t   set_continue(   R   t   arg(    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/contrib/rdb.pyt   do_continue   s    

c         C@ s   |  j    |  j   d S(   Ni   (   RK   t   set_quit(   R   RR   (    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/contrib/rdb.pyt   do_quit   s    

c         C@ s   t  j d   d  S(   N(   R   t   settraceR7   (   R   (    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/contrib/rdb.pyRT   ₯   s    N(   t   __name__t
   __module__R    R7   t
   _prev_outsR   R   R   R   R   R-   R   R$   RJ   RM   RK   RS   t   do_ct   do_contRU   t   do_qt   do_exitRT   (    (    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/contrib/rdb.pyR   Q   s   					
	
c          C@ s8   t  d }  |  d k s  |  j r4 t   }  t  d <n  |  S(   sH   Return the current debugger instance (if any),
    or creates a new one.i    N(   t   _currentR7   R   R   (   t   rdb(    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/contrib/rdb.pyR	   ͺ   s    
c         C@ s+   |  d k r t   j }  n  t   j |   S(   s8   Set breakpoint at current location, or a specified frameN(   R7   t   _framet   f_backR	   R
   (   t   frame(    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/contrib/rdb.pyR
   ³   s    (    t   __doc__t
   __future__R    R   R=   t   osR8   R   t   pdbR   t   billiardR   t   celery.fiveR   t   __all__R   t   environt   getR   R5   R   R7   R^   t   getattrR`   RA   R%   R*   RP   R   R	   R
   (    (    (    s4   /tmp/pip-unpacked-wheel-gV1wwp/celery/contrib/rdb.pyt   <module>$   s,   		Y		