
    ^j8                       d dl mZ d dlZd dlZd dlZd dlmZ d dlmZ d dl	m
Z
mZmZm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mZmZ ddlmZ ddlmZmZ er,d dlZd dl m!Z! ddl"m#Z# ejH                  dk\  rd dlm%Z% nd dl&m%Z% ed   Z'ed   Z( ed       G d d             Z) ed       G d d             Z*e G d d             Z+ G d d      Z, G d de,e      Z- G d d e      Z.g d!Z/y)"    )annotationsN)suppress)	dataclass)EventThreadcurrent_threadlocal)TYPE_CHECKINGLiteral   )LeaseSettingsMismatch)owner_is_stale)MarkerSoftFileLock	OwnerModeOwnerRecordparse_marker)_read_lock_file)break_lock_filetouch)Callable)LockOptions)      )Unpack)marker-missingowner-changedrefresh-failed)oklost	transientT)frozenc                  >    e Zd ZU dZded<   ded<   ded<   dZded	<   y)
LeaseCompromisez6Why a held lease stopped being this process's to hold.str	lock_filetokenCompromiseReasonreasonNOSError | Noneerror)__name__
__module____qualname____doc____annotations__r*        Z/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/filelock/_lease.pyr#   r#   !   s    @NJ E> r1   r#   c                  &    e Zd ZU dZded<   ded<   y)
_HeartbeatzPA running heartbeat and the event that stops it, which only ever exist together.r   threadr   stopN)r+   r,   r-   r.   r/   r0   r1   r2   r4   r4   +   s    ZN
Kr1   r4   c                  <    e Zd ZU dZdZded<   dZded<   dZded<   y)	_LeaseClaimzNThe state of one claim: its token, its heartbeat, and how that claim was lost.N
str | Noner&   LeaseCompromise | None
compromisez_Heartbeat | None	heartbeat)r+   r,   r-   r.   r&   r/   r;   r<   r0   r1   r2   r8   r8   3   s%    XE:)-J&-#'I 'r1   r8   c                      e Zd ZdZddZy)_LeaseClaimHolderz,Holds the claim its owning context acquired.c                "    t               | _        y N)r8   claimselfs    r2   __init__z_LeaseClaimHolder.__init__B   s     ]
r1   NreturnNone)r+   r,   r-   r.   rD   r0   r1   r2   r>   r>   <   s
    6
#r1   r>   c                      e Zd ZdZy)_ThreadLocalLeaseClaimHolderz:A thread local version of the ``_LeaseClaimHolder`` class.N)r+   r,   r-   r.   r0   r1   r2   rI   rI   F   s    Dr1   rI   c                  X    e Zd ZU dZdZded<   dZded<   dZd	ed
<   dddd	 	 	 	 	 	 	 	 	 	 	 d fdZe	dd       Z
e	dd       Ze	d d       Ze	d!d       Zd" fdZd" fdZd# fdZd" fdZd$dZd%dZd"dZ	 	 	 	 	 	 	 	 	 	 	 	 d&dZ	 	 	 	 	 	 	 	 	 	 d'dZ	 	 	 	 	 	 	 	 	 	 d(dZ xZS ))SoftFileLeasea  
    Existence lock whose claim expires, so a peer may take it while the previous holder still runs.

    A lease trades mutual exclusion for progress. The holder publishes a claim and refreshes it every
    ``heartbeat_interval`` seconds; a contender takes the marker once it is ``lease_duration`` seconds stale. Nothing
    stops the expired holder: it keeps running, and it keeps using whatever the lock protects. Treat the lease as a hint
    about who *should* be working, not as a guarantee that only one worker is.

    To make a protected resource reject a superseded holder, that resource must be linearizable and must fence on a
    monotonic generation it controls. :attr:`token` names a claim; it does not fence one. Where overlap is unacceptable,
    use :class:`StrictSoftFileLock <filelock.StrictSoftFileLock>` instead.

    Every contender for a path must agree on ``lease_duration``. A contender that finds a claim published under a
    different duration raises :class:`LeaseSettingsMismatch <filelock.LeaseSettingsMismatch>` rather than apply its own
    expiry to a peer that never agreed to it.

    Expiry reclaims less on Windows, which refuses to rename or delete a file another process holds open. A peer there
    takes an expired claim only once the previous holder's process exits and its handle closes; a holder that lives on
    but stops refreshing keeps the marker. Unix reclaims the marker either way.

    ``on_compromise`` fires from the heartbeat thread when a refresh fails, or when the marker vanishes or names another
    owner. The holder should stop touching the protected resource when it runs. Because it runs on that thread, a
    ``release()`` inside it only takes effect when the lease was built with ``thread_local=False``; the default
    thread-local context hides the claim from every thread but the one that acquired it, so the release does nothing.
    Signal the acquiring thread instead when the context stays thread-local.

    .. versionadded:: 3.30.0

    leaser   _owner_modeFbool_lifetime_supportedz(lease_duration sets when a lease expiresr$   _lifetime_unsupported_reasong      >@N)lease_durationheartbeat_intervalon_compromisec               "   |dk  rd|}t        |      ||dz  }d|cxk  r|k  sn d|}t        |      t        |   |fi | || _        || _        || _         | j                         rt               | _	        yt               | _	        y)a  
        Create a lease.

        :param lease_duration: seconds of marker staleness after which a contender may take the claim. Every contender
            for the path must pass the same value.
        :param heartbeat_interval: seconds between refreshes. Defaults to a third of ``lease_duration``, leaving room
            for two missed refreshes before a peer may take the claim. Must be shorter than ``lease_duration``.
        :param on_compromise: called from the heartbeat thread with a :class:`LeaseCompromise` when the claim is lost.
        :param kwargs: every other :class:`BaseFileLock <filelock.BaseFileLock>` option, ``timeout`` and ``mode`` among
            them. The metaclass passes them all by keyword, and taking them here lets
            :class:`AsyncSoftFileLease <filelock.AsyncSoftFileLease>` add the async plumbing a fixed signature would
            hide.

        r   z%lease_duration must be positive, got Nr   zBheartbeat_interval must be positive and below lease_duration, got )

