B
    @)H[ö%  ã               @   s„  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ƒsJej	e_
eejdƒsbejjej_dddd	d
ddddddddgZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd	„ d	eƒZG dd
„ d
eƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdd„ Zd d„ Zd!d„ Zd"d„ Zd(d#d„Zeed$ƒ�rjd%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
>>> print lock.is_locked()
True
>>> lock.release()

>>> lock = LockFile('somefile')
>>> print lock.is_locked()
False
>>> with lock:
...    print lock.is_locked()
True
>>> print 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...
>>> print 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               @   s   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__© r   r   ú^C:\Users\HIRONO~1\AppData\Local\Temp\pip-install-qz7kpjmn\pip\pip\_vendor\lockfile\__init__.pyr   J   s   c               @   s   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
    N)r   r   r   r   r   r   r   r   r   V   s   c               @   s   e Zd ZdZdS )r   z�Raised when lock creation fails within a user-defined period of time.

    >>> try:
    ...   raise LockTimeout
    ... except LockError:
    ...   pass
    N)r   r   r   r   r   r   r   r   r   b   s   c               @   s   e Zd ZdZdS )r   zˆSome other thread/process is locking the file.

    >>> try:
    ...   raise AlreadyLocked
    ... except LockError:
    ...   pass
    N)r   r   r   r   r   r   r   r   r   m   s   c               @   s   e Zd ZdZdS )r	   z‡Lock file creation failed for some other reason.

    >>> try:
    ...   raise LockFailed
    ... except LockError:
    ...   pass
    N)r   r   r   r   r   r   r   r   r	   x   s   c               @   s   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
    N)r   r   r   r   r   r   r   r   r
   ƒ   s   c               @   s   e Zd ZdZdS )r   z’Raised when an attempt is made to unlock an unlocked file.

    >>> try:
    ...   raise NotLocked
    ... except UnlockError:
    ...   pass
    N)r   r   r   r   r   r   r   r   r   �   s   c               @   s   e Zd ZdZdS )r   zœRaised when an attempt is made to unlock a file someone else locked.

    >>> try:
    ...   raise NotMyLock
    ... except UnlockError:
    ...   pass
    N)r   r   r   r   r   r   r   r   r   š   s   c               @   s>   e Zd Zdd„ Zddd„Zdd„ Zdd	„ Zd
d„ Zdd„ ZdS )Ú_SharedBasec             C   s
   || _ d S )N)Úpath)Úselfr   r   r   r   Ú__init__¦   s    z_SharedBase.__init__Nc             C   s   t dƒ‚dS )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.
        zimplement in subclassN)ÚNotImplemented)r   Útimeoutr   r   r   Úacquire©   s    z_SharedBase.acquirec             C   s   t dƒ‚dS )zX
        Release the lock.

        If the file is not locked, raise NotLocked.
        zimplement in subclassN)r   )r   r   r   r   Úrelease¹   s    z_SharedBase.releasec             C   s   |   ¡  | S )z*
        Context manager support.
        )r   )r   r   r   r   Ú	__enter__Á   s    z_SharedBase.__enter__c             G   s   |   ¡  dS )z*
        Context manager support.
        N)r   )r   Ú_excr   r   r   Ú__exit__È   s    z_SharedBase.__exit__c             C   s   d| j j| jf S )Nz<%s: %r>)Ú	__class__r   r   )r   r   r   r   Ú__repr__Î   s    z_SharedBase.__repr__)N)	r   r   r   r   r   r   r    r"   r$   r   r   r   r   r   ¥   s   
