
    !                         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mZ  ej                  e	      Z
d Z G d de      Z G d de      Z G d d	e      Zej                   d
k(  r
d dlZeZd Zyd dlZeZd Zy)    N)_utilsc                    	 t        j                  |        y# t        $ rj}|j                  t        j                  k(  r%t         j
                  j                  |       s Y d}~y|j                  t        j                  k(  rY d}~y d}~ww xY w)zfCreate a directory (and any ancestor directories required).

    :param path: Directory to create
    TNF)osmakedirsOSErrorerrnoEEXISTpathisdirEISDIR)r
   es     1platform/bq/third_party/fasteners/process_lock.py_ensure_treer       sf    

D   	77ell"77==&WW$	s     	B=B#BBBc                       e Zd ZdZdZ	 dZ	 ej                  dfdZd Z	d Z
deedfd	Zd
 Zd Zd Zd Zd Zd Zd Zy)_InterProcessLockaT  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                 z    d | _         || _        d| _        || _        t	        j
                  |t              | _        y )NF)lockfiler
   acquired
sleep_funcr   pick_first_not_noneLOGlogger)selfr
   r   r   s       r   __init__z_InterProcessLock.__init__V   s2    	$00=    c                 @   	 | j                          y# t        $ r}|j                  t        j                  t        j                  fv r+|r|j                         rY d }~yt        j                         t        j                  d| j                  |dz        d }~ww xY w)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_acquirez_InterProcessLock._try_acquire]   s    	LLN    	/ww5<<665==?  ++--++ -= 9=		=>-.-. / /	/s    	B>B9BBc                 F   t         j                  j                  | j                        }|r8t        |      }|r+| j                  j                  t        j                  d|       | j                  | j                  j                  rt        | j                  d      | _        y y )NzCreated lock base path `%s`a)r   r
   dirnamer   r   logr   BLATHERr   closedopen)r   basedirmade_basedirs      r   _do_openz_InterProcessLock._do_openq   ss    ''//$)),'0L =wH
 == DMM$8$8 C0DM %9r   Tc                    |dk  rt        d      ||dk  rt        d      ||k\  r|}| j                          t        j                  |      }t        j                  ||| j
                  |      }|5   || j                  ||      }ddd       sd| _        yd| _        | j                  j                  t        j                  d	| j                  |j                         |j                         y# 1 sw Y   jxY w)
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])
ValueErrorr2   r   	StopWatchRetryr   r(   r   r   r,   r-   r
   elapsedattempts)r   r&   delay	max_delaytimeoutr'   rgottens           r   acquirez_InterProcessLock.acquire~   s    ( 19JKK7Q;LMMII  '2LL	$(OO5Bt(((E:F !DM DMKKOOFNN78<		!MMOQZZ9  Us   5C33C<c                 `    | j                   "| j                   j                          d | _         y y N)r   closer   s    r   	_do_closez_InterProcessLock._do_close   s(    ==$MM! DM %r   c                 &    | j                          | S rA   )r?   rC   s    r   	__enter__z_InterProcessLock.__enter__   s    r   c                    | j                   st        j                  d      	 | j                          d| _         	 | j	                          | j
                  j                  t        j                  d| j                         y# t        $ r) | j
                  j                  d| j                         Y yw xY w# t        $ r) | j
                  j                  d| j                         Y yw xY w)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%   unlockrD   r   r,   r   r-   r
   r   r   rC   s    r   releasez_InterProcessLock.release   s    }}'' )0 1 1	4KKM
 "DM4 
 !()-4	  D%% '89=DD  	9KK!! #-.2ii9	9s"   B6 B /B32B36/C('C(c                 $    | j                          y rA   )rI   )r   exc_typeexc_valexc_tbs       r   __exit__z_InterProcessLock.__exit__   s    r   c                 T    t         j                  j                  | j                        S )z<Checks if the path that this lock exists at actually exists.)r   r
   existsrC   s    r   rP   z_InterProcessLock.exists   s    ww~~dii((r   c                     t               rA   NotImplementedErrorrC   s    r   r   z_InterProcessLock.trylock       !##r   c                     t               rA   rR   rC   s    r   rH   z_InterProcessLock.unlock   rT   r   )__name__
__module____qualname____doc__	MAX_DELAYDELAY_INCREMENTtimesleepr   r(   r2   r?   rD   rF   rI   rN   rP   r   rH    r   r   r   r   5   so    $ I
 O )-

4 >(1  $%)V!
4,)$$r   r   c                       e Zd ZdZd Zd Zy)_WindowsLockz?Interprocess lock implementation that works on windows systems.c                 ~    t        j                  | j                  j                         t         j                  d       y N   )msvcrtlockingr   filenoLK_NBLCKrC   s    r   r   z_WindowsLock.trylock   #    t}}++-vBr   c                 ~    t        j                  | j                  j                         t         j                  d       y rb   )rd   re   r   rf   LK_UNLCKrC   s    r   rH   z_WindowsLock.unlock   rh   r   NrV   rW   rX   rY   r   rH   r^   r   r   r`   r`      s    ICCr   r`   c                       e Zd ZdZd Zd Zy)
_FcntlLockz=Interprocess lock implementation that works on posix systems.c                     t        j                  | j                  t         j                  t         j                  z         y rA   )fcntllockfr   LOCK_EXLOCK_NBrC   s    r   r   z_FcntlLock.trylock   s!    DMM5==5==#@Ar   c                 `    t        j                  | j                  t         j                         y rA   )ro   rp   r   LOCK_UNrC   s    r   rH   z_FcntlLock.unlock   s    DMM5==1r   Nrk   r^   r   r   rm   rm      s    GB2r   rm   ntc                 (    t        |       fd}|S )zSAcquires & releases a interprocess lock around call into
       decorated function.c                 F     t        j                          fd       }|S )Nc                  D    5   | i |cd d d        S # 1 sw Y   y xY wrA   r^   )argskwargsflocks     r   wrapperz7interprocess_locked.<locals>.decorator.<locals>.wrapper   s    $)&) s   )sixwraps)r{   r}   r|   s   ` r   	decoratorz&interprocess_locked.<locals>.decorator   s$    	1	* 
	* r   )InterProcessLock)r
   r   r|   s     @r   interprocess_lockedr      s     D!D r   )r   loggingr   r$   r\   r~   	fastenersr   	getLoggerrV   r   r   objectr   r`   rm   namerd   r   ro   r   r^   r   r   <module>r      s   &   	   
 g!*^$ ^$BC$ C2" 2 77d?#	 !r   