ó
ßüÚ\c           @   s  d  d l  Z  d  d l Z d  d l Z d  d l Z d g Z y d  d l Z Wnü e k
 rGy( d  d l Z d  d l Z d  d l	 Z	 Wn e k
 r  e d ƒ ‚ qiXe j
 d d e ƒZ e j Z e j j e _ e j j g e j j g d e _ e j Z e j j e _ e j j g e j j g d e _ e Z d „  Z d „  Z n" Xe Z d	 „  Z d
 „  Z d „  Z e j ƒ  Z e j ƒ  Z d „  Z  d „  Z! d e" f d „  ƒ  YZ# d „  Z$ d e% f d „  ƒ  YZ& d e% f d „  ƒ  YZ' d e% f d „  ƒ  YZ( d e% f d „  ƒ  YZ) d S(   iÿÿÿÿNt	   lock_files?   Platform not supported (failed to import fcntl, ctypes, msvcrt)t   kernel32t   use_last_errori   c         C   sc   t  t j |  j ƒ  ƒ d d d d ƒ } | r1 t St j ƒ  } | d k r[ t j | ƒ ‚ n  t Sd  S(   Ni    i   i!   (	   t   _WinAPI_LockFilet   msvcrtt   get_osfhandlet   filenot   Truet   ctypest   get_last_errort   WinErrort   False(   t   file_t   rest   err(    (    s1   lib/python2.7/site-packages/distributed/locket.pyt   _lock_file_non_blocking)   s    'c         C   s)   t  t j |  j ƒ  ƒ d d d d ƒ d  S(   Ni    i   (   t   _WinAPI_UnlockFileR   R   R   (   R   (    (    s1   lib/python2.7/site-packages/distributed/locket.pyt   _unlock_file4   s    c         C   s   t  j |  j ƒ  t  j ƒ d  S(   N(   t   fcntlt   flockR   t   LOCK_EX(   R   (    (    s1   lib/python2.7/site-packages/distributed/locket.pyt   _lock_file_blocking;   s    c         C   sd   y( t  j |  j ƒ  t  j t  j Bƒ t SWn5 t k
 r_ } | j t j t j	 g k rY t
 S‚  n Xd  S(   N(   R   R   R   R   t   LOCK_NBR   t   IOErrort   errnot   EACCESt   EAGAINR   (   R   t   error(    (    s1   lib/python2.7/site-packages/distributed/locket.pyR   >   s     c         C   s   t  j |  j ƒ  t  j ƒ d  S(   N(   R   R   R   t   LOCK_UN(   R   (    (    s1   lib/python2.7/site-packages/distributed/locket.pyR   H   s    c         K   s]   t  j ƒ  z8 t j |  ƒ } | d  k rA t |  ƒ } | t |  <n  Wd  t  j ƒ  Xt | |  S(   N(   t   _locks_lockt   acquiret   _lockst   gett   Nonet   _create_lock_filet   releaset   _Locker(   t   patht   kwargst   lock(    (    s1   lib/python2.7/site-packages/distributed/locket.pyR    P   s    
c         C   s(   t  |  ƒ } t |  ƒ } t | | g ƒ S(   N(   t   _ThreadLockt	   _LockFilet   _LockSet(   R%   t   thread_lockt	   file_lock(    (    s1   lib/python2.7/site-packages/distributed/locket.pyR"   \   s    t	   LockErrorc           B   s   e  Z RS(    (   t   __name__t
   __module__(    (    (    s1   lib/python2.7/site-packages/distributed/locket.pyR-   b   s   c         C   sŒ   | d  k r d } n  t j ƒ  } xd t r‡ |  ƒ  } | r= d  S| d  k	 rw t j ƒ  | | k rw t d j | ƒ ƒ ‚ q$ t j | ƒ q$ Wd  S(   Ngš™™™™™©?s   Couldn't lock {0}(   R!   t   timeR   R-   t   formatt   sleep(   R   t   timeoutt   retry_periodR%   t
   start_timet   success(    (    s1   lib/python2.7/site-packages/distributed/locket.pyt   _acquire_non_blockingf   s    			"R*   c           B   s#   e  Z d  „  Z d „  Z d „  Z RS(   c         C   s   | |  _  d  S(   N(   R   (   t   selft   locks(    (    s1   lib/python2.7/site-packages/distributed/locket.pyt   __init__v   s    c         C   sm   g  } y5 x. |  j  D]# } | j | | ƒ | j | ƒ q WWn+ x t | ƒ D] } | j ƒ  qN W‚  n Xd  S(   N(   R   R   t   appendt   reversedR#   (   R8   R3   R4   t   acquired_locksR'   t   acquired_lock(    (    s1   lib/python2.7/site-packages/distributed/locket.pyR   y   s    c         C   s(   x! t  |  j ƒ D] } | j ƒ  q Wd  S(   N(   R<   R   R#   (   R8   R'   (    (    s1   lib/python2.7/site-packages/distributed/locket.pyR#   …   s    (   R.   R/   R:   R   R#   (    (    (    s1   lib/python2.7/site-packages/distributed/locket.pyR*   u   s   		R(   c           B   s)   e  Z d  „  Z d d d „ Z d „  Z RS(   c         C   s   | |  _  t j ƒ  |  _ d  S(   N(   t   _patht	   threadingt   Lockt   _lock(   R8   R%   (    (    s1   lib/python2.7/site-packages/distributed/locket.pyR:   Œ   s    	c      	      sK   | d  k r ˆ  j j ƒ  n+ t d ‡  f d †  d | d | d ˆ  j ƒ d  S(   NR   c              s   ˆ  j  j t ƒ S(   N(   RB   R   R   (    (   R8   (    s1   lib/python2.7/site-packages/distributed/locket.pyt   <lambda>•   t    R3   R4   R%   (   R!   RB   R   R7   R?   (   R8   R3   R4   (    (   R8   s1   lib/python2.7/site-packages/distributed/locket.pyR      s    c         C   s   |  j  j ƒ  d  S(   N(   RB   R#   (   R8   (    (    s1   lib/python2.7/site-packages/distributed/locket.pyR#   ›   s    N(   R.   R/   R:   R!   R   R#   (    (    (    s1   lib/python2.7/site-packages/distributed/locket.pyR(   ‹   s   	R)   c           B   s)   e  Z d  „  Z d d d „ Z d „  Z RS(   c         C   s%   | |  _  d  |  _ t j ƒ  |  _ d  S(   N(   R?   R!   t   _fileR@   RA   t   _thread_lock(   R8   R%   (    (    s1   lib/python2.7/site-packages/distributed/locket.pyR:       s    		c      	      sx   ˆ  j  d  k r' t ˆ  j d ƒ ˆ  _  n  | d  k rI t rI t ˆ  j  ƒ n+ t d ‡  f d †  d | d | d ˆ  j ƒ d  S(   Nt   wbR   c              s   t  ˆ  j ƒ S(   N(   R   RE   (    (   R8   (    s1   lib/python2.7/site-packages/distributed/locket.pyRC   ¬   RD   R3   R4   R%   (   RE   R!   t   openR?   t   _lock_file_blocking_availableR   R7   (   R8   R3   R4   (    (   R8   s1   lib/python2.7/site-packages/distributed/locket.pyR   ¥   s    c         C   s'   t  |  j ƒ |  j j ƒ  d  |  _ d  S(   N(   R   RE   t   closeR!   (   R8   (    (    s1   lib/python2.7/site-packages/distributed/locket.pyR#   ²   s    N(   R.   R/   R:   R!   R   R#   (    (    (    s1   lib/python2.7/site-packages/distributed/locket.pyR)   Ÿ   s   	R$   c           B   sA   e  Z d  Z d d d „ Z d „  Z d „  Z d „  Z d „  Z RS(   sg   
    A lock wrapper to always apply the given *timeout* and *retry_period*
    to acquire() calls.
    c         C   s   | |  _  | |  _ | |  _ d  S(   N(   RB   t   _timeoutt   _retry_period(   R8   R'   R3   R4   (    (    s1   lib/python2.7/site-packages/distributed/locket.pyR:   ¾   s    		c         C   s   |  j  j |  j |  j ƒ d  S(   N(   RB   R   RK   RL   (   R8   (    (    s1   lib/python2.7/site-packages/distributed/locket.pyR   Ã   s    c         C   s   |  j  j ƒ  d  S(   N(   RB   R#   (   R8   (    (    s1   lib/python2.7/site-packages/distributed/locket.pyR#   Æ   s    c         C   s   |  j  ƒ  |  S(   N(   R   (   R8   (    (    s1   lib/python2.7/site-packages/distributed/locket.pyt	   __enter__É   s    
c         G   s   |  j  ƒ  d  S(   N(   R#   (   R8   t   args(    (    s1   lib/python2.7/site-packages/distributed/locket.pyt   __exit__Í   s    N(	   R.   R/   t   __doc__R!   R:   R   R#   RM   RO   (    (    (    s1   lib/python2.7/site-packages/distributed/locket.pyR$   ¸   s   			(*   R0   R   R@   t   weakreft   __all__R   t   ImportErrorR   t   ctypes.wintypesR   t   WinDLLR   t	   _kernel32t   LockFileR   t   wintypest   BOOLt   restypet   HANDLEt   DWORDt   argtypest
   UnlockFileR   R   RI   R   R   R   RA   R   t   WeakValueDictionaryR   R    R"   t	   ExceptionR-   R7   t   objectR*   R(   R)   R$   (    (    (    s1   lib/python2.7/site-packages/distributed/locket.pyt   <module>   sT   						
				