
    *#h5                        d Z 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Z	 e	 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yereZyeZe ej.                  d       yy# e$ r dZY w xY w# e$ r dZY w xY w# e$ r dZY w xY w# e
$ r eZ	Y w xY w)zD
A platform independent file lock that supports the with-statement.
    N)TimeoutBaseFileLockWindowsFileLockUnixFileLockSoftFileLockFileLockz3.0.12c                  P    t         xs t        j                  t              a t         S )z0Returns the logger instance used in this module.)_loggerlogging	getLogger__name__     T/var/www/html/sandstorm/venv/lib/python3.12/site-packages/datasets/utils/filelock.pyloggerr   P   s     4**84GNr   c                       e Zd ZdZd Zd Zy)r   zN
    Raised when the lock could not be acquired in *timeout*
    seconds.
    c                     || _         y N	lock_file)selfr   s     r   __init__zTimeout.__init___   s     #r   c                 &    d| j                    d}|S )NzThe file lock 'z' could not be acquired.r   )r   temps     r   __str__zTimeout.__str__e   s      00HIr   N)r   
__module____qualname____doc__r   r   r   r   r   r   r   Y   s    
r   r   c                       e Zd Zd Zd Zd Zy)_Acquire_ReturnProxyc                     || _         y Nlock)r   r%   s     r   r   z_Acquire_ReturnProxy.__init__w   s    	r   c                     | j                   S r#   r$   r   s    r   	__enter__z_Acquire_ReturnProxy.__enter__{   s    yyr   c                 8    | j                   j                          y r#   )r%   releaser   exc_type	exc_value	tracebacks       r   __exit__z_Acquire_ReturnProxy.__exit__~   s    		r   N)r   r   r   r   r(   r/   r   r   r   r!   r!   v   s    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ededefdZy)r   z3
    Implements the base class of a file lock.
    Nc                     ||nd}| j                  ||      }|| _        d| _        || _        t	        j
                         | _        d| _        y)r   N   r   )hash_filename_if_too_long
_lock_file_lock_file_fdtimeout	threadingLock_thread_lock_lock_counter)r   r   r6   max_filename_lengths       r   r   zBaseFileLock.__init__   s\    5H5T1Z]229>QR	# "  &NN,
 r   c                     | j                   S )z,
        The path to the lock file.
        )r4   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.

        *New in version 2.0.0*
        )_timeoutr'   s    r   r6   zBaseFileLock.timeout   s     }}r   c                 $    t        |      | _        yr   )floatr>   )r   values     r   r6   zBaseFileLock.timeout   s     er   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.
        rC   r'   s    r   _releasezBaseFileLock._release   s     "##r   c                     | j                   duS )z{
        True, if the object holds the file lock.

            This was previously a method and is now a property.
        N)r5   r'   s    r   	is_lockedzBaseFileLock.is_locked   s     !!--r   c           	         || j                   }| j                  5  | xj                  dz  c_        ddd       t        |       }| j                  }t        j
                         }	 	 | j                  5  | j                  s/t               j                  d| d|        | j                          ddd       | j                  r t               j                  d| d|        n|dk\  rNt        j
                         |z
  |kD  r4t               j                  d| d|        t        | j                        t               j                  d| d	| d
| d       t        j                  |       		 t        |       S # 1 sw Y   MxY w# 1 sw Y   xY w#  | j                  5  t        d| j                  dz
        | _        ddd        # 1 sw Y    xY wxY w)a  
        Acquires the file lock or fails with a :exc:`Timeout` error.

        ```py
        # 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.

            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  on Lock z acquired on r   zTimeout on acquiring lock z not acquired on z
, waiting z seconds ...r$   )r6   r9   r:   idr4   timerI   r   debugrE   r   sleepmaxr!   )r   r6   poll_intervalllock_idlock_filename
start_times         r   acquirezBaseFileLock.acquire   s   D ?llG  	$!#	$ T(YY[
	&& (>>)DWITR_Q`'ab(
 >>HNNU7)=#PQ\diikJ&>&HHNN%?y]O#\]!$//22HNNy(9-
