
    Ϫf*                         d Z ddlZddlZddlmZ ddlmZ d ZddlmZ  ej                         sddlm
Z
mZmZmZ dZnd	Z	 ddlZdd
lmZ dZdZd Z
eZd Zd Zd Z G d d      Zd ZddgZy# e$ r dZ
Y &w xY w)z&
Filesystem-based interprocess mutex.
    N)time)platformc                  @    t        t        t               dz              S )Ni  )strint_uniquefloat     9/usr/lib/python3/dist-packages/twisted/python/lockfile.pyuniquer      s    s<>D()**r
   )rename)killreadlinkremovesymlinkFT)OpenProcess   W   c                    	 t        dd|        t        d      # t        j                  $ rQ}|j                  d   t
        k(  rY d }~y |j                  d   t        k(  rt        t        j                  d        d }~ww xY w)Nr   z OpenProcess is required to fail.)
r   RuntimeError
pywintypeserrorargsERROR_ACCESS_DENIEDERROR_INVALID_PARAMETEROSErrorerrnoESRCH)pidsignales      r   r   r   4   ss    	GAq#& ##EFF ## 66!9 33VVAY"99!%++t44s    A>A91A99A>c                    |dz   t               z   dz   }t        j                  j                  |d      }t        j                  |       d}t        ||      5 }|j                  |        |j                          ddd       	 t        ||       y# 1 sw Y   xY w# t        $ r, t        j                  |       t        j                  |        w xY w)z
        Write a file at C{filename} with the contents of C{value}. See the
        above comment block as to why this is needed.
        .z.newlinkr   wN)r   ospathjoinmkdir_openwriteflushr   BaseExceptionr   rmdir)valuefilenamenewlinkname
newvalnamemodefs         r   r   r   E   s     nvx/*<WW\\+y9

 :t$ 	GGENGGI		;)	 	  	IIj!HH[!	s   "BB B5Cc                 p   	 t        t        j                  j                  | d      d      }|5  |j	                         }ddd       |S # 1 sw Y   S xY w# t
        $ rV}|j                  t        j                  k(  s|j                  t        j                  k(  rt        |j                  d       d}~ww xY w)zq
        Read the contents of C{filename}. See the above comment block as to why
        this is needed.
        r   rN)	r)   r%   r&   r'   readr   r   ENOENTEIO)r/   fObjresultr!   s       r   r   r   `   s    
		h	:C@D  %%M%M  	ww%,,&!''UYY*>aggt,,	s#   *A A		A	B5AB00B5c                     t        j                  t         j                  j                  | d             t        j                  |        y )Nr   )r%   r   r&   r'   r-   )r/   s    r   rmlinkr<   r   s)    
		"'',,x34
r
   c                   *    e Zd ZdZdZdZd Zd Zd Zy)FilesystemLocka  
    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.
    NFc                     || _         y )N)name)selfr@   s     r   __init__zFilesystemLock.__init__   s	    	r
   c                    d}	 	 t        t        t        j                               | j                         d| _        || _        y# t
        $ r}t        r1|j                  t        j                  t        j                  fv rY d}~y|j                  t        j                  k(  r,	 t        | j                        }nd# t
        $ rX}|j                  t        j                  k(  r	Y d}~Y d}~t        r&|j                  t        j                  k(  r	Y d}~Y d}~y d}~ww xY w	 t        t        t        |      d       n# t
        $ r}|j                  t        j                  k(  r`	 t!        | j                         n=# t
        $ r1}|j                  t        j                  k(  rY d}~Y d}~Y d}~ d}~ww xY wd}Y d}~Y d}~ d}~ww xY wY d}~y d}~ww xY w)z
        Acquire this lock.

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

        @raise OSError: Any exception L{os.symlink()} may raise,
            other than L{errno.EEXIST}.
        TNFr   )r   r   r%   getpidr@   r   _windowsr   EACCESr8   EEXISTr   r7   r   r   r   r<   lockedclean)rA   rI   r!   r   s       r   lockzFilesystemLock.lock   sc    ,BIIK($))4X DKDJ[  *ELL%))+D D !77ell*&tyy1" 77ell2 %H%!''U\\*A $)5+ S1-" 77ekk1& &tyy 1#* &#$77ell#: %-H %& %*E$H  !U*s   1A 
G2GG%B;:G;	DD!G*#DGDDG D<;G<	GG#E98G9	F3F.G#G-F..F33G8GGGGGGc                     t        | j                        }t        |      t        j                         k7  rt        d| j                  d      t        | j                         d| _        y)z
        Release this lock.

        This deletes the directory with the given name.

        @raise OSError: Any exception L{os.readlink()} may raise.
        @raise ValueError: If the lock is not owned by this process.
        zLock z not owned by this processFN)r   r@   r   r%   rD   
ValueErrorr<   rH   )rA   r   s     r   unlockzFilesystemLock.unlock   sP     tyy!s8ryy{"uTYYM1KLMMtyyr
   )	__name__
__module____qualname____doc__rI   rH   rB   rJ   rM   r	   r
   r   r>   r>   w   s#    $ EF;zr
   r>   c                     t        |       }d}	 |j                         }|r|j                          | S # |r|j                          w w xY w)z
    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)r>   rJ   rM   )r@   lr:   s      r   isLockedrT      sL     	tAFHHJ: HHJ s	   4 A	rT   )rQ   r   r%   r   r   twisted.python.runtimer   r   r   	isWindowsr   r   r   r<   r   rE   r   win32apir   r   r   ImportErroropenr)   r>   rT   __all__r	   r
   r   <module>r[      s   
  	 % ++ x<<HHG(  "$
	G E6$
c cL( Z
(K  s   
A1 1A;:A;