
    +j                    f   U d Z ddlm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	ddl
Z
ddlZddlZddlZddlmZmZ ddlmZ ddlmZ ddlmZmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddl m!Z! erddl"m#Z#m$Z$ ed         Z%dZ&dZ' e(edd          Z) e(edd          Z*e
j+        dk    oej,        ej-        v Z. e            Z/de0d<    ej1                    a2da3da4 ed           G d d                      Z5e G d d                      Z6 ed           G d d                      Z7e G d d                       Z8 G d! d"e9          Z: G d# d$e:%          Z; G d& d'ej<                  Z=dd(dDd0Z>dd(dEd2Z?dFd6Z@dd(dGd;ZAdd(dHd<ZBdd(dHd=ZCdId?ZDdJd@ZEdKdAZFdKdBZGdKdCZHd$gZIdS )LzZCross-process and cross-host reader/writer lock built on :class:`SoftFileLock` primitives.    )annotationsN)contextmanagersuppress)	dataclass)Path)TYPE_CHECKINGLiteral)WeakValueDictionary)AcquireReturnProxy)Timeout)SoftFileLock)ensure_directory_exists)Callable	Generator)readwritez.breaki   
O_NOFOLLOW
O_NONBLOCKwin32,WeakValueDictionary[Path, SoftReadWriteLock]_all_instancesFT)frozenc                  .    e Zd ZU ded<   ded<   ded<   dS )_Pathsstrstater   readersN__name__
__module____qualname____annotations__     [/home/wildlama/visual-decline/.venv/lib/python3.11/site-packages/filelock/_soft_rw/_sync.pyr   r   .   s+         JJJJJJLLLLLr$   r   c                  .    e Zd ZU ded<   ded<   ded<   dS )_Locksthreading.Lockinternaltransactionr   r   Nr   r#   r$   r%   r'   r'   5   s6         r$   r'   c                  .    e Zd ZU ded<   ded<   ded<   dS )_MarkerInfor   tokenintpidhostnameNr   r#   r$   r%   r,   r,   <   s+         JJJHHHMMMMMr$   r,   c                  d    e Zd ZU dZded<   ded<   ded<   ded	<   d
ed<   ded<   ded<   ded<   dS )_HoldzYEverything that exists only while a lock is held; ``None`` when the instance has no lock.r.   level_Modemode
int | Nonewrite_thread_idr   marker_namebool	is_readerr-   _HeartbeatThreadheartbeat_threadthreading.Eventheartbeat_stopN)r   r    r!   __doc__r"   r#   r$   r%   r2   r2   C   sl         ccJJJKKKOOOJJJ&&&&######r$   r2   c                  H     e Zd ZU ded<   ded<   	 dddddd	d
d fdZ xZS )_SoftRWMetar   
_instancesr(   _instances_lockT      >@N      ?blockingis_singletonheartbeat_intervalstale_thresholdpoll_interval	lock_filestr | os.PathLike[str]timeoutfloatrH   r9   rI   rJ   rK   float | NonerL   returnSoftReadWriteLockc          
        |s(t                                          |||||||          S t          |                                          }| j        5  | j                            |          }	|	3t                                          |||||||          }	|	| j        |<   n=|	j        |k    s|	j        |k    r'd|	j         d|	j         d| d| }
t          |
          |	cd d d            S # 1 swxY w Y   d S )NrG   z$Singleton lock created with timeout=z, blocking=z, cannot be changed to timeout=)
