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ddlZeeZ	ej
jZG dd deZdS )z[Provides a mechanism for not competing with other processes interacting with an MSAL cache.    Nc                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )CrossPlatLockzOffers a mechanism for waiting until another process is finished interacting with a shared
    resource. This is specifically written to interact with a class of the same name in the .NET
    extensions library.
    c                 C   s&   || _ tj|dtjtjB dd| _d S )Nzwb+r   )modeflags	buffering)	_lockpathportalockerLockLOCK_EXLOCK_NB_lock)selfZlockfile_path r   Q/home/app/Keep/.python/lib/python3.10/site-packages/msal_extensions/cache_lock.py__init__   s   
zCrossPlatLock.__init__c                 C   s   d}d}t tdtj}| | }t }|| kr]zt| jd 	 W d    W dS 1 s.w   Y  W n# tyC   td Y dS  t	yW   t
d|| t| Y nw || ksdS )	N   g      ?	monotonicxTz1Python 2 does not support atomic creation of fileFz?Process %d found existing lock file, will retry after %f second)getattrtimeosgetpidopenr   
ValueErrorloggerwarningFileExistsErrordebugsleep)r   timeoutZcheck_intervalcurrent_timeZtimeout_endpidr   r   r   _try_to_create_lock_file%   s,   

&

z&CrossPlatLock._try_to_create_lock_filec                 C   sH   t  }|  std| | j }|d|t	j
d d |S )Nz%Process %d failed to create lock filez{} {}r   zutf-8)r   r   r!   r   r   r   	__enter__writeformatsysargvencode)r   r    Zfile_handler   r   r   r"   9   s   
zCrossPlatLock.__enter__c              
   G   sZ   | j j|  z	t| j W d S  ty, } z|jtjtjfvr! W Y d }~d S d }~ww )N)	r   __exit__r   remover   OSErrorerrnoENOENTEACCES)r   argsexr   r   r   r(   A   s   zCrossPlatLock.__exit__N)__name__
__module____qualname____doc__r   r!   r"   r(   r   r   r   r   r      s    r   )r3   r   r%   r+   r   loggingr   	getLoggerr0   r   
exceptionsZLockExceptionZ	LockErrorobjectr   r   r   r   r   <module>   s    
