o r^~$@srdZddlmZddlZddlZddlZddlZddlZeeds%ej e_ eej ds1ej j ej _ gdZGdddeZGd d d eZGd d d eZGd ddeZGdddeZGdddeZGdddeZGdddeZGdddeZGdddeZddZddZdd Zd!d"Zd)d#d$Zeed%rd&d'l m!Z"e"j#Z$e$Z(dSd&d(l m%Z&e&j'Z$e$Z(dS)*a lockfile.py - Platform-independent advisory file locks. Requires Python 2.5 unless you apply 2.4.diff Locking is done on a per-thread basis instead of a per-process basis. Usage: >>> lock = LockFile('somefile') >>> try: ... lock.acquire() ... except AlreadyLocked: ... print('somefile', "is locked already.") ... except LockFailed: ... print('somefile', "can't be locked.") ... else: ... print("got lock") got lock >>> lock.is_locked() True >>> lock.release() >>> lock = LockFile('somefile') >>> lock.is_locked() False >>> with lock: ... lock.is_locked() True >>> lock.is_locked() False >>> lock = LockFile('somefile') >>> # It is okay to lock twice from the same thread... >>> with lock: ... lock.acquire() ... >>> # Though no counter is kept, so you can't unlock multiple times... >>> lock.is_locked() False Exceptions: Error - base class for other exceptions LockError - base class for all locking exceptions AlreadyLocked - Another thread or process already holds the lock LockFailed - Lock failed for some other reason UnlockError - base class for all unlocking exceptions AlreadyUnlocked - File was not locked. NotMyLock - File was locked but not by the current thread/process )absolute_importNcurrent_threadget_name) Error LockError LockTimeout AlreadyLocked LockFailed UnlockError NotLocked NotMyLock LinkFileLock MkdirFileLockSQLiteFileLockLockBaselockedc@eZdZdZdS)rzw Base class for other exceptions. >>> try: ... raise Error ... except Exception: ... pass N__name__ __module__ __qualname____doc__rr3/usr/lib/python3/dist-packages/lockfile/__init__.pyrJrc@r)rz Base class for error arising from attempts to acquire the lock. >>> try: ... raise LockError ... except Error: ... pass NrrrrrrVrrc@r)rzRaised when lock creation fails within a user-defined period of time. >>> try: ... raise LockTimeout ... except LockError: ... pass Nrrrrrrbrc@r)rzSome other thread/process is locking the file. >>> try: ... raise AlreadyLocked ... except LockError: ... pass Nrrrrrrmrrc@r)r zLock file creation failed for some other reason. >>> try: ... raise LockFailed ... except LockError: ... pass Nrrrrrr xrr c@r)r z Base class for errors arising from attempts to release the lock. >>> try: ... raise UnlockError ... except Error: ... pass Nrrrrrr rr c@r)r zRaised when an attempt is made to unlock an unlocked file. >>> try: ... raise NotLocked ... except UnlockError: ... pass Nrrrrrr rr c@r)r zRaised when an attempt is made to unlock a file someone else locked. >>> try: ... raise NotMyLock ... except UnlockError: ... pass Nrrrrrr rr c@s>eZdZddZdddZddZdd Zd d Zd d ZdS) _SharedBasecCs ||_dSN)path)selfrrrr__init__s z_SharedBase.__init__NcCtd)a Acquire the lock. * If timeout is omitted (or None), wait forever trying to lock the file. * If timeout > 0, try to acquire the lock for that many seconds. If the lock period expires and the file is still locked, raise LockTimeout. * If timeout <= 0, raise AlreadyLocked immediately if the file is already locked. implement in subclassNotImplemented)rtimeoutrrracquiresz_SharedBase.acquirecCr!)zX Release the lock. If the file is not locked, raise NotLocked. r"r#rrrrreleasesz_SharedBase.releasecCs ||S)* Context manager support. )r&r'rrr __enter__sz_SharedBase.__enter__cGs |dS)r)N)r()r_excrrr__exit__s z_SharedBase.__exit__cCsd|jj|jfS)Nz<%s: %r>) __class__rrr'rrr__repr__sz_SharedBase.__repr__r) rrrr r&r(r*r,r.rrrrrs  rcsBeZdZdZdfdd ZddZdd Zd d Zd d ZZ S)rz.Base class for platform-specific lock classes.TNc stt||tj|d|_t|_ t |_ |r1t }t|dt|}d|d@|_nd|_tj|j}tj|d|j |j|j t|jf|_||_dS)zi >>> lock = LockBase('somefile') >>> lock = LockBase('somefile', threaded=False) z.lockidentz-%xlz %s%s.%s%sN)superrr osrabspath lock_filesocket gethostnamehostnamegetpidpid threadingrgetattrhashtnamedirnamejoin unique_namer%)rrthreadedr%tr/r>r-rrr s$    zLockBase.__init__cCr!)z9 Tell whether or not the file is locked. r"r#r'rrr is_lockedzLockBase.is_lockedcCr!)zA Return True if this object is locking the file. r"r#r'rrr i_am_lockingrEzLockBase.i_am_lockingcCr!)zN Remove a lock. Useful if a locking thread failed to unlock. r"r#r'rrr break_lockrEzLockBase.break_lockcCsd|jj|j|jfS)Nz<%s: %r -- %r>)r-rr@rr'rrrr.s zLockBase.__repr__)TN) rrrrr rDrFrGr. __classcell__rrrCrrs!rcOsTtjd|tddt|dts|dd}t|dkr#|s#d|d<||i|S)Nz1Import from %s module instead of lockfile package) stacklevelrTrA)warningswarnDeprecationWarning isinstancestrlen)clsmodargskwdsrrr _fl_helper s  rVcO&ddlm}t|jdg|Ri|S)zFactory function provided for backwards compatibility. Do not use in new code. Instead, import LinkLockFile from the lockfile.linklockfile module. rK linklockfilezlockfile.linklockfile)r0rYrV LinkLockFile)rTrUrYrrrr   r cOrW)zFactory function provided for backwards compatibility. Do not use in new code. Instead, import MkdirLockFile from the lockfile.mkdirlockfile module. rK mkdirlockfilezlockfile.mkdirlockfile)r0r]rV MkdirLockFile)rTrUr]rrrr%r[rcOrW)zFactory function provided for backwards compatibility. Do not use in new code. Instead, import SQLiteLockFile from the lockfile.mkdirlockfile module. rK)sqlitelockfilezlockfile.sqlitelockfile)r0r_rVSQLiteLockFile)rTrUr_rrrr0r[rcsfdd}|S)aDecorator which enables locks for decorated function. Arguments: - path: path for lockfile. - timeout (optional): Timeout for acquiring lock. Usage: @locked('/var/run/myname', timeout=0) def myname(...): ... cstfdd}|S)Ncs8td}|z |i|W|S|w)N)r%)FileLockr&r()rTkwargslock)funcrr%rrwrapperHs z&locked..decor..wrapper) functoolswraps)rdrerr%)rdrdecorGszlocked..decorr)rr%rirrhrr;s  rlinkrKrXr\r))r __future__rrfr2r5r:rLhasattr currentThreadrThreadgetNamer__all__ Exceptionrrrrr r r r objectrrrVr rrrr0rY_llfrZLockFiler]_mlfr^rarrrrsD 3           -: