
    \jA2                       U d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlm	Z	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mZmZ dd
lmZ ddlmZmZmZmZ dZded<   dZ ded<   dZ!ded<   dZ"ded<    G d de      Z#ddZ$ddZ%ddZ&dgZ'y)    )annotationsN)suppress)EACCESEEXISTEPERM)Path)Final   )BaseFileLock_raise_grouped_errors)	host_nameowner_is_staleprocess_start_token)STRICT_SOFT_SENTINEL_RECORD)break_lock_fileensure_directory_existsraise_on_not_writable_file	write_allg       @zFinal[float]_MALFORMED_LOCK_AGE_THRESHOLDi   z
Final[int]_MAX_LOCK_FILE_SIZE
   _UNLINK_MAX_RETRIES   #_MARKER_WITH_START_TOKEN_LINE_COUNTc                      e Zd ZU dZdZded<   dZded<   dZded	<   dZded
<   ddZ	ddZ
ddZedd       Zedd       Zedd       ZddZddZddZddZy)SoftFileLockaz  
    Cooperative file lock based on a shared existence marker.

    Unlike :class:`UnixFileLock <filelock.UnixFileLock>` and :class:`WindowsFileLock <filelock.WindowsFileLock>`, this
    lock does not use OS-level locking primitives. Instead, it creates the lock file with ``O_CREAT | O_EXCL`` and
    treats its existence as the lock indicator. The filesystem must provide coherent exclusive creation and directory
    updates to each participating process. A crash can leave the marker behind.

    The marker contains the holder's PID and hostname. A contender may remove it when it can no longer find a same-host
    process with that PID. A configured :attr:`~filelock.BaseFileLock.lifetime` also permits removal based on marker
    age, including while the holder remains alive. Age-based expiry can overlap protected operations and does not
    provide strict mutual exclusion.

    Tbool_lifetime_supported)StrictSoftFileLockSoftFileLeaseztuple[str, str] | None_lifetime_replacementsF_preserve_lock_file_supported_on_acquired_supportedc                r   t        | j                         t        | j                         t        j                  t        j
                  z  t        j                  z  t        j                  z  }t        t        dd       x}||z  }	 t        j                  | j                  || j                               }| j%                  |       | j'                  |       y # t        $ rT}|j                  t        k(  s'|j                  t        k(  rt        j                   dk(  s | j#                          Y d }~y d }~ww xY w)N
O_NOFOLLOWwin32)r   	lock_filer   osO_WRONLYO_CREATO_EXCLO_TRUNCgetattropen
_open_modeOSErrorerrnor   r   sysplatform_try_break_stale_lock_mark_descriptor_pending_publish_held_marker)selfflags
o_nofollowfd	exceptions        A/root/.hermes/venv/lib/python3.12/site-packages/filelock/_soft.py_acquirezSoftFileLock._acquire3   s    "4>>2/ bjj(2994rzzA!"lD99JFZE	0ABB 	%%b)!!"%  	6)ioo.GCLL\cLc&&(	s   /C 	D6"A
D11D6c                   d }	 t        t        j                  |            }| j                  |       | j                  ||       y # t        $ r | j                          t        j                  |       t        t              5  |Nt        t        j                  | j                              |k(  r#t        | j                        j                          d d d         # 1 sw Y    xY ww xY wN)_file_identityr(   fstat_write_lock_infoBaseException_mark_descriptor_releasedcloser   r0   lstatr'   r   unlink_mark_descriptor_owned)r7   r:   identitys      r<   r6   z!SoftFileLock._publish_held_markerG   s     ,0		%bhhrl3H!!"% 	##B1  	**,HHRL'" 2'N288DNN;S,TX`,`(//12 2 	s$   /A =C*AC	C*C&	"C*c                   t        t        t              5  t        | j                        \  }}}|t
        k(  r
	 d d d        y t        |      }|?t        j                         |z
  t        k\  rt        | j                  ||       	 d d d        y t        | rt        | j                  ||       d d d        y # 1 sw Y   y xY wr?   )r   r0   
