o
    ôjÒK  ã                   @  s2  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 dd	lmZ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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„ deƒZ&d.dd„Z'd/d"d#„Z(d0d'd(„Z)d1d+d,„Z*g d-¢Z+dS )2é    )ÚannotationsN)Úsuppress)ÚEACCESÚEEXISTÚEPERM)Úislice)ÚPath)ÚFinalé   )ÚBaseFileLockÚ_raise_grouped_errors)Ú	host_nameÚowner_is_current_processÚ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                      s8  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Z	ded< dZ
ded< ded< dFdd„ZdGdd„ZdGdd„ZdFdd„ZdHd!d"„ZdId$d%„ZedJd(d)„ƒZedGd*d+„ƒZedKd-d.„ƒZedLd/d0„ƒZdFd1d2„ZdM‡ fd4d5„ZdFd6d7„ZdNd9d:„ZdOd;d<„ZdFd=d>„ZdFd?d@„ZdPdAdB„ZdQdDdE„Z ‡  Z!S )RÚ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_supportedNz&tuple[tuple[float, int], float] | NoneÚ_marker_seenútuple[int, int] | NoneÚ_marker_cleanupz	list[int]Ú_marker_fdsÚreturnÚNonec              
   C  sÐ   |   ¡  t| jƒ t| jƒ tjtjB tjB tjB }t	tdd ƒ }d ur(||O }zt 
| j||  ¡ ¡}W n' ty[ } z|jtksL|jtkrKtjdksL‚ |  ¡  W Y d }~d S d }~ww |  |¡ |  |¡ d S )NÚ
O_NOFOLLOWÚwin32)Ú_finish_marker_cleanupr   Ú	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Ú	exception© rB   úa/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/filelock/_soft.pyÚ_acquire=   s"   

€ú
zSoftFileLock._acquirer@   Úintc                 C  sx   d }zt t |¡ƒ}|  |¡ |  |¡ W n ty3   |  ¡  zt |¡ W |  |¡ ‚ |  |¡ w w |  	||¡ d S ©N)
Ú_file_identityr.   ÚfstatÚ_apply_explicit_modeÚ_write_lock_infoÚBaseExceptionÚ_mark_descriptor_releasedÚcloseÚ_unlink_held_markerÚ_mark_descriptor_owned)r=   r@   ÚidentityrB   rB   rC   r<   R   s   

ÿûz!SoftFileLock._publish_held_markerc                 C  sV   | j r'tjdkr)ttƒ� t || jj¡ W d   ƒ d S 1 s w   Y  d S d S d S ©Nr+   )	Úhas_explicit_moder8   r9   r   ÚPermissionErrorr.   ÚfchmodÚ_contextÚmode)r=   r@   rB   rB   rC   rI   d   s
   
"ÿÿz!SoftFileLock._apply_explicit_modec                 C  sÀ   t ttƒ�Q t| jƒ\}}}|tkr	 W d   ƒ d S t|ƒ}|d u r;|  ||¡tkr2t	| j||ƒ 	 W d   ƒ d S t
|Ž rNt	| j||ƒ W d   ƒ d S W d   ƒ d S 1 sYw   Y  d S rF   )r   r6   Ú
ValueErrorÚ_read_lock_filer-   r   Ú_parse_lock_holderÚ_marker_unchanged_forr   r   r   )r=   ÚcontentÚmtimeÚinoÚholderrB   rB   rC   r:   k   s    ýôñ"òz"SoftFileLock._try_break_stale_lockr\   Úfloatr]   c                 C  sD   t  ¡ }| j }d u s|d ||fkr||f|f| _dS ||d  S )Nr   g        r
   )ÚtimeÚ	monotonicr$   )r=   r\   r]   ÚnowÚseenrB   rB   rC   rZ   }   s
   z"SoftFileLock._marker_unchanged_forú
str | Nonec                 C  sŒ   t ttƒ�7 |  t| jƒd ¡ }r,|d tƒ kr4d|d › d|d › �W  d   ƒ S W d   ƒ d S W d   ƒ d S 1 s?w   Y  d S )Nr   r
   zpid z on )r   r6   rW   Ú_recorded_holderrX   r-   r   ©r=   r^   rB   rB   rC   Ú_unprobeable_holder†   s   &þ
