
    r^~$                     N   d Z ddlmZ ddlZddlZddlZddlZddlZ eed          sej	        e_
         eej        d          sej        j        ej        _        g dZ G d de          Z G d	 d
e          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Zd Zd Zd Zd Zd$dZ eed           rd!d"l m!Z" e"j#        Z$nd!d#l m%Z& e&j'        Z$e$Z(dS )%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_importNcurrent_threadget_name)Error	LockErrorLockTimeoutAlreadyLocked
LockFailedUnlockError	NotLocked	NotMyLockLinkFileLockMkdirFileLockSQLiteFileLockLockBaselockedc                       e Zd ZdZdS )r   zw
    Base class for other exceptions.

    >>> try:
    ...   raise Error
    ... except Exception:
    ...   pass
    N__name__
__module____qualname____doc__     3/usr/lib/python3/dist-packages/lockfile/__init__.pyr   r   J             	Dr   r   c                       e Zd ZdZdS )r   z
    Base class for error arising from attempts to acquire the lock.

    >>> try:
    ...   raise LockError
    ... except Error:
    ...   pass
    Nr   r   r   r   r   r   V   r   r   r   c                       e Zd ZdZdS )r   zRaised when lock creation fails within a user-defined period of time.

    >>> try:
    ...   raise LockTimeout
    ... except LockError:
    ...   pass
    Nr   r   r   r   r   r   b             	Dr   r   c                       e Zd ZdZdS )r	   zSome other thread/process is locking the file.

    >>> try:
    ...   raise AlreadyLocked
    ... except LockError:
    ...   pass
    Nr   r   r   r   r	   r	   m   r   r   r	   c                       e Zd ZdZdS )r
   zLock file creation failed for some other reason.

    >>> try:
    ...   raise LockFailed
    ... except LockError:
    ...   pass
    Nr   r   r   r   r
   r
   x   r   r   r
   c                       e Zd ZdZdS )r   z
    Base class for errors arising from attempts to release the lock.

    >>> try:
    ...   raise UnlockError
    ... except Error:
    ...   pass
    Nr   r   r   r   r   r      r   r   r   c                       e Zd ZdZdS )r   zRaised when an attempt is made to unlock an unlocked file.

    >>> try:
    ...   raise NotLocked
    ... except UnlockError:
    ...   pass
    Nr   r   r   r   r   r      r   r   r   c                       e Zd ZdZdS )r   zRaised when an attempt is made to unlock a file someone else locked.

    >>> try:
    ...   raise NotMyLock
    ... except UnlockError:
    ...   pass
    Nr   r   r   r   r   r      r   r   r   c                   4    e Zd Zd ZddZd Zd Zd Zd ZdS )	_SharedBasec                     || _         d S N)path)selfr)   s     r   __init__z_SharedBase.__init__   s    			r   Nc                      t          d          )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 subclassNotImplemented)r*   timeouts     r   acquirez_SharedBase.acquire   s     4555r   c                      t          d          )zX
        Release the lock.

        If the file is not locked, raise NotLocked.
        r-   r.   r*   s    r   releasez_SharedBase.release   s     4555r   c                 .    |                                   | S )*
        Context manager support.
        )r1   r3   s    r   	__enter__z_SharedBase.__enter__   s     	r   c                 .    |                                   dS )r6   N)r4   )r*   _excs     r   __exit__z_SharedBase.__exit__   s     	r   c                 2    d| j         j        d| j        dS )N<: >)	__class__r   r)   r3   s    r   __repr__z_SharedBase.__repr__   s      !^444diii@@r   r(   )	r   r   r   r+   r1   r4   r7   r:   r@   r   r   r   r&   r&      sx          6 6 6 6 6 6 6    A A A A Ar   r&   c                   <     e Zd ZdZd	 fd	Zd Zd Zd Zd Z xZ	S )
