
    AHj                         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j                  e      Z	ej                  j                  Z G d de      Zy)z[Provides a mechanism for not competing with other processes interacting with an MSAL cache.    Nc                   (    e Zd ZdZd Zd Zd Zd Zy)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                     || _         t        j                  |dt        j                  t        j                  z  d      | _        y )Nzwb+r   )modeflags	buffering)	_lockpathportalockerLockLOCK_EXLOCK_NB_lock)selflockfile_paths     R/root/tools/cai/cai_env/lib/python3.12/site-packages/msal_extensions/cache_lock.py__init__zCrossPlatLock.__init__   s<    & %%
 %%(;(;; 

    c                    d}d}t        t        dt        j                        } |       |z   }t        j                         }| |       kD  r"	 t	        | j
                  d      5  	 d d d        yy# 1 sw Y   nxY wnZ# t        $ r t        j                  d       Y yt        $ r/ t        j                  d||       t        j                  |       Y nw xY w| |       kD  rt)	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   timeoutcheck_intervalcurrent_timetimeout_endpids         r   _try_to_create_lock_filez&CrossPlatLock._try_to_create_lock_file%   s    t[$))<"nw.iikLN*
+$..#.            RS" +U) 

>*	+ LN*s0   A< #A/%A< /A84A< <C4CCc                 0   t        j                         }| j                         st        j	                  d|       | j
                  j                         }|j                  dj                  |t        j                  d         j                  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'   file_handles      r   r*   zCrossPlatLock.__enter__9   sl    iik,,.NNBCHjj**,'..chhqk:AA'JKr   c                      | j                   j                  |  	 t        j                  | j                         y # t
        $ r7}|j                  t        j                  t        j                  fvr Y d }~y d }~ww xY w)N)	r   __exit__r   remover	   OSErrorerrnoENOENTEACCES)r   argsexs      r   r2   zCrossPlatLock.__exit__A   s^    

T"	 IIdnn% 	xxell;; <	s   ; 	A;-A66A;N)__name__
__module____qualname____doc__r   r(   r*   r2    r   r   r   r      s    
(	r   r   )r=   r   r-   r5   r   loggingr
   	getLoggerr:   r   
exceptionsLockException	LockErrorobjectr   r>   r   r   <module>rE      sM    a 	 
     
		8	$ ""00	9F 9r   