o
    i                     @   sZ   d Z ddlZddlZddlZddlZddlZeeZG dd de	Z
G dd deZdS )zEA cross-process lock based on exclusive creation of a given file name    Nc                   @   s   e Zd ZdZdS )	LockErrorz.It will be raised when unable to obtain a lockN)__name__
__module____qualname____doc__ r   r   O/home/app/Keep/.python/lib/python3.10/site-packages/msal_extensions/filelock.pyr      s    r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )CrossPlatLockz5This implementation relies only on ``open(..., 'x')``c                 C   s
   || _ d S )N)	_lockpath)selfZlockfile_pathr   r   r   __init__   s   
zCrossPlatLock.__init__c                 C   s&   |  dt tjd d | S )Nz{} {}r   zutf-8)_create_lock_fileformatosgetpidsysargvencode)r   r   r   r   	__enter__   s   zCrossPlatLock.__enter__c                 C   s   d}d}t tdtj}| | }|| kr\z t| jd}|| W d    W d S 1 s.w   Y  W d S  ty@   td tyV   t	dt
 | t| Y nw || kstd|| j)N   g      ?	monotonicxbz1Python 2 does not support atomic creation of filez?Process %d found existing lock file, will retry after %f secondznUnable to obtain lock, despite trying for {} second(s). You may want to manually remove the stale lock file {})getattrtimeopenr
   write
ValueErrorr   FileExistsErrorloggerdebugr   r   sleepr   )r   contenttimeoutZcheck_intervalcurrent_timeZtimeout_endZ	lock_filer   r   r   r      s:   



zCrossPlatLock._create_lock_filec              
   G   sZ   z	t | j W d S  ty, } z|jtjtjfv r td n W Y d }~d S d }~ww )NzUnable to remove lock file)	r   remover
   OSErrorerrnoENOENTEACCESr   r   )r   argsexr   r   r   __exit__4   s   zCrossPlatLock.__exit__N)r   r   r   r   r   r   r   r+   r   r   r   r   r	      s    r	   )r   r   r   r&   r   logging	getLoggerr   r   RuntimeErrorr   objectr	   r   r   r   r   <module>   s    
