
    !                         S SK r S SKrS SKrS SKrS SKrS SKrS SKJr  \R                  " \	5      r
S r " S S\5      r " S S\5      r " S S	\5      r\R                   S
:X  a  S SKr\rOS SKr\rS rg)    N)_utilsc                 .    [         R                  " U 5        g! [         aq  nUR                  [        R                  :X  a*  [         R
                  R                  U 5      (       d  e  SnAgUR                  [        R                  :X  a   SnAge SnAff = f)z^Create a directory (and any ancestor directories required).

:param path: Directory to create
TNF)osmakedirsOSErrorerrnoEEXISTpathisdirEISDIR)r
   es     )lib/third_party/fasteners/process_lock.py_ensure_treer      si    

D   	77ell"77==&&WW$	s!    
BAB+BBBc                       \ rS rSrSrSr Sr \R                  S4S jr	S r
S rS	\\S4S
 jrS rS rS rS rS rS rS rSrg)_InterProcessLock4   a   An interprocess locking implementation.

This is a lock implementation which allows multiple locks, working around
issues like http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=632857 and
does not require any cleanup. Since the lock is always held on a file
descriptor rather than outside of the process, the lock gets dropped
automatically if the process crashes, even if ``__exit__`` is not
executed.

There are no guarantees regarding usage by multiple threads in a
single process here. This lock works only between processes.

