
    Vh!                    T   U d Z ddlmZ ddlZddlmZmZmZ ddl	m
Z
 ddlmZmZmZ ddlmZmZ ddlmZ dd	lmZ  G d
 de      Z ee       G d d             Z ee       G d d             Z e       Zded<    G d de      Z G d de      Z G d de      Z G d de      Zy)z
PID file.
    )annotationsN)getpidkillname)TracebackType)AnyOptionalType)	Interfaceimplementer)Logger)FilePathc                  P    e Zd ZdZd	dZd
dZd
dZddZddZ	 	 	 	 	 	 	 	 ddZ	y)IPIDFilez5
    Manages a file that remembers a process ID.
    c                      y)a+  
        Read the process ID stored in this PID file.

        @return: The contained process ID.

        @raise NoPIDFound: If this PID file does not exist.
        @raise EnvironmentError: If this PID file cannot be read.
        @raise ValueError: If this PID file's content is invalid.
        N r       S/home/dcms/DCMS/lib/python3.12/site-packages/twisted/application/runner/_pidfile.pyreadzIPIDFile.read       r   c                      y)z
        Store the PID of the current process in this PID file.

        @raise EnvironmentError: If this PID file cannot be written.
        Nr   r   r   r   writeRunningPIDzIPIDFile.writeRunningPID%   r   r   c                      y)zm
        Remove this PID file.

        @raise EnvironmentError: If this PID file cannot be removed.
        Nr   r   r   r   removezIPIDFile.remove,   r   r   c                      y)a  
        Determine whether there is a running process corresponding to the PID
        in this PID file.

        @return: True if this PID file contains a PID and a process with that
            PID is currently running; false otherwise.

        @raise EnvironmentError: If this PID file cannot be read.
        @raise InvalidPIDFileError: If this PID file's content is invalid.
        @raise StalePIDFileError: If this PID file's content refers to a PID
            for which there is no corresponding running process.
        Nr   r   r   r   	isRunningzIPIDFile.isRunning3   r   r   c                      y)z
        Enter a context using this PIDFile.

        Writes the PID file with the PID of the running process.

        @raise AlreadyRunningError: A process corresponding to the PID in this
            PID file is already running.
        Nr   r   r   r   	__enter__zIPIDFile.__enter__A   r   r   c                     y)zS
        Exit a context using this PIDFile.

        Removes the PID file.
        Nr   )excTypeexcValue	tracebacks      r   __exit__zIPIDFile.__exit__K   r   r   Nreturnintr%   Noner%   bool)r%   z
'IPIDFile')r    Optional[Type[BaseException]]r!   Optional[BaseException]r"   Optional[TracebackType]r%   zOptional[bool])
__name__
__module____qualname____doc__r   r   r   r   r   r#   r   r   r   r   r      sI    		.	)	 +	 
		r   r   c                      e Zd ZdZ e       Zedd       ZddZddZ	ddZ
ddZddZddZedd	       Zdd
Z	 	 	 	 	 	 	 	 ddZy)PIDFilez
    Concrete implementation of L{IPIDFile}.

    This implementation is presently not supported on non-POSIX platforms.
    Specifically, calling L{PIDFile.isRunning} will raise
    L{NotImplementedError}.
    c                :    t        |        dj                         S )z
        Format a PID file's content.

        @param pid: A process ID.

        @return: Formatted PID file contents.
        
)r&   encodepids    r   _formatzPIDFile._formatc   s     c(2%%''r   c                    || _         y)zD
        @param filePath: The path to the PID file on disk.
        N)filePath)selfr;   s     r   __init__zPIDFile.__init__n   s     !r   c                8   d}	 | j                   j                         5 }|D ]  } n d d d        	 t        |      S # 1 sw Y   xY w# t        $ r.}|j                  t        j                  k(  rt        d       d }~ww xY w# t        $ r t        d|      w xY w)Nr   PID file does not existz#non-integer PID value in PID file: )	r;   openOSErrorerrnoENOENT
NoPIDFoundr&   
ValueErrorInvalidPIDFileError)r<   	pidStringfhes       r   r   zPIDFile.readt   s    		##% !# I	y>!   	ww%,,& !:;;	  	%5i]C 	s6   A 	;A 
B A A 	A>)A99A>Bc                Z    | j                   j                  | j                  |             y)z
        Store a PID in this PID file.

        @param pid: A PID to store.

        @raise EnvironmentError: If this PID file cannot be written.
        r7   N)r;   
setContentr9   r<   r8   s     r   _writezPIDFile._write   s!     	  #!67r   c                6    | j                  t                      y N)rM   r   r<   s    r   r   zPIDFile.writeRunningPID   s    FHr   c                8    | j                   j                          y rO   )r;   r   rP   s    r   r   zPIDFile.remove   s    r   c                    	 | j                         }t        dk(  r| j                  |      S t	        dt               # t        $ r Y yw xY w)NFposixz isRunning is not implemented on )r   rD   SYSTEM_NAME_pidIsRunningPOSIXNotImplementedErrorrL   s     r   r   zPIDFile.isRunning   sT    	))+C '!**3//%(H&VWW  		s   > 	A
	A
