ó
‚•oTc           @   s}  d  d l  Z  d  d l Z y e Wn! e k
 rC d  d l m Z n Xd  d l Z d  d l Z d  d l	 Z	 d  d l
 Z
 d e f d „  ƒ  YZ d e f d „  ƒ  YZ y d  d l Z d  d l Z Wn e k
 rÛ d \ Z Z n Xd e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e
 j f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   Sett   SimplePluginc           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s   | |  _  d  S(   N(   t   bus(   t   selfR   (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyt   __init__   s    c         C   sO   xH |  j  j D]: } t |  | d  ƒ } | d  k	 r |  j  j | | ƒ q q Wd  S(   N(   R   t	   listenerst   getattrt   Nonet	   subscribe(   R   t   channelt   method(    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR      s    c         C   sO   xH |  j  j D]: } t |  | d  ƒ } | d  k	 r |  j  j | | ƒ q q Wd  S(   N(   R   R   R   R   t   unsubscribe(   R   R	   R
   (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR      s    (   t   __name__t
   __module__R   R   R   (    (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR      s   		t   SignalHandlerc           B   s¢   e  Z i  Z xL e e ƒ j ƒ  D]8 \ Z Z e j d  ƒ r e j d ƒ r e e e <q q W[ [ d „  Z	 d „  Z
 d „  Z d d „ Z d d d „ Z d „  Z RS(	   t   SIGt   SIG_c         C   sC   | |  _  i |  j  j d 6|  j d 6|  j  j d 6|  _ i  |  _ d  S(   Nt   SIGTERMt   SIGHUPt   SIGUSR1(   R   t   exitt   handle_SIGHUPt   gracefult   handlerst   _previous_handlers(   R   R   (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR   9   s
    	
c         C   sL   xE |  j  j ƒ  D]4 \ } } y |  j | | ƒ Wq t k
 rC q Xq Wd  S(   N(   R   t	   iteritemst   set_handlert
   ValueError(   R   t   sigt   func(    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR   C   s
    c         C   sö   xï |  j  j ƒ  D]Þ \ } } |  j | } | d  k rU |  j j d | ƒ t j } n |  j j d | | f ƒ yE t j | | ƒ } | d  k r³ |  j j d | | f d d ƒn  Wq t	 k
 rí |  j j d | | f d d d t
 ƒq Xq Wd  S(	   Ns    Restoring %s handler to SIG_DFL.s   Restoring %s handler %r.s?   Restored old %s handler %r, but our handler was not registered.t   leveli   s    Unable to restore %s handler %r.i(   t	   traceback(   R   R   t   signalsR   R   t   logt   _signalt   SIG_DFLt   signalR   t   True(   R   t   signumt   handlert   signamet   our_handler(    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR   J   s    c         C   sà   t  | t ƒ rI t t | d  ƒ } | d  k r@ t d | ƒ ‚ n  | } n; y |  j | } Wn! t k
 r} t d | ƒ ‚ n X| } t j | |  j	 ƒ } | |  j
 | <| d  k	 rÜ |  j j d | ƒ |  j j | | ƒ n  d  S(   Ns   No such signal: %rs   Listening for %s.(   t
   isinstancet
   basestringR   R"   R   R   R    t   KeyErrorR$   t   _handle_signalR   R   R!   R   (   R   R$   t   listenerR&   R(   t   prev(    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR   ^   s    		c         C   s5   |  j  | } |  j j d | ƒ |  j j | ƒ d  S(   Ns   Caught signal %s.(   R    R   R!   t   publish(   R   R&   t   frameR(   (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR-   z   s    c         C   sY   t  j t j j ƒ  ƒ r8 |  j j d ƒ |  j j ƒ  n |  j j d ƒ |  j j ƒ  d  S(   Ns*   SIGHUP caught but not daemonized. Exiting.s+   SIGHUP caught while daemonized. Restarting.(	   t   ost   isattyt   syst   stdint   filenoR   R!   R   t   restart(   R   (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR   €   s
    N(   R   R   R    t   varsR"   t   itemst   kt   vt
   startswithR   R   R   R   R   R-   R   (    (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR   '   s   	
		t   DropPrivilegesc           B   s¡   e  Z d d d d  „ Z d „  Z d „  Z e e e d d ƒZ d „  Z d „  Z	 e e e	 d d ƒZ
 d „  Z d	 „  Z e e e d d
 ƒZ d „  Z d e _ RS(   c         C   s8   t  j |  | ƒ t |  _ | |  _ | |  _ | |  _ d  S(   N(   R   R   t   Falset	   finalizedt   uidt   gidt   umask(   R   R   RB   R@   RA   (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR   –   s
    			c         C   s   |  j  S(   N(   t   _uid(   R   (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyt   _get_uid   s    c         C   sl   | d  k	 r_ t d  k r7 |  j j d d d ƒd  } q_ t | t ƒ r_ t j | ƒ d } q_ n  | |  _ d  S(   Ns'   pwd module not available; ignoring uid.R   i   i   (   R   t   pwdR   R!   R*   R+   t   getpwnamRC   (   R   t   val(    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyt   _set_uidŸ   s    	t   docs   The uid under which to run.c         C   s   |  j  S(   N(   t   _gid(   R   (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyt   _get_gidª   s    c         C   sl   | d  k	 r_ t d  k r7 |  j j d d d ƒd  } q_ t | t ƒ r_ t j | ƒ d } q_ n  | |  _ d  S(   Ns'   grp module not available; ignoring gid.R   i   i   (   R   t   grpR   R!   R*   R+   t   getgrnamRJ   (   R   RG   (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyt   _set_gid¬   s    	s   The gid under which to run.c         C   s   |  j  S(   N(   t   _umask(   R   (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyt
   _get_umask·   s    c         C   sW   | d  k	 rJ y t j WqJ t k
 rF |  j j d d d ƒd  } qJ Xn  | |  _ d  S(   Ns-   umask function not available; ignoring umask.R   i   (   R   R2   RB   t   AttributeErrorR   R!   RO   (   R   RG   (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyt
   _set_umask¹   s    s   The umask under which to run.c         C   sž  d „  } |  j  rM |  j d  k o- |  j d  k s|  j j d | ƒ  ƒ qn¸ |  j d  k r“ |  j d  k r“ t sw t r|  j j d d d ƒqnr |  j j d | ƒ  ƒ |  j d  k	 rÌ t j	 |  j ƒ n  |  j d  k	 rî t j
 |  j ƒ n  |  j j d | ƒ  ƒ |  j  r:|  j d  k	 r‘|  j j d |  j ƒ q‘nW |  j d  k rb|  j j d	 d d ƒn/ t j |  j ƒ } |  j j d
 | |  j f ƒ t |  _  d  S(   Nc          S   sZ   d \ }  } t r. t j t j ƒ  ƒ d }  n  t rP t j t j ƒ  ƒ d } n  |  | f S(   Ni    (   NN(   R   RE   t   getpwuidR2   t   getuidRL   t   getgrgidt   getgid(   t   namet   group(    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyt   current_idsÆ   s    s"   Already running as uid: %r gid: %rs   uid/gid not setR   i   s   Started as uid: %r gid: %rs   Running as uid: %r gid: %rs   umask already set to: %03os   umask not sets   umask old: %03o, new: %03o(   R?   R@   R   RA   R   R!   RE   RL   R2   t   setgidt   setuidRB   R%   (   R   RY   t	   old_umask(    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyt   startÄ   s.    				iM   N(   R   R   R   R   RD   RH   t   propertyR@   RK   RN   RA   RP   RR   RB   R]   t   priority(    (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR=      s   										+t
   Daemonizerc           B   s,   e  Z d  d  d  d „ Z d „  Z d e _ RS(   s	   /dev/nullc         C   s8   t  j |  | ƒ | |  _ | |  _ | |  _ t |  _ d  S(   N(   R   R   R5   t   stdoutt   stderrR>   R?   (   R   R   R5   Ra   Rb   (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR     s
    			c         C   s>  |  j  r |  j j d ƒ n  t j ƒ  d k rQ |  j j d t j ƒ  d d ƒn  t j j ƒ  t j	 j ƒ  y< t
 j ƒ  } | d k r‰ n |  j j d ƒ t
 j d ƒ Wn: t k
 rã } t j d t j d | j | j f ƒ n Xt
 j ƒ  y< t
 j ƒ  } | d k r)|  j j d	 ƒ t
 j d ƒ n  Wn: t k
 rf} t j d
 t j d | j | j f ƒ n Xt
 j d ƒ t
 j d ƒ t |  j d ƒ } t |  j d ƒ } t |  j	 d d ƒ } t
 j | j ƒ  t j j ƒ  ƒ t
 j | j ƒ  t j j ƒ  ƒ t
 j | j ƒ  t j	 j ƒ  ƒ |  j j d t
 j ƒ  ƒ t |  _  d  S(   Ns   Already deamonized.i   sH   There are %r active threads. Daemonizing now may cause strange failures.R   i   i    s   Forking once.s   %s: fork #1 failed: (%d) %s
s   Forking twice.s   %s: fork #2 failed: (%d) %s
t   /t   rs   a+s   Daemonized to PID: %s(   R?   R   R!   t	   threadingt   activeCountt	   enumerateR4   Ra   t   flushRb   R2   t   forkt   _exitt   OSErrorR   t   argvt   errnot   strerrort   setsidt   chdirRB   t   openR5   t   dup2R6   t   getpidR%   (   R   t   pidt   exct   sit   sot   se(    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR]     sF    		"
	"iA   (   R   R   R   R]   R_   (    (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR`   ò   s   	At   PIDFilec           B   s,   e  Z d  „  Z d „  Z d e _ d „  Z RS(   c         C   s&   t  j |  | ƒ | |  _ t |  _ d  S(   N(   R   R   t   pidfileR>   R?   (   R   R   Rz   (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR   R  s    	c         C   s~   t  j ƒ  } |  j r5 |  j j d | |  j f ƒ nE t |  j d ƒ j t | ƒ ƒ |  j j d | |  j f ƒ t	 |  _ d  S(   Ns   PID %r already written to %r.t   wbs   PID %r written to %r.(
   R2   Rs   R?   R   R!   Rz   Rq   t   writet   strR%   (   R   Rt   (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR]   W  s    	 iF   c         C   sR   y+ t  j |  j ƒ |  j j d |  j ƒ Wn  t t f k
 rG ‚  n n Xd  S(   Ns   PID file removed: %r.(   R2   t   removeRz   R   R!   t   KeyboardInterruptt
   SystemExit(   R   (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR   a  s    (   R   R   R   R]   R_   R   (    (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyRy   O  s   			t   PerpetualTimerc           B   s   e  Z d  „  Z RS(   c         C   sM   xF t  rH |  j j |  j ƒ |  j j ƒ  r/ d  S|  j |  j |  j Ž  q Wd  S(   N(   R%   t   finishedt   waitt   intervalt   isSett   functiont   argst   kwargs(   R   (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyt   runn  s
    	(   R   R   R‰   (    (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR   k  s   t   Monitorc           B   s>   e  Z d  Z d  d „ Z d „  Z d e _ d „  Z d „  Z RS(   i<   c         C   s/   t  j |  | ƒ | |  _ | |  _ d  |  _ d  S(   N(   R   R   t   callbackt	   frequencyR   t   thread(   R   R   R‹   RŒ   (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR   €  s    		c         C   s‘   |  j  d k r |  j j } |  j d  k rv t |  j  |  j ƒ |  _ |  j j | ƒ |  j j ƒ  |  j	 j
 d | ƒ q |  j	 j
 d | ƒ n  d  S(   Ni    s   Started monitor thread %r.s"   Monitor thread %r already started.(   RŒ   t	   __class__R   R   R   R   R‹   t   setNameR]   R   R!   (   R   t
   threadname(    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR]   †  s    iF   c         C   sŽ   |  j  d  k r, |  j j d |  j j ƒ n^ |  j  t j ƒ  k	 r |  j  j ƒ  } |  j  j	 ƒ  |  j  j
 ƒ  |  j j d | ƒ n  d  |  _  d  S(   Ns   No thread running for %s.s   Stopped thread %r.(   R   R   R   R!   RŽ   R   Re   t   currentThreadt   getNamet   cancelt   join(   R   RW   (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyt   stop“  s    c         C   s   |  j  ƒ  |  j ƒ  d  S(   N(   R•   R]   (   R   (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR   Ÿ  s    
(   R   R   RŒ   R   R]   R_   R•   R   (    (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyRŠ   v  s   			t   Autoreloaderc           B   s>   e  Z d  Z d Z d  d d „ Z d „  Z d e _ d „  Z RS(   i   s   .*c         C   s;   i  |  _  t ƒ  |  _ | |  _ t j |  | |  j | ƒ d  S(   N(   t   mtimest   sett   filest   matchRŠ   R   R‰   (   R   R   RŒ   Rš   (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR   «  s    		c         C   s,   |  j  d  k r i  |  _ n  t j |  ƒ d  S(   N(   R   R   R—   RŠ   R]   (   R   (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR]   ±  s    iF   c         C   s±  t  ƒ  } x‡ t j j ƒ  D]v \ } } t j |  j | ƒ r t | d ƒ rm t | j d ƒ rm | j j } qm n  t	 | d d  ƒ } | j | ƒ q q Wx| |  j BD]} | r¡ | j d ƒ rÉ | d  } n  |  j j | d ƒ } | d  k rð q¡ n  y t j | ƒ j } Wn t k
 rd  } n X| |  j k r?| |  j | <q©| d  k sW| | k r©|  j j d | ƒ |  j j ƒ  |  j j d |  j j ƒ  ƒ |  j j ƒ  d  Sq¡ q¡ Wd  S(	   Nt
   __loader__t   archivet   __file__s   .pyciÿÿÿÿi    s   Restarting because %s changed.s   Stopped thread %r.(   R˜   R4   t   modulesR9   t   reRš   t   hasattrR›   Rœ   R   R   t   addR™   t   endswithR—   t   getR2   t   statt   st_mtimeRk   R   R!   R   R“   R’   R7   (   R   t   sysfilesR:   t   mt   filenamet   oldtimet   mtime(    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR‰   ¸  s6    	
(   R   R   RŒ   Rš   R   R]   R_   R‰   (    (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR–   ¥  s   		t   ThreadManagerc           B   s2   e  Z d  „  Z d „  Z d „  Z d „  Z e Z RS(   c         C   sO   i  |  _  t j |  | ƒ |  j j j d t ƒ  ƒ |  j j j d t ƒ  ƒ d  S(   Nt   acquire_threadt   release_thread(   t   threadsR   R   R   R   t
   setdefaultR˜   (   R   R   (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR   ð  s    	c         C   sU   t  j ƒ  } | |  j k rQ t |  j ƒ d } | |  j | <|  j j d | ƒ n  d  S(   Ni   t   start_thread(   Re   t
   _get_identR®   t   lenR   R0   (   R   t   thread_identt   i(    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR¬   ö  s
    c         C   sG   t  j ƒ  } |  j j | d  ƒ } | d  k	 rC |  j j d | ƒ n  d  S(   Nt   stop_thread(   Re   R±   R®   t   popR   R   R0   (   R   R³   R´   (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR­     s    c         C   sD   x0 |  j  j ƒ  D] \ } } |  j j d | ƒ q W|  j  j ƒ  d  S(   NRµ   (   R®   R   R   R0   t   clear(   R   R³   R´   (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR•     s    (   R   R   R   R¬   R­   R•   R   (    (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyR«   à  s
   				(   NN(   R2   RŸ   R˜   t	   NameErrort   setsR    R$   R"   R4   t   timeRe   t   objectR   R   RE   RL   t   ImportErrorR   R=   R`   Ry   t   _TimerR   RŠ   R–   R«   (    (    (    s,   /var/www/SABnzbd/cherrypy/process/plugins.pyt   <module>   s,   cb]/;