ó
^†êWc           @@  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 ƒ sp 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 e f d „  ƒ  YZ d e f 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   LinkFileLockt   MkdirFileLockt   SQLiteFileLockt   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__(    (    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/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   (    (    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/lockfile/__init__.pyR   V   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   (    (    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/lockfile/__init__.pyR   b   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   (    (    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/lockfile/__init__.pyR   m   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   (    (    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/lockfile/__init__.pyR   x   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   (    (    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/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   (    (    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/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   (    (    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/lockfile/__init__.pyR
   š   s   t   _SharedBasec           B@  sA   e  Z d  „  Z d d „ Z d „  Z d „  Z d „  Z d „  Z RS(   c         C@  s   | |  _  d  S(   N(   t   path(   t   selfR   (    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/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   t   timeout(    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/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   (    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/lockfile/__init__.pyt   release¹   s    c         C@  s   |  j  ƒ  |  S(   s*   
        Context manager support.
        (   R   (   R   (    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/lockfile/__init__.pyt	   __enter__Á   s    
c         G@  s   |  j  ƒ  d S(   s*   
        Context manager support.
        N(   R   (   R   t   _exc(    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/lockfile/__init__.pyt   __exit__È   s    c         C@  s   d |  j  j |  j f S(   Ns   <%s: %r>(   t	   __class__R   R   (   R   (    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/lockfile/__init__.pyt   __repr__Î   s    N(	   R   R   R   t   NoneR   R   R   R   R   (    (    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/lockfile/__init__.pyR   ¥   s   				c           B@  sA   e  Z d  Z e d d „ Z d „  Z d „  Z d „  Z d „  Z	 RS(   s.   Base class for platform-specific lock classes.c         C@  sð   t  t |  ƒ j | ƒ 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   superR   R   t   osR   t   abspatht	   lock_filet   sockett   gethostnamet   hostnamet   getpidt   pidt	   threadingR   t   getattrt   hasht   tnamet   dirnamet   joint   unique_nameR   (   R   R   t   threadedR   t   tR!   R0   (    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/lockfile/__init__.pyR   Ô   s     			c         C@  s   t  d ƒ ‚ d S(   s9   
        Tell whether or not the file is locked.
        s   implement in subclassN(   R   (   R   (    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/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   (    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/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   (    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/lockfile/__init__.pyt
   break_lock  s    c         C@  s   d |  j  j |  j |  j f S(   Ns   <%s: %r -- %r>(   R   R   R2   R   (   R   (    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/lockfile/__init__.pyR     s    N(
   R   R   R   t   TrueR    R   R5   R6   R7   R   (    (    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/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   R3   (   t   warningst   warnt   DeprecationWarningt
   isinstancet   strt   lenR8   (   t   clst   modt   argst   kwds(    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/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"   RE   RD   t   LinkLockFile(   RB   RC   RE   (    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/lockfile/__init__.pyR     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"   RG   RD   t   MkdirLockFile(   RB   RC   RG   (    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/lockfile/__init__.pyR   %  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"   RI   RD   t   SQLiteLockFile(   RB   RC   RI   (    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/lockfile/__init__.pyR   0  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   (   RB   t   kwargst   lock(   t   funcR   R   (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/lockfile/__init__.pyt   wrapperH  s
    
(   t	   functoolst   wraps(   RN   RO   (   R   R   (   RN   sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/lockfile/__init__.pyt   decorG  s    $(    (   R   R   RR   (    (   R   R   sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/lockfile/__init__.pyR   ;  s    
t   linki   (   RE   (   RG   (*   R   t
   __future__R    RP   R$   R'   R,   R:   t   hasattrt   currentThreadR   t   Threadt   getNameR   t   __all__t	   ExceptionR   R   R   R   R   R   R	   R
   t   objectR   R   RD   R   R   R   R    R   R"   RE   t   _llfRF   t   LockFileRG   t   _mlfRH   RK   (    (    (    sn   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-IcAT_k/pip/pip/_vendor/lockfile/__init__.pyt   <module>4   sF   	-:					