
    eCi3                        d Z ddlZddlZddlZddlZ	 ddlZn# e$ r dZY nw xY w	 ddlZn# e$ r dZY nw xY w	 ddlZn# e$ r dZY nw xY w	 e	 n# e
$ r eZ	Y nw xY wg dZdZdad Z G d de	          Z G d d	          Z G d
 d          Z G d de          Z G d de          Z G d de          ZdZereZdS ereZdS eZe ej        d           dS dS )zD
A platform independent file lock that supports the with-statement.
    N)TimeoutBaseFileLockWindowsFileLockUnixFileLockSoftFileLockFileLockz3.0.12c                  P    t           pt          j        t                    a t           S )z0Returns the logger instance used in this module.)_loggerlogging	getLogger__name__     2/b/s/w/ir/x/w/install/emscripten/tools/filelock.pyloggerr   S   s     4*844GNr   c                       e Zd ZdZd Zd ZdS )r   zN
    Raised when the lock could not be acquired in *timeout*
    seconds.
    c                     || _         dS 	
        N)	lock_file)selfr   s     r   __init__zTimeout.__init__b   s     #tr   c                 :    d                     | j                  }|S )Nz)The file lock '{}' could not be acquired.)formatr   )r   temps     r   __str__zTimeout.__str__i   s    :t~&& 	r   N)r   
__module____qualname____doc__r   r   r   r   r   r   r   \   s<         
      r   r   c                        e Zd Zd Zd Zd ZdS )_Acquire_ReturnProxyc                     || _         d S Nlock)r   r%   s     r   r   z_Acquire_ReturnProxy.__init__|   s    	tr   c                     | j         S r#   r$   r   s    r   	__enter__z_Acquire_ReturnProxy.__enter__   s
    yr   c                 8    | j                                          d S r#   )r%   releaser   	_exc_type
_exc_value
_tracebacks       r   __exit__z_Acquire_ReturnProxy.__exit__   s    	tr   N)r   r   r   r   r(   r/   r   r   r   r!   r!   z   sA                r   r!   c                       e Zd ZdZddZed             Zed             Zej        d             Zd Z	d Z
ed	             ZddZddZd Zd Zd Zd
S )r   z3
    Implements the base class of a file lock.
    c                 n    || _         d| _        || _        t          j                    | _        d| _        dS )r   Nr   )
_lock_file_lock_file_fdtimeout	threadingLock_thread_lock_lock_counter)r   r   r5   s      r   r   zBaseFileLock.__init__   s?     $ "  &N,,
 tr   c                     | j         S )z,
        The path to the lock file.
        )r3   r'   s    r   r   zBaseFileLock.lock_file   s    
 r   c                     | j         S )a~  
        You can set a default timeout for the filelock. It will be used as
        fallback value in the acquire method, if no timeout value (*None*) is
        given.

        If you want to disable the timeout, set it to a negative value.

        A timeout of 0 means, that there is exactly one attempt to acquire the
        file lock.

        .. versionadded:: 2.0.0
        )_timeoutr'   s    r   r5   zBaseFileLock.timeout   s     }r   c                 .    t          |          | _        dS r   )floatr<   )r   values     r   r5   zBaseFileLock.timeout   s     etr   c                     t                      )z
        Platform dependent. If the file lock could be
        acquired, self._lock_file_fd holds the file descriptor
        of the lock file.
        NotImplementedErrorr'   s    r   _acquirezBaseFileLock._acquire   s     "###r   c                     t                      )zH
        Releases the lock and sets self._lock_file_fd to None.
        rA   r'   s    r   _releasezBaseFileLock._release   s     "###r   c                     | j         duS )z
        True, if the object holds the file lock.

        .. versionchanged:: 2.0.0

            This was previously a method and is now a property.
        N)r4   r'   s    r   	is_lockedzBaseFileLock.is_locked   s     !--r   N皙?c                    || j         }| j        5  | xj        dz  c_        ddd           n# 1 swxY w Y   t          |           }| j        }t          j                    }	 	 | j        5  | j        s7t                                          d||           | 	                                 ddd           n# 1 swxY w Y   | j        r$t                                          d||           n|dk    rQt          j                    |z
  |k    r7t                                          d||           t          | j                  t                                          d|||           t          j        |           nB#  | j        5  t          d| j        dz
            | _        ddd           n# 1 swxY w Y    xY wt          | 	          S )
aY  
        Acquires the file lock or fails with a :exc:`Timeout` error.

        .. code-block:: python

            # You can use this method in the context manager (recommended)
            with lock.acquire():
                pass

            # Or use an equivalent try-finally construct:
            lock.acquire()
            try:
                pass
            finally:
                lock.release()

        :arg float timeout:
            The maximum time waited for the file lock.
            If ``timeout < 0``, there is no timeout and this method will
            block until the lock could be acquired.
            If ``timeout`` is None, the default :attr:`~timeout` is used.

        :arg float poll_intervall:
            We check once in *poll_intervall* seconds if we can acquire the
            file lock.

        :raises Timeout:
            if the lock could not be acquired in *timeout* seconds.

        .. versionchanged:: 2.0.0

            This method returns now a *proxy* object instead of *self*,
            so that it can be used in a with statement without side effects.
        N   Tz#Attempting to acquire lock %s on %szLock %s acquired on %sr   z"Timeout on acquiring lock %s on %sz2Lock %s not acquired on %s, waiting %s seconds ...r$   )r5   r8   r9   idr3   timerG   r   debugrC   r   sleepmaxr!   )r   r5   poll_intervalllock_idlock_filename
