U g@sdZddlZddlZddlZddlZddlmZddlZ ddddgZ e Z da ddZGd ddeZGd ddeZGd ddeZdS) ag!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 ... @endcodeNLockingDisableddisable_lockingLockFileLockHeldTc CsFdatD]8}z |Wqttfk r>}zW5d}~XYqXqdS)ac!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.FN) locks_okaylocks release_impl Exceptionr)locklr =/lfs/h1/ops/prod/packages/hafs.v2.0.7/ush/produtil/locking.pyr&s  c@seZdZdZdS)rzy!This exception is raised when a LockFile cannot lock a file because another process or thread has locked it already.N__name__ __module__ __qualname____doc__r r r r r9sc@seZdZdZdS)rz!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.Nrr r r r r=sc@sZeZdZdZddZddZdd d Zd dZddZddZ ddZ ddZ ddZ dS)rz!Automates locking of a lockfile @code with LockFile("/path/to/lock.file"): ... do things while file is locked ... ...file is no longer locked. @endcodecCs tt|S)z!Return a hash of this object.)hashidselfr r r __hash__LszLockFile.__hash__cCs||kS)z$!Is this lock the same as that lock?r )rotherr r r __eq__OszLockFile.__eq__N rFcCsVts td||_|dk stt|dks.t||_||_||_||_||_ d|_ dS)a5!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 lockzDAttempted to create a LockFile object while the process was exiting.Nr) rr_loggerAssertionErrorlen _filename _max_tries _sleep_time _first_warn _giveup_quiet_fd)rfilenameZuntilloggerZ max_triesZ sleep_time first_warn giveup_quietr r r __init__Rs  zLockFile.__init__c Csts tdtj|j}|r*tj||j dkrBt |jd|_ zt |j t jt jBWnRtk r}z4|jtjks|jtjkrtd|jt|fW5d}~XYnXdS)z!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.z:Attempted to acquire a lock while the process was exiting.Nwbz3%s: already locked by another process or thread: %s)rrospathdirnamerprodutilZfileopmakedirsr$openfcntllockffilenoZLOCK_EXZLOCK_NBEnvironmentErrorerrnoZEAGAINZ EWOULDBLOCKrstr)rZthedirer r r acquire_implis   zLockFile.acquire_implcCs2|jdk r.t|jtj|jd|_dS)z!Internal implementation function; do not call directly. Does the actual work of releasing the lock, without retries, logging or sleeping.N)r$r1r2r3ZLOCK_UNcloserr r r r}s  zLockFile.release_implc Cs4t|tj|j|j|j|jd|j|j |j dS)zY!Acquire the lock. Will try for a while, and will raise LockHeld when giving up.z : cannot lockZfailr&r'r() raddretryretry_ior r!r8rrr"r#rr r r acquires zLockFile.acquirec CsJz,tj|j|j|j|jd|j|j|jdWSYn Xt |dS)zH!Release the lock. May raise exceptions on unexpected failures.z: cannot unlockr:N) r<r=r r!rrrr"r#rremoverr r r releaseszLockFile.releasecCs ||S)z*!Calls self.acquire() to acquire the lock.)r>rr r r __enter__szLockFile.__enter__cCs |dS)zh!Calls self.release() to release the lock. @param etype,evalue,etraceback Exception information.N)r@)retypeZevalueZ etracebackr r r __exit__szLockFile.__exit__)NNrrrF) rrrrrrr)r8rr>r@rArCr r r r rDs  )rr1timer5Zos.pathr+Zprodutil.retryr<produtil.fileopr.__all__setrrrr r BaseExceptionrobjectrr r r r s