Note these locks are released when the descriptor is closed, so it's not
safe to close the file descriptor while another thread holds the
lock. Just opening and closing the lock file can break synchronization,
so lock files must be accessed only using this abstraction.
g?g{Gz?Nc                 x    S U l         Xl        SU l        X l        [        R
                  " U[        5      U l        g )NF)lockfiler
   acquired
sleep_funcr   pick_first_not_noneLOGlogger)selfr
   r   r   s       r   __init___InterProcessLock.__init__U   s.    	$00=    c                 ^    U R                  5         g! [         a  nUR                  [        R                  [        R                  4;   a6  U(       a  UR                  5       (       a   S nAg[        R                  " 5       e[        R                  " SU R                  US.-  5      eS nAff = f)NTFz9Unable to acquire lock on `%(path)s` due to %(exception)s)r
   	exception)trylockIOErrorr   EACCESEAGAINexpiredr   
RetryAgain	threadingThreadErrorr
   )r   blockingwatchr   s       r   _try_acquire_InterProcessLock._try_acquire\   s    	LLN    	/ww5<<665==??  ++--++ -= 9=		=>-.-. / /	/s    
B,A
B',;B''B,c                 f   [         R                  R                  U R                  5      nU(       a=  [        U5      nU(       a+  U R                  R                  [        R                  SU5        U R                  b  U R                  R                  (       a  [        U R                  S5      U l        g g )NzCreated lock base path `%s`a)r   r
   dirnamer   r   logr   BLATHERr   closedopen)r   basedirmade_basedirs      r   _do_open_InterProcessLock._do_openp   ss    ''//$)),'0L =wH
 == DMM$8$8 C0DM %9r   Tc                    US:  a  [        S5      eUb  US:  a  [        S5      eX#:  a  UnU R                  5         [        R                  " US9n[        R                  " X#U R
                  US9nU   U" U R                  X5      nSSS5        W(       d  SU l        gSU l        U R                  R                  [        R                  S	U R                  UR                  5       UR                  5        g! , (       d  f       Nt= f)
a~  Attempt to acquire the given lock.

:param blocking: whether to wait forever to try to acquire the lock
:type blocking: bool
:param delay: when blocking this is the delay time in seconds that
              will be added after each failed acquisition
:type delay: int/float
:param max_delay: the maximum delay to have (this limits the
                  accumulated delay(s) added after each failed
                  acquisition)
:type max_delay: int/float
:param timeout: an optional timeout (limits how long blocking
                will occur for)
:type timeout: int/float
:returns: whether or not the acquisition succeeded
:rtype: bool
r   z+Delay must be greater than or equal to zeroNz-Timeout must be greater than or equal to zero)duration)r   r)   FTzHAcquired file lock `%s` after waiting %0.3fs [%s attempts were required])
ValueErrorr5   r   	StopWatchRetryr   r*   r   r   r/   r0   r
   elapsedattempts)r   r(   delay	max_delaytimeoutr)   rgottens           r   acquire_InterProcessLock.acquire}   s    ( 19JKK7Q;LMMI  '2LL$(OO5Bt(((:F !DM DMKKOOFNN78<		!MMOQZZ9  Us   3C55
Dc                 b    U R                   b"  U R                   R                  5         S U l         g g N)r   closer   s    r   	_do_close_InterProcessLock._do_close   s(    ==$MM! DM %r   c                 &    U R                  5         U $ rF   )rC   rH   s    r   	__enter___InterProcessLock.__enter__   s    r   c                    U R                   (       d  [        R                  " S5      e U R                  5         SU l          U R	                  5         U R
                  R                  [        R                  SU R                  5        g! [         a)    U R
                  R                  SU R                  5         gf = f! [         a)    U R
                  R                  SU R                  5         gf = f)z%Release the previously acquired lock.z$Unable to release an unacquired lockFz*Unlocked and closed file lock open on `%s`z.Could not close the file handle opened on `%s`z1Could not unlock the acquired lock opened on `%s`N)r   r&   r'   unlockrI   r   r/   r   r0   r
   r!   r   rH   s    r   release_InterProcessLock.release   s    }}'' )0 1 1	4KKM
 "DM4 
 !()-4	  D%% '89=DD  	9KK!! #-.2ii9	9s#   B= B 0B:9B:=0C0/C0c                 $    U R                  5         g rF   )rP   )r   exc_typeexc_valexc_tbs       r   __exit___InterProcessLock.__exit__   s    r   c                 T    [         R                  R                  U R                  5      $ )z<Checks if the path that this lock exists at actually exists.)r   r
   existsrH   s    r   rY   _InterProcessLock.exists   s    ww~~dii((r   c                     [        5       erF   NotImplementedErrorrH   s    r   r    _InterProcessLock.trylock       !##r   c                     [        5       erF   r\   rH   s    r   rO   _InterProcessLock.unlock   r_   r   )r   r   r   r
   r   )__name__
__module____qualname____firstlineno____doc__	MAX_DELAYDELAY_INCREMENTtimesleepr   r*   r5   rC   rI   rL   rP   rV   rY   r    rO   __static_attributes__ r   r   r   r   4   so    $ I
 O )-

4 >(1  $%)V!
4,)$$r   r   c                   $    \ rS rSrSrS rS rSrg)_WindowsLock   z?Interprocess lock implementation that works on windows systems.c                     [         R                  " U R                  R                  5       [         R                  S5        g N   )msvcrtlockingr   filenoLK_NBLCKrH   s    r   r    _WindowsLock.trylock   #    t}}++-vBr   c                     [         R                  " U R                  R                  5       [         R                  S5        g rq   )rs   rt   r   ru   LK_UNLCKrH   s    r   rO   _WindowsLock.unlock   rx   r   rl   Nrb   rc   rd   re   rf   r    rO   rk   rl   r   r   rn   rn      s    ICCr   rn   c                   $    \ rS rSrSrS rS rSrg)
_FcntlLock   z=Interprocess lock implementation that works on posix systems.c                     [         R                  " U R                  [         R                  [         R                  -  5        g rF   )fcntllockfr   LOCK_EXLOCK_NBrH   s    r   r    _FcntlLock.trylock   s!    DMM5==5==#@Ar   c                 b    [         R                  " U R                  [         R                  5        g rF   )r   r   r   LOCK_UNrH   s    r   rO   _FcntlLock.unlock   s    DMM5==1r   rl   Nr|   rl   r   r   r~   r~      s    GB2r   r~   ntc                 *   ^ [        U 5      mU4S jnU$ )zLAcquires & releases a interprocess lock around call into
decorated function.c                 J   >^  [         R                  " T 5      U U4S j5       nU$ )Nc                  N   > T   T" U 0 UD6sS S S 5        $ ! , (       d  f       g = frF   rl   )argskwargsflocks     r   wrapper7interprocess_locked.<locals>.decorator.<locals>.wrapper   s    $)&) s   
$)sixwraps)r   r   r   s   ` r   	decorator&interprocess_locked.<locals>.decorator   s$    	1	* 
	* r   )InterProcessLock)r
   r   r   s     @r   interprocess_lockedr      s     D!D r   )r   loggingr   r&   ri   r   	fastenersr   	getLoggerrb   r   r   objectr   rn   r~   namers   r   r   r   rl   r   r   <module>r      s   $   	   
 !*^$ ^$BC$ C2" 2 77d?#!r   