ó
Y[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 m Z m Z m	 Z	 m
 Z
 m Z d d l m Z e j e ƒ Z e j g Z e j d k rø xQ e j e j e j e j g D]. Z e j e ƒ e j k rÃ e j e ƒ qÃ qÃ Wn  d e f d „  ƒ  YZ d e f d	 „  ƒ  YZ d S(
   sB   Registers functions to be called if an exception or signal occurs.iÿÿÿÿN(   t   Anyt   Callablet   Dictt   Listt   Union(   t   errorst   ntt   ErrorHandlerc           B   sb   e  Z d  Z d
 d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z RS(   sG  Context manager for running code that must be cleaned up on failure.

    The context manager allows you to register functions that will be called
    when an exception (excluding SystemExit) or signal is encountered.
    Usage::

        handler = ErrorHandler(cleanup1_func, *cleanup1_args, **cleanup1_kwargs)
        handler.register(cleanup2_func, *cleanup2_args, **cleanup2_kwargs)

        with handler:
            do_something()

    Or for one cleanup function::

        with ErrorHandler(func, args, kwargs):
            do_something()

    If an exception is raised out of do_something, the cleanup functions will
    be called in last in first out order. Then the exception is raised.
    Similarly, if a signal is encountered, the cleanup functions are called
    followed by the previously received signal handler.

    Each registered cleanup function is called exactly once. If a registered
    function raises an exception, it is logged and the next function is called.
    Signals received while the registered functions are executing are
    deferred until they finish.

    c         O   sS   t  |  _ t  |  _ g  |  _ i  |  _ g  |  _ | d  k	 rO |  j | | | Ž n  d  S(   N(   t   Falset   call_on_regular_exitt   body_executedt   funcst   prev_handlerst   received_signalst   Nonet   register(   t   selft   funct   argst   kwargs(    (    s9   /usr/lib/python2.7/dist-packages/certbot/error_handler.pyt   __init__<   s    					c         C   s   t  |  _ |  j ƒ  d  S(   N(   R   R
   t   _set_signal_handlers(   R   (    (    s9   /usr/lib/python2.7/dist-packages/certbot/error_handler.pyt	   __enter__E   s    	c         C   s°   t  |  _ t } | t k r | S| d  k r; |  j sŽ | SnS | t j k rf t j	 d |  j
 ƒ t  } n( t j	 d d j t j | | | ƒ ƒ ƒ |  j ƒ  |  j ƒ  |  j ƒ  | S(   Ns   Encountered signals: %ss   Encountered exception:
%st    (   t   TrueR
   R   t
   SystemExitR   R	   R   t
   SignalExitt   loggert   debugR   t   joint	   tracebackt   format_exceptiont   _call_registeredt   _reset_signal_handlerst   _call_signals(   R   t	   exec_typet
   exec_valuet   tracet   retval(    (    s9   /usr/lib/python2.7/dist-packages/certbot/error_handler.pyt   __exit__I   s     			


c         O   s#   |  j  j t j | | | Ž ƒ d S(   s   Sets func to be run with the given arguments during cleanup.

        :param function func: function to be called in case of an error

        N(   R   t   appendt	   functoolst   partial(   R   R   R   R   (    (    s9   /usr/lib/python2.7/dist-packages/certbot/error_handler.pyR   ^   s    c         C   sg   t  j d ƒ xS |  j rb y |  j d ƒ  Wn$ t k
 rQ t  j d d t ƒn X|  j j ƒ  q Wd S(   s   Calls all registered functionss   Calling registered functionsiÿÿÿÿs'   Encountered exception during recovery: t   exc_infoN(   R   R   R   t	   Exceptiont   errorR   t   pop(   R   (    (    s9   /usr/lib/python2.7/dist-packages/certbot/error_handler.pyR    g   s    c         C   sS   xL t  D]D } t j | ƒ } | d k	 r | |  j | <t j | |  j ƒ q q Wd S(   s-   Sets signal handlers for signals in _SIGNALS.N(   t   _SIGNALSt   signalt	   getsignalR   R   t   _signal_handler(   R   t   signumt   prev_handler(    (    s9   /usr/lib/python2.7/dist-packages/certbot/error_handler.pyR   q   s
    c         C   s<   x( |  j  D] } t j | |  j  | ƒ q
 W|  j  j ƒ  d S(   s/   Resets signal handlers for signals in _SIGNALS.N(   R   R0   t   clear(   R   R3   (    (    s9   /usr/lib/python2.7/dist-packages/certbot/error_handler.pyR!   z   s    c         C   s)   |  j  j | ƒ |  j s% t j ‚ n  d S(   s  Replacement function for handling received signals.

        Store the received signal. If we are executing the code block in
        the body of the context manager, stop by raising signal exit.

        :param int signum: number of current signal

        N(   R   R(   R
   R   R   (   R   R3   t   unused_frame(    (    s9   /usr/lib/python2.7/dist-packages/certbot/error_handler.pyR2   €   s    		c         C   s>   x7 |  j  D], } t j d | ƒ t j t j ƒ  | ƒ q
 Wd S(   s"   Finally call the deferred signals.s   Calling signal %sN(   R   R   R   t   ost   killt   getpid(   R   R3   (    (    s9   /usr/lib/python2.7/dist-packages/certbot/error_handler.pyR"      s    N(   t   __name__t
   __module__t   __doc__R   R   R   R'   R   R    R   R!   R2   R"   (    (    (    s9   /usr/lib/python2.7/dist-packages/certbot/error_handler.pyR      s   						
				t   ExitHandlerc           B   s   e  Z d  Z d d „ Z RS(   sÔ   Context manager for running code that must be cleaned up.

    Subclass of ErrorHandler, with the same usage and parameters.
    In addition to cleaning up on all signals, also cleans up on
    regular exit.
    c         O   s#   t  j |  | | | Ž t |  _ d  S(   N(   R   R   R   R	   (   R   R   R   R   (    (    s9   /usr/lib/python2.7/dist-packages/certbot/error_handler.pyR   š   s    N(   R:   R;   R<   R   R   (    (    (    s9   /usr/lib/python2.7/dist-packages/certbot/error_handler.pyR=   “   s   (   R<   R)   t   loggingR7   R0   R   t   acme.magic_typingR    R   R   R   R   t   certbotR   t	   getLoggerR:   R   t   SIGTERMR/   t   namet   SIGHUPt   SIGQUITt   SIGXCPUt   SIGXFSZt   signal_codeR1   t   SIG_IGNR(   t   objectR   R=   (    (    (    s9   /usr/lib/python2.7/dist-packages/certbot/error_handler.pyt   <module>   s    (t