SaRbbno JJ~.!   $..=	$ 	$( ( 	"" D%(D,>,>,B%C"D D sH   E<%F 2<F	.CF <F	FF G#G	GG	Gc                    | j                   5  | j                  r| xj                  dz  c_        | j                  dk(  s|rlt        |       }| j                  }t               j                  d| d|        | j                          d| _        t               j                  d| d|        ddd       y# 1 sw Y   yxY w)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.
        rK   r   zAttempting to release lock rL   rM   z released on N)r9   rI   r:   rN   r4   r   rP   rG   )r   forcerT   rU   s       r   r*   zBaseFileLock.release#  s      	R~~""a'"%%*e hG$(OOMHNN%@	m_#]^MMO)*D&HNNU7)=#PQ	R 	R s   BB55B>c                 &    | j                          | S r#   )rW   r'   s    r   r(   zBaseFileLock.__enter__?  s    r   c                 $    | j                          y r#   r*   r+   s       r   r/   zBaseFileLock.__exit__C  s    r   c                 (    | j                  d       y )NT)rY   r\   r'   s    r   __del__zBaseFileLock.__del__G  s    4 r   path
max_lengthreturnc                 J   t         j                  j                  |      }t        |      |kD  ru|dkD  rpt         j                  j	                  |      }t        t        |            }|d |t        |      z
  dz
   dz   |z   dz   }t         j                  j                  ||      S |S )Nr      z...z.lock)osr_   basenamelendirnamestrhashjoin)r   r_   r`   filenamerg   hashed_filenamenew_filenames          r   r3   z&BaseFileLock.hash_filename_if_too_longK  s    77##D)x=:%*q.ggood+G!$x.1O#$Kj33G&G!&KLuTWffippL77<<66Kr   N)Ng?)F)r   r   r   r   r   propertyr   r6   setterrE   rG   rI   rW   r*   r(   r/   r^   rh   intr3   r   r   r   r   r      s    4     ^^ $$ . .E/N8c s s r   r   c                   0     e Zd ZdZd fd	Zd Zd Z xZS )r   ze
    Uses the :func:`msvcrt.locking` function to hard lock the lock file on
    windows systems.
    c                 j    ddl m} t        |   |||       d || j                        z   | _        y )NrK   )relative_to_absolute_pathr6   r;   z\\?\)
file_utilsru   superr   r   r4   )r   r   r6   r;   ru   	__class__s        r   r   zWindowsFileLock.__init__`  s1    9GI\]#&?&OOr   c                 j   t         j                  t         j                  z  t         j                  z  }	 t        j                  | j
                  |      }	 t        j                  |t        j                  d       || _	        y # t        $ r t        j                  |       Y y w xY w# t        $ r Y y w xY wNrK   )rd   O_RDWRO_CREATO_TRUNCopenr4   msvcrtlockingLK_NBLCKr5   OSErrorcloser   	open_modefds      r   rE   zWindowsFileLock._acquiref  s    II

*RZZ7	
	()4B(r6??A6 &("	   	  	 	s#    B& %B B#"B#&	B21B2c                     | j                   }d | _         t        j                  |t        j                  d       t	        j
                  |       	 t	        j                  | j                         y # t        $ r Y y w xY wr{   )	r5   r   r   LK_UNLCKrd   r   remover4   r   r   r   s     r   rG   zWindowsFileLock._releasev  se    !r6??A.
	IIdoo&
   		s   A/ /	A;:A;rn   r   r   r   r   r   rE   rG   __classcell__ry   s   @r   r   r   Z  s    
P r   r   c                   0     e Zd ZdZd fd	Zd Zd Z xZS )r   zR
    Uses the :func:`fcntl.flock` to hard lock the lock file on unix systems.
    c                     t        j                  t         j                  j                  |            j                  }t
        |   |||       y )Nrv   )rd   statvfsr_   rg   	f_namemaxrx   r   )r   r   r6   r;   ry   s       r   r   zUnixFileLock.__init__  s:     jj)CDNNGI\]r   c                 j   t         j                  t         j                  z  t         j                  z  }t        j                  | j
                  |      }	 t        j                  |t        j                  t        j                  z         || _
        y # t        $ r t        j                  |       Y y w xY wr#   )rd   r|   r}   r~   r   r4   fcntlflockLOCK_EXLOCK_NBr5   r   r   r   s      r   rE   zUnixFileLock._acquire  s    II

*RZZ7	WWT__i0	$KKEMMEMM9: "$D	  	HHRL 		s   5B B21B2c                     | j                   }d | _         t        j                  |t        j                         t	        j
                  |       y r#   )r5   r   r   LOCK_UNrd   r   r   s     r   rG   zUnixFileLock._release  s7    
 !B&
r   rn   r   r   s   @r   r   r     s    ^
	r   r   c                       e Zd ZdZd Zd Zy)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                  |      }|| _        y # t        $ r Y y w xY wr#   )	rd   O_WRONLYr}   O_EXCLr~   r   r4   r5   r   r   s      r   rE   zSoftFileLock._acquire  se    KK"**,ryy82::E		$)4B "$D	  	 		s    A- -	A98A9c                     t        j                  | j                         d | _        	 t        j                  | j                         y # t
        $ r Y y w xY wr#   )rd   r   r5   r   r4   r   r'   s    r   rG   zSoftFileLock._release  sM    
##$!	IIdoo&   		s   A 	AAN)r   r   r   r   rE   rG   r   r   r   r   r     s    	r   r   z only soft file lock is available)r   r   rd   r7   rO   warningsImportErrorr   r   TimeoutError	NameErrorr   __all____version__r
   r   r   r!   r   r   r   r   r   warnr   r   r   <module>r      s(  4  	    l :
 
P Pn(l (^< J< B 	H
HH89 _  H
  F
  E  LsD   B B! B. B; BB!B+*B+.B87B8;CC