
    G@d|                      
   d Z e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 ZddlmZ ej        dk    sdd	lmZ dd
lmZ ddlmZ ddlmZ dZn!dZddlZddlmZ d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dS )a  
Filesystem-based interprocess mutex.

Taken from the Twisted project.
Distributed under the MIT (Expat) license.

Changes by the Spyder Team to the original module:
  * Rewrite kill Windows function to make it more reliable.
  * Detect if the process that owns the lock is an Spyder one.

Adapted from src/twisted/python/lockfile.py of the
`Twisted project <https://github.com/twisted/twisted>`_.
    N)time)to_binary_string)is_spyder_processc                  X    t          t          t                      dz                      S )Ni  )strint_uniquefloat     >lib/python3.11/site-packages/spyder/utils/external/lockfile.pyuniquer   !   s!    s<>>D())***r   )renament)kill)symlink)readlink)removeFT)wintypesi   i  c                 <   t           j        j        }|                    t          d|           }|dk    rdS t          j                    }|                    |t          j        |                    }|dk    }|	                    |           |p|j
        t          k    S )z/Taken from https://www.madebuild.org/blog/?p=30r   F)ctypeswindllkernel32OpenProcessPROCESS_QUERY_INFORMATIONr   DWORDGetExitCodeProcessbyrefCloseHandlevalueSTILL_ACTIVE)pidr   handle	exit_coderetval
is_runnings         r   _is_pid_runningr&   8   s    =)%%&?CHHQ;;5 N$$	,,V-3\)-D-DF Fk
V$$$ <Y_<<r   c                 X    t          |           st          t          j        d           d S N)r&   OSErrorerrnoESRCH)r!   signals     r   r   r   K   s)    s## 	%+t,,,Fr   c                    |dz   t                      z   dz   }t          j                            |d          }t          j        |           t          |d          }|                    t          |                      |                                 |	                                 	 t          ||           d S #  	 t          j        |           t          j        |           n# t          t          f$ r Y Y d S w xY w xY w)N.z.newlinkr   wb)r   ospathjoinmkdir_openwriter   flushcloser   r   rmdirIOErrorr)   )r   filenamenewlinkname
newvalnamefs        r   r   r   T   s    sl688+J6W\\+y99

*d##	 ''(((									;)))))			*%%%%%%%W%   s0   'B9 9C?<(C%$C?%C;6C?:C;;C?c                 |   	 t          t          j                            | d          d          }|                                                                }|                                 |S # t          $ rE}|j        t          j	        k    s|j        t          j
        k    rt          |j        d            d }~ww xY w)Nr   rb)r4   r0   r1   r2   readdecoder7   r9   r*   ENOENTEIOr)   )r:   fObjresultes       r   r   r   i   s    		h	::DAAD YY[[''))FJJLLLM  	 	 	w%,&&!'UY*>*>agt,,,	s   .A, ,
B;6A B66B;c                     t          j        t           j                            | d                     t          j        |            d S )Nr   )r0   r   r1   r2   r8   )r:   s    r   rmlinkrH   u   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 r(   )name)selfrL   s     r   __init__zFilesystemLock.__init__   s    			r   c                    d}	 	 t          t          t          j                              | j                   n# t
          $ r}t          r%|j        t          j        t          j	        fv rY d}~dS |j        t          j
        k    r]	 t          | j                  }ne# t
          $ r$}|j        t          j        k    r	Y d}~Y d}~ d}~wt          $ r,}t          r|j        t          j        k    r
Y d}~Y d}~dS  d}~ww xY w	 t          t          t          |          d           t!          t          |                    st          t          j        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}~ 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: Any exception os.symlink() may raise, other than
        EEXIST.
        TNFr   zNo such process)r   r   r0   getpidrL   r)   _windowsr*   EACCESrC   EEXISTr   rB   r9   r   r   r   r+   rH   lockedclean)rM   rU   rF   r!   s       r   lockzFilesystemLock.lock   s]    4	0BIKK(($)4444 . . . !EL%)+D D D !555557el**&ty11"   7el22 %HHHHHHHH"   # )5<(?(? $)555555555+ S1---0S:: K")%+7H"I"IJK"   7ek11& &ty 1 1 1 1#* & & &#$7el#:#: %-HHHHHHHHHHHH %& %*E$HHHHHHHH  !55555].^ DKDJ4s   3: G*&G%1G%BG%
C?'C<G%CC?C:/G%9C::C??G%AEG%
G(G>FG
GG2G6G% GGGG%GGG%$G%%G*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: Any exception os.readlink() may raise, or
        ValueError if the lock is not owned by this process.
        zLock z not owned by this processFN)r   rL   r   r0   rP   
ValueErrorrH   rT   )rM   r!   s     r   unlockzFilesystemLock.unlock   s^     ty!!s88ry{{""*DIIIOPPPtyr   )	__name__
__module____qualname____doc__rU   rT   rN   rV   rY   r
   r   r   rJ   rJ   {   sX         $ EF  ? ? ?B    r   rJ   c                     t          |           }d}	 |                                }|r|                                 n# |r|                                 w w xY w| S )zDetermine 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)rJ   rV   rY   )rL   lrE   s      r   isLockedr`      sh     	tAF 	HHJJJ  	HHJJJJ	:s	   > Ar`   )r]   type__metaclass__r*   r0   r   r	   spyder.py3compatr   spyder.utils.programsr   r   r   rL   r   r   r   r   rH   rQ   r   r   r   r    r&   openr4   rJ   r`   __all__r
   r   r   <module>rg      s             % % % % % % - - - - - - 3 3 3 3 3 3+ + +      	w$######HHHMMM !& L= = =&   E  *
 
 
  g g g g g g g gT  & Z
(r   