ó ‹÷Îgc@sÁdZddlZddlZddlZddlZddljZddlZ ddddgZ e ƒZ e ad„Zdefd„ƒYZdefd „ƒYZdefd „ƒYZdS( sg!Handles file locking using Python "with" blocks. This module implements a Python with construct that can hold a lock and release it at the end of the "with" block. It also implements a safety feature to allow the program to disable locking, ensuring a fatal exception (LockingDisabled) if anything tries to lock a file. That functionality is connected to the produtil.sigsafety module, which will disable locking if a fatal signal is received. @code import produtil.locking with produtil.locking.LockFile("some.lockfile"): ... do things while the file is locked... ... the file is now unlocked ... @endcodeiÿÿÿÿNtLockingDisabledtdisable_lockingtLockFiletLockHeldcCsEtax8tD]0}y|jƒWq ttfk r<}q Xq WdS(sc!Entirely disables all locking in this module. If this is called, any locking attempts will raise LockingDisabled. That exception derives directly from BaseException, which well-written Python code will never catch, hence ensuring a rapid, abnormal exit of the program. This routine should never be called directly: it is only used as part of the implementation of the produtil.sigsafety, to prevent file locking after catching a terminal signal, hence allowing the program to exit as quickly as possible, and present a stack trace to any locations that attempt locking.N(tFalset locks_okaytlockst release_implt ExceptionR(tlocktl((s=/lfs/h1/ops/prod/packages/hafs.v2.0.7/ush/produtil/locking.pyR&s  cBseZdZRS(sy!This exception is raised when a LockFile cannot lock a file because another process or thread has locked it already.(t__name__t __module__t__doc__(((s=/lfs/h1/ops/prod/packages/hafs.v2.0.7/ush/produtil/locking.pyR9scBseZdZRS(sð!This exception is raised when a thread attempts to acquire a lock while Python is exiting according to produtil.sigsafety. @warning This is a subclass of BaseException, not Exception, to attempt to cleanly kill the thread.(R R R (((s=/lfs/h1/ops/prod/packages/hafs.v2.0.7/ush/produtil/locking.pyR=scBsqeZdZd„Zd„Zd d ddded„Zd„Zd„Z d „Z d „Z d „Z d „Z RS(s·!Automates locking of a lockfile @code with LockFile("/path/to/lock.file"): ... do things while file is locked ... ...file is no longer locked. @endcodecCstt|ƒƒS(s!Return a hash of this object.(thashtid(tself((s=/lfs/h1/ops/prod/packages/hafs.v2.0.7/ush/produtil/locking.pyt__hash__LscCs ||kS(s$!Is this lock the same as that lock?((Rtother((s=/lfs/h1/ops/prod/packages/hafs.v2.0.7/ush/produtil/locking.pyt__eq__Osi iicCs‚tstdƒ‚n||_|dk s0t‚t|ƒdksHt‚||_||_||_||_ ||_ d|_ dS(s5!Creates an object that will lock the specified file. @param filename the file to lock @param until Unused. @param logger Optional: a logging.Logger to log messages @param max_tries Optional: maximum tries before giving up on locking @param sleep_time Optional: approximate sleep time between locking attempts. @param first_warn Optional: first locking failure at which to write warnings to the logger @param giveup_quiet Optional: if True, do not log the final failure to locksDAttempted to create a LockFile object while the process was exiting.iN( RRt_loggertNonetAssertionErrortlent _filenamet _max_triest _sleep_timet _first_warnt _giveup_quiett_fd(Rtfilenametuntiltloggert max_triest sleep_timet first_warnt giveup_quiet((s=/lfs/h1/ops/prod/packages/hafs.v2.0.7/ush/produtil/locking.pyt__init__Rs       cCsôtstdƒ‚ntjj|jƒ}|rCtjj|ƒn|j dkrjt |jdƒ|_ ny't j |j jƒt jt jBƒWn\tk rï}|jtjksÇ|jtjkrétd|jt|ƒfƒ‚n‚nXdS(sÖ!Internal implementation function; do not call directly. Does the actual work of acquiring the lock, without retries, logging or sleeping. Will raise LockHeld if it cannot acquire the lock.s:Attempted to acquire a lock while the process was exiting.twbs3%s: already locked by another process or thread: %sN(RRtostpathtdirnameRtprodutiltfileoptmakedirsRRtopentfcntltlockftfilenotLOCK_EXtLOCK_NBtEnvironmentErrorterrnotEAGAINt EWOULDBLOCKRtstr(Rtthedirte((s=/lfs/h1/ops/prod/packages/hafs.v2.0.7/ush/produtil/locking.pyt acquire_implis'$cCsH|jdk rDtj|jjƒtjƒ|jjƒd|_ndS(sš!Internal implementation function; do not call directly. Does the actual work of releasing the lock, without retries, logging or sleeping.N(RRR.R/R0tLOCK_UNtclose(R((s=/lfs/h1/ops/prod/packages/hafs.v2.0.7/ush/produtil/locking.pyR}s c CsQtj|ƒtj|j|j|jd|jdd|jd|j d|j ƒS(sY!Acquire the lock. Will try for a while, and will raise LockHeld when giving up.tfails : cannot lockR R#R$( Rtaddtretrytretry_ioRRR:RRRR(R((s=/lfs/h1/ops/prod/packages/hafs.v2.0.7/ush/produtil/locking.pytacquire…s c CsfyHtj|j|j|jd|jdd|jd|jd|jƒSWn ‚nXt j |ƒdS(sH!Release the lock. May raise exceptions on unexpected failures.R=s: cannot unlockR R#R$N( R?R@RRRRRRRRtremove(R((s=/lfs/h1/ops/prod/packages/hafs.v2.0.7/ush/produtil/locking.pytreleaseŒscCs|jƒ|S(s*!Calls self.acquire() to acquire the lock.(RA(R((s=/lfs/h1/ops/prod/packages/hafs.v2.0.7/ush/produtil/locking.pyt __enter__—s cCs|jƒdS(sh!Calls self.release() to release the lock. @param etype,evalue,etraceback Exception information.N(RC(Rtetypetevaluet etraceback((s=/lfs/h1/ops/prod/packages/hafs.v2.0.7/ush/produtil/locking.pyt__exit__›sN(R R R RRRRR%R:RRARCRDRH(((s=/lfs/h1/ops/prod/packages/hafs.v2.0.7/ush/produtil/locking.pyRDs      (R R.ttimeR4tos.pathR'tprodutil.retryR?tprodutil.fileopR*t__all__tsetRtTrueRRRRt BaseExceptionRtobjectR(((s=/lfs/h1/ops/prod/packages/hafs.v2.0.7/ush/produtil/locking.pyts0