super__call__r   resolverC   rB   getrO   rH   
ValueError)clsrM   rO   rH   rI   rJ   rK   rL   
normalizedinstancemsg	__class__s              r%   rV   z_SoftRWMeta.__call__U   s     		77##!)#5 /+ $    )__,,..
  	 	~))*55H 77++%!-'9$3"/ ,   .6z**!W,,0AX0M0MT8;K T TX`Xi T T5<T TIQT T  !oo%'	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   BC//C36C3rD   )rM   rN   rO   rP   rH   r9   rI   r9   rJ   rP   rK   rQ   rL   rP   rR   rS   )r   r    r!   r"   rV   __classcell__r^   s   @r%   rA   rA   Q   s|         <<<<####
 *
 !$((,#* * * * * * * * * * * *r$   rA   c                  b   e Zd ZU dZ e            Zded<    ej                    Z		 d>dddddd	d?dZ
ed@dddAd            Zed@dddAd            Zd@dddBdZd@dddBdZdCd Zd!d"dDd$Ze	 d>dddEd%            ZdFd(ZdGd+ZdHd,ZdId1ZdId2ZdJd5ZdCd6ZdKd7ZdLd9ZdMd;ZdKd<ZdCd=ZdS )NrS   u
  
    Cross-process and cross-host reader/writer lock built on :class:`SoftFileLock` primitives.

    Use this class instead of :class:`~filelock.ReadWriteLock` when the lock file lives on a network
    filesystem (NFS, Lustre with ``-o flock``, HPC cluster shared storage). ``ReadWriteLock`` is backed
    by SQLite and cannot run on NFS because SQLite's ``fcntl`` locking is unreliable there.

    Layout on disk for a lock at ``foo.lock``:

    - ``foo.lock.state`` — a :class:`SoftFileLock` taken only during state transitions (microseconds).
    - ``foo.lock.write`` — writer marker; its presence means a writer is claiming or holding the lock.
    - ``foo.lock.readers/<host>.<pid>.<uuid>`` — one file per reader.

    Each marker stores a random token (``secrets.token_hex(16)``), the holder's pid, and the holder's
    hostname. A daemon heartbeat thread refreshes ``mtime`` on every held marker. A marker whose mtime
    has not advanced in ``stale_threshold`` seconds may be evicted by any process on any host, giving
    correct behavior when a compute node crashes with a lock held.

    Writer acquire is two-phase and writer-preferring: phase 1 claims ``.write`` (blocking any new
    reader), phase 2 waits for existing readers to drain. Writer starvation is impossible.

    Reentrancy, upgrade/downgrade rules, thread pinning, and singleton caching by resolved path match
    :class:`~filelock.ReadWriteLock`.

    Forking while holding a lock invalidates the inherited instance in the child so the child cannot
    double-own the lock with its parent; ``release()`` on a fork-invalidated instance is a no-op, and
    the child must re-acquire if it needs a lock.

    Trust boundary: protects against same-UID non-cooperating processes (one host or cross-host) and
    same-host different-UID users via ``0o600`` / ``0o700`` permissions. Does not protect against root
    compromise, NTP tampering on same-UID cross-host nodes, or multi-tenant mounts where hostile
    co-tenants share the UID.

    :param lock_file: path to the lock file; sidecar state/write/readers live next to it
    :param timeout: maximum wait time in seconds; ``-1`` means block indefinitely
    :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately on contention
    :param is_singleton: if ``True``, reuse existing instances for the same resolved path
    :param heartbeat_interval: seconds between heartbeat refreshes; default 30 s
    :param stale_threshold: seconds of ``mtime`` inactivity before a marker is stale; defaults to
        ``3 * heartbeat_interval``, matching etcd's ``LeaseKeepAlive`` convention
    :param poll_interval: seconds between acquire retries under contention; default 0.25 s

    .. versionadded:: 3.27.0

    r   rB   rD   TrE   NrF   rG   rM   rN   rO   rP   rH   r9   rI   rJ   rK   rQ   rL   rR   Nonec               J   |dk    rd| }t          |          ||dz  }||k    rd| d| d}t          |          |dk    rd| }t          |          t          j        |          | _        || _        || _        || _        || _        || _        t          | j         d| j         d	| j         d
          | _
        t          | j                   t          t          j                    t          j                    t          | j
        j        d                    | _        d | _        d | _        d| _        d| _        t,          5  | t.          t1          | j                                                  <   t5                       d d d            d S # 1 swxY w Y   d S )Nr   z)heartbeat_interval must be positive, got    z0stale_threshold must exceed heartbeat_interval (z <= )z$poll_interval must be positive, got z.statez.writez.readers)r   r   r   rD   rO   r)   r*   r   F)rY   osfspathrM   rO   rH   rJ   rK   rL   r   _pathsr   r'   	threadingLockr   r   _locks_readers_dir_fd_hold_fork_invalidated_closed_all_instances_lockr   r   rW   _register_hooks)	selfrM   rO   rH   rI   rJ   rK   rL   r]   s	            r%   __init__zSoftReadWriteLock.__init__   s    ""R>PRRCS//!"014O000o_ooZloooCS//!AHHHCS//! i	22%&);&5$1^+++^+++~///
 
 

 	 ///^%%!((t{0"===
 
 

 ,0#'
