o
    /iE                     @  s   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G d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   c                   @  sT   e Zd ZdZdddZdddZedddZedddZdddZ	d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              
   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yo } z3|jtksH|jtkrGtjdksH |jtkr]tjdkrd|   W Y d }~d S W Y d }~d S W Y d }~d S d }~ww | | || j_d S )N
O_NOFOLLOWwin32)r   	lock_filer   osO_WRONLYO_CREATO_EXCLO_TRUNCgetattropenZ
_open_modeOSErrorerrnor   r   sysplatform_try_break_stale_lock_write_lock_info_contextlock_file_fd)selfflagsZ
o_nofollowfile_handler	exception r'   H/home/kim/smarthome/.venv/lib/python3.10/site-packages/filelock/_soft.py_acquire    s0   


zSoftFileLock._acquirec                 C  s   t ttd t| jjdd}|  }t|dkr$	 W d    d S |\}}|t	
 kr7	 W d    d S t|}| |rI	 W d    d S | j dt  }t| j| t|  W d    d S 1 slw   Y  d S )Nzutf-8)encoding   z.break.)r   r   
ValueErrorr   r   	read_textstrip
splitlineslensocketgethostnameint_is_process_aliver   getpidrenameunlink)r#   contentlinesZpid_strhostnamepidZ
break_pathr'   r'   r(   r   8   s$   
"z"SoftFileLock._try_break_stale_lockr;   r3   boolc              
   C  s   t jdkr#dd l}|jj}|td| }|r|| dS | t	kS z	t
| d W dS  tyQ } z|jtkr@W Y d }~dS |jtkrLW Y d }~dS  d }~ww )Nr   r   TF)r   r   ctypesZwindllkernel32ZOpenProcess_WIN_SYNCHRONIZEZCloseHandleZGetLastError_WIN_ERROR_INVALID_PARAMETERr   killr   r   r   r   )r;   r=   r>   handleexcr'   r'   r(   r4   H   s&   



zSoftFileLock._is_process_alivefdc                 C  sR   t t t| t  dt  d  W d    d S 1 s"w   Y  d S )N
)r   r   r   writer5   r1   r2   encode)rD   r'   r'   r(   r    ]   s   
&"zSoftFileLock._write_lock_infoc                 C  sx   | j jd usJ t| j j d | j _tjdkr|   d S tt t	| j
  W d    d S 1 s5w   Y  d S )Nr   )r!   r"   r   closer   r   _windows_unlink_with_retryr   r   r   r   r7   )r#   r'   r'   r(   _releaseb   s   

"zSoftFileLock._releasec                 C  s   d}d}t |D ]=}zt| j  W  d S  tyE } z$|jtthvr,W Y d }~ d S ||d k r;t	| |d9 }W Y d }~qd }~ww d S )N
   gMbP?r	   r+   )
ranger   r   r7   r   r   r   r   timesleep)r#   max_retriesZretry_delayattemptrC   r'   r'   r(   rI   l   s   
z'SoftFileLock._windows_unlink_with_retryN)r   r   )r;   r3   r   r<   )rD   r3   r   r   )__name__
__module____qualname____doc__r)   r   staticmethodr4   r    rJ   rI   r'   r'   r'   r(   r      s    



r   )
__future__r   r   r1   r   rM   
contextlibr   r   r   r   r   r   pathlibr   Z_apir
   Z_utilr   r   r?   r@   r   __all__r'   r'   r'   r(   <module>   s    l