ó
æ™xTc           @   sþ   d  d l  m 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 d „  ƒ  YZ d e j f d „  ƒ  YZ d e j f d „  ƒ  YZ d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d S(   iÿÿÿÿ(   t   divisionNt
   FileServerc           B   s   e  Z d  d  d „ Z RS(   t    c         C   ss   t  j d ƒ t j ƒ  } | | f } | r= t | | ƒ } n t | t ƒ } d | _ x | j rn | j ƒ  qX Wd  S(   Ni    i   (   t   syst   setcheckintervalt   sockett   getfqdnt   _FileServert   FileRequestHandlert
   keep_alivet   handle_request(   t   selft   portt   handlert   localt   server_addresst   ddbs(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyt   __init__   s    	(   t   __name__t
   __module__R   (    (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyR      s   R   c           B   s   e  Z d  „  Z RS(   c         C   s—   t  j j |  ƒ |  j j ƒ  \ } } | s8 | d k rG t j ƒ  } n  | |  _ | |  _ t j	 ƒ  d |  _
 t j ƒ  |  _ t j ƒ  |  _ i  |  _ d S(   s.   Override server_bind to store the server name.s   0.0.0.0i    N(   t   SocketServert   ThreadingTCPServert   server_bindR   t   getsocknameR   t   server_namet   server_portt	   threadingt	   enumeratet   main_threadt   Lockt
   ServerLockt   ost   getpidt   pidt   FileLockDictionary(   R   t   hostR   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyR   $   s    		(   R   R   R   (    (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyR   "   s   R   c           B   s¶   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d d „ Z d „  Z d „  Z d „  Z d „  Z RS(   c         C   s‡   |  j  j } d } |  j  j j ƒ  zM | | j ƒ  k r@ d } n  | | j ƒ  k r[ d } n  | rn | | | <n  Wd  |  j  j j ƒ  X| S(   Ni   i    (   t   serverR"   R   t   acquiret   keyst   valuest   release(   R   t   filet   idt   DICTt   rc(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyt	   _LockFile<   s    		c         C   sB   |  j  | | ƒ } x) | s= t j d ƒ |  j  | | ƒ } q Wd  S(   Ng{®Gáz„?(   R-   t   timet   sleep(   R   R)   R*   R,   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyt   LockFileK   s    	c         C   sS   y, t  | d ƒ } t j | ƒ } | j ƒ  Wn  t k
 rN d G| GHd  } n X| S(   Nt   rbs   failed to load(   t   opent   picklet   loadt   closet	   Exceptiont   None(   R   R)   t   ft   obj(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyt   ReadPickleFileQ   s    	
c         C   sN   y- t  | d ƒ } t j | | ƒ | j ƒ  Wn t k
 rI d G| GHn Xd  S(   Nt   wbs   failed to dump(   R2   R3   t   dumpR5   R6   (   R   R9   R)   R8   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyt   WritePickleFile[   s    c         C   sP   y) t  | d ƒ } | j ƒ  } | j ƒ  Wn  t k
 rK d G| GHd } n X| S(   NR1   s   failed to readR   (   R2   t   readR5   R6   (   R   R)   R8   t   lines(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyt   ReadFilec   s    	
c         C   sK   y* t  | d ƒ } | j | ƒ | j ƒ  Wn t k
 rF d G| GHn Xd  S(   NR;   s   failed to write(   R2   t   writeR5   R6   (   R   R?   R)   R8   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyt	   WriteFilem   s    c         C   s„   |  j  j } d } |  j  j j ƒ  zJ | | j ƒ  k r@ d } n  | | j ƒ  k r[ d } n  | rk | | =n  Wd  |  j  j j ƒ  X| S(   Ni   i    (   R$   R"   R   R%   R&   R'   R(   (   R   R)   R*   R+   R,   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyt   _UnlockFileu   s    		c         C   sB   |  j  | | ƒ } x) | s= t j d ƒ |  j  | | ƒ } q Wd  S(   Ng{®Gáz„?(   RC   R.   R/   (   R   R)   R*   R,   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyt
   UnlockFile„   s    	c         C   s   t  j j | ƒ S(   N(   R   t   patht   exists(   R   R)   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyt
   FileExistsŠ   s    c         C   s   |  j  ƒ  } | S(   N(   t   GetSocketConnection(   R   t	   server_sc(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyt   GetServerDetails�   s    c         C   s4   t  j |  j j t j ƒ  |  j j t j ƒ  ƒ } | S(   s,   
    Returns the socket connection data
    (   t
   FileSockett   SocketConnectionR$   R   R   t   getcwdR   R    (   R   t   s(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyRH   ™   s
    		c         C   s
   |  j  j S(   N(   R$   R   (   R   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyt   GetHost¤   s    c         C   s
   |  j  j S(   N(   R$   R   (   R   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyt   GetPort§   s    c         C   sK   t  j  d | d | ƒ } y | j ƒ  |  j j ƒ  Wn t k
 rF n Xd  S(   NR#   R   (   t
   FileClientt   testert   eventt   setR6   (   R   R#   R   t   client(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyt   _tester­   s    	
i   c         C   s{   t  j ƒ  |  _ |  j j ƒ  t  j d |  j d | | f ƒ } | j d ƒ | j ƒ  |  j j | ƒ |  j j	 ƒ  rw d Sd  S(   Nt   targett   argsi   (   R   t   EventRS   t   cleart   ThreadRV   t	   setDaemont   startt   waitt   isSetR7   (   R   R#   R   t   timeoutt   t(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyt
   ClientTest¶   s    
c         C   s
   t  j ƒ  S(   N(   R   R    (   R   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyR    È   s    c         C   s8   |  j  j j ƒ  z d |  j  _ Wd  |  j  j j ƒ  Xd  S(   Ni    (   R$   R   R%   R	   R(   (   R   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyt   shutdownË   s    c         C   s&   d t  |  ƒ t  |  j j ƒ t j f S(   s5   
    Called by client to test if server is alive
    s    Hello from
	%s
 on %s %s(   t   strR$   R   R   t   platform(   R   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyRR   Ò   s    c         C   sÖ  y˜x‘|  j  j r–y t j |  j ƒ } Wn t k
 r< Pn X| d k rX d |  j  _ n  y t j |  j ƒ } Wn) t k
 r™ d } d G| GHd G| GHn Xy t j |  j ƒ } Wn  t k
 rÒ i  } d G| GHn Xt |  | ƒ } y, t | | | ƒ } t j | |  j	 d ƒ Wn] t k
 rmy t j d |  j	 d ƒ Wn t k
 rKn Xd G| GHd G| GHd	 G| GH‚  n Xy |  j	 j
 ƒ  Wq t k
 r’q Xq WWn* t k
 rÄ|  j j d
 GHt j ƒ  n Xt j j
 ƒ  d S(   s<   
    This method handles the arguments of a remote call
    Rc   i    t   Methods   error in args s   error in kw i   s   0
s   Args  s   Kw    s	   .handle: N(    (   R$   R	   R3   R4   t   rfileR6   t   getattrt   applyR<   t   wfilet   flusht	   __class__R   t	   tracebackt	   print_excR   t   stdout(   R   t   methodRX   t   kwt   meth_objt   result(    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyt   handleÜ   sR    				
(   R   R   R-   R0   R:   R=   R@   RB   RC   RD   RG   RJ   RH   RO   RP   RV   Rb   R    Rc   RR   Rt   (    (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyR   6   s&   			
		
														
c          C   sA   t  j ƒ  }  |  j ƒ  d } t j d t d | f ƒ } | | f S(   Ni   RW   RX   (   RK   t	   GetSocketR   R   R[   R   (   RN   R   Ra   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyt   StartServer   s
    c         C   s~   d } xq | ry y, t  j  d |  d | ƒ } | j ƒ  d } Wq	 t k
 ru t j d ƒ | d 7} | d k rv d  Sq	 Xq	 W| S(   Ni   R#   R   i    gš™™™™™É?i
   (   RQ   RR   R6   R.   R/   R7   (   R#   R   t   nobindRU   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyt
   BindClient(  s    		


c          C   sa   d  }  d  } y2 t d d ƒ } t j | ƒ \ }  } | j ƒ  Wn t k
 rV d GHn X|  | f S(   Ns   server.portR1   s   failed to read server port file(   R7   R2   R3   R4   R5   R6   (   R#   R   R8   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyt   ReadServerFile9  s    	c         C   sV   y9 t  d d ƒ } t j t j ƒ  |  f | ƒ | j ƒ  Wn t k
 rQ d GHn Xd  S(   Ns   server.portR;   s    failed to write server port file(   R2   R3   R<   R   R   R5   R6   (   R   R8   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyt   WriteServerFileD  s    c          C   s1   t  ƒ  \ }  } d  } |  r- t |  | ƒ } n  | S(   N(   Ry   R7   Rx   (   R#   R   RU   (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyt   GetServerClientO  s
    (    (   t
   __future__R    R   R3   R   R   Rm   R   R   R.   RQ   RK   R   R   R   t   StreamRequestHandlerR   Rv   Rx   Ry   Rz   R{   (    (    (    sF   /public2/ftp/pub/ccp4/6.5/ccp4-6.5/lib/py2/libtbx/server/FileServer.pyt   <module>   s$   ê				