
    Yf!                       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dS )z
PID file.
    )annotationsN)getpidkillname)TracebackType)AnyOptionalType)	Interfaceimplementer)Logger)FilePathc                  B    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S )IPIDFilez5
    Manages a file that remembers a process ID.
    returnintc                     dS )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       U/var/www/html/env/lib/python3.11/site-packages/twisted/application/runner/_pidfile.pyreadzIPIDFile.read         r   Nonec                     dS )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                     dS )zm
        Remove this PID file.

        @raise EnvironmentError: If this PID file cannot be removed.
        Nr   r   r   r   removezIPIDFile.remove,   r   r   boolc                     dS )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   
'IPIDFile'c                     dS )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   excTypeOptional[Type[BaseException]]excValueOptional[BaseException]	tracebackOptional[TracebackType]Optional[bool]c                    dS )zS
        Exit a context using this PIDFile.

        Removes the PID file.
        Nr   )r$   r&   r(   s      r   __exit__zIPIDFile.__exit__K   r   r   Nr   r   r   r   r   r   )r   r!   )r$   r%   r&   r'   r(   r)   r   r*   )
__name__
__module____qualname____doc__r   r   r   r    r#   r,   r   r   r   r   r      s         	 	 	 	            	 	 	 	 	 	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dS )%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}.
    pidr   r   bytesc                J    t          |            d                                S )z
        Format a PID file's content.

        @param pid: A process ID.

        @return: Formatted PID file contents.
        
)r   encoder6   s    r   _formatzPIDFile._formatc   s"     c((%%'''r   filePathFilePath[Any]r   c                    || _         dS )zD
        @param filePath: The path to the PID file on disk.
        N)r=   )selfr=   s     r   __init__zPIDFile.__init__n   s     !r   c                D   d}	 | j                                         5 }|D ]} d d d            n# 1 swxY w Y   n7# t          $ r*}|j        t          j        k    rt          d           d }~ww xY w	 t          |          S # 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                	 	 	w%,&& !:;;;	
	y>>! 	 	 	%CiCC  	s9   ; /; 3; 3; 
A/%A**A/3B Bc                b    | j                             |                     |                     dS )z
        Store a PID in this PID file.

        @param pid: A PID to store.

        @raise EnvironmentError: If this PID file cannot be written.
        r;   N)r=   
setContentr<   r@   r6   s     r   _writezPIDFile._write   s.     	  #!6!677777r   c                H    |                      t                                 d S N)rQ   r   r@   s    r   r   zPIDFile.writeRunningPID   s    FHHr   c                8    | j                                          d S rS   )r=   r   rT   s    r   r   zPIDFile.remove   s    r   r   c                    	 |                                  }n# t          $ r Y dS w xY wt          dk    r|                     |          S t	          dt                     )NFposixz isRunning is not implemented on )r   rH   SYSTEM_NAME_pidIsRunningPOSIXNotImplementedErrorrP   s     r   r    zPIDFile.isRunning   sp    	))++CC 	 	 	55	 '!!**3///%&V&V&VWWWs    
%%c                    	 t          | d           dS # t          $ rE}|j        t          j        k    rt	          d          |j        t          j        k    rY d}~dS  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   rE   rF   ESRCHStalePIDFileErrorEPERM)r6   rM   s     r   rY   zPIDFile._pidIsRunningPOSIX   sy    "
	aLLL 4  	 	 	w%+%%'(QRRREK''ttttt	s    
A#9AAA#	'PIDFile'c                    	 |                                  rt                      n*# t          $ r | j                            d           Y nw xY w|                                  | S )Nz&Replacing stale PID file: {log_source})r    AlreadyRunningErrorr]   _loginfor   rT   s    r   r#   zPIDFile.__enter__   s|    	E~~ ,)+++,  	E 	E 	EINNCDDDDD	Es   "% $AAr$   r%   r&   r'   r(   r)   c                .    |                                   d S rS   )r   r@   r$   r&   r(   s       r   r,   zPIDFile.__exit__   s     	tr   N)r6   r   r   r7   )r=   r>   r   r   r-   r6   r   r   r   r.   r/   )r6   r   r   r   )r   r_   r$   r%   r&   r'   r(   r)   r   r   )r0   r1   r2   r3   r   rb   staticmethodr<   rA   r   rQ   r   r   r    rY   r#   r,   r   r   r   r5   r5   W   s         688D( ( ( \(! ! ! !   $8 8 8 8      	X 	X 	X 	X    \8        r   r5   c                  R    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dS )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.
    r   r   c                    d S rS   r   rT   s    r   rA   zNonePIDFile.__init__   s    r   r   c                     t          d          )NrC   )rH   rT   s    r   r   zNonePIDFile.read   s    2333r   r6   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)rE   rF   r^   rP   s     r   rQ   zNonePIDFile._write   s     ek#<===r   c                0    |                      d           d S )Nr   )rQ   rT   s    r   r   zNonePIDFile.writeRunningPID   s    Ar   c                6    t          t          j        d          )NzNo such file or directory)rE   rF   rG   rT   s    r   r   zNonePIDFile.remove   s    el$?@@@r   r   c                    dS )NFr   rT   s    r   r    zNonePIDFile.isRunning   s    ur   'NonePIDFile'c                    | S rS   r   rT   s    r   r#   zNonePIDFile.__enter__   s    r   r$   r%   r&   r'   r(   r)   c                    d S rS   r   re   s       r   r,   zNonePIDFile.__exit__   s	     tr   Nr.   r-   rf   r/   )r   rq   rg   )r0   r1   r2   r3   rA   r   rQ   r   r   r    r#   r,   r   r   r   rj   rj      s            4 4 4 4
> 
> 
> 
>   A A A A           r   rj   nonePIDFilec                      e Zd ZdZdS )ra   z%
    Process is already running.
    Nr0   r1   r2   r3   r   r   r   ra   ra                r   ra   c                      e Zd ZdZdS )rJ   z(
    PID file contents are invalid.
    Nrv   r   r   r   rJ   rJ   
  rw   r   rJ   c                      e Zd ZdZdS )r]   z[
    PID file contents are valid, but there is no process with the referenced
    PID.
    Nrv   r   r   r   r]   r]     s           r   r]   c                      e Zd ZdZdS )rH   z#
    No PID found in PID file.
    Nrv   r   r   r   rH   rH     rw   r   rH   ) r3   
__future__r   rF   osr   r   r   rX   typesr   typingr   r	   r
   zope.interfacer   r   twisted.loggerr   twisted.python.filepathr   r   r5   rj   rt   __annotations__	Exceptionra   rJ   r]   rH   r   r   r   <module>r      s9  
   # " " " " "  0 0 0 0 0 0 0 0 0 0       & & & & & & & & & & 1 1 1 1 1 1 1 1 ! ! ! ! ! ! , , , , , ,? ? ? ? ?y ? ? ?D Xv v v v v v v vr X, , , , , , , ,^ $ % % % %    )       )       	            r   