ÿþ
ÿýz SoftFileLock._unprobeable_holderr[   útuple[int, str] | Nonec                 C  s    t |ƒ }d u r
d S |d d… S )Né   )rY   )Úclsr[   r^   rB   rB   rC   re   �   s    zSoftFileLock._recorded_holderc                 C  sJ   t  ¡ › dtƒ › d�}tt  ¡ ƒ }d ur||› d�7 }t| | ¡ ƒ d S )NÚ
)r.   Úgetpidr   r   r   Úencode)r@   ÚinfoÚtokenrB   rB   rC   rJ   ‘   s   zSoftFileLock._write_lock_infoú
int | Nonec                 C  s`   t ttƒ�! tt| jƒd ƒ}|dur|d W  d  ƒ S W d  ƒ dS 1 s)w   Y  dS )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   r6   rW   rY   rX   r-   rf   rB   rB   rC   Úpid›   s   ý
þüzSoftFileLock.pidc                 C  s`   t ttƒ�! tt| jƒd ƒ }durt|Ž W  d  ƒ S W d  ƒ dS 1 s)w   Y  dS )zÒ
        Whether this lock is held by the current process.

        :returns: ``True`` if the lock file exists and names the current process's PID, hostname and, when recorded,
            start token

        r   NF)r   r6   rW   rY   rX   r-   r   rf   rB   rB   rC   Úis_lock_held_by_us©   s   	þ
ÿýzSoftFileLock.is_lock_held_by_usc                 C  s<   t tƒ� t| jƒ ¡  W d  ƒ dS 1 sw   Y  dS )zNForcibly break the lock by removing the lock file, regardless of who holds it.N)r   r6   r   r-   Úunlink©r=   rB   rB   rC   Ú
break_lock·   s   
"ÿzSoftFileLock.break_lockÚforcec                  s   t ƒ j|d� |  ¡  d S )N)rv   )ÚsuperÚ_release_currentr,   )r=   rv   ©Ú	__class__rB   rC   rx   ¼   s   zSoftFileLock._release_currentc              	   C  s´   | j j}|d us
J ‚d }ttƒ� tt |¡ƒ}W d   ƒ n1 s"w   Y  tjdkr4|  	||¡ d S |  
|¡}|  ¡  z| j|dd� W |sO|  |¡ d S d S |sY|  |¡ w w )Nr+   F©Údefault_suppresses)rU   Úlock_file_fdr   r6   rG   r.   rH   r8   r9   Ú_release_then_unlinkÚ_unlink_open_markerrL   Ú_close_released_fdrN   )r=   r@   rP   ZunlinkedrB   rB   rC   Ú_releaseÀ   s"   
ÿ

ÿÿzSoftFileLock._releaserP   c                 C  s®   | j �J |g| _|| _|  ¡  z|  ¡  W n+ ty@ } zz|  ¡  W ‚  ty; } ztd||ƒ W Y d }~‚ d }~ww d }~ww |  ¡  W d   ƒ d S 1 sPw   Y  d S )Nz4lock descriptor close and marker cleanup both failed)Ú_transition_lockr'   r&   rL   Ú_close_marker_fdrK   r,   r   )r=   r@   rP   Zclose_errorZcleanup_errorrB   rB   rC   r~   Ú   s.   
úý€ú€ý

"ïz!SoftFileLock._release_then_unlinkc                 C  s–   z@t tƒ�1 |d ur$tt | j¡ƒ|kr-t| jƒ ¡  W d   ƒ W dS W d   ƒ W dS W d   ƒ W dS 1 s9w   Y  W dS  tyJ   Y dS w )NFT)	r   ÚFileNotFoundErrorrG   r.   Úlstatr-   r   rs   r6   ©r=   rP   rB   rB   rC   r   ð   s"   
þüÿüÿûþÿz SoftFileLock._unlink_open_markerc                 C  s–   | j d urG| jjdd�rIz4ttƒ� |  ¡  W d   ƒ n1 s w   Y  | j  }d ur9|  |¡ W | j ¡  d S W | j ¡  d S | j ¡  w d S d S )NF)Úblocking)r&   r‚   Úacquirer   r6   rƒ   Ú_windows_unlink_if_oursÚreleaser†   rB   rB   rC   r,   ù   s   

ÿýøz#SoftFileLock._finish_marker_cleanupc              	   C  sf   zt ttjttt| ƒ dg ¡jd ƒdƒƒƒ W d S  t	y"   Y d S  t
y2   | jdd�s/‚ Y d S w )Nr'   r
   Fr{   )ÚlistÚmapr.   rM   r   ÚiterÚvarsÚ
