
    \dZ                        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
n# e$ r dZ
Y nw xY weZd Zd Zd Z G d d          Zd ZddgZdS )z&
Filesystem-based interprocess mutex.
    N)time)platformc                  X    t          t          t                      dz                      S )Ni  )strint_uniquefloat     7lib/python3.11/site-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        $ rR}|j        d         t
          k    rY d }~d S |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   s    	GAq#&&& ##EFFF #   6!9 333FFFFFVAY"999!%+t444s   " BA>1A>>Bc                    |dz   t                      z   dz   }t          j                            |d          }t          j        |           d}t          ||          5 }|                    |            |                                 ddd           n# 1 swxY w Y   	 t          ||           dS # 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    nvxx/*<W\\+y99

 :t$$ 	GGENNNGGIII	 	 	 	 	 	 	 	 	 	 	 	 	 	 		;))))) 	 	 	Ij!!!H[!!!	s   *BBB B2 24C&c                 d   	 t          t          j                            | d          d          }|5  |                                }ddd           n# 1 swxY w Y   |S # t
          $ rE}|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  	 	 	w%,&&!'UY*>*>agt,,,	s)   .A  AAA 
B/*A B**B/c                     t          j        t           j                            | d                     t          j        |            d S )Nr   )r%   r   r&   r'   r-   )r/   s    r   rmlinkr<   r   s8    
	"',,x33444
r
   c                   ,    e Zd ZdZdZdZd Zd Z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                     || _         d S )N)name)selfr@   s     r   __init__zFilesystemLock.__init__   s    			r
   c                 r   d}	 	 t          t          t          j                              | j                   no# t
          $ ra}t          r%|j        t          j        t          j	        fv rY d}~dS |j        t          j
        k    r	 t          | j                  }nW# t
          $ rJ}|j        t          j        k    r	Y d}~Y d}~t          r|j        t          j        k    r
Y d}~Y d}~dS  d}~ww xY w	 t          t          t          |          d           n# t
          $ rs}|j        t          j        k    rX	 t!          | j                   n6# t
          $ r)}|j        t          j        k    rY d}~Y d}~Y d}~y d}~ww xY wd}Y d}~Y d}~ d}~ww xY wY d}~dS  d}~ww xY 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%   getpidr@   r   _windowsr   EACCESr8   EEXISTr   r7   r   r   r   r<   lockedclean)rA   rI   r!   r   s       r   lockzFilesystemLock.lock   s    0	,BIKK(($)4444 * * * !EL%)+D D D !555557el**&ty11"   7el22 %HHHHHHHH% )!'U\*A*A $)555555555+ S1---"   7ek11& &ty 1 1 1 1#* & & &#$7el#:#: %-HHHHHHHHHHHH %& %*E$HHHHHHHH  !55555U*V DKDJ4s   3: F&&F!1F!BF!
C1'C,<F!C,!F!+C,,C11F!5$DF!
F$F:EF
FE=.F2F!<E==FFF!FFF! F!!F&c                     t          | j                  }t          |          t          j                    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   r@   r   r%   rD   
ValueErrorr<   rH   )rA   r   s     r   unlockzFilesystemLock.unlock   sa     ty!!s88ry{{""LTYLLLMMMtyr
   )	__name__
__module____qualname____doc__rI   rH   rB   rJ   rM   r	   r
   r   r>   r>   w   sX         $ EF  ; ; ;z    r
   r>   c                     t          |           }d}	 |                                }|r|                                 n# |r|                                 w w xY w| 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)r>   rJ   rM   )r@   lr:   s      r   isLockedrT      sh     	tAF 	HHJJJ  	HHJJJJ	:s	   > 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 \<<<<<<<<<<<<HHHG((((((  "$
	G 
	G 
	G 
	G    * E  6  $  
c c c c c c c cL  ( Z
(s   
A AA