',"  	 	=AN4//7799:	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   =FFFrH   bool | NoneGenerator[None]c             #     K   |                      ||           	 dV  |                                  dS # |                                  w xY w)a  
        Context manager that acquires and releases a shared read lock.

        Falls back to instance defaults for *timeout* and *blocking* when ``None``.

        :param timeout: maximum wait time in seconds, or ``None`` to use the instance default
        :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately; ``None`` uses the instance default

        :raises RuntimeError: if a write lock is already held on this instance
        :raises Timeout: if the lock cannot be acquired within *timeout* seconds

        rw   N)acquire_readreleaseru   rO   rH   s      r%   	read_lockzSoftReadWriteLock.read_lock   sR       	'H555	EEELLNNNNNDLLNNNN	   5 Ac             #     K   |                      ||           	 dV  |                                  dS # |                                  w xY w)a@  
        Context manager that acquires and releases an exclusive write lock.

        Falls back to instance defaults for *timeout* and *blocking* when ``None``.

        :param timeout: maximum wait time in seconds, or ``None`` to use the instance default
        :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately; ``None`` uses the instance default

        :raises RuntimeError: if a read lock is already held, or a write lock is held by a different thread
        :raises Timeout: if the lock cannot be acquired within *timeout* seconds

        rw   N)acquire_writer|   r}   s      r%   
write_lockzSoftReadWriteLock.write_lock   sR       	7X666	EEELLNNNNNDLLNNNNr   r   c               2    |                      d||          S )uf  
        Acquire a shared read lock.

        If this instance already holds a read lock, the lock level is incremented (reentrant). Attempting to acquire a
        read lock while holding a write lock raises :class:`RuntimeError` (downgrade not allowed). On the 0→1
        transition a daemon heartbeat thread is started that refreshes the reader marker's ``mtime`` every
        ``heartbeat_interval`` seconds so peers on other hosts do not evict the marker as stale.

        :param timeout: maximum wait time in seconds, or ``None`` to use the instance default; ``-1`` means block
            indefinitely
        :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately when the lock is unavailable;
            ``None`` uses the instance default

        :returns: a proxy that can be used as a context manager to release the lock

        :raises RuntimeError: if a write lock is already held on this instance, if this instance was invalidated by
            :func:`os.fork`, or if :meth:`close` was called
        :raises Timeout: if the lock cannot be acquired within *timeout* seconds

        r   rw   _acquirer}   s      r%   r{   zSoftReadWriteLock.acquire_read  s    * }}VWx}@@@r$   c               2    |                      d||          S )a  
        Acquire an exclusive write lock.

        If this instance already holds a write lock from the same thread, the lock level is incremented (reentrant).
        Attempting to acquire a write lock while holding a read lock raises :class:`RuntimeError` (upgrade not
        allowed). Write locks are pinned to the acquiring thread: a different thread trying to re-enter also raises
        :class:`RuntimeError`.

        Writer acquisition runs in two phases. Phase 1 atomically claims ``<path>.write`` via ``O_CREAT | O_EXCL``,
        which immediately blocks any new reader on any host. Phase 2 waits for existing readers to drain. Writer
        starvation is impossible: new readers see ``<path>.write`` during phase 2 and wait behind the pending writer.

        :param timeout: maximum wait time in seconds, or ``None`` to use the instance default; ``-1`` means block
            indefinitely
        :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately when the lock is unavailable;
            ``None`` uses the instance default

        :returns: a proxy that can be used as a context manager to release the lock

        :raises RuntimeError: if a read lock is already held, if a write lock is held by a different thread, if this
            instance was invalidated by :func:`os.fork`, or if :meth:`close` was called
        :raises Timeout: if the lock cannot be acquired within *timeout* seconds

        r   rw   r   r}   s      r%   r   zSoftReadWriteLock.acquire_write%  s    2 }}Wg}AAAr$   c                Z   |                      d           | j        j        5  | j        r	 ddd           dS d| _        | j        Lt          t                    5  t          j        | j                   ddd           n# 1 swxY w Y   d| _        ddd           dS # 1 swxY w Y   dS )u  
        Release any held lock and release internal filesystem resources.

        Idempotent. After calling this method the instance can no longer acquire locks — subsequent acquires raise
        :class:`RuntimeError`. A fork-invalidated instance is closed without raising.
        TforceN)	r|   rn   r)   rr   ro   r   OSErrorri   closeru   s    r%   r   zSoftReadWriteLock.close@  sL    	4   [! 	, 	,| 	, 	, 	, 	, 	, 	, 	, 	,  DL#/g&& 3 3HT12223 3 3 3 3 3 3 3 3 3 3 3 3 3 3'+$	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,s:   	B "B B5B B	B B		
B  B$'B$Fr   r   c               h   | j         j        5  | j        rd| _        	 ddd           dS | j        }|:|r	 ddd           dS d| j         dt          |            d}t          |          |rd|_        n|xj        dz  c_        |j        dk    r	 ddd           dS d| _        ddd           n# 1 swxY w Y   |j        	                                 |j
                            | j        dz              |j        rt          |j        | j        	           dS t          |j                   dS )
