a
    )(bZ                     @   s   d Z ddlZddlZddlmZ ddlmZ dd ZddlmZ e	 sfddlm
Z
mZmZmZ d	Zn`d
ZzddlZddlmZ W n ey   dZ
Y n0 dZdZdd Z
eZdd Zdd Zdd ZG dd dZdd ZddgZdS )z&
Filesystem-based interprocess mutex.
    N)time)platformc                   C   s   t tt d S )Ni  )strint_uniquefloat r   r   6lib/python3.9/site-packages/twisted/python/lockfile.pyunique   s    r	   )rename)killreadlinkremovesymlinkFT)OpenProcess   W   c              
   C   s|   zt dd|  W n^ tjyn } zD|jd tkr>W Y d }~d S |jd tkrXttjd  W Y d }~nd }~0 0 t	dd S )Nr   z OpenProcess is required to fail.)
r   
pywintypeserrorargsERROR_ACCESS_DENIEDERROR_INVALID_PARAMETEROSErrorerrnoESRCHRuntimeError)pidsignaler   r   r   r   4   s    r   c                 C   s   |d t   d }tj|d}t| d}t||"}||  |  W d   n1 s`0    Y  zt|| W n( t	y   t
| t|  Y n0 dS )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filenameZnewlinknameZ
newvalnamemodefr   r   r   r   E   s    

&

r   c              
   C   s   zt tj| dd}W nJ tyb } z2|jtjks@|jtjkrLt|jd W Y d}~n:d}~0 0 | | }W d   n1 s0    Y  |S dS )zq
        Read the contents of C{filename}. See the above comment block as to why
        this is needed.
        r   rN)	r$   r    r!   r"   r   r   ENOENTEIOread)r*   ZfObjr   resultr   r   r   r   `   s    &r   c                 C   s"   t t j| d t |  d S )Nr   )r    r   r!   r"   r(   )r*   r   r   r   rmlinkr   s    r2   c                   @   s0   e Zd ZdZdZdZdd Zdd Zdd	 ZdS )
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                 C   s
   || _ d S )N)name)selfr4   r   r   r   __init__   s    zFilesystemLock.__init__c                 C   s  d}zt tt | j W n ty } ztrV|jtjtj	fv rVW Y d}~dS |jtj
krzt| j}W nr ty } zZ|jtjkrW Y d}~W Y d}~qn(tr|jtjkrW Y d}~W Y d}~dS  W Y d}~n
d}~0 0 ztdurtt|d W n ty } z|jtjkrzt| j W nV ty } z<|jtjkrnW Y d}~W Y d}~W Y d}~q W Y d}~n
d}~0 0 d}W Y d}~W Y d}~q W Y d}~n
d}~0 0 W Y d}~dS  W Y d}~n
d}~0 0 d| _|| _dS )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    getpidr4   r   _windowsr   ZEACCESr/   ZEEXISTr   r.   r   r   r   r2   lockedclean)r5   r:   r   r   r   r   r   lock   sD    
 zFilesystemLock.lockc                 C   s@   t | j}t|t kr,td| jdt| j d| _dS )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   r4   r   r    r7   
ValueErrorr2   r9   )r5   r   r   r   r   unlock   s
    	

zFilesystemLock.unlock)	__name__
__module____qualname____doc__r:   r9   r6   r;   r=   r   r   r   r   r3   w   s   =r3   c                 C   s:   t | }d}z| }W |r4|  n|r2|  0 | S )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)r3   r;   r=   )r4   lr1   r   r   r   isLocked   s    



rC   )rA   r   r    r   r   Ztwisted.python.runtimer   r	   r
   Z	isWindowsr   r   r   r2   r   r8   r   Zwin32apir   ImportErrorr   r   openr$   r3   rC   __all__r   r   r   r   <module>   s2   
f