ó
Xé?_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 e j	 ƒ  Z
 d e f d „  ƒ  YZ d e f d „  ƒ  YZ e ƒ  Z e j ƒ  e _ e j ƒ  e _ e j ƒ  e _ e j ƒ  e _ e j ƒ  e _ y d d l Z Wn e k
 rd Z n. Xy e j d ƒ Z Wn e k
 r<d	 Z n Xd
 e f d „  ƒ  YZ e ƒ  Z d S(   sˆ
  An implementation of the Web Site Process Bus.

This module is completely standalone, depending only on the stdlib.

Web Site Process Bus
--------------------

A Bus object is used to contain and manage site-wide behavior:
daemonization, HTTP server start/stop, process reload, signal handling,
drop privileges, PID file management, logging for all of these,
and many more.

In addition, a Bus object provides a place for each web framework
to register code that runs in response to site-wide events (like
process start and stop), or which controls or otherwise interacts with
the site-wide components mentioned above. For example, a framework which
uses file-based templates would add known template filenames to an
autoreload component.

Ideally, a Bus object will be flexible enough to be useful in a variety
of invocation scenarios:

 1. The deployer starts a site from the command line via a
    framework-neutral deployment script; applications from multiple frameworks
    are mixed in a single site. Command-line arguments and configuration
    files are used to define site-wide components such as the HTTP server,
    WSGI component graph, autoreload behavior, signal handling, etc.
 2. The deployer starts a site via some other process, such as Apache;
    applications from multiple frameworks are mixed in a single site.
    Autoreload and signal handling (from Python at least) are disabled.
 3. The deployer starts a site via a framework-specific mechanism;
    for example, when running tests, exploring tutorials, or deploying
    single applications from a single framework. The framework controls
    which site-wide components are enabled as it sees fit.

The Bus object in this package uses topic-based publish-subscribe
messaging to accomplish all this. A few topic channels are built in
('start', 'stop', 'exit', 'graceful', 'log', and 'main'). Frameworks and
site containers are free to define their own. If a message is sent to a
channel that has not been defined or has no listeners, there is no effect.

In general, there should only ever be a single Bus object per process.
Frameworks and site containers share a single Bus object by publishing
messages and subscribing listeners.

The Bus object works as a finite state machine which models the current
state of the process. Bus methods move it from one state to another;
those methods then publish to subscribed listeners on the channel for
the new state.::

                        O
                        |
                        V
       STOPPING --> STOPPED --> EXITING -> X
          A   A         |
          |    \___     |
          |        \    |
          |         V   V
        STARTED <-- STARTING

iÿÿÿÿNt   ChannelFailuresc           B   sM   e  Z d  Z d Z d „  Z d „  Z d „  Z d „  Z e Z d „  Z	 e	 Z
 RS(   sK   Exception raised when errors occur in a listener during Bus.publish().
    s   
c         O   s#   t  j |  | | Ž t ƒ  |  _ d  S(   N(   t	   Exceptiont   __init__t   listt   _exceptions(   t   selft   argst   kwargs(    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyR   V   s    c         C   s   |  j  j t j ƒ  d ƒ d S(   s%   Append the current exception to self.i   N(   R   t   appendt   syst   exc_info(   R   (    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyt   handle_exception\   s    c         C   s   |  j  S(   s*   Return a list of seen exception instances.(   R   (   R   (    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyt   get_instances`   s    c         C   s%   t  t |  j ƒ  ƒ } |  j j | ƒ S(   N(   t   mapt   reprR   t	   delimitert   join(   R   t   exception_strings(    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyt   __str__d   s    c         C   s   t  |  j ƒ S(   N(   t   boolR   (   R   (    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyt   __bool__j   s    (   t   __name__t
   __module__t   __doc__R   R   R   R   R   t   __repr__R   t   __nonzero__(    (    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyR    P   s   					t
   _StateEnumc           B   s'   e  Z d  e f d „  ƒ  YZ d „  Z RS(   t   Statec           B   s   e  Z d Z d  „  Z RS(   c         C   s   d |  j  S(   Ns	   states.%s(   t   name(   R   (    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyR   v   s    N(   R   R   t   NoneR   R   (    (    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyR   s   s   c         C   s5   t  | |  j ƒ r | | _ n  t j |  | | ƒ d  S(   N(   t
   isinstanceR   R   t   objectt   __setattr__(   R   t   keyt   value(    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyR    y   s    (   R   R   R   R   R    (    (    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyR   q   s   i    t   SC_OPEN_MAXi   t   Busc           B   sÔ   e  Z d  Z e Z e j Z e Z e Z	 d „  Z
 d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d	 „  Z d
 d „ Z d
 d d „ Z d „  Z d „  Z d „  Z d d d „ Z d d e d „ Z RS(   ss  Process state-machine and messenger for HTTP site deployment.

    All listeners for a given channel are guaranteed to be called even
    if others at the same channel fail. Each failure is logged, but
    execution proceeds on to the next listener. The only way to stop all
    processing from inside a listener is to raise SystemExit and stop the
    whole server.
    c         C   sM   t  |  _ t j |  _ t g  d D] } | t ƒ  f ^ q ƒ |  _ i  |  _ d  S(   Nt   startt   stopt   exitt   gracefult   logt   main(   s   starts   stops   exits   gracefuls   logs   main(	   t   Falset   execvt   statest   STOPPEDt   statet   dictt   sett	   listenerst   _priorities(   R   t   channel(    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyR       s    	%c         C   sn   | |  j  k r" t ƒ  |  j  | <n  |  j  | j | ƒ | d k rW t | d d ƒ } n  | |  j | | f <d S(   s=   Add the given callback at the given channel (if not present).t   priorityi2   N(   R2   R1   t   addR   t   getattrR3   (   R   R4   t   callbackR5   (    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyt	   subscribe¨   s    c         C   sH   |  j  j | ƒ } | rD | | k rD | j | ƒ |  j | | f =n  d S(   s(   Discard the given callback (if present).N(   R2   t   gett   discardR3   (   R   R4   R8   R2   (    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyt   unsubscribe²   s    c   
      O   sk  | |  j  k r g  St ƒ  } g  } g  |  j  | D] } |  j | | f | f ^ q0 } y | j d d „  ƒ Wn t k
 r‰ | j ƒ  n XxË | D]Ã \ } } y | j | | | Ž  ƒ Wq‘ t k
 rÍ ‚  q‘ t k
 rt j	 ƒ  d }	 | r|	 j
 d k rd |	 _
 n  ‚  q‘ | j ƒ  | d k r-qT|  j d | | f d d d	 t ƒq‘ Xq‘ W| rg| ‚ n  | S(
   s7   Return output of all subscribers for the given channel.R!   c         S   s   |  d S(   Ni    (    (   t   item(    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyt   <lambda>Ä   s    i   i    R)   s   Error in %r listener %rt   leveli(   t	   traceback(   R2   R    R3   t   sortt	   TypeErrorR   t   KeyboardInterruptt
   SystemExitR	   R
   t   codeR   R)   t   True(
   R   R4   R   R   t   exct   outputt   listenert   itemsR5   t   e(    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyt   publish¹   s:    	0
	c         C   s:   |  j  t j k r6 t j d |  j  t ƒ |  j ƒ  n  d S(   s7   An atexit handler which asserts the Bus is not running.sÀ   The main thread is exiting, but the Bus is in the %r state; shutting it down automatically now. You must either call bus.block() after start(), or call bus.exit() before the main thread exits.N(   R/   R-   t   EXITINGt   warningst   warnt   RuntimeWarningR'   (   R   (    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyt   _clean_exità   s
    c         C   sÁ   t  j |  j ƒ t j |  _ |  j d ƒ y* |  j d ƒ t j |  _ |  j d ƒ Wng t	 t
 f k
 ro ‚  nN |  j d d d d t ƒt j ƒ  d } y |  j ƒ  Wn n X| ‚ n Xd	 S(
   s   Start all services.s   Bus STARTINGR%   s   Bus STARTEDs-   Shutting down due to error in start listener:R?   i(   R@   i   N(   t   atexitt   registerRQ   R-   t   STARTINGR/   R)   RL   t   STARTEDRC   RD   RF   R	   R
   R'   (   R   t   e_info(    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyR%   ê   s$    c         C   s„   |  j  } yA |  j ƒ  t j |  _  |  j d ƒ |  j d ƒ |  j d ƒ Wn t j d ƒ n X| t j k r€ t j d ƒ n  d S(   s2   Stop all services and prepare to exit the process.s   Bus EXITINGR'   s
   Bus EXITEDiF   N(	   R/   R&   R-   RM   R)   RL   t   ost   _exitRT   (   R   t	   exitstate(    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyR'     s    	
c         C   s   t  |  _ |  j ƒ  d S(   sÊ   Restart the process (may close connections).

        This method does not restart the process from the calling thread;
        instead, it stops the bus and asks the main thread to call execv.
        N(   RF   R,   R'   (   R   (    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyt   restart  s    	c         C   s   |  j  d ƒ |  j d ƒ d S(   s   Advise all services to reload.s   Bus gracefulR(   N(   R)   RL   (   R   (    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyR(   %  s    gš™™™™™¹?c         C   sA  y  |  j  t j d | d d ƒWnX t t f k
 rP |  j d ƒ |  j ƒ  n+ t k
 rz |  j d ƒ |  j ƒ  ‚  n X|  j d ƒ xœ t j	 ƒ  D]Ž } | t j
 ƒ  k r• | j ƒ  r• t | t j ƒ r• t t j d ƒ rê | j } n | j ƒ  } | s#|  j d | j ƒ  ƒ | j ƒ  q#q• q• W|  j r=|  j ƒ  n  d	 S(
   sº  Wait for the EXITING state, KeyboardInterrupt or SystemExit.

        This function is intended to be called only by the main thread.
        After waiting for the EXITING state, it also waits for all threads
        to terminate, and then calls os.execv if self.execv is True. This
        design allows another thread to call bus.restart, yet have the main
        thread perform the actual execv call (required on some platforms).
        t   intervalR4   R*   s%   Keyboard Interrupt: shutting down buss$   SystemExit raised: shutting down buss)   Waiting for child threads to terminate...t   daemons   Waiting for thread %s.N(   t   waitR-   RM   RC   t   IOErrorR)   R'   RD   t	   threadingt	   enumeratet   currentThreadt   isAliveR   t   _MainThreadt   hasattrt   ThreadR\   t   isDaemont   getNameR   R,   t	   _do_execv(   R   R[   t   tt   d(    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyt   block*  s,    	 
	c            s|   t  | t t f ƒ r | ‰ n	 | g ‰ ‡  ‡ ‡ ‡ f d †  } y t j d j | ƒ Wn t t f k
 rp n X| ƒ  d S(   s=   Poll for the given state(s) at intervals; publish to channel.c              s4   x- ˆ j  ˆ k r/ t j ˆ ƒ ˆ j ˆ  ƒ q Wd  S(   N(   R/   t   timet   sleepRL   (    (   R4   R[   R   R-   (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyt   _waitc  s    t   psycoN(   R   t   tupleR   R	   t   modulest   cannotcompilet   KeyErrort   AttributeError(   R   R/   R[   R4   Rn   (    (   R4   R[   R   R-   s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyR]   \  s    		c         C   sÌ   t  j } |  j d d j | ƒ ƒ t  j d  d k rP d d l m } | ‚ nx | j d t  j ƒ t  j d k r’ g  | D] } d	 | ^ qy } n  t	 j
 t ƒ |  j rµ |  j ƒ  n  t	 j t  j | ƒ d
 S(   sÀ   Re-execute the current process.

        This must be called from the main thread, because certain platforms
        (OS X) don't allow execv to be called in a child thread very well.
        s   Re-spawning %st    i   t   javaiÿÿÿÿ(   t   SystemRestarti    t   win32s   "%s"N(   R	   t   argvR)   R   t   platformt   _systemrestartRw   t   insertt
   executableRW   t   chdirt   _startup_cwdt   max_cloexec_filest   _set_cloexecR,   (   R   R   Rw   t   arg(    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyRh   u  s    
	 	c         C   sn   xg t  d |  j ƒ D]S } y t j | t j ƒ } Wn t k
 rH q n Xt j | t j | t j Bƒ q Wd S(   sÞ  Set the CLOEXEC flag on all open files (except stdin/out/err).

        If self.max_cloexec_files is an integer (the default), then on
        platforms which support it, it represents the max open files setting
        for the operating system. This function will be called just before
        the process is restarted via os.execv() to prevent open files
        from persisting into the new process.

        Set self.max_cloexec_files to 0 to disable this behavior.
        i   N(   t   rangeR€   t   fcntlt   F_GETFDR^   t   F_SETFDt
   FD_CLOEXEC(   R   t   fdt   flags(    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyR   ‹  s    c         C   sC   t  j |  _ |  j d ƒ |  j d ƒ t  j |  _ |  j d ƒ d S(   s   Stop all services.s   Bus STOPPINGR&   s   Bus STOPPEDN(   R-   t   STOPPINGR/   R)   RL   R.   (   R   (    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyR&     s
    c            s“   | d k r d } n  | d k r* i  } n  | f | } ‡  f d †  } t j d | d | d | ƒ } | j d | j ƒ  ƒ | j ƒ  ˆ  j ƒ  | S(   s?   Start 'func' in a new thread T, then start self (and return T).c            s!   ˆ  j  t j ƒ |  | | Ž  d  S(   N(   R]   R-   RU   (   t   funct   at   kw(   R   (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyt	   _callback­  s    t   targetR   R   s   Bus Callback N(    (   R   R_   Re   t   setNameRg   R%   (   R   R‹   R   R   RŽ   Ri   (    (   R   s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyt   start_with_callback¥  s    		

t    i   c         C   sF   | r/ | d d j  t j t j ƒ  Œ  ƒ 7} n  |  j d | | ƒ d S(   s>   Log the given message. Append the last traceback if requested.s   
R’   R)   N(   R   t
   _tracebackt   format_exceptionR	   R
   RL   (   R   t   msgR?   R@   (    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyR)   ¸  s    )N(   R   R   R   R-   R.   R/   R+   R,   t	   max_filesR€   R   R   R9   R<   RL   RQ   R%   R'   RZ   R(   Rk   R]   Rh   R   R&   R‘   R)   (    (    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyR$      s*   			
		'	
					2			(   R   RR   RW   R	   R_   Rl   R@   R“   RN   t   getcwdR   R   R    R   R   R-   R   R.   RT   RU   RŠ   RM   R„   t   ImportErrorR–   t   sysconfRt   R$   t   bus(    (    (    s~   /local/mnt/workspace/CRMBuilds/Saipan.LA.2.0-00145-STD.PROD-1_20200821_083004/b/common/sectools/ext/cherrypy/process/wspbus.pyt   <module>=   s6   !	

ÿ /