
    j                        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mZ d dlmZ ddlmZ ddlmZmZ d	Zd
ZdZdZ G d de          ZdgZdS )    )annotationsN)suppress)EACCESEEXISTEPERMESRCH)Path   )BaseFileLock)ensure_directory_existsraise_on_not_writable_filei   W   i   g       @c                      e Zd ZdZddZddZddZedd
            Ze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S )SoftFileLocka  
    Portable file lock based on file existence.

    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. This makes it work on any filesystem but leaves stale lock files behind
    if the process crashes without releasing the lock.

    To mitigate stale locks, the lock file contains the PID and hostname of the holding process. On contention, if the
    holder is on the same host and its PID no longer exists, the stale lock is broken automatically.

    returnNonec                N   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        _        d S # t          $ rP}|j        t           k    s!|j        t"          k    rt$          j        dk    s |                                  Y d }~d S 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_write_lock_info_contextlock_file_fdOSErrorerrnor   r   sysplatform_try_break_stale_lock)selfflags
o_nofollowfile_handler	exceptions        X/lsinfo/ai/hellotax_ai/base_platform/venv/lib/python3.11/site-packages/filelock/_soft.py_acquirezSoftFileLock._acquire"   s'   "4>222///Kji j 	 ""lD999JFZE
	674>5$//:K:KLLL !!,///)5DM&&&  	) 	) 	)6))io.G.GCL\cLcLc&&(((((((((	)s   :-C
 

D$ADD$c                   t          t          t                    5  t          | j                  }|                                }|                    d          }|                                                                }t          |          dvrFt          j
                    |j        z
  t          k    r|                                  	 d d d            d S |d         |d         }}t          |          dk    r|d         nd }|t          j                    k    r	 d d d            d S t!          |          }|                     |          r[t$          j        dk    r=|;t!          |          }	|                     |          }
|
|
|	k    rn	 d d d            d S 	 d d d            d S |                                  d d d            d S # 1 swxY w Y   d S )	Nutf-8encoding>         r   r
   r3   r2   r   )r   r"   
ValueErrorr	   r   stat	read_textstrip
splitlineslentimest_mtime_MALFORMED_LOCK_AGE_THRESHOLD_evict_lock_filesocketgethostnameint_is_process_aliver$   r%   _get_process_creation_time)r'   	lock_pathstat_resultcontentlinespid_strhostnamecreation_time_strpidstoredactuals              r,   r&   z"SoftFileLock._try_break_stale_lock9   so   gz** 	$ 	$T^,,I#..**K))7);;GMMOO..00E5zz''9;;!559VVV))+++	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ !&a%(XG,/JJ!OOa6-////	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$" g,,C%%c** 	<7**/@/L !233F!<<SAAF)f.>.>5	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$8 9	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$< !!###=	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$s,   B/G	AG	)A$G	G	(G		GGc                    | j          dt          j                     }t          | j                                       |           t          |                                           d S )Nz.break.)r   r   getpidr	   renameunlink)r'   
break_paths     r,   r=   zSoftFileLock._evict_lock_fileZ   s[    <<ry{{<<
T^##J///Z!!!!!    rJ   r@   boolc                   t           j        dk    rbdd l}|j        j        }|                    t          d|           }|r|                    |           dS |                                t          k    S 	 t          j        | d           n?# t          $ r2}|j        t          k    rY d }~dS |j        t          k    rY d }~dS  d }~ww xY wdS )Nr   r   TF)r$   r%   ctypeswindllkernel32OpenProcess_WIN_SYNCHRONIZECloseHandleGetLastError_WIN_ERROR_INVALID_PARAMETERr   killr"   r#   r   r   )rJ   rU   rW   handleexcs        r,   rA   zSoftFileLock._is_process_alive_   s    <7""MMM}-H))*:AsCCF $$V,,,t((**.JJJ	GCOOOO 	 	 	yE!!uuuuuyE!!ttttt	 ts$   4B
 

CC*C CC
int | Nonec           	        t           j        dk    rdS ddl}ddlm} |j        j        }|                    t          d|           }|sdS 	 |                                }|                                }|                                }|                                }|	                    ||
                    |          |
                    |          |
                    |          |
                    |                    s	 |                    |           dS 	 |                    |           n# |                    |           w xY w|j        dz  |j        z  S )zRReturn the process creation FILETIME as an integer on Windows, ``None`` otherwise.r   Nr   )wintypes    )r$   r%   rU   rb   rV   rW   rX   &_WIN_PROCESS_QUERY_LIMITED_INFORMATIONFILETIMEGetProcessTimesbyrefrZ   dwHighDateTimedwLowDateTime)	rJ   rU   rb   rW   r^   creation	exit_timekernel_time	user_times	            r,   rB   z'SoftFileLock._get_process_creation_timet   sn    <7""4######=)%%&LaQTUU 	4	)((**H ))++I"++--K ))++I++X&&Y''[))Y''     (((((   ((((H  (((('2-1GGGs   
