σ
΄ΌχXc           @`  s   d  d l  m Z m Z d  d l Z d  d l Z y e Wn e k
 rO e Z n Xd d l m	 Z	 m
 Z
 m Z m Z m Z d e	 f d     YZ d S(   i    (   t   absolute_importt   divisionNi   (   t   LockBaset	   NotLockedt	   NotMyLockt   LockTimeoutt   AlreadyLockedt   SQLiteLockFilec           B`  s\   e  Z d  Z d Z e d d  Z d d  Z d   Z d   Z	 d   Z
 d   Z d   Z RS(	   s   Demonstrate SQL-based locking.c   
      C`  s  t  j |  | | |  t |  j  |  _ t |  j  |  _ t j d k r d d l } | j	   \ } } t
 j |  t
 j |  ~ ~ | t _ n  d d l } | j t j  |  _ |  j j   } y | j d  Wn | j k
 rκ n0 X|  j j   d d l }	 |	 j t
 j t j  d S(   su   
        >>> lock = SQLiteLockFile('somefile')
        >>> lock = SQLiteLockFile('somefile', threaded=False)
        i    NsG   create table locks(   lock_file varchar(32),   unique_name varchar(32))(   R   t   __init__t   unicodet	   lock_filet   unique_nameR   t   testdbt   Nonet   tempfilet   mkstempt   ost   closet   unlinkt   sqlite3t   connectt
   connectiont   cursort   executet   OperationalErrort   committ   atexitt   register(
   t   selft   patht   threadedt   timeoutR   t   _fdR   R   t   cR   (    (    s@   /tmp/pip-build-g7Urym/pip/pip/_vendor/lockfile/sqlitelockfile.pyR      s(    c         C`  sΡ  | d  k	 r | n |  j } t j   } | d  k	 rL | d k rL | | 7} n  | d  k ra d } n | d k rv d } n
 | d } |  j j   } x;t rΜ|  j   s.| j d |  j |  j	 f  |  j j
   | j d |  j	 f  | j   } t |  d k r'| j d |  j	 f  |  j j
   qfd  Sn8 | j d |  j	 f  | j   } t |  d k rfd  S| d  k	 rΌt j   | k rΌ| d k r¦t d |  j   qΌt d	 |  j   n  t j |  q Wd  S(
   Ni    gΉ?i
   s;   insert into locks  (lock_file, unique_name)  values  (?, ?)s*   select * from locks  where unique_name = ?i   s(   delete from locks  where unique_name = ?s&   Timeout waiting to acquire lock for %ss   %s is already locked(   R   R   t   timeR   R   t   Truet	   is_lockedR   R
   R   R   t   fetchallt   lenR   R   R   t   sleep(   R   R   t   end_timet   waitR   t   rows(    (    s@   /tmp/pip-build-g7Urym/pip/pip/_vendor/lockfile/sqlitelockfile.pyt   acquire5   sD    		
					c         C`  s   |  j    s" t d |  j   n  |  j   sP t d |  j |  j   f   n  |  j j   } | j	 d |  j f  |  j j
   d  S(   Ns   %s is not lockeds#   %s is locked, but not by me (by %s)s(   delete from locks  where unique_name = ?(   R$   R   R   t   i_am_lockingR   R   t   _who_is_lockingR   R   R   R   (   R   R   (    (    s@   /tmp/pip-build-g7Urym/pip/pip/_vendor/lockfile/sqlitelockfile.pyt   releaset   s    	c         C`  s3   |  j  j   } | j d |  j f  | j   d S(   Ns2   select unique_name from locks  where lock_file = ?i    (   R   R   R   R
   t   fetchone(   R   R   (    (    s@   /tmp/pip-build-g7Urym/pip/pip/_vendor/lockfile/sqlitelockfile.pyR-      s    	c         C`  s7   |  j  j   } | j d |  j f  | j   } | S(   Ns(   select * from locks  where lock_file = ?(   R   R   R   R
   R%   (   R   R   R*   (    (    s@   /tmp/pip-build-g7Urym/pip/pip/_vendor/lockfile/sqlitelockfile.pyR$      s
    	c         C`  s7   |  j  j   } | j d |  j |  j f  | j   S(   Ns?   select * from locks  where lock_file = ?    and unique_name = ?(   R   R   R   R
   R   R%   (   R   R   (    (    s@   /tmp/pip-build-g7Urym/pip/pip/_vendor/lockfile/sqlitelockfile.pyR,      s    	c         C`  s6   |  j  j   } | j d |  j f  |  j  j   d  S(   Ns&   delete from locks  where lock_file = ?(   R   R   R   R
   R   (   R   R   (    (    s@   /tmp/pip-build-g7Urym/pip/pip/_vendor/lockfile/sqlitelockfile.pyt
   break_lock   s    	N(   t   __name__t
   __module__t   __doc__R   R   R#   R   R+   R.   R-   R$   R,   R0   (    (    (    s@   /tmp/pip-build-g7Urym/pip/pip/_vendor/lockfile/sqlitelockfile.pyR      s   "?				(   t
   __future__R    R   R"   R   R	   t	   NameErrort   strt    R   R   R   R   R   R   (    (    (    s@   /tmp/pip-build-g7Urym/pip/pip/_vendor/lockfile/sqlitelockfile.pyt   <module>   s   
(