ValueError_read_lock_filer'   r   _parse_lock_holdertimer   r   r   )r7   contentmtimeinoholders        r<   r4   z"SoftFileLock._try_break_stale_lockX   s    gz* 	<"1$.."AGUC55	< 	< (0F~ 99;&*GG#DNNE3?	< 	< v&s;	< 	< 	<s   $B6AB6B66B?c                    t        j                          dt                d}t        t        j                               x}|| dz  }t	        | |j                                y )N
)r(   getpidr   r   r   encode)r:   infotokens      r<   rB   zSoftFileLock._write_lock_infoj   sT    
 ))+bR0(55EBugRL D"dkkm$    c                    t        t        t              5  t        t	        | j
                        d         }||d   cddd       S 	 ddd       y# 1 sw Y   yxY w)z
        The PID of the process holding this lock, read from the lock file.

        :returns: the PID as an integer, or ``None`` if the lock file does not exist or cannot be parsed

        r   N)r   r0   rK   rM   rL   r'   )r7   rR   s     r<   pidzSoftFileLock.pidt   sZ     gz* 	!'(G(JKF!ay	! 	!!	! 		! s   (AAc                   t        t        t              5  t        t	        | j
                        d         }|5|\  }}}|t        j                         k(  xr |t               k(  cddd       S 	 ddd       y# 1 sw Y   yxY w)z
        Whether this lock is held by the current process.

        :returns: ``True`` if the lock file exists and names the current process's PID and hostname

        r   NF)	r   r0   rK   rM   rL   r'   r(   rU   r   )r7   rR   r[   hostname_s        r<   is_lock_held_by_uszSoftFileLock.is_lock_held_by_us   s~     gz* 	F'(G(JKF!#) Xqbiik)Eh)+.E		F 	F!	F
 	F
 s   AA88Bc                    t        t              5  t        | j                        j	                          ddd       y# 1 sw Y   yxY w)zNForcibly break the lock by removing the lock file, regardless of who holds it.N)r   r0   r   r'   rG   )r7   s    r<   
break_lockzSoftFileLock.break_lock   s4    g 	* '')	* 	* 	*s	   $=Ac                   | j                   j                  }|J d }t        t              5  t	        t        j                  |            }d d d        | j                          	 | j                  |d       | j                  |       y # 1 sw Y   ?xY w# t        $ r;}	 | j                  |        # t        $ r}t        d||       Y d }~ d }~ww xY wd }~ww xY w)NF)default_suppressesz4lock descriptor close and marker cleanup both failed)_contextlock_file_fdr   r0   r@   r(   rA   rD   _close_released_fdrC   _unlink_held_markerr   )r7   r:   rI   close_errorcleanup_errors        r<   _releasezSoftFileLock._release   s    ]]''~~ ,0g 	4%bhhrl3H	4 	&&(	##B5#A 	  *%	4 	4  		((2  ! %J! 
 		sG   B	$B 	B	CB10C1	C:CCCCCc                8   |y t         j                  dk(  r| j                  |       y t        t              5  t        t        j                  | j                              |k(  r#t        | j                        j                          d d d        y # 1 sw Y   y xY w)Nr&   )r2   r3   _windows_unlink_if_oursr   r0   r@   r(   rF   r'   r   rG   )r7   rI   s     r<   rg   z SoftFileLock._unlink_held_marker   sq    <<7"((2'" 2!"((4>>":;xG(//12 2 2s   ABBc                   d}t        t              D ]T  }	 t        t        j                  | j
                              |k7  r y t        | j
                        j                           y  y # t        $ rN}|j                  t        t        hvrY d }~ y |t        dz
  k  rt        j                  |       |dz  }Y d }~d }~ww xY w)NgMbP?r
      )ranger   r@   r(   rF   r'   r   rG   r0   r1   r   r   rN   sleep)r7   rI   retry_delayattemptexcs        r<   rl   z$SoftFileLock._windows_unlink_if_ours   s    01 	G!"((4>>":;xGT^^$++- 	  %99VUO30144JJ{+1$K%s#   +A*#A**	C3B<&B<<CN)returnNone)r:   intrt   ru   )rt   z
int | None)rt   r   )rI   ztuple[int, int] | Nonert   ru   )rI   tuple[int, int]rt   ru   )__name__
__module____qualname____doc__r   __annotations__r!   r"   r#   r=   r6   r4   staticmethodrB   propertyr[   r_   ra   rj   rg   rl    rY   r<   r   r      s      !%$ 6]2\ +0!4/ $)D(&(2"<$ % %    *
+62rY   r   c                2    | j                   | j                  fS r?   )st_devst_ino)sts    r<   r@   r@      s     99biirY   c           	     z   t        j                  |       }t        j                  |j                        sd |j
                  |j                  fS t        j                  | t         j                  t        t         dd      z  t        t         dd      z        }	 t        j                  |      }t        j                  |j                        s.d |j
                  |j                  ft        j                  |       S t        j                  |t        dz         }t        j                  |       t        |      t        k  rAt        t               5  |j#                  d      |j
                  |j                  fcd d d        S d |j
                  |j                  fS # t        j                  |       w xY w# 1 sw Y   <xY w)Nr%   r   
O_NONBLOCKr
   zutf-8)r(   rF   statS_ISREGst_modest_mtimer   r.   O_RDONLYr-   rA   rE   readr   lenr   UnicodeDecodeErrordecode)pathr   r:   datas       r<   rL   rL      sB   
 
$B<<

#R[["))++ 
r{{WRq%AAGBP\^_D``	aBXXb\||BJJ'bii/ 	 wwr.23

4y''() 	@;;w'bii?	@ 	@bii''	 		@ 	@s   AF 9F (F1F.1F:c                
   | r+t        | j                         j                         x}      dvry 	 t        |d         }t        |      t        k(  rt        |d         nd }d|cxk  rdk  sy  y ||d   |fS # t
        $ r Y y w xY w)N>   rn   r   r   rn   r
   i)r   strip
splitlinesrv   r   rK   )rO   linesr[   start_tokens       r<   rM   rM      s    
 c7==?#=#=#??%@N%(m'*5z5X'Xc%(m^b  y  !a+%%  s   0A6 6	BB)r   zos.stat_resultrt   rw   )r   strrt   ztuple[str | None, float, int])rO   z
str | Nonert   z"tuple[int, str, int | None] | None)(
__future__r   r(   r   r2   rN   
contextlibr   r1   r   r   r   pathlibr   typingr	   _apir   r   	_identityr   r   r   _soft_protocolr   _utilr   r   r   r   r   r|   r   r   r   r   r@   rL   rM   __all__r   rY   r<   <module>r      s    " 	  
   ' '   5 E E 7 b b.1 | 1"& Z &"$ Z $23 #Z 3t< tn (2&, rY   