
    Ki9                        S SK Jr  S SKrS SKrS SKrS SKrS SKJr  S SKJ	r	J
r
JrJr  S SKJr  SSKJr  SSKJrJr  S	rS
r " S S\5      rS/rg)    )annotationsN)suppress)EACCESEEXISTEPERMESRCH)Path   )BaseFileLock)ensure_directory_existsraise_on_not_writable_filei   W   c                  h    \ rS rSrSrSS jrSS jr\SS j5       r\SS j5       r	SS jr
SS jrS	rg
)SoftFileLock   a  
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.

c                   [        U R                  5        [        U R                  5        [        R                  [        R
                  -  [        R                  -  [        R                  -  n[        [        SS 5      =nb  X-  n [        R                  " U R                  XR                  5       5      nU R                  U5        X0R                  l        g ! [         a  nUR                  [         :X  d)  UR                  ["        :X  a  [$        R&                  S:X  d  e UR                  [         :X  a.  [$        R&                  S:w  a  U R)                  5          S nAg  S nAg  S nAg S nAff = f)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        P/var/www/html/dynamic-report/venv/lib/python3.13/site-packages/filelock/_soft.py_acquireSoftFileLock._acquire    s   "4>>2/KKjjii jj 	 ""lD99JFE	6774>>5//:KLL !!,/)5MM&  	-6)ioo.GCLL\cLc&(S\\W-D**,, .E(	-s   /C 
E+"A5E&&E+c                n   [        [        5         [        U R                  5      R	                  SS9nUR                  5       R                  5       n[        U5      S:w  a
   S S S 5        g Uu  p4U[        R                  " 5       :w  a
   S S S 5        g [        U5      nU R                  U5      (       a
   S S S 5        g U R                   S[        R                  " 5        3n[        U R                  5      R                  U5        [        U5      R                  5         S S S 5        g ! , (       d  f       g = f)Nzutf-8)encoding   z.break.)r   r!   r	   r   	read_textstrip
splitlineslensocketgethostnameint_is_process_aliver   getpidrenameunlink)r&   contentlinespid_strhostnamepid
break_paths          r+   r%   "SoftFileLock._try_break_stale_lock8   s    g4>>*44g4FGMMO..0E5zQ	 
 !&G6--//  g,C%%c**  !NN+7299;-@J ''
3##% s   AD&*D&"D&<A!D&&
D4c                   [         R                  S:X  aa  SS KnUR                  R                  nUR                  [        SU 5      nU(       a  UR                  U5        gUR                  5       [        :g  $  [        R                  " U S5        g! [         a8  nUR                  [        :X  a   S nAgUR                  [        :X  a   S nAge S nAff = f)Nr   r   TF)r#   r$   ctypeswindllkernel32OpenProcess_WIN_SYNCHRONIZECloseHandleGetLastError_WIN_ERROR_INVALID_PARAMETERr   killr!   r"   r   r   )r@   rD   rF   handleexcs        r+   r8   SoftFileLock._is_process_aliveH   s    <<7"}}--H))*:AsCF$$V,((*.JJJ	GGCO   	yyE!yyE!	s$   7B 
CC2CCCc                    [        [        5         [        R                  " U [        R                  " 5        S[
        R                  " 5        S3R                  5       5        S S S 5        g ! , (       d  f       g = f)N
)r   r!   r   writer9   r5   r6   encode)fds    r+   r   SoftFileLock._write_lock_info]   sJ    gHHRBIIK=6+=+=+?*@CJJLM s   AA++
A9c                   U R                   R                  c   e[        R                  " U R                   R                  5        S U R                   l        [        R
                  S:X  a  U R                  5         g [        [        5         [        U R                  5      R                  5         S S S 5        g ! , (       d  f       g = f)Nr   )r   r    r   closer#   r$   _windows_unlink_with_retryr   r!   r	   r   r;   )r&   s    r+   _releaseSoftFileLock._releaseb   s{    }}))555
++,%)"<<7"++-'"T^^$++- #""s   	$B66
Cc                :   SnSn[        U5       H'  n [        U R                  5      R                  5           g    g ! [         aT  nUR
                  [        [        1;  a   S nA  g X1S-
  :  a!  [        R                  " U5        US-  n S nAM   S nAM  S nAff = f)N
   gMbP?r
   r0   )
ranger	   r   r;   r!   r"   r   r   timesleep)r&   max_retriesretry_delayattemptrN   s        r+   rX   'SoftFileLock._windows_unlink_with_retryl   s    [)G	T^^$++-  *  %99VUO31_,JJ{+1$K -%s   #<
BB&#BB N)returnNone)r@   r7   re   bool)rT   r7   re   rf   )__name__
__module____qualname____firstlineno____doc__r,   r%   staticmethodr8   r   rY   rX   __static_attributes__rd       r+   r   r      sE    60&   ( N N.ro   r   )
__future__r   r   r5   r#   r^   
contextlibr   r"   r   r   r   r   pathlibr	   _apir   _utilr   r   rH   rK   r   __all__rd   ro   r+   <module>rv      sM    " 	  
   . .   F ! h< hX ro   