a8  
        Release one level of the current lock.

        When the lock level reaches zero the heartbeat thread is stopped and the held marker file is unlinked. On a
        fork-invalidated instance (that is, the child of a :func:`os.fork` call made while the parent held a lock)
        this method is a no-op so inherited ``with`` blocks can unwind cleanly in the child.

        :param force: if ``True``, release the lock completely regardless of the current lock level

        :raises RuntimeError: if no lock is currently held and *force* is ``False``

        NzCannot release a lock on  (lock id: z) that is not heldr      g      ?rg   dir_fd)rn   r)   rq   rp   rM   idRuntimeErrorr3   r>   setr<   joinrJ   r:   _unlinkr8   ro   )ru   r   holdr]   s       r%   r|   zSoftReadWriteLock.releaseQ  s    [! 	 	% !
		 	 	 	 	 	 	 	
 :D| 	 	 	 	 	 	 	 	 j$.iiRPTXXiii"3'''  



a

zA~~!	 	 	 	 	 	 	 	" DJ#	 	 	 	 	 	 	 	 	 	 	 	 	 	 	, 	!!!""4+BS+H"III> 	&D$T-ABBBBBBD$%%%%%s#   B3B3AB3 B33B7:B7c                    | |||          S )a  
        Return the singleton :class:`SoftReadWriteLock` for *lock_file*.

        :param lock_file: path to the lock file; sidecar state/write/readers live next to it
        :param timeout: maximum wait time in seconds; ``-1`` means block indefinitely
        :param blocking: if ``False``, raise :class:`~filelock.Timeout` immediately when the lock is unavailable

        :returns: the singleton lock instance

        :raises ValueError: if an instance already exists for this path with different *timeout* or *blocking* values

        rw   r#   )rZ   rM   rO   rH   s       r%   get_lockzSoftReadWriteLock.get_lock{  s    ( s9g9999r$   r5   r4   c               ,   || j         n|}|| j        n|}| j        j        5  | j        rd| j         d}t          |          | j        rd| j         d}t          |          | j        !| 	                    |          cd d d            S 	 d d d            n# 1 swxY w Y   t          j                    }|s!| j        j                            d          }nH|dk    r!| j        j                            d          }n!| j        j                            d|          }|st          | j                  d 	 |                     ||||          | j        j                                         S # | j        j                                         w xY w)	NzSoftReadWriteLock on z4 was invalidated by fork(); construct a new instancez has been closedFrw   rD   T)rH   rO   )rO   rH   rn   r)   rq   rM   r   rr   rp   _validate_reentranttimeperf_counterr*   acquirer   _do_acquire_innerr|   )ru   r5   rO   rH   r]   startacquireds          r%   r   zSoftReadWriteLock._acquire  s    #*/$,,w$,$44==([! 	6 	6% (rdnrrr"3'''| (NdnNNN"3'''z%//55	6 	6 	6 	6 	6 	6 	6 	6 &	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 !## 	W{.666FFHH]]{.666EEHH{.66g6VVH 	4$.))t3	.))$)RRK#++----DK#++----s   ABB"B<E3 3 Feffective_timeoutr   c                  | j         j        5  | j        !|                     |          cd d d            S 	 d d d            n# 1 swxY w Y   |dk    rd n||z   }t	          j        d          }|dk    r|                     |||          \  }}n|                     |||          \  }}t          j	                    }	t          | j        | j        |	dt          |           d          }
| j         j        5  t          d||dk    rt          j                    nd ||||
|		          | _        d d d            n# 1 swxY w Y   |
                                 t#          | 
          S )NrD      r   deadlinerH   zfilelock-heartbeat-x)refreshinterval