r   c                   sB   e Zd ZdZd‡ fdd„	Zdd„ Zdd	„ Zd
d„ Zdd„ Z‡  Z	S )r   z.Base class for platform-specific lock classes.TNc          	      s¨   t t| ƒ |¡ tj |¡d | _t ¡ | _	t 
¡ | _|rbt ¡ }t|dt|ƒƒ}d|d@  | _nd| _tj | j¡}tj |d| j	| j| jt| jƒf ¡| _|| _dS )zi
        >>> lock = LockBase('somefile')
        >>> lock = LockBase('somefile', threaded=False)
        z.lockÚidentz-%xl   ÿÿ Ú z	%s%s.%s%sN)Úsuperr   r   Úosr   ÚabspathÚ	lock_fileÚsocketÚgethostnameÚhostnameÚgetpidÚpidÚ	threadingr   ÚgetattrÚhashÚtnameÚdirnameÚjoinÚunique_namer   )r   r   Úthreadedr   Útr%   r4   )r#   r   r   r   Ô   s     

	zLockBase.__init__c             C   s   t dƒ‚dS )z9
        Tell whether or not the file is locked.
        zimplement in subclassN)r   )r   r   r   r   Ú	is_lockedõ   s    zLockBase.is_lockedc             C   s   t dƒ‚dS )zA
        Return True if this object is locking the file.
        zimplement in subclassN)r   )r   r   r   r   Úi_am_lockingû   s    zLockBase.i_am_lockingc             C   s   t dƒ‚dS )zN
        Remove a lock.  Useful if a locking thread failed to unlock.
        zimplement in subclassN)r   )r   r   r   r   Ú
break_lock  s    zLockBase.break_lockc             C   s   d| j j| j| jf S )Nz<%s: %r -- %r>)r#   r   r6   r   )r   r   r   r   r$     s    zLockBase.__repr__)TN)
r   r   r   r   r   r9   r:   r;   r$   Ú__classcell__r   r   )r#   r   r   Ò   s   !c             O   sP   t jd| tdd� t|d tƒs.|dd … }t|ƒdkrF|sFd|d< | ||ŽS )Nz1Import from %s module instead of lockfile packageé   )Ú
stacklevelr   é   Tr7   )ÚwarningsÚwarnÚDeprecationWarningÚ
isinstanceÚstrÚlen)ÚclsÚmodÚargsÚkwdsr   r   r   Ú
_fl_helper  s    

rJ   c              O   s    ddl m} t|jdf| ž|ŽS )z¡Factory function provided for backwards compatibility.

    Do not use in new code.  Instead, import LinkLockFile from the
    lockfile.linklockfile module.
    r?   )Úlinklockfilezlockfile.linklockfile)r&   rK   rJ   ÚLinkLockFile)rH   rI   rK   r   r   r   r     s    
c              O   s    ddl m} t|jdf| ž|ŽS )z£Factory function provided for backwards compatibility.

    Do not use in new code.  Instead, import MkdirLockFile from the
    lockfile.mkdirlockfile module.
    r?   )Úmkdirlockfilezlockfile.mkdirlockfile)r&   rM   rJ   ÚMkdirLockFile)rH   rI   rM   r   r   r   r   %  s    
c              O   s    ddl m} t|jdf| ž|ŽS )z¤Factory function provided for backwards compatibility.

    Do not use in new code.  Instead, import SQLiteLockFile from the
    lockfile.mkdirlockfile module.
    r?   )Úsqlitelockfilezlockfile.sqlitelockfile)r&   rO   rJ   ÚSQLiteLockFile)rH   rI   rO   r   r   r   r   0  s    
c                s   ‡ ‡fd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                s   t  ˆ ¡‡ ‡‡fdd„ƒ}|S )Nc                 s.   t ˆˆd�}| ¡  z
ˆ | |ŽS | ¡  X d S )N)r   )ÚFileLockr   r   )rH   ÚkwargsÚlock)Úfuncr   r   r   r   ÚwrapperH  s
    
z&locked.<locals>.decor.<locals>.wrapper)Ú	functoolsÚwraps)rT   rU   )r   r   )rT   r   ÚdecorG  s    zlocked.<locals>.decorr   )r   r   rX   r   )r   r   r   r   ;  s    
Úlinkr?   )rK   )rM   )N))r   Ú
__future__r   rV   r(   r+   r0   r@   ÚhasattrÚcurrentThreadr   ÚThreadÚgetNamer   Ú__all__Ú	Exceptionr   r   r   r   r	   r
   r   r   Úobjectr   r   rJ   r   r   r   r   r&   rK   Ú_llfrL   ÚLockFilerM   Z_mlfrN   rQ   r   r   r   r   Ú<module>4   sF   
-:
