ó
¨9Vc           @@  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 d d l Z d d l	 Z	 d d l
 Z
 e e d ƒ s” e j e _ n  e e j d ƒ s» e j j e j _ n  d d d d	 d
 d d d d d d d d g Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d	 e f d „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d d% d „  ƒ  YZ 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(&   s  
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
i    (   t   absolute_importNt   current_threadt   get_namet   Errort	   LockErrort   LockTimeoutt   AlreadyLockedt
   LockFailedt   UnlockErrort	   NotLockedt	   NotMyLockt   LinkLockFilet   MkdirLockFilet   SQLiteLockFilet   LockBaset   lockedc           B@  s   e  Z d  Z RS(   sw   
    Base class for other exceptions.

    >>> try:
    ...   raise Error
    ... except Exception:
    ...   pass
    (   t   __name__t
   __module__t   __doc__(    (    (    s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyR   J   s   c           B@  s   e  Z d  Z RS(   s–   
    Base class for error arising from attempts to acquire the lock.

    >>> try:
    ...   raise LockError
    ... except Error:
    ...   pass
    (   R   R   R   (    (    (    s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyR   U   s   c           B@  s   e  Z d  Z RS(   s   Raised when lock creation fails within a user-defined period of time.

    >>> try:
    ...   raise LockTimeout
    ... except LockError:
    ...   pass
    (   R   R   R   (    (    (    s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyR   `   s   c           B@  s   e  Z d  Z RS(   sˆ   Some other thread/process is locking the file.

    >>> try:
    ...   raise AlreadyLocked
    ... except LockError:
    ...   pass
    (   R   R   R   (    (    (    s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyR   j   s   c           B@  s   e  Z d  Z RS(   s‡   Lock file creation failed for some other reason.

    >>> try:
    ...   raise LockFailed
    ... except LockError:
    ...   pass
    (   R   R   R   (    (    (    s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyR   t   s   c           B@  s   e  Z d  Z RS(   s™   
    Base class for errors arising from attempts to release the lock.

    >>> try:
    ...   raise UnlockError
    ... except Error:
    ...   pass
    (   R   R   R   (    (    (    s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyR   ~   s   c           B@  s   e  Z d  Z RS(   s’   Raised when an attempt is made to unlock an unlocked file.

    >>> try:
    ...   raise NotLocked
    ... except UnlockError:
    ...   pass
    (   R   R   R   (    (    (    s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyR	   ‰   s   c           B@  s   e  Z d  Z RS(   sœ   Raised when an attempt is made to unlock a file someone else locked.

    >>> try:
    ...   raise NotMyLock
    ... except UnlockError:
    ...   pass
    (   R   R   R   (    (    (    s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyR
   “   s   c           B@  sh   e  Z d  Z e d
 d „ Z d
 d „ Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d „  Z d	 „  Z RS(   s.   Base class for platform-specific lock classes.c         C@  sã   | |  _  t j  j | ƒ d |  _ t j ƒ  |  _ t j ƒ  |  _ | r~ t	 j
 ƒ  } t | d t | ƒ ƒ } d | d @|  _ n	 d |  _ t j  j |  j ƒ } t j  j | d |  j |  j |  j t |  j  ƒ f ƒ |  _ | |  _ d S(   si   
        >>> lock = LockBase('somefile')
        >>> lock = LockBase('somefile', threaded=False)
        s   .lockt   idents   -%xIÿÿÿÿ    t    s	   %s%s.%s%sN(   t   patht   ost   abspatht	   lock_filet   sockett   gethostnamet   hostnamet   getpidt   pidt	   threadingR   t   getattrt   hasht   tnamet   dirnamet   joint   unique_namet   timeout(   t   selfR   t   threadedR%   t   tR   R"   (    (    s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyt   __init__Ÿ   s     				c         C@  s   t  d ƒ ‚ d S(   s  
        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.
        s   implement in subclassN(   t   NotImplemented(   R&   R%   (    (    s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyt   acquireÀ   s    c         C@  s   t  d ƒ ‚ d S(   sX   
        Release the lock.

        If the file is not locked, raise NotLocked.
        s   implement in subclassN(   R*   (   R&   (    (    s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyt   releaseÐ   s    c         C@  s   t  d ƒ ‚ d S(   s9   
        Tell whether or not the file is locked.
        s   implement in subclassN(   R*   (   R&   (    (    s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyt	   is_lockedØ   s    c         C@  s   t  d ƒ ‚ d S(   sA   
        Return True if this object is locking the file.
        s   implement in subclassN(   R*   (   R&   (    (    s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyt   i_am_lockingÞ   s    c         C@  s   t  d ƒ ‚ d S(   sN   
        Remove a lock.  Useful if a locking thread failed to unlock.
        s   implement in subclassN(   R*   (   R&   (    (    s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyt
   break_lockä   s    c         C@  s   |  j  ƒ  |  S(   s*   
        Context manager support.
        (   R+   (   R&   (    (    s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyt	   __enter__ê   s    
c         G@  s   |  j  ƒ  d S(   s*   
        Context manager support.
        N(   R,   (   R&   t   _exc(    (    s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyt   __exit__ñ   s    c         C@  s   d |  j  j |  j |  j f S(   Ns   <%s: %r -- %r>(   t	   __class__R   R$   R   (   R&   (    (    s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyt   __repr__÷   s    N(   R   R   R   t   Truet   NoneR)   R+   R,   R-   R.   R/   R0   R2   R4   (    (    (    s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyR      s   !						c         O@  sm   t  j d | t d d ƒt | d t ƒ s: | d } n  t | ƒ d k r` | r` t | d <n  |  | | Ž  S(   Ns1   Import from %s module instead of lockfile packaget
   stackleveli   i    i   R'   (   t   warningst   warnt   DeprecationWarningt
   isinstancet   strt   lenR5   (   t   clst   modt   argst   kwds(    (    s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyt
   _fl_helperû   s    c          O@  s&   d d l  m } t | j d |  | Ž S(   s¡   Factory function provided for backwards compatibility.

    Do not use in new code.  Instead, import LinkLockFile from the
    lockfile.linklockfile module.
    i   (   t   linklockfiles   lockfile.linklockfile(   R   RC   RB   R   (   R@   RA   RC   (    (    s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyt   LinkFileLock  s    c          O@  s&   d d l  m } t | j d |  | Ž S(   s£   Factory function provided for backwards compatibility.

    Do not use in new code.  Instead, import MkdirLockFile from the
    lockfile.mkdirlockfile module.
    i   (   t   mkdirlockfiles   lockfile.mkdirlockfile(   R   RE   RB   R   (   R@   RA   RE   (    (    s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyt   MkdirFileLock  s    c          O@  s&   d d l  m } t | j d |  | Ž S(   s¤   Factory function provided for backwards compatibility.

    Do not use in new code.  Instead, import SQLiteLockFile from the
    lockfile.mkdirlockfile module.
    i   (   t   sqlitelockfiles   lockfile.sqlitelockfile(   R   RG   RB   R   (   R@   RA   RG   (    (    s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyt   SQLiteFileLock  s    c         @  s   ‡  ‡ f d †  } | S(   s  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  j ˆ  ƒ ‡  ‡ ‡ f d †  ƒ } | S(   Nc          @  s?   t  ˆ d ˆ ƒ} | j ƒ  z ˆ  |  | Ž  SWd  | j ƒ  Xd  S(   NR%   (   t   FileLockR+   R,   (   R@   t   kwargst   lock(   t   funcR   R%   (    s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyt   wrapper3  s
    
(   t	   functoolst   wraps(   RL   RM   (   R   R%   (   RL   s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyt   decor2  s    $(    (   R   R%   RP   (    (   R   R%   s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyR   &  s    
t   linki   (   RC   (   RE   (    (+   R   t
   __future__R    t   sysR   R   R   t   timet   urllibR8   RN   t   hasattrt   currentThreadR   t   Threadt   getNameR   t   __all__t	   ExceptionR   R   R   R   R   R   R	   R
   R   RB   RD   RF   RH   R6   R   R   RC   t   _llfR   t   LockFileRE   t   _mlfR   RI   (    (    (    s:   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/lockfile/__init__.pyt   <module>2   sJ   	




^		
	
	
	