U
    ¹êW[&  ã                   @   s   d Z ddlmZmZ ddlZddlZddlmZ ddlm	Z	 ddl
mZ dd„ Zdd	lmZ e	 ¡ sšdd
lmZ ddlmZ ddlmZ ddlmZ dZnbdZzddlmZ ddlZW n ek
rÎ   dZY nX dZdZdd„ ZeZdd„ Zdd„ Zdd„ ZG dd„ deƒZdd„ ZddgZ dS )z&
Filesystem-based interprocess mutex.
é    )Úabsolute_importÚdivisionN)Útime)Úplatform)Ú_PY3c                   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)Úsymlink)Úreadlink)ÚremoveFT)ÚOpenProcessé   éW   c              
   C   sv   zt dd| ƒ W nX tjk
rh } z8|jd tkr<W Y ¢$d S |jd tkrVttjd ƒ‚‚ W 5 d }~X Y n
X 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   5   s    
r   c              	   C   s–   |d t ƒ  d }tj |d¡}t |¡ tr4d}nd}t||ƒ}| | ¡ | ¡  W 5 Q R X zt	||ƒ W n"   t 
|¡ t |¡ ‚ Y nX 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   ÚwZwcN)r   ÚosÚpathÚjoinÚmkdirr   Ú_openÚwriteÚflushr   r   Úrmdir)ÚvalueÚfilenameZnewlinknameZ
newvalnameÚmodeÚfr
   r
   r   r   E   s    



r   c              
   C   s‚   zt tj | d¡dƒ}W nH tk
r` } z*|jtjksB|jtjkrNt|jdƒ‚‚ W 5 d}~X Y nX | | 	¡ }W 5 Q R X |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%   ÚIOErrorr   ÚENOENTÚEIOr   Úread)r,   ZfObjr    Úresultr
   r
   r   r   b   s    r   c                 C   s"   t  t j | d¡¡ t  | ¡ d S )Nr   )r#   r   r$   r%   r*   )r,   r
   r
   r   Úrmlinks   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k
r° } zptrV|jtjtj	fkrVW Y ¢RdS |jtj
kržzt| jƒ}W nf ttfk
rØ } zD|jtjkr¦W Y ¢2W Y ¢qn trÆ|jtjkrÆW Y ¢W Y ¢âdS ‚ W 5 d}~X Y nX ztdk	ròtt|ƒdƒ W nž tk
r’ } z~|jtjkr€zt| jƒ W nH tk
rl } z(|jtjkrZW Y ¢W Y ¢4W Y ¢Lq‚ W 5 d}~X Y nX d}W Y ¢W Y ¢&q‚ W 5 d}~X Y nX W Y ¢
dS ‚ W 5 d}~X Y nX d| _|| _dS )zÏ
        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.
        TFNr   )r   r   r#   Úgetpidr7   r   Ú_windowsr   ZEACCESr2   ZEEXISTr   r0   r1   r   r   r   r5   ÚlockedÚclean)r8   r=   r    r   r
   r
   r   Úlock“   sD    

zFilesystemLock.lockc                 C   s>   t | jƒ}t|ƒt ¡ kr*td| jf ƒ‚t| jƒ d| _dS )zÕ
        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.
        z!Lock %r 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>   r@   r
   r
   r
   r   r6   y   s   >r6   c                 C   s.   t | ƒ}d}z| ¡ }W 5 |r&| ¡  X | 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)r6   r@   r>   )r7   Úlr4   r
   r
   r   ÚisLockedã   s    

rF   )!rD   Z
__future__r   r   r   r#   r   r	   Ztwisted.python.runtimer   Ztwisted.python.compatr   r   r   Z	isWindowsr   r   r   r   r5   r;   Zwin32apir   r   ÚImportErrorr   r   Úopenr'   Úobjectr6   rF   Ú__all__r
   r
   r
   r   Ú<module>   s<   
j