o VEV!@sddlZddlZddlZddlZddlZddlZddlmZee Z ddZ Gddde Z Gddde ZGd d d e Zejd krKddlZeZnddlZeZd d ZdS)N)_utilsc Csnzt|WdSty6}z"|jtjkr$tj|sWYd}~dS|jtjkr1WYd}~dSd}~ww)zfCreate a directory (and any ancestor directories required). :param path: Directory to create NFT)osmakedirsOSErrorerrnoEEXISTpathisdirEISDIR)rer 8/usr/lib/python3/dist-packages/fasteners/process_lock.py _ensure_trees     rc@seZdZdZdZ dZ ejdfddZddZ d d Z d eedfd d Z ddZ ddZ ddZddZddZddZddZdS)_InterProcessLockaTAn interprocess locking implementation. This is a lock implementation which allows multiple locks, working around issues like http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=632857 and does not require any cleanup. Since the lock is always held on a file descriptor rather than outside of the process, the lock gets dropped automatically if the process crashes, even if ``__exit__`` is not executed. There are no guarantees regarding usage by multiple threads in a single process here. This lock works only between processes. Note these locks are released when the descriptor is closed, so it's not safe to close the file descriptor while another thread holds the lock. Just opening and closing the lock file can break synchronization, so lock files must be accessed only using this abstraction. g?g{Gz?NcCs*d|_||_d|_||_t|t|_dS)NF)lockfileracquired sleep_funcrpick_first_not_noneLOGlogger)selfrrrr r r __init__Us z_InterProcessLock.__init__c Csrz|WdSty8}z%|jtjtjfvr)|r|r%WYd}~dStt d|j |dd}~ww)NFz9Unable to acquire lock on `%(path)s` due to %(exception)s)r exceptionT) trylockIOErrorrEACCESEAGAINexpiredr RetryAgain threading ThreadErrorr)rblockingwatchr r r r _try_acquire\s  z_InterProcessLock._try_acquirecCsXtj|j}|rt|}|r|jtjd||jdus!|jj r*t |jd|_dSdS)NzCreated lock base path `%s`a) rrdirnamerrlogrBLATHERrclosedopen)rbasedir made_basedirr r r _do_openps z_InterProcessLock._do_openTcCs|dkrtd|dur|dkrtd||kr|}|tj|d}tj|||j|d}|||j||}Wdn1sBwY|sNd|_dSd|_|j tj d |j | |j dS) aAttempt to acquire the given lock. :param blocking: whether to wait forever to try to acquire the lock :type blocking: bool :param delay: when blocking this is the delay time in seconds that will be added after each failed acquisition :type delay: int/float :param max_delay: the maximum delay to have (this limits the accumulated delay(s) added after each failed acquisition) :type max_delay: int/float :param timeout: an optional timeout (limits how long blocking will occur for) :type timeout: int/float :returns: whether or not the acquisition succeeded :rtype: bool rz+Delay must be greater than or equal to zeroNz-Timeout must be greater than or equal to zero)duration)rr"FTzHAcquired file lock `%s` after waiting %0.3fs [%s attempts were required]) ValueErrorr,r StopWatchRetryrr#rrr&r'relapsedattempts)rr!delay max_delaytimeoutr"rgottenr r r acquire}s0   z_InterProcessLock.acquirecCs"|jdur|jd|_dSdSN)rcloserr r r _do_closes   z_InterProcessLock._do_closecCs ||Sr9)r8r;r r r __enter__sz_InterProcessLock.__enter__cCs|jstdz|Wnty |jd|jYdSwd|_z|Wnty<|jd|jYdSw|j t j d|jdS)z%Release the previously acquired lock.z$Unable to release an unacquired lockz1Could not unlock the acquired lock opened on `%s`Fz.Could not close the file handle opened on `%s`z*Unlocked and closed file lock open on `%s`N) rrr unlockrrrrr<r&rr'r;r r r releases*        z_InterProcessLock.releasecCs |dSr9)r?)rexc_typeexc_valexc_tbr r r __exit__s z_InterProcessLock.__exit__cCstj|jS)zrHz_InterProcessLock.unlock)__name__ __module__ __qualname____doc__ MAX_DELAYDELAY_INCREMENTtimesleeprr#r,r8r<r=r?rCrDrr>r r r r r4s(  + rc@ eZdZdZddZddZdS) _WindowsLockz?Interprocess lock implementation that works on windows systems.cCt|jtjddSN)msvcrtlockingrfilenoLK_NBLCKr;r r r rz_WindowsLock.trylockcCrSrT)rVrWrrXLK_UNLCKr;r r r r>rZz_WindowsLock.unlockNrIrJrKrLrr>r r r r rR rRc@rQ) _FcntlLockz=Interprocess lock implementation that works on posix systems.cCst|jtjtjBdSr9)fcntllockfrLOCK_EXLOCK_NBr;r r r rrZz_FcntlLock.trylockcCst|jtjdSr9)r_r`rLOCK_UNr;r r r r>sz_FcntlLock.unlockNr\r r r r r^r]r^ntcst|fdd}|S)zSAcquires & releases a interprocess lock around call into decorated function.cstfdd}|S)Ncs6|i|WdS1swYdSr9r )argskwargs)flockr r wrappers $z7interprocess_locked..decorator..wrapper)sixwraps)rgrirh)rgr decoratorsz&interprocess_locked..decorator)InterProcessLock)rrmr rlr interprocess_lockeds  ro)rloggingrrrOrj fastenersr getLoggerrIrrobjectrrRr^namerVrnr_ror r r r s&  "