o
    ¯bô  ã                   @   sô   U d 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 ddlmZmZ ddlmZ ddlmZ G d	d
„ d
eƒZeeƒG dd„ dƒƒZeeƒG dd„ dƒƒZeƒ Zeed< G dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )z
PID file.
é    N)ÚgetpidÚkillÚname)ÚTracebackType)ÚOptionalÚType)Ú	InterfaceÚimplementer)ÚLogger)ÚFilePathc                	   @   sx   e Zd ZdZdefdd„Zddd„Zddd	„Zdefd
d„Z	ddd„Z
deee  dee dee dee fdd„ZdS )ÚIPIDFilez5
    Manages a file that remembers a process ID.
    Úreturnc                   C   ó   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   r   r   úE/usr/lib/python3/dist-packages/twisted/application/runner/_pidfile.pyÚread   ó    zIPIDFile.readNc                   C   r   )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   r   ÚwriteRunningPID$   r   zIPIDFile.writeRunningPIDc                   C   r   )zm
        Remove this PID file.

        @raise EnvironmentError: If this PID file cannot be removed.
        Nr   r   r   r   r   Úremove+   r   zIPIDFile.removec                   C   r   )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   r   Ú	isRunning2   r   zIPIDFile.isRunningc                   C   r   )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   r   Ú	__enter__@   r   zIPIDFile.__enter__ÚexcTypeÚexcValueÚ	tracebackc                 C   r   )zS
        Exit a context using this PIDFile.

        Removes the PID file.
        Nr   )r   r   r   r   r   r   Ú__exit__J   r   zIPIDFile.__exit__©r   N)r   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úintr   r   r   Úboolr   r   r   r   ÚBaseExceptionr   r   r   r   r   r   r      s     




ÿþýür   c                   @   sÊ   e Zd ZdZeƒ Zededefdd„ƒZ	de
ddfdd	„Zdefd
d„Zdeddfdd„Zddd„Zddd„Zdefdd„Zededefdd„ƒZddd„Zdeee  dee dee ddf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   c                 C   s   t | ƒ› d ¡ S )z€
        Format a PID file's content.

        @param pid: A process ID.

        @return: Formatted PID file contents.
        Ú
)r    Úencode©r$   r   r   r   Ú_formatb   s   	zPIDFile._formatÚfilePathNc                 C   s
   || _ dS )zD
        @param filePath: The path to the PID file on disk.
        N)r)   )Úselfr)   r   r   r   Ú__init__m   s   
zPIDFile.__init__c              
   C   s”   d}z| j  ¡ }|D ]} W d   ƒ n1 sw   Y  W n ty5 } z|jtjkr0tdƒ‚‚ d }~ww zt|ƒW S  tyI   td|›ƒ‚w )Nó    úPID file does not existz#non-integer PID value in PID file: )	r)   ÚopenÚOSErrorÚerrnoÚENOENTÚ
NoPIDFoundr    Ú
ValueErrorÚInvalidPIDFileError)r*   Ú	pidStringÚfhÚer   r   r   r   s   s*   €þ€€ý
ÿÿzPIDFile.readc                 C   s   | j  | j|d¡ 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*   r$   r   r   r   Ú_write…   s   zPIDFile._writec                 C   s   |   tƒ ¡ d S ©N)r:   r   ©r*   r   r   r   r      s   zPIDFile.writeRunningPIDc                 C   s   | j  ¡  d S r;   )r)   r   r<   r   r   r   r   ’   ó   zPIDFile.removec                 C   sB   z|   ¡ }W n
 ty   Y dS w tdkr|  |¡S tdt› ƒ‚)NFÚposixz isRunning is not implemented on )r   r2   ÚSYSTEM_NAMEÚ_pidIsRunningPOSIXÚNotImplementedErrorr9   r   r   r   r   •   s   ÿ
zPIDFile.isRunningc              
   C   sZ   zt | dƒ W dS  ty, } z|jtjkrtdƒ‚|jtjkr'W Y d}~dS ‚ d}~w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   z'PID file refers to non-existing processNT)r   r/   r0   ÚESRCHÚStalePIDFileErrorÚEPERM)r$   r7   r   r   r   r@       s   	ø€úzPIDFile._pidIsRunningPOSIXc                 C   s>   z	|   ¡ rtƒ ‚W n ty   | j d¡ Y nw |  ¡  | S )Nz&Replacing stale PID file: {log_source})r   ÚAlreadyRunningErrorrC   Ú_logÚinfor   r<   r   r   r   r   ½   s   ÿÿzPIDFile.__enter__r   r   r   c                 C   s   |   ¡  d S r;   )r   ©r*   r   r   r   r   r   r   r   Æ   s   zPIDFile.__exit__r   )r   r#   )r   r   r   r   r
   rF   Ústaticmethodr    Úbytesr(   r   r+   r   r:   r   r   r!   r   r@   r   r   r   r"   r   r   r   r   r   r   r#   V   s.    




	
þýüûr#   c                   @   s   e Zd ZdZddd„Zdefdd„Zdeddfd	d
„Zddd„Zddd„Z	de
fdd„Zddd„Zdeee  dee dee ddf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   Nc                 C   ó   d S r;   r   r<   r   r   r   r+   Ù   ó   zNonePIDFile.__init__c                 C   s   t dƒ‚)Nr-   )r2   r<   r   r   r   r   Ü   s   zNonePIDFile.readr$   c                 C   ó   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)r/   r0   rD   r9   r   r   r   r:   ß   s   
zNonePIDFile._writec                 C   s   |   d¡ d S )Nr   )r:   r<   r   r   r   r   ë   r=   zNonePIDFile.writeRunningPIDc                 C   rN   )NzNo such file or directory)r/   r0   r1   r<   r   r   r   r   î   s   zNonePIDFile.removec                 C   r   )NFr   r<   r   r   r   r   ñ   rM   zNonePIDFile.isRunningc                 C   s   | S r;   r   r<   r   r   r   r   ô   rM   zNonePIDFile.__enter__r   r   r   c                 C   rL   r;   r   rH   r   r   r   r   ÷   s   zNonePIDFile.__exit__r   )r   rK   )r   r   r   r   r+   r    r   r:   r   r   r!   r   r   r   r   r"   r   r   r   r   r   r   rK   Ð   s$    




þýüûrK   ÚnonePIDFilec                   @   ó   e Zd ZdZdS )rE   z%
    Process is already running.
    N©r   r   r   r   r   r   r   r   rE     ó    rE   c                   @   rP   )r4   z(
    PID file contents are invalid.
    NrQ   r   r   r   r   r4   	  rR   r4   c                   @   rP   )rC   z[
    PID file contents are valid, but there is no process with the referenced
    PID.
    NrQ   r   r   r   r   rC     rR   rC   c                   @   rP   )r2   z#
    No PID found in PID file.
    NrQ   r   r   r   r   r2     rR   r2   )r   r0   Úosr   r   r   r?   Útypesr   Útypingr   r   Úzope.interfacer   r	   Útwisted.loggerr
   Útwisted.python.filepathr   r   r#   rK   rO   Ú__annotations__Ú	ExceptionrE   r4   rC   r2   r   r   r   r   Ú<module>   s$   By/