ValueErrorsuperrD   _lease_duration_heartbeat_interval_on_compromiseis_thread_localrI   r>   _claims)rC   r%   rQ   rR   rS   kwargsmsg	__class__s          r2   rD   zSoftFileLease.__init__o   s    . Q9.9KLCS/!%!/!!3%66VWiVlmCS/!-f--#5 + ZD,@,@,B(+HY+r1   c                .    | j                   j                  S r@   )r[   rA   rB   s    r2   _claimzSoftFileLease._claim   s    ||!!!r1   c                    | j                   S )zEThe staleness in seconds after which a contender may take this claim.)rW   rB   s    r2   rQ   zSoftFileLease.lease_duration   s     ###r1   c                .    | j                   j                  S )z
        The token naming the claim this process published.

        :returns: the token while the lease is held, ``None`` otherwise. It identifies a claim; it does not fence one.

        )r`   r&   rB   s    r2   r&   zSoftFileLease.token   s     {{   r1   c                .    | j                   j                  S )z
        The loss of claim the heartbeat observed.

        :returns: the :class:`LeaseCompromise`, or ``None`` while the claim still holds

        )r`   r;   rB   s    r2   r;   zSoftFileLease.compromise   s     {{%%%r1   c                .   | j                   }| j                          t        j                  d      x|_        }d |_        t        |           | j                  j                  x}/| j                  j                  x}	 | j                  ||||       y y y )N   )r`   _stop_heartbeatsecrets	token_hexr&   r;   rV   _acquire_contextlock_file_fdlock_file_fd_identity_start_heartbeat)rC   rA   r&   fdidentityr^   s        r2   ri   zSoftFileLease._acquire   s    %//33e --,,,B9;;;H? !!%Xu=?9r1   c                d    | j                          d | j                  _        t        |           y r@   )rf   r`   r&   rV   _releaserC   r^   s    r2   rq   zSoftFileLease._release   s&     r1   c                ~    t         |          j                  | j                  j                  | j
                        S )N)r&   rQ   )rV   _published_record_replacer`   r&   rW   rr   s    r2   rt   zSoftFileLease._published_record   s2    w(*33$++:K:K\`\p\p3qqr1   c                d   | j                         x}t        | 	          y |\  }}}|j                  dk7  ry |j                  | j
                  k7  r4| j                   d|j                  d| j
                  d}t        |      t        t              5  t        |j                  |j                  |j                        r!t        | j                  ||       	 d d d        y t        j                         |z
  | j
                  k\  rt        | j                  ||       d d d        y # 1 sw Y   y xY w)NrL   z holds a lease of z s but this contender configured z:s; every contender for a path must agree on lease_duration)
_read_peerrV   _try_break_stale_lockmoderQ   rW   r%   r   r   OSErrorr   pidhostnamestartr   time)rC   peerownermtimeinor]   r^   s         r2   rx   z#SoftFileLease._try_break_stale_lock   s   OO%%D.
 G)+ uc :: 4#7#77>>""4U5I5I4LLl''**df  (,, g 	< eiiEs;	< 	< yy{U"d&:&::s;	< 	< 	<s   AD&";D&&D/c                    t        t        t              5  t        | j                        \  }}}t        |      x}|||fcd d d        S 	 d d d        y # 1 sw Y   y xY wr@   )r   rz   rU   r   r%   r   )rC   contentr   r   r   s        r2   rw   zSoftFileLease._read_peer   sb    gz* 	)"1$.."AGUC%g..;eS(	) 	);	) 		) s   +AAc                    t               }t        | j                  |||||fdt        j                          d      }t        ||      |_        |j                          y )Nzfilelock-lease-T)targetargsnamedaemon)r   r   _refresh_until_stoppedosgetpidr4   r<   r}   )rC   rA   rn   ro   r&   r6   r5   s          r2   rm   zSoftFileLease._start_heartbeat   sX     w..Xud3"299;-0	
 %VT2r1   c                    | j                   }|j                  x}y |j                  j                          d |_        |j                  t               ur'|j                  j                  | j                         y y )N)timeout)r`   r<   r6   setr5   r   joinrX   )rC   rA   r<   s      r2   rf   zSoftFileLease._stop_heartbeat   sg    (I1>#33!!$*B*B!C 4r1   c                   t        j                         }|j                  | j                        s| j	                  ||||      \  }}|dk(  ry |dk(  rt        j                         }nFt        j                         |z
  | j
                  | j                  z
  k\  r| j                  |d||       y |j                  | j                        sy y )Nr   r   r   )r~   	monotonicwaitrX   _refresh_claimrW   _report_compromise)	rC   rA   rn   ro   r&   r6   last_successoutcomer*   s	            r2   r   z$SoftFileLease._refresh_until_stopped  s     ~~'))D445!00HeLNGU& $#~~/!L0D4H4H4KcKc4cc''/?N ))D445r1   c                   	 t        j                  | j                        }|j                  |j                  f|k7  r| j	                  |dd |       y	 t        | j                  |       y# t        $ r}| j	                  |d||       Y d }~yd }~wt
        $ r}d|fcY d }~S d }~ww xY w# t
        $ r}d|fcY d }~S d }~ww xY w)Nr   )r   Nr    r   )rn   )r   N)	r   lstatr%   FileNotFoundErrorr   rz   st_devst_inor   )rC   rA   rn   ro   r&   str*   s          r2   r   zSoftFileLease._refresh_claim  s    	&$..)B IIryy!X-##E?D%H	&$..R(  ! 	 ##E+;UEJ 	&%%	&  	&%%	&sG   A* B) *	B&3BB&B!B&!B&)	C 2B;5C ;C c                    t        | j                  |||      |_        | j                  | j                  |j                         y y )N)r%   r&   r(   r*   )r#   r%   r;   rY   )rC   rA   r(   r*   r&   s        r2   r   z SoftFileLease._report_compromise6  sB     +T^^5Y_glm* 0 01 +r1   )r%   zstr | os.PathLike[str]rQ   floatrR   zfloat | NonerS   z(Callable[[LeaseCompromise], None] | Noner\   zUnpack[LockOptions]rF   rG   )rF   r8   )rF   r   )rF   r9   )rF   r:   rE   )rF   r   )rF   z%tuple[OwnerRecord, float, int] | None)
