a
    *a1                     @   sN  d Z ddlZddlZddlZddlZzddlZW n eyF   dZY n0 zddlZW n eyj   dZY n0 zddlZW n ey   dZY n0 ze	 W n e
y   eZ	Y n0 g dZdZeeZG dd de	ZG dd deZG d	d
 d
eZG dd deZG dd deZdZer&eZn$er2eZneZedurJed dS )zD
A platform independent file lock that supports the with-statement.
    N)TimeoutBaseFileLockWindowsFileLockUnixFileLockSoftFileLockFileLockz2.0.8c                   @   s    e Zd ZdZdd Zdd ZdS )r   zN
    Raised when the lock could not be acquired in *timeout*
    seconds.
    c                 C   s
   || _ dS 	
        N)	lock_file)selfr
    r   Blib/python3.9/site-packages/navigator_updater/external/filelock.py__init__Q   s    zTimeout.__init__c                 C   s   d | j}|S )Nz)The file lock '{}' could not be acquired.)formatr
   )r   Ztempr   r   r   __str__X   s    zTimeout.__str__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   K   s   r   c                   @   s   e Zd ZdZdddZedd Zedd Zejd	d Zd
d Z	dd Z
edd ZdddZdddZdd Zdd Zdd ZdS ) r   z3
    Implements the base class of a file lock.
    c                 C   s&   || _ d| _|| _t | _d| _dS )r	   Nr   )
_lock_file_lock_file_fdtimeout	threadingZLock_thread_lock_lock_counter)r   r
   r   r   r   r   r   e   s    
zBaseFileLock.__init__c                 C   s   | j S )z,
        The path to the lock file.
        )r   r   r   r   r   r
   }   s    zBaseFileLock.lock_filec                 C   s   | 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   r   r   r   r      s    zBaseFileLock.timeoutc                 C   s   t || _dS r   )floatr   )r   valuer   r   r   r      s    
c                 C   s
   t  dS )z
        Platform dependent. If the file lock could be
        acquired, self._lock_file_fd holds the file descriptor
        of the lock file.
        NNotImplementedErrorr   r   r   r   _acquire   s    zBaseFileLock._acquirec                 C   s
   t  dS )zH
        Releases the lock and sets self._lock_file_fd to None.
        Nr    r   r   r   r   _release   s    zBaseFileLock._releasec                 C   s
   | 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)r   r   r   r   r   	is_locked   s    	zBaseFileLock.is_lockedN皙?c                 C   sj  |du r| j }| j |  jd7  _W d   n1 s80    Y  zt }t| }| j}| j, | js~td|| | 	  W d   n1 s0    Y  | jrt
d|| qqL|dkrt | |krtd|| t| jqLtd||| t| qLW nH   | j" td| jd | _W d   n1 s>0    Y   Y n0 G dd	 d	t}|| d
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 you use an equal 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   z#Attempting to acquire lock %s on %szLock %s acquired on %sr   z!Timeout on aquiring lock %s on %sz2Lock %s not acquired on %s, waiting %s seconds ...c                   @   s$   e Zd Zdd Zdd Zdd ZdS )z)BaseFileLock.acquire.<locals>.ReturnProxyc                 S   s
   || _ d S Nlock)r   r)   r   r   r   r     s    z2BaseFileLock.acquire.<locals>.ReturnProxy.__init__c                 S   s   | j S r'   r(   r   r   r   r   	__enter__  s    z3BaseFileLock.acquire.<locals>.ReturnProxy.__enter__c                 S   s   | j   d S r'   )r)   releaser   exc_type	exc_value	tracebackr   r   r   __exit__  s    
z2BaseFileLock.acquire.<locals>.ReturnProxy.__exit__N)r   r   r   r   r*   r0   r   r   r   r   ReturnProxy  s   r1   r(   )r   r   r   timeidr   r$   loggerdebugr"   infor   sleepmaxobject)r   r   Zpoll_intervallZ
start_timelock_idlock_filenamer1   r   r   r   acquire   sJ    $,&2zBaseFileLock.acquireFc                 C   s   | j j | jrb|  jd8  _| jdks*|rbt| }| j}td|| |   d| _td|| W d   n1 sv0    Y  dS )aV  
        Releases the file lock.

        Please note, that the lock is only completly 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.
        r&   r   z#Attempting to release lock %s on %szLock %s released on %sN)	r   r$   r   r3   r   r4   r5   r#   r6   )r   forcer:   r;   r   r   r   r+     s     "zBaseFileLock.releasec                 C   s   |    | S r'   )r<   r   r   r   r   r*   A  s    zBaseFileLock.__enter__c                 C   s   |    d S r'   r+   r,   r   r   r   r0   E  s    zBaseFileLock.__exit__c                 C   s   | j dd d S )NT)r=   r>   r   r   r   r   __del__I  s    zBaseFileLock.__del__)r   )Nr%   )F)r   r   r   r   r   propertyr
   r   setterr"   r#   r$   r<   r+   r*   r0   r?   r   r   r   r   r   `   s"   



		


e
"r   c                   @   s    e Zd ZdZdd Zdd ZdS )r   ze
    Uses the :func:`msvcrt.locking` function to hard lock the lock file on
    windows systems.
    c              	   C   sx   t jt jB t jB }zt | j|}W n ty6   Y n>0 zt|tj	d W n  t
tfyl   t | Y n0 || _d S Nr&   )osO_RDWRO_CREATO_TRUNCopenr   OSErrormsvcrtlockingZLK_NBLCKIOErrorcloser   r   Z	open_modefdr   r   r   r"   X  s    zWindowsFileLock._acquirec                 C   sN   | j }d | _ t|tjd t| zt| j W n tyH   Y n0 d S rB   )	r   rI   rJ   ZLK_UNLCKrC   rL   remover   rH   r   rN   r   r   r   r#   h  s    
zWindowsFileLock._releaseNr   r   r   r   r"   r#   r   r   r   r   r   R  s   r   c                   @   s    e Zd ZdZdd Zdd ZdS )r   zR
    Uses the :func:`fcntl.flock` to hard lock the lock file on unix systems.
    c              	   C   sd   t jt jB t jB }t | j|}zt|tjtj	B  W n  t
tfyX   t | Y n0 || _d S r'   )rC   rD   rE   rF   rG   r   fcntlflockZLOCK_EXZLOCK_NBrK   rH   rL   r   rM   r   r   r   r"     s    zUnixFileLock._acquirec                 C   s(   | j }d | _ t|tj t| d S r'   )r   rR   rS   ZLOCK_UNrC   rL   rP   r   r   r   r#     s
    
zUnixFileLock._releaseNrQ   r   r   r   r   r   {  s   r   c                   @   s    e Zd ZdZdd Zdd ZdS )r   z8
    Simply watches the existence of the lock file.
    c              	   C   sL   t jt jB t jB t jB }zt | j|}W n ttfy@   Y n0 || _	d S r'   )
rC   O_WRONLYrE   O_EXCLrF   rG   r   rK   rH   r   rM   r   r   r   r"     s    zSoftFileLock._acquirec                 C   s:   t | j d | _zt | j W n ty4   Y n0 d S r'   )rC   rL   r   rO   r   rH   r   r   r   r   r#     s    zSoftFileLock._releaseNrQ   r   r   r   r   r     s   
r   z only soft file lock is available)r   ZloggingrC   r   r2   warningsImportErrorrI   rR   TimeoutError	NameErrorrH   __all____version__Z	getLoggerr   r4   r   r9   r   r   r   r   r   warnr   r   r   r   <module>   sJ   




 s)!