stop_eventnamer   )r3   r5   r7   r8   r:   r-   r<   r>   lock)rn   r)   rp   r   secrets	token_hex_acquire_writer_slot_acquire_reader_slotrl   Eventr;   _refresh_markerrJ   r   r2   	get_identr   r   )ru   r5   r   r   rH   r   r-   r8   r:   r   	heartbeats              r%   r   z#SoftReadWriteLock._do_acquire_inner  s+    [! 	6 	6z%//55	6 	6 	6 	6 	6 	6 	6 	6%	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 	6 -2244@Q8Q!"%%7??%)%>%>uxbj%>%k%k"K%)%>%>uxbj%>%k%k"K_&&
$(,!3r$xx333	
 
 
	 [! 
	 
	9=	 3 5 5 5d'#!*)	 	 	DJ
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 	!t,,,,s#   AA
A96D;;D?D?c                   | j         }|J |j        |k    rG|dk    rdnd}|dk    rdnd}d| d| j         dt          |            d| d	| d
}t	          |          |dk    rRt          j                    x}|j        k    r4d| j         dt          |            d| d|j         }t	          |          |xj        dz  c_        t          |           S )Nr   r   	downgradeupgradezCannot acquire z	 lock on r   z): already holding a z lock (z not allowed)zCannot acquire write lock on z) from thread z while it is held by thread r   r   )
rp   r5   rM   r   r   rl   r   r7   r3   r   )ru   r5   r   opposite	directionr]   curs          r%   r   z%SoftReadWriteLock._validate_reentrant  sY   z9"&&..wwfH'+v~~9IO$ O O O OBtHH O O%-O O6?O O O  s###7??y':'<'< <AUUUW W W2d88 W W"W W@D@TW W  s###

a

!t,,,,r$   r-   r   r   tuple[str, bool]c                                                      d fd}d fd}                     |||           	                      |||           n(# t          $ r t           j        j                    w xY w j        j        dfS )NrR   r9   c                 t    j         j        5  t           j        j         j        t          j                               t           j        j                  r	 d d d            dS 	 t           j        j                   n# t          $ r Y d d d            dS w xY w	 d d d            n# 1 swxY w Y   dS )NrK   nowFT)
rn   r   _break_stale_markerrk   r   rK   r   _file_exists_atomic_create_markerFileExistsError)ru   r-   s   r%   try_claim_writerz@SoftReadWriteLock._acquire_writer_slot.<locals>.try_claim_writer  s:   " ! !#DK$5tG[aeajalalmmmm 122 ! ! ! ! ! ! ! ! !!)$+*;UCCCC& ! ! ! ! ! ! ! ! ! ! !! D! ! ! ! ! ! ! ! ! ! ! ! ! ! ! 4s6   AB-)BB-
BB-BB--B14B1c                 P    j         j        5  t          t                    5  t	           j        j                   d d d            n# 1 swxY w Y                        t          j                                	                                 cd d d            S # 1 swxY w Y   d S N)
rn   r   r   r   _touchrk   r   _break_stale_readersr   _any_readersr   s   r%   readers_drained_touchingzHSoftReadWriteLock._acquire_writer_slot.<locals>.readers_drained_touching  s   " / / g&& . .4;,---. . . . . . . . . . . . . . .))$)++666,,.../ / / / / / / / / / / / / / / / / /s3   BA	B	A	BA	=BB"Br   FrR   r9   )_open_readers_dir	_wait_forr   r   rk   r   )ru   r-   r   rH   r   r   s   ``    r%   r   z&SoftReadWriteLock._acquire_writer_slot  s     	   		 		 		 		 		 		 			/ 	/ 	/ 	/ 	/ 	/ 	'(XNNN	NN3hQYNZZZZ 	 	 	DK%&&&	 { %''s   A %A;c               D                                       t          j                    j         dt	          j                      j        t          t           j	        j
                  z            d fd}                     |||           ndfS )N.rR   r9   c                 P   j         j        5  t          j        j        j        t          j                               t          j        j                  r	 d d d            dS  t                      nt                     	 d d d            dS # 1 swxY w Y   d S )Nr   Fr   T)	rn   r   r   rk   r   rK   r   r   r   )r   full_reader_pathreader_nameru   r-   s   r%   try_claim_readerz@SoftReadWriteLock._acquire_reader_slot.<locals>.try_claim_reader!  s   "  #DK$5tG[aeajalalmmmm 122 !         %)+uVLLLLL)*:EBBB                 s   AB(&BB"Br   Tr   )r   uuiduuid4hexri   getpidro   r   r   rk   r   r   )ru   r-   r   rH   r   r   r   r   s   ``   @@@r%   r   z&SoftReadWriteLock._acquire_reader_slot  s     	   )99BIKK99%tDK$788;FGG		 		 		 		 		 		 		 		 		 		 	'(XNNN%17G$NNr$   	predicateCallable[[], bool]c               $   	  |            rd S t          j                    }|st          | j                  |||k    rt          | j                  | j        }|!t          |t          ||z
  d                    }t          j        |           )NTg        )r   r   r   rM   rL   minmaxsleep)ru   r   r   rH   r   	sleep_fors         r%   r   zSoftReadWriteLock._wait_for/  s    	"y{{ #%%C .dn---#xdn---*I#	3x#~s+C+CDD	Jy!!!	"r$   c                Z   t          | j        j                  }t          t                    5  |                    d           d d d            n# 1 swxY w Y   t          j        | j        j                  }t          j	        |j
                  st          j        |j
                  s| j        j         d}t          |          | j        Xt          rSt          j        t!          t          dd          z  t"          z  }t          j        | j        j        |          | _        d S d S d S )Ni  )r5   zB exists but is not a directory or is a symlink; refusing to use itO_DIRECTORYr   )r   rk   r   r   r   mkdirri   lstatstatS_ISLNKst_modeS_ISDIRr   ro   _SUPPORTS_DIR_FDO_RDONLYgetattr_O_NOFOLLOWopen)ru   readers_pathstr]   flagss        r%   r   z#SoftReadWriteLock._open_readers_dirC  sE   DK/00o&& 	+ 	+E***	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ Xdk)**<