rA   r8   rn   intro   tuple[int, int]r&   r$   rF   rG   )rA   r8   rn   r   ro   r   r&   r$   r6   r   rF   rG   )
rA   r8   rn   r   ro   r   r&   r$   rF   z&tuple[_RefreshOutcome, OSError | None])
rA   r8   r(   r'   r*   r)   r&   r$   rF   rG   )r+   r,   r-   r.   rM   r/   rO   rP   rD   propertyr`   rQ   r&   r;   ri   rq   rt   rx   rw   rm   rf   r   r   r   __classcell__)r^   s   @r2   rK   rK   J   s   < %K$ !&%(R #R !%+/BF')' 	'
 )' @' &' 
'R " " $ $ ! ! & &>
r<<D  "	
   
0  "	
  
0022 !2 	2
 2 
2r1   rK   )r'   r#   rK   )0
__future__r   r   rg   r~   
contextlibr   dataclassesr   	threadingr   r   r   r	   typingr
   r   _errorr   	_identityr   _markerr   r   r   r   _softr   _utilr   r   syscollections.abcr   _apir   version_infor   typing_extensionsr'   _RefreshOutcomer#   r4   r8   r>   rI   rK   __all__r0   r1   r2   <module>r      s    " 	    ! : : ) ) % M M " )(!
7"!,NO 34 $! ! ! $   ( ( (# #E#4e Ex2& x2vr1   