o
    ¯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 	¡ s3ddlm
Z
mZmZmZ d	Zn0d
ZzddlZddlmZ W n eyL   dZ
Y n	w 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   ú9/usr/lib/python3/dist-packages/twisted/python/lockfile.pyÚunique   s   r	   )Úrename)ÚkillÚreadlinkÚremoveÚsymlinkFT)ÚOpenProcessé   éW   c              
   C   sj   zt dd| ƒ W t	dƒ‚ tjy4 } z|jd tkr"W Y d }~d S |jd tkr/ttjd ƒ‚‚ d }~ww )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 s0w   Y  zt||ƒ W dS  t	yO   t 
|¡ t |¡ ‚ 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Úfr   r   r   r   E   s   


þ

ýr   c              
   C   sŠ   zt tj | d¡dƒ}W n ty+ } z|jtjks |jtjkr&t|jdƒ‚‚ d}~ww |� | ¡ }W d  ƒ |S 1 s>w   Y  |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*   Ú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   r5   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)Úselfr7   r   r   r   Ú__init__�   s   
zFilesystemLock.__init__c                 C   s®  d}	 zt tt ¡ ƒ| jƒ W n¿ tyÎ } z³tr)|jtjtj	fv r)W Y d}~dS |jtj
krÉzt| jƒ}W n2 tyh } z&|jtjkrOW Y d}~W Y d}~qtrc|jtjkrcW Y d}~W Y d}~dS ‚ d}~ww ztdurutt|ƒdƒ W nK tyÁ } z?|jtjkr¼zt| jƒ W n# ty® } z|jtjkr©W Y d}~W Y d}~W Y d}~q‚ d}~ww d}W Y d}~W Y d}~q‚ d}~ww W Y d}~dS ‚ d}~ww 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    Úgetpidr7   r   Ú_windowsr   ÚEACCESr1   ÚEEXISTr   r0   r   r   r   r5   ÚlockedÚclean)r8   r?   r   r   r   r   r   Úlock�   sX   
€ô€ €ú€ñ€Ö+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   r7   r   r    r:   Ú
ValueErrorr5   r>   )r8   r   r   r   r   ÚunlockÍ   s
   
	

zFilesystemLock.unlock)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r?   r>   r9   r@   rB   r   r   r   r   r6   w   s    =r6   c                 C   s:   t | ƒ}d}z| ¡ }W |r| ¡  | S |r| ¡  w 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)r6   r@   rB   )r7   Úlr4   r   r   r   ÚisLockedÝ   s   

þ
ÿrH   )rF   r   r    r   r   Útwisted.python.runtimer   r	   r
   Ú	isWindowsr   r   r   r5   r   r;   r   Úwin32apir   ÚImportErrorr   r   Úopenr$   r6   rH   Ú__all__r   r   r   r   Ú<module>   s6   ÿf