start_times         r   acquirezBaseFileLock.acquire   s   H ?lG  	$ 	$!#	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ T((Y[[
	/& ( (> ('LgWdeee( ( ( ( ( ( ( ( ( ( ( ( ( ( (
 > /HHNN#;WmTTT\\dikkJ&>&H&HHHNN#GR_```!$/222HHNNL   J~...#/ 	" D D%(D,>,B%C%C"D D D D D D D D D D D D D D D #40000sa   .22#E> +?B6*E> 6B::E> =B:>B?E> >	F=F1%F=1F5	5F=8F5	9F=Fc                 z   | j         5  | j        r| xj        dz  c_        | j        dk    s|rwt          |           }| j        }t                                          d||           |                                  d| _        t                                          d||           ddd           n# 1 swxY w Y   dS )aW  
        Releases the file lock.

        Please note, that the lock is only completely released, if the lock
        counter is 0.

        Also note, that the lock file itself is not automatically deleted.

        :arg bool force:
            If true, the lock counter is ignored and the lock is released in
            every case.
        rJ   r   z#Attempting to release lock %s on %szLock %s released on %sN)r8   rG   r9   rK   r3   r   rM   rE   )r   forcerQ   rR   s       r   r*   zBaseFileLock.release,  s
     	U 	U~ 
U""a'""%**e* hhG$(OMHHNN#H'S`aaaMMOOO)*D&HHNN#;WmTTT	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	U 	U ts   BB00B47B4c                 .    |                                   | S r#   )rT   r'   s    r   r(   zBaseFileLock.__enter__I  s    r   c                 .    |                                   d S r#   r*   r+   s       r   r/   zBaseFileLock.__exit__M  s    tr   c                 2    |                      d           d S )NT)rV   rY   r'   s    r   __del__zBaseFileLock.__del__Q  s    T"""tr   )r1   )NrH   )F)r   r   r   r   r   propertyr   r5   setterrC   rE   rG   rT   r*   r(   r/   r[   r   r   r   r   r      s           0   X   X ^  ^$ $ $$ $ $ . . X.H1 H1 H1 H1T   :        r   r   c                       e Zd ZdZd Zd ZdS )r   ze
    Uses the :func:`msvcrt.locking` function to hard lock the lock file on
    windows systems.
    c                 P   t           j        t           j        z  t           j        z  }	 t          j        | j        |          }	 t          j        |t          j        d           || _	        n;# t          t          f$ r t          j        |           Y nw xY w# t          $ r Y nw xY wd S NrJ   )osO_RDWRO_CREATO_TRUNCopenr3   msvcrtlockingLK_NBLCKr4   IOErrorOSErrorcloser   	open_modefds      r   rC   zWindowsFileLock._acquire_  s    I
*RZ7	
	()44B(r6?A666 &("" W%     	 	 	D	 ts#   B  A+ +%BB
B#"B#c                     | j         }d | _         t          j        |t          j        d           t	          j        |           	 t	          j        | j                   n# t          $ r Y nw xY wd S r`   )	r4   rf   rg   LK_UNLCKra   rk   remover3   rj   r   rn   s     r   rE   zWindowsFileLock._releaseo  sv    !r6?A...
	Ido&&&&  	 	 	D	ts   A 
A+*A+Nr   r   r   r   rC   rE   r   r   r   r   r   Y  s<         
       r   r   c                       e Zd ZdZd Zd ZdS )r   zR
    Uses the :func:`fcntl.flock` to hard lock the lock file on unix systems.
    c                    t           j        t           j        z  t           j        z  }t          j        | j        |          }	 t          j        |t          j        t          j	        z             t          j
        |          }|j        dk    rt          j        |           n3|| _        n+# t          t          f$ r t          j        |           Y nw xY wd S )Nr   )ra   rb   rc   rd   re   r3   fcntlflockLOCK_EXLOCK_NBfstatst_nlinkrk   r4   ri   rj   )r   rm   rn   sts       r   rC   zUnixFileLock._acquire  s    I
*RZ7	WT_i00	&KEMEM9::: "B{a hrllll#%d   ! 	 	 	HRLLLLL	 ts   ,B* *%CCc                     | j         }d | _         t          j        | j                   t	          j        |t          j                   t          j        |           d S r#   )r4   ra   unlinkr3   rv   rw   LOCK_UNrk   rr   s     r   rE   zUnixFileLock._release  sJ    !
	$/"""B&&&
tr   Nrs   r   r   r   r   r     s<           *    r   r   c                       e Zd ZdZd Zd ZdS )r   z8
    Simply watches the existence of the lock file.
    c                     t           j        t           j        z  t           j        z  t           j        z  }	 t          j        | j        |          }|| _        n# t          t          f$ r Y nw xY wd S r#   )
ra   O_WRONLYrc   O_EXCLrd   re   r3   r4   ri   rj   rl   s      r   rC   zSoftFileLock._acquire  sq    K"*,ry82:E		$)44B "$D ! 	 	 	D	 ts   A A+*A+c                     t          j        | j                   d | _        	 t          j        | j                   n# t
          $ r Y nw xY wd S r#   )ra   rk   r4   rq   r3   rj   r'   s    r   rE   zSoftFileLock._release  s\    
#$$$!	Ido&&&& 	 	 	D	ts   < 
A	A	Nrs   r   r   r   r   r     s<           	 	 	 	 	r   r   z only soft file lock is available)r   r   ra   r6   rL   warningsImportErrorrf   rv   TimeoutError	NameErrorrj   __all____version__r
   r   r   r!   r   r   r   r   r   warnr   r   r   <module>r      s  B   				     OOOO   HHHMMMM   FFFLLLL   EEELL   LLL          l   <       K K K K K K K Kb" " " " "l " " "N         <      J    <   B 	 :HHH
 :HHHH899999 s9    ##, 66? A	A	A AA