B5D. .Efdc                   t          t                    5  t          j                     dt	          j                     d}t          j        dk    r5t          	                    t          j                              x}|| dz  }t          j
        | |                                           d d d            d S # 1 swxY w Y   d S )N
r   )r   r"   r   rN   r>   r?   r$   r%   r   rB   writeencode)rn   infocts      r,   r   zSoftFileLock._write_lock_info   s    g 	( 	(ikk==V%7%9%9===D|w&&,2Y2YZ\ZcZeZe2f2f,fB+s2			!HR'''		( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	( 	(s   BB99B= B=c                   	 t          | j                                      d          }|                                                                }|rt          |d                   S n# t          t          f$ r Y nw xY w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/   r0   r   N)r	   r   r6   r7   r8   r@   r"   r4   )r'   rE   rF   s      r,   rJ   zSoftFileLock.pid   s    	4>**44g4FFGMMOO..00E %58}}$%$ 	 	 	D	ts   A$A( (A<;A<c                :    | j         t          j                    k    S )z
        Whether this lock is held by the current process.

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

        )rJ   r   rN   r'   s    r,   is_lock_held_by_uszSoftFileLock.is_lock_held_by_us   s     x29;;&&rR   c                    t          t                    5  t          | j                                                   ddd           dS # 1 swxY w Y   dS )zNForcibly break the lock by removing the lock file, regardless of who holds it.N)r   r"   r	   r   rP   rw   s    r,   
break_lockzSoftFileLock.break_lock   s    g 	* 	*  '')))	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	*s   'A		AAc                h   | j         j        J t          j        | j         j                   d | j         _        t          j        dk    r|                                  d S t          t                    5  t          | j
                                                   d d d            d S # 1 swxY w Y   d S )Nr   )r    r!   r   closer$   r%   _windows_unlink_with_retryr   r"   r	   r   rP   rw   s    r,   _releasezSoftFileLock._release   s    })555
+,,,%)"<7""++-----'"" . .T^$$++---. . . . . . . . . . . . . . . . . .s   3'B''B+.B+c                .   d}d}t          |          D ]}	 t          | j                                                    d S # t          $ rH}|j        t          t          hvrY d }~ d S ||dz
  k     rt          j	        |           |dz  }Y d }~yd }~ww xY wd S )N
   gMbP?r
   r2   )
ranger	   r   rP   r"   r#   r   r   r:   sleep)r'   max_retriesretry_delayattemptr_   s        r,   r}   z'SoftFileLock._windows_unlink_with_retry   s    [)) 	 	G	T^$$++---   % % %9VUO33FFFFFF[1_,,J{+++1$K%		 	s   &A  
B
B&"BBN)r   r   )rJ   r@   r   rS   )rJ   r@   r   r`   )rn   r@   r   r   )r   r`   )r   rS   )__name__
__module____qualname____doc__r-   r&   r=   staticmethodrA   rB   r   propertyrJ   rx   rz   r~   r}    rR   r,   r   r      s;        6 6 6 6.$ $ $ $B" " " "
    \( H H H \H8 ( ( ( \(    X  ' ' ' X'* * * *
. . . .     rR   r   )
__future__r   r   r>   r$   r:   
contextlibr   r#   r   r   r   r   pathlibr	   _apir   _utilr   r   rY   r\   rd   r<   r   __all__r   rR   r,   <module>r      s   " " " " " " 				  



        . . . . . . . . . . . .             F F F F F F F F ! )/ & # } } } } }< } } }B rR   