## 	$4<
+C+C 	$[(lllCs###',<'K'"mQ"?"??+ME#%74;+>#F#FD    ('''s   AAAc                8    |                                  D ]} dS dS )NTF)_iter_reader_entries)ru   _s     r%   r   zSoftReadWriteLock._any_readersQ  s)    **,, 	 	A44ur$   Generator[tuple[str, bool]]c              #    K   | j         Xt          j        | j                   5 }|D ]!}t          |j                  s|j        dfV  "	 ddd           n# 1 swxY w Y   dS t          | j        j                  }t          j        |          5 }|D ]1}t          |j                  st          ||j        z            dfV  2	 ddd           dS # 1 swxY w Y   dS )a#  
        Yield ``(name, dirfd_relative)`` pairs for every live reader marker.

        ``dirfd_relative`` is ``True`` when *name* should be passed to ``dir_fd=``-aware syscalls; ``False``
        when *name* is a full path because dirfd-relative I/O is unavailable on this platform.
        NTF)	ro   ri   scandir_is_housekeeping_namer   r   rk   r   r   )ru   itentryr   s       r%   r   z&SoftReadWriteLock._iter_reader_entriesV  s      +D011 /R / /E0<< /#j$....// / / / / / / / / / / / / / / FDK/00Z%% 	@ @ @,UZ88 @lUZ788%????@	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@ 	@s#   %AAA5CCCr   c                    g }	 |                                  D ]%\  }}|                    ||r| j        nd f           &n# t          $ r Y d S w xY w|D ]\  }}t	          || j        ||           d S )N)rK   r   r   )r   appendro   r   r   rK   )ru   r   namesr   dirfd_relativefds         r%   r   z&SoftReadWriteLock._break_stale_readersi  s    .0	(,(A(A(C(C W W$ndN$TD$8$8PTUVVVVW 	 	 	FF	 	` 	`HD"d6JPS\^_____	` 	`s   :? 