r   z.Base class for platform-specific lock classes.TNc           
         t          t          |                               |           t          j                            |          dz   | _        t          j                    | _	        t          j
                    | _        |r?t          j                    }t          |dt          |                    }d|dz  z  | _        nd| _        t          j                            | j                  }t          j                            || j	        | j        d| j        t          | j                            | _        || _        dS )zi
        >>> lock = LockBase('somefile')
        >>> lock = LockBase('somefile', threaded=False)
        z.lockidentz-%xl     .N)superr   r+   osr)   abspath	lock_filesocketgethostnamehostnamegetpidpid	threadingr   getattrhashtnamedirnamejoinunique_namer0   )r*   r)   threadedr0   trC   rS   r?   s          r   r+   zLockBase.__init__   s   
 	h&&t,,,..8*,,9;; 	(**A AwQ00E%*"45DJJDJ'//$.11 7<<7;}}7;zzz7;xx7;DI)HI I
 r   c                      t          d          )z9
        Tell whether or not the file is locked.
        r-   r.   r3   s    r   	is_lockedzLockBase.is_locked        4555r   c                      t          d          )zA
        Return True if this object is locking the file.
        r-   r.   r3   s    r   i_am_lockingzLockBase.i_am_locking   rZ   r   c                      t          d          )zN
        Remove a lock.  Useful if a locking thread failed to unlock.
        r-   r.   r3   s    r   
break_lockzLockBase.break_lock  rZ   r   c                 B    d| j         j        d| j        d| j        dS )Nr<   r=   z -- r>   )r?   r   rU   r)   r3   s    r   r@   zLockBase.__repr__  s0     #'>#:#:#:D<L<L<L#'999. 	.r   )TN)
r   r   r   r   r+   rY   r\   r^   r@   __classcell__)r?   s   @r   r   r      s        88     B6 6 66 6 66 6 6. . . . . . .r   r   c                     t          j        d|z  t          d           t          |d         t                    s
|dd          }t          |          dk    r|sd|d<    | |i |S )Nz1Import from %s module instead of lockfile package   )
stacklevelr      TrV   )warningswarnDeprecationWarning
isinstancestrlen)clsmodargskwdss       r   
_fl_helperro     s    MEK$4 4 4 4
 d1gs## ABBx
4yyA~~d~Z3r   c                  :    ddl m} t          |j        dg| R i |S )zFactory function provided for backwards compatibility.

    Do not use in new code.  Instead, import LinkLockFile from the
    lockfile.linklockfile module.
    rd   linklockfilezlockfile.linklockfile)rD   rr   ro   LinkLockFile)rm   rn   rr   s      r   r   r     sJ     l/1H %% % %#% % %r   c                  :    ddl m} t          |j        dg| R i |S )zFactory function provided for backwards compatibility.

    Do not use in new code.  Instead, import MkdirLockFile from the
    lockfile.mkdirlockfile module.
    rd   mkdirlockfilezlockfile.mkdirlockfile)rD   rv   ro   MkdirLockFile)rm   rn   rv   s      r   r   r   %  sJ      m13K %% % %#% % %r   c                  :    ddl m} t          |j        dg| R i |S )zFactory function provided for backwards compatibility.

    Do not use in new code.  Instead, import SQLiteLockFile from the
    lockfile.mkdirlockfile module.
    rd   )sqlitelockfilezlockfile.sqlitelockfile)rD   ry   ro   SQLiteLockFile)rm   rn   ry   s      r   r   r   0  sJ     !     n35N %% % %#% % %r   c                       fd}|S )a  Decorator 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(...):
             ...
    c                 L     t          j                    fd            }|S )Nc                      t                    }|                                 	  | i ||                                 S # |                                 w xY w)N)r0   )FileLockr1   r4   )rm   kwargslockfuncr)   r0   s      r   wrapperz&locked.<locals>.decor.<locals>.wrapperH  sZ    D'222DLLNNNtT,V,,s   A A)	functoolswraps)r   r   r)   r0   s   ` r   decorzlocked.<locals>.decorG  sC    				 	 	 	 	 	 
		 r   r   )r)   r0   r   s   `` r   r   r   ;  s)    	 	 	 	 	 	 Lr   linkrd   rq   ru   r(   ))r   
__future__r   r   rG   rJ   rO   re   hasattrcurrentThreadr   ThreadgetNamer   __all__	Exceptionr   r   r   r	   r
   r   r   r   objectr&   r   ro   r   r   r   r   rD   rr   _llfrs   LockFilerv   _mlfrw   r~   r   r   r   <module>r      s  1 1f ' & & & & &     				       wy*++ 7(6Iwy,, 9 ) 0 8I! ! !		 		 		 		 		I 		 		 				 		 		 		 		 		 		 			 	 	 	 	) 	 	 		 	 	 	 	I 	 	 		 	 	 	 	 	 	 			 		 		 		 		% 		 		 			 	 	 	 	 	 	 		 	 	 	 	 	 	 	*A *A *A *A *A& *A *A *AZ7. 7. 7. 7. 7.{ 7. 7. 7.t  % % %% % %% % %   2 72v "&&&&&& HH''''''!Hr   