
    +gd5                        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__     7lib/python3.11/site-packages/datasets/utils/filelock.pyloggerr   P   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___   s     #tr   c                     d| j          d}|S )NzThe file lock 'z' could not be acquired.r   )r   temps     r   __str__zTimeout.__str__e   s    III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dS )_Acquire_ReturnProxyc                     || _         d S Nlock)r   r%   s     r   r   z_Acquire_ReturnProxy.__init__w   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!   v   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ededefdZdS )r   z3
    Implements the base class of a file lock.
    Nc                     ||nd}|                      ||          }|| _        d| _        || _        t	          j                    | _        d| _        dS )r   N   r   )hash_filename_if_too_long
_lock_file_lock_file_fdtimeout	threadingLock_thread_lock_lock_counter)r   r   r7   max_filename_lengths       r   r   zBaseFileLock.__init__   se    5H5T11Z]229>QRR	# "  &N,,
 tr   c                     | j         S )z,
        The path to the lock file.
        )r5   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   r7   zBaseFileLock.timeout   s     }r   c                 .    t          |          | _        dS r   )floatr?   )r   values     r   r7   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.
        rD   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)r6   r'   s    r   	is_lockedzBaseFileLock.is_locked   s     !--r   皙?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        s;t                                          d| d|            | 	                                 ddd           n# 1 swxY w Y   | j        r(t                                          d| d|            n|dk    rUt          j                    |z
  |k    r;t                                          d	| d|            t          | j                  t                                          d| d
| d| 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 )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   TzAttempting to acquire lock  on Lock z acquired on r   zTimeout on acquiring lock z not acquired on z
, waiting z seconds ...r$   )r7   r:   r;   idr5   timerJ   r   debugrF   r   sleepmaxr!   )r   r7   poll_intervalllock_idlock_filename
start_times         r   acquirezBaseFileLock.acquire   s   D ?lG  	$ 	$!#	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ T((Y[[
	/& ( (> ('aW'a'aR_'a'abbb( ( ( ( ( ( ( ( ( ( ( ( ( ( (
 > 
/HHNN#P7#P#P#P#PQQQ\\dikkJ&>&H&HHHNN#\#\#\]#\#\]]]!$/222HHNNooo-ooSaooo   J~...!/ 	" D D%(D,>,B%C%C"D D D D D D D D D D D D D D D #....sb   .22#F +AB:.F :B>>F B>CF 	GG8GG	GG	GFc                    | j         5  | j        r| xj        dz  c_        | j        dk    s|rt          |           }| j        }t                                          d| d|            |                                  d| _        t                                          d| d|            ddd           n# 1 swxY w 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.
        rM   r   zAttempting to release lock rN   rO   z released on N)r:   rJ   r;   rP   r5   r   rR   rH   )r   forcerV   rW   s       r   r*   zBaseFileLock.release#  s!     	R 	R~ 
R""a'""%**e* hhG$(OMHHNN#]#]#]m#]#]^^^MMOOO)*D&HHNN#P7#P#P#P#PQQQ	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R ts   B$B88B<?B<c                 .    |                                   | S r#   )rY   r'   s    r   r(   zBaseFileLock.__enter__?  s    r   c                 .    |                                   d S r#   r*   r+   s       r   r/   zBaseFileLock.__exit__C  s    tr   c                 2    |                      d           d S )NT)r[   r^   r'   s    r   __del__zBaseFileLock.__del__G  s    4   tr   path
max_lengthreturnc                 x   t           j                            |          }t          |          |k    r|dk    rt           j                            |          }t          t          |                    }|d |t          |          z
  dz
           dz   |z   dz   }t           j                            ||          S |S )Nr      z...z.lock)osra   basenamelendirnamestrhashjoin)r   ra   rb   filenameri   hashed_filenamenew_filenames          r   r4   z&BaseFileLock.hash_filename_if_too_longK  s    7##D))x==:%%*q..good++G!$x..11O#$Kj33G3G&G!&K$KLuTWffippL7<<666Kr   r1   N)NrK   )F)r   r   r   r   r   propertyr   r7   setterrF   rH   rJ   rY   r*   r(   r/   r`   rj   intr4   r   r   r   r   r      sA           4   X   X ^  ^$ $ $$ $ $ . . X.E/ E/ E/ 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.
    r1   Nc                     ddl m} t                                          |||           d || j                  z   | _        d S )NrM   )relative_to_absolute_pathr7   r<   z\\?\)
file_utilsrv   superr   r   r5   )r   r   r7   r<   rv   	__class__s        r   r   zWindowsFileLock.__init__`  sS    999999GI\]]]#&?&?&O&OOr   c                 B   t           j        t           j        z  t           j        z  }	 t          j        | j        |          }	 t          j        |t          j        d           || _	        n4# t          $ r t          j        |           Y nw xY w# t          $ r Y nw xY wd S NrM   )rf   O_RDWRO_CREATO_TRUNCopenr5   msvcrtlockingLK_NBLCKr6   OSErrorcloser   	open_modefds      r   rF   zWindowsFileLock._acquiref  s    I
*RZ7	
	()44B(r6?A666 &(""      	 	 	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|   )	r6   r   r   LK_UNLCKrf   r   remover5   r   r   r   s     r   rH   zWindowsFileLock._releasev  sv    !r6?A...
	Ido&&&&  	 	 	D	ts   A 
A+*A+rp   r   r   r   r   r   rF   rH   __classcell__rz   s   @r   r   r   Z  sk         
P P P P P 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.
    r1   Nc                     t          j        t           j                            |                    j        }t                                          |||           d S )Nrw   )rf   statvfsra   ri   	f_namemaxry   r   )r   r   r7   r<   rz   s       r   r   zUnixFileLock.__init__  sJ     j)C)CDDNGI\]]]]]r   c                 8   t           j        t           j        z  t           j        z  }t          j        | j        |          }	 t          j        |t          j        t          j	        z             || _
        n$# t          $ r t          j        |           Y nw xY wd S r#   )rf   r}   r~   r   r   r5   fcntlflockLOCK_EXLOCK_NBr6   r   r   r   s      r   rF   zUnixFileLock._acquire  s    I
*RZ7	WT_i00	$KEMEM9::: "$D  	 	 	HRLLLLL	 ts   ,A6 6BBc                     | j         }d | _         t          j        |t          j                   t	          j        |           d S r#   )r6   r   r   LOCK_UNrf   r   r   s     r   rH   zUnixFileLock._release  s:    
 !B&&&
tr   rp   r   r   s   @r   r   r     sk         ^ ^ ^ ^ ^ ^
 
 
	 	 	 	 	 	 	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          $ r Y nw xY wd S r#   )	rf   O_WRONLYr~   O_EXCLr   r   r5   r6   r   r   s      r   rF   zSoftFileLock._acquire  sl    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#   )rf   r   r6   r   r5   r   r'   s    r   rH   zSoftFileLock._release  s\    
#$$$!	Ido&&&& 	 	 	D	ts   < 
A	A	N)r   r   r   r   rF   rH   r   r   r   r   r     s<           	 	 	 	 	r   r   z only soft file lock is available)r   r   rf   r8   rQ   warningsImportErrorr   r   TimeoutError	NameErrorr   __all____version__r
   r   r   r!   r   r   r   r   r   warnr   r   r   <module>r      s  4   				     OOOO   HHHMMMM   FFFLLLL   EEELL   LLL          l   :
 
 
 
 
 
 
 
P P P P P P P Pn( ( ( ( (l ( ( (^    <   J    <   B 	 :HHH
 :HHHH899999 s9    ##, 66? A	A	A AA