setdefaultÚpopÚ
IndexErrorr6   Ú_suppresses_close_errorrt   rB   rB   rC   rƒ     s   0ÿÿzSoftFileLock._close_marker_fdc                 C  s„   |d u rd S t jdkr|  |¡ d S ttƒ�" tt | j¡ƒ|kr0t	| jƒ 
¡  W d   ƒ d S W d   ƒ d S 1 s;w   Y  d S rQ   )r8   r9   r‰   r   r6   rG   r.   r…   r-   r   rs   r†   rB   rB   rC   rN     s   

þ"ÿz SoftFileLock._unlink_held_markerútuple[int, int]c                 C  s°   | j }d}ttƒD ]L}ztt |¡ƒ|krd | _t |¡ W n1 tyO } z%|j	t
thv rE|| _|td k r?t |¡ |d9 }W Y d }~q	W Y d }~nd }~ww d | _ d S d S )Ngü©ñÒMbP?r
   ri   )r-   Úranger   rG   r.   r…   r&   rs   r6   r7   r   r   r`   Úsleep)r=   rP   ÚpathZretry_delayÚattemptÚexcrB   rB   rC   r‰     s*   
€
ú€ÿîz$SoftFileLock._windows_unlink_if_ours)r(   r)   )r@   rE   r(   r)   )r\   r_   r]   rE   r(   r_   )r(   rd   )r[   rd   r(   rh   )r(   rp   )r(   r   )rv   r   r(   r)   )r@   rE   rP   r%   r(   r)   )rP   r%   r(   r   )rP   r%   r(   r)   )rP   r“   r(   r)   )"Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__annotations__r!   r"   r#   r$   r&   rD   r<   rI   r:   rZ   rg   Úclassmethodre   ÚstaticmethodrJ   Úpropertyrq   rr   ru   rx   r�   r~   r   r,   rƒ   rN   r‰   Ú__classcell__rB   rB   ry   rC   r      s@   
 





		




	


r   Ústúos.stat_resultr(   r“   c                 C  s   | j | jfS rF   )Úst_devÚst_ino)r¢   rB   rB   rC   rG   2  s   rG   r–   Ústrútuple[str | None, float, int]c              	   C  sü   t  | ¡}t |j¡sd |j|jfS t  | t jt	t ddƒB t	t ddƒB ¡}z't  
|¡}t |j¡s>d |j|jfW t  |¡ S t  |td ¡}W t  |¡ nt  |¡ w t|ƒtkrwttƒ� | d¡|j|jfW  d   ƒ S 1 srw   Y  d |j|jfS )Nr*   r   Ú
O_NONBLOCKr
   zutf-8)r.   r…   ÚstatÚS_ISREGÚst_modeÚst_mtimer¥   r4   ÚO_RDONLYr3   rH   rM   Úreadr   Úlenr   ÚUnicodeDecodeErrorÚdecode)r–   r¢   r@   ÚdatarB   rB   rC   rX   8  s    
&
þ
 ÿrX   r[   rd   ú"tuple[int, str, int | None] | Nonec                 C  s–   | r|   d¡rt|  ¡  ¡  }ƒdvrd S zt|d ƒ}t|ƒtkr(t|d ƒnd }W n
 ty5   Y d S w d|  krAdksDd S  d S ||d |fS )Nrk   >   ri   r   r   ri   r
   iÿÿÿ)Úendswithr¯   ÚstripÚ
splitlinesÚparse_decimalr   rW   )r[   Úlinesrq   Ústart_tokenrB   rB   rC   rY   Q  s   & ÿÿrY   ÚtextrE   c                 C  s*   |   ¡ r|  ¡ sd| ›�}t|ƒ‚t| ƒS )zú
    Read a PID or start token as a writer formats it: ASCII digits only.

    :raises ValueError: for anything else ``int()`` would accept, such as ``1_000``, surrounding whitespace, a sign or
        non-ASCII digits; no filelock writes those

    znot a decimal integer: )ÚisasciiÚisdigitrW   rE   )rº   ÚmsgrB   rB   rC   r·   h  s   
r·   )r   r   r·   )r¢   r£   r(   r“   )r–   r¦   r(   r§   )r[   rd   r(   r³   )rº   r¦   r(   rE   ),Ú
__future__r   r.   r©   r8   r`   Ú
contextlibr   r7   r   r   r   Ú	itertoolsr   Úpathlibr   Útypingr	   Ú_apir   r   Ú	_identityr   r   r   r   Z_soft_protocolr   Ú_utilr   r   r   r   r   r�   r   r   r   r   rG   rX   rY   r·   Ú__all__rB   rB   rB   rC   Ú<module>   s4      