AAc                p   | j         j        5  | j        }|	 d d d            dS |j        }|j        }|j        r| j        nd }d d d            n# 1 swxY w Y   t          ||          }|dS |\  }}|t          j	        |j        |          sdS 	 t          ||           n# t          $ r Y dS w xY wdS )NFr   T)rn   r)   rp   r8   r-   r:   ro   _read_markerhmaccompare_digestr   FileNotFoundError)ru   r   r8   r-   r   read_resultinfo_mtimes           r%   r   z!SoftReadWriteLock._refresh_markers  s`   [! 	F 	F:D|	F 	F 	F 	F 	F 	F 	F 	F *KJE-1^ET))F	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F #;v>>>5"f <t24:uEE<5	;v.....  	 	 	55	ts(   AAAAB% %
B32B3c                    t          t          j                    t          j                    t          | j        j        d                    | _        d | _        d | _        d| _	        d S )NrD   rg   rh   T)
r'   rl   rm   r   rk   r   rn   rp   ro   rq   r   s    r%   _reset_after_fork_in_childz,SoftReadWriteLock._reset_after_fork_in_child  sd     ^%%!((t{0"===
 
 

 
#!%r$   r_   )rM   rN   rO   rP   rH   r9   rI   r9   rJ   rP   rK   rQ   rL   rP   rR   rc   r   )rO   rQ   rH   rx   rR   ry   )rO   rQ   rH   rx   rR   r   rR   rc   )r   r9   rR   rc   )rM   rN   rO   rP   rH   r9   rR   rS   )r5   r4   rO   rQ   rH   rx   rR   r   )
r5   r4   r   rP   r   rP   rH   r9   rR   r   )r5   r4   rR   r   )r-   r   r   rQ   rH   r9   rR   r   )r   r   r   rQ   rH   r9   rR   rc   r   )rR   r   )r   rP   rR   rc   ) r   r    r!   r?   r
   rB   r"   rl   rm   rC   rv   r   r~   r   r{   r   r   r|   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r  r#   r$   r%   rS   rS      s        , ,\ @S?R?T?TJTTTT$in&&O
 0
 !$((,#0 0 0 0 0 0d QU      ^& RV      ^&ATX A A A A A A.BUY B B B B B B6, , , ," (- (& (& (& (& (& (&T  :
 : : : : : [:* .  .  .  .D$- $- $- $-L- - - -(&( &( &( &(PO O O O4" " " "(G G G G   
@ @ @ @&` ` ` `   .& & & & & &r$   rS   )	metaclassc                  (     e Zd Zd fdZddZ xZS )r;   r   r   r   rP   r   r=   r   r   rR   rc   c                x    t                                          |d           || _        || _        || _        d S )NT)r   daemon)rU   rv   _refresh	_interval_stop_event)ru   r   r   r   r   r^   s        r%   rv   z_HeartbeatThread.__init__  s>     	d4000!%r$   c                    | j                             | j                  sP|                                 s| j                                          d S | j                             | j                  Nd S d S r   )r  waitr  r  r   r   s    r%   runz_HeartbeatThread.run  sx    "''77 	==??  $$&&& "''77 	 	 	 	 	r$   )
r   r   r   rP   r   r=   r   r   rR   rc   r  )r   r    r!   rv   r  r`   ra   s   @r%   r;   r;     sQ        
& 
& 
& 
& 
& 
&       r$   r;   r   r   r   r-   r   r6   rR   rc   c                  t           j        t           j        z  t           j        z  t          z  }t
          r|t          j        | |d|          }nt          j        | |d          }	 | dt          j                     dt          j	                     d
                    d          }t          j        ||           t          j        |           d S # t          j        |           w xY w)Ni  r   
ascii)ri   O_CREATO_EXCLO_WRONLYr   r   r   r   socketgethostnameencoder   r   )r   r-   r   r   r   contents         r%   r   r     s     J"R[0;>E )F.WT5%777WT5%((EEbikkEEV-?-A-AEEELLWUU
W
s   (AC C)'tuple[_MarkerInfo | None, float] | Nonec                  t           j        t          z  t          z  }	 t          r|t          j        | ||          nt          j        | |          }n# t          $ r Y d S w xY w	 	 t          j        |          }t          j        |t          dz             }n%# t          $ r Y t          j
        |           d S w xY w	 t          j
        |           n# t          j
        |           w xY wt          |          |j        fS )Nr   r   )ri   r   r   _O_NONBLOCKr   r   r   fstatr   _MAX_MARKER_SIZEr   _parse_marker_bytesst_mtime)r   r   r   r   r   datas         r%   r   r     s    K+%3E4DuI[RWT50000acahimotauau   tt	"B72/!344DD 	 	 	
	  	t$$bk11s;   5A 
A"!A"'1B C 
B;#C :B;;C C*r%  bytes_MarkerInfo | Nonec                J   | rt          |           t          k    rd S 	 |                     d          }n# t          $ r Y d S w xY wt	          j        d|t          j                  }|d S t          |d         d          }|dk    rd S t          |d         ||d                   S )	Nr  u  
        \A                                  # start of string
        (?P<token>    [0-9a-f]{32}     ) \n # 128-bit hex token
        (?P<pid>      [1-9][0-9]{0,9}  ) \n # decimal pid: no leading zero, ≤ 10 digits
        (?P<hostname> [\x21-\x7e]{1,253})   # printable non-whitespace ASCII (RFC 1123 hostname limit)
        \n*                                 # tolerate sloppy writers that append extra newlines
        \Z                                  # end of string
        r/   
   ir-   r0   )r-   r/   r0   )	lenr"  decodeUnicodeDecodeErrorrematchVERBOSEr.   r,   )r%  textr.  r/   s       r%   r#  r#    s      3t99///t{{7##   ttH	 	

 E }t
eElB

C
YtU7^uZ?PQQQQs   4 
AArK   rP   r   r9   c               b   t          | |          }|dS |\  }}||z
  |k    rdS |t          | |           dS |  t           dt          j                     dt          j        d           }	 t          r|t          j        | |||           n"t          |                               |           n# t          $ r Y dS w xY wt          ||          }|dS |\  }	}
|	t          ||           dS t          j        |j        |	j                  sdS |
|k    rdS t          ||           dS )Nr   FTr   r   )
src_dir_fd
dst_dir_fd)r   r   _BREAK_SUFFIXri   r   r   r   r   renamer   r   r   r  r-   )r   rK   r   r   r  info_beforemtime_before
break_name
read_after
info_aftermtime_afters              r%   r   r     s    tF333Ku +K
\_,,uV$$$$tO-OO")++OO8I"8M8MOOJ 	* 2IdJ6fMMMMMJJj)))   uu j888Ju(J
6****t{0*2BCC u\!!uJv&&&&4s   0AB5 5
CCc                   t          t                    5  t          r|t          j        | |           n!t          |                                            d d d            d S # 1 swxY w Y   d S Nr   )r   r  r   ri   unlinkr   r   r   s     r%   r   r     s    	#	$	$     	  2Id6*****JJ                                   s   AA$$A(+A(c               t    t           r|t          j        | d |           d S t          j        | d            d S r=  )r   ri   utimer?  s     r%   r   r   #  sD     F.
tF++++++
tr$   pathc                    	 t          j        |           }n# t          $ r Y dS w xY wt          j        |j                  S )NF)ri   r   r  r   S_ISREGr   )rB  r   s     r%   r   r   *  sJ    Xd^^   uu<
###s    
%%c                >    |                      d          pt          | v S )Nr   )
startswithr4  )r   s    r%   r   r   2  s    ??38=D#88r$   c                     t          j                    at          t                                                    D ]} |                                  d S r   )rl   rm   rs   listr   valuesr  r\   s    r%   _reset_all_after_forkrK  6  sT    
 $.**..0011 . .++----. .r$   c                     t          t                                                    D ]D} t          t                    5  |                     d           d d d            n# 1 swxY w Y   Ed S )NTr   )rH  r   rI  r   	Exceptionr|   rJ  s    r%   _cleanup_all_instancesrN  @  s    ..0011 ) )i   	) 	)4(((	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	) 	)) )s   A  A$	'A$	c                     t           st          j        t                     da t          s3t          t          d          r t          j        t                     dad S d S d S )NTregister_at_fork)after_in_child)	_atexit_registeredatexitregisterrN  _fork_registeredhasattrri   rP  rK  r#   r$   r%   rt   rt   F  sp     ".///!  ,> ? ?  
+@AAAA       r$   )r   r   r-   r   r   r6   rR   rc   )r   r   r   r6   rR   r  )r%  r&  rR   r'  )
r   r   rK   rP   r   rP   r   r6   rR   r9   )r   r   r   r6   rR   rc   )rB  r   rR   r9   )r   r   rR   r9   r  )Jr?   
__future__r   rS  r   ri   r-  r   r  r   sysrl   r   r   
contextlibr   r   dataclassesr   pathlibr   typingr   r	   weakrefr
   filelock._apir   filelock._errorr   filelock._softr   filelock._utilr   collections.abcr   r   r4   r4  r"  r   r   r   platformr   supports_dir_fdr   r   r"   rm   rs   rR  rU  r   r'   r,   r2   typerA   rS   Threadr;   r   r   r#  r   r   r   r   r   rK  rN  rt   __all__r#   r$   r%   <module>rh     s   ` ` ` " " " " " "   				 				    



       / / / / / / / / ! ! ! ! ! !       ) ) ) ) ) ) ) ) ' ' ' ' ' ' , , , , , , # # # # # # ' ' ' ' ' ' 2 2 2 2 2 2 433333333 	  gb,**gb,** <7*Lrw":L/L ?R?R?T?T T T T T$in&&    $                $        
$ 
$ 
$ 
$ 
$ 
$ 
$ 
$. . . . .$ . . .bS& S& S& S& S&+ S& S& S& S&l    y'   ( JN       59 2 2 2 2 2 2(R R R RF * * * * * *Z 04             /3      $ $ $ $9 9 9 9. . . .) ) ) )        r$   