ó
[³XMc           @   sC  d  Z  e Z d d l Z d d l Z d d l m Z d „  Z d d l m Z yJ d d l m	 Z	 d d l m
 Z
 d d l m Z d d	 l m Z e Z Wn} e Z y  d d
 l m Z d d l Z Wn e k
 rÜ d Z	 n Xd Z d Z d „  Z	 e Z d „  Z
 d „  Z d „  Z n Xd d d „  ƒ  YZ d „  Z d d g Z d S(   s&   
Filesystem-based interprocess mutex.
iÿÿÿÿN(   t   timec           C   s   t  t t ƒ  d ƒ ƒ S(   Niè  (   t   strt   longt   _uniquefloat(    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/python/lockfile.pyt   unique   s    (   t   rename(   t   kill(   t   symlink(   t   readlink(   t   remove(   t   OpenProcessi   iW   c         C   s   y t  d d |  ƒ WnX t j k
 rn } | j d t k r@ d  S| j d t k rh t t j d  ƒ ‚ n  ‚  n Xt
 d ƒ ‚ d  S(   Ni    s    OpenProcess is required to fail.(   R
   t
   pywintypest   errort   argst   ERROR_ACCESS_DENIEDt   ERROR_INVALID_PARAMETERt   OSErrort   errnot   ESRCHt   Nonet   RuntimeError(   t   pidt   signalt   e(    (    s;   /usr/lib/python2.7/dist-packages/twisted/python/lockfile.pyR   &   s    c         C   s£   | d t  ƒ  d } t j j | d ƒ } t j | ƒ t | d ƒ } | j |  ƒ | j ƒ  | j ƒ  y t	 | | ƒ Wn$ t j
 | ƒ t j | ƒ ‚  n Xd  S(   Nt   .s   .newlinkR   t   wcb(   R   t   ost   patht   joint   mkdirt   _opent   writet   flusht   closeR   R	   t   rmdir(   t   valuet   filenamet   newlinknamet
   newvalnamet   f(    (    s;   /usr/lib/python2.7/dist-packages/twisted/python/lockfile.pyR   5   s    

c         C   s’   y" t  t j j |  d ƒ d ƒ } WnO t k
 rs } | j t j k sX | j t j k rm t | j d  ƒ ‚ n  ‚  n X| j
 ƒ  } | j ƒ  | Sd  S(   NR   t   rb(   R   R   R   R   t   IOErrorR   t   ENOENTt   EIOR   R   t   readR!   (   R$   t   fObjR   t   result(    (    s;   /usr/lib/python2.7/dist-packages/twisted/python/lockfile.pyR   D   s    "$
c         C   s-   t  j t  j j |  d ƒ ƒ t  j |  ƒ d  S(   NR   (   R   R	   R   R   R"   (   R$   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/python/lockfile.pyt   rmlinkP   s    t   FilesystemLockc           B   s5   e  Z d  Z d Z e Z d „  Z d „  Z d „  Z	 RS(   s  
    A mutex.

    This relies on the filesystem property that creating
    a symlink is an atomic operation and that it will
    fail if the symlink already exists.  Deleting the
    symlink will release the lock.

    @ivar name: The name of the file associated with this lock.

    @ivar clean: Indicates whether this lock was released cleanly by its
        last owner.  Only meaningful after C{lock} has been called and
        returns True.

    @ivar locked: Indicates whether the lock is currently held by this
        object.
    c         C   s   | |  _  d  S(   N(   t   name(   t   selfR1   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/python/lockfile.pyt   __init__l   s    c         C   s²  t  } x¥t  r­y  t t t j ƒ  ƒ |  j ƒ Wnet k
 r–} t rf | j t j	 t j
 f k rf t S| j t j k ry t |  j ƒ } Wn_ t k
 r» } | j t j k rµ q	 n  ‚  n2 t k
 rì } t ræ | j t j	 k ræ t S‚  n Xy& t d k	 rt t | ƒ d ƒ n  Wnv t k
 r‹} | j t j k r…y t |  j ƒ Wn. t k
 rx} | j t j k rrq	 n  ‚  n Xt } q	 n  ‚  n Xt S‚  n Xt  |  _ | |  _ t  SWd S(   sÏ   
        Acquire this lock.

        @rtype: C{bool}
        @return: True if the lock is acquired, false otherwise.

        @raise: Any exception os.symlink() may raise, other than
        EEXIST.
        i    N(   t   TrueR   R   R   t   getpidR1   R   t   _windowsR   t   EACCESR+   t   Falset   EEXISTR   R*   R)   R   R   t   intR   R/   t   lockedt   clean(   R2   R<   R   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/python/lockfile.pyt   lockp   sJ    
	 !		c         C   sZ   t  |  j ƒ } t | ƒ t j ƒ  k r@ t d |  j f ƒ ‚ n  t |  j ƒ t |  _ d S(   sÕ   
        Release this lock.

        This deletes the directory with the given name.

        @raise: Any exception os.readlink() may raise, or
        ValueError if the lock is not owned by this process.
        s!   Lock %r not owned by this processN(	   R   R1   R:   R   R5   t
   ValueErrorR/   R8   R;   (   R2   R   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/python/lockfile.pyt   unlock°   s
    	N(
   t   __name__t
   __module__t   __doc__R   R<   R8   R;   R3   R=   R?   (    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/python/lockfile.pyR0   V   s   		@c         C   s>   t  |  ƒ } d } z | j ƒ  } Wd | r8 | j ƒ  n  X| S(   sÚ   Determine if the lock of the given name is held or not.

    @type name: C{str}
    @param name: The filesystem path to the lock to test

    @rtype: C{bool}
    @return: True if the lock is held, False otherwise.
    N(   R0   R   R=   R?   (   R1   t   lR.   (    (    s;   /usr/lib/python2.7/dist-packages/twisted/python/lockfile.pyt   isLockedÀ   s    	RD   (    (   RB   t   typet   __metaclass__R   R   R    R   R   R   R   R   R   R	   R/   R8   R6   R4   t   win32apiR
   R   t   ImportErrorR   R   R   t   fileR   R0   RD   t   __all__(    (    (    s;   /usr/lib/python2.7/dist-packages/twisted/python/lockfile.pyt   <module>   s8   	

			j	