c                    	 t        | d       y# t        $ rP}|j                  t        j                  k(  rt	        d      |j                  t        j
                  k(  rY d}~y d}~ww xY w)a1  
        POSIX implementation for running process check.

        Determine whether there is a running process corresponding to the given
        PID.

        @param pid: The PID to check.

        @return: True if the given PID is currently running; false otherwise.

        @raise EnvironmentError: If this PID file cannot be read.
        @raise InvalidPIDFileError: If this PID file's content is invalid.
        @raise StalePIDFileError: If this PID file's content refers to a PID
            for which there is no corresponding running process.
        r   Tz'PID file refers to non-existing processN)r   rA   rB   ESRCHStalePIDFileErrorEPERM)r8   rI   s     r   rU   zPIDFile._pidIsRunningPOSIX   sY    "
	aL   	ww%++%'(QRREKK'	s    	A(AA#"A##A(c                    	 | j                         r
t               	 | j                          | S # t        $ r | j                  j	                  d       Y 8w xY w)Nz&Replacing stale PID file: {log_source})r   AlreadyRunningErrorrY   _loginfor   rP   s    r   r   zPIDFile.__enter__   sW    	E~~)++   	 ! 	EIINNCD	Es   / $AAc                $    | j                          y rO   )r   r<   r    r!   r"   s       r   r#   zPIDFile.__exit__   s     	r   N)r8   r&   r%   bytes)r;   zFilePath[Any]r%   r(   r$   r8   r&   r%   r(   r'   r)   )r8   r&   r%   r*   )r%   z	'PIDFile'r    r+   r!   r,   r"   r-   r%   r(   )r.   r/   r0   r1   r   r]   staticmethodr9   r=   r   rM   r   r   r   rU   r   r#   r   r   r   r3   r3   W   s     8D( (!$8	X  8. * +	
 
r   r3   c                  `    e Zd ZdZddZddZddZddZddZddZ	ddZ
	 	 	 	 	 	 	 	 dd	Zy
)NonePIDFilez
    PID file implementation that does nothing.

    This is meant to be used as a "active None" object in place of a PID file
    when no PID file is desired.
    c                     y rO   r   rP   s    r   r=   zNonePIDFile.__init__   s    r   c                    t        d      )Nr?   )rD   rP   s    r   r   zNonePIDFile.read   s    233r   c                6    t        t        j                  d      )z
        Store a PID in this PID file.

        @param pid: A PID to store.

        @raise EnvironmentError: If this PID file cannot be written.

        @note: This implementation always raises an L{EnvironmentError}.
        zOperation not permitted)rA   rB   rZ   rL   s     r   rM   zNonePIDFile._write   s     ekk#<==r   c                &    | j                  d       y )Nr   )rM   rP   s    r   r   zNonePIDFile.writeRunningPID   s    Ar   c                6    t        t        j                  d      )NzNo such file or directory)rA   rB   rC   rP   s    r   r   zNonePIDFile.remove   s    ell$?@@r   c                     y)NFr   rP   s    r   r   zNonePIDFile.isRunning   s    r   c                    | S rO   r   rP   s    r   r   zNonePIDFile.__enter__   s    r   c                     y rO   r   r`   s       r   r#   zNonePIDFile.__exit__   s     r   Nr'   r$   rb   r)   )r%   z'NonePIDFile'rc   )r.   r/   r0   r1   r=   r   rM   r   r   r   r   r#   r   r   r   rf   rf      sV    4
>A. * +	
 
r   rf   nonePIDFilec                      e Zd ZdZy)r\   z%
    Process is already running.
    Nr.   r/   r0   r1   r   r   r   r\   r\         r   r\   c                      e Zd ZdZy)rF   z(
    PID file contents are invalid.
    Nrq   r   r   r   rF   rF   
  rr   r   rF   c                      e Zd ZdZy)rY   z[
    PID file contents are valid, but there is no process with the referenced
    PID.
    Nrq   r   r   r   rY   rY     s    r   rY   c                      e Zd ZdZy)rD   z#
    No PID found in PID file.
    Nrq   r   r   r   rD   rD     rr   r   rD   ) r1   
__future__r   rB   osr   r   r   rT   typesr   typingr   r	   r
   zope.interfacer   r   twisted.loggerr   twisted.python.filepathr   r   r3   rf   ro   __annotations__	Exceptionr\   rF   rY   rD   r   r   r   <module>r      s   
 #  0 0  & & 1 ! ,?y ?D Xv v vr X, , ,^ $X %) ) 	  r   