U
    ª€lj!  ã                   @  s  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„ deƒZeeƒG dd„ dƒƒZeeƒG dd„ dƒƒZeƒ Zd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.
é    )ÚannotationsN)ÚgetpidÚkillÚname)ÚTracebackType)ÚAnyÚOptionalÚType)Ú	InterfaceÚimplementer)ÚLogger)ÚFilePathc                   @  sj   e Zd ZdZddœdd„Zddœdd„Zddœd	d
„Zddœdd„Zddœdd„Zdddddœdd„Z	dS )ÚIPIDFilez5
    Manages a file that remembers a process ID.
    Úint©Úreturnc                   C  s   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   úG/tmp/pip-unpacked-wheel-aghyhbke/twisted/application/runner/_pidfile.pyÚread   s    zIPIDFile.readÚNonec                   C  s   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   r   ÚwriteRunningPID%   s    zIPIDFile.writeRunningPIDc                   C  s   dS )zm
        Remove this PID file.

        @raise EnvironmentError: If this PID file cannot be removed.
        Nr   r   r   r   r   Úremove,   s    zIPIDFile.removeÚboolc                   C  s   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   r   Ú	isRunning3   s    zIPIDFile.isRunningz
'IPIDFile'c                   C  s   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   r   Ú	__enter__A   s    zIPIDFile.__enter__úOptional[Type[BaseException]]úOptional[BaseException]úOptional[TracebackType]zOptional[bool]©ÚexcTypeÚexcValueÚ	tracebackr   c                 C  s   dS )zS
        Exit a context using this PIDFile.

        Removes the PID file.
        Nr   )r   r    r!   r   r   r   Ú__exit__K   s    zIPIDFile.__exit__N)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r"   r   r   r   r   r      s   
r   c                   @  s¸   e Zd ZdZeƒ Zedddœdd„ƒZddd	œd
d„Zddœdd„Z	dddœdd„Z
ddœdd„Zddœdd„Zddœdd„Zedddœdd„ƒZddœdd„Zddddd œ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}.
    r   Úbytes©Ú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   Ú_formatc   s    	zPIDFile._formatzFilePath[Any]r   )ÚfilePathr   c                 C  s
   || _ dS )zD
        @param filePath: The path to the PID file on disk.
        N)r/   )Úselfr/   r   r   r   Ú__init__n   s    zPIDFile.__init__r   c              
   C  s˜   d}z(| j  ¡ �}|D ]} q qW 5 Q R X W n8 tk
rd } z|jtjkrRtdƒ‚‚ W 5 d }~X Y nX z
t|ƒW S  tk
r’   td|›�ƒ‚Y nX d S )Nó    úPID file does not existz#non-integer PID value in PID file: )	r/   ÚopenÚOSErrorÚerrnoÚENOENTÚ
NoPIDFoundr   Ú
ValueErrorÚInvalidPIDFileError)r0   Z	pidStringÚfhÚer   r   r   r   t   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/   Z
setContentr.   ©r0   r*   r   r   r   Ú_write†   s    zPIDFile._writec                 C  s   |   tƒ ¡ d S ©N)r>   r   ©r0   r   r   r   r   �   s    zPIDFile.writeRunningPIDc                 C  s   | j  ¡  d S r?   )r/   r   r@   r   r   r   r   “   s    zPIDFile.remover   c                 C  sH   z|   ¡ }W n tk
r"   Y dS X tdkr6|  |¡S tdt› �ƒ‚d S )NFÚposixz isRunning is not implemented on )r   r8   ÚSYSTEM_NAMEÚ_pidIsRunningPOSIXÚNotImplementedErrorr=   r   r   r   r   –   s    
zPIDFile.isRunningc              
   C  sh   zt | dƒ W nP tk
r^ } z2|jtjkr6tdƒ‚n|jtjkrLW Y ¢
dS ‚ W 5 d}~X Y nX dS dS )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 processTN)r   r5   r6   ZESRCHÚStalePIDFileErrorÚEPERM)r*   r<   r   r   r   rC   ¡   s    

zPIDFile._pidIsRunningPOSIXz	'PIDFile'c                 C  s@   z|   ¡ rtƒ ‚W n  tk
r2   | j d¡ Y nX |  ¡  | S )Nz&Replacing stale PID file: {log_source})r   ÚAlreadyRunningErrorrE   Ú_logÚinfor   r@   r   r   r   r   ¾   s    
zPIDFile.__enter__r   r   r   r   c                 C  s   |   ¡  d S r?   )r   ©r0   r   r    r!   r   r   r   r"   Ç   s    zPIDFile.__exit__N)r#   r$   r%   r&   r   rH   Ústaticmethodr.   r1   r   r>   r   r   r   rC   r   r"   r   r   r   r   r'   W   s   

	r'   c                   @  sˆ   e Zd ZdZddœdd„Zddœdd„Zddd	œd
d„Zddœdd„Zddœdd„Zddœdd„Z	ddœdd„Z
dddddœ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                 C  s   d S r?   r   r@   r   r   r   r1   Ú   s    zNonePIDFile.__init__r   c                 C  s   t dƒ‚d S )Nr3   )r8   r@   r   r   r   r   Ý   s    zNonePIDFile.readr)   c                 C  s   t t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.

        @note: This implementation always raises an L{EnvironmentError}.
        zOperation not permittedN)r5   r6   rF   r=   r   r   r   r>   à   s    
zNonePIDFile._writec                 C  s   |   d¡ d S )Nr   )r>   r@   r   r   r   r   ì   s    zNonePIDFile.writeRunningPIDc                 C  s   t tjdƒ‚d S )NzNo such file or directory)r5   r6   r7   r@   r   r   r   r   ï   s    zNonePIDFile.remover   c                 C  s   dS )NFr   r@   r   r   r   r   ò   s    zNonePIDFile.isRunningz'NonePIDFile'c                 C  s   | S r?   r   r@   r   r   r   r   õ   s    zNonePIDFile.__enter__r   r   r   r   c                 C  s   d S r?   r   rJ   r   r   r   r"   ø   s    zNonePIDFile.__exit__N)r#   r$   r%   r&   r1   r   r>   r   r   r   r   r"   r   r   r   r   rL   Ñ   s   rL   ÚnonePIDFilec                   @  s   e Zd ZdZdS )rG   z%
    Process is already running.
    N©r#   r$   r%   r&   r   r   r   r   rG     s   rG   c                   @  s   e Zd ZdZdS )r:   z(
    PID file contents are invalid.
    NrN   r   r   r   r   r:   
  s   r:   c                   @  s   e Zd ZdZdS )rE   z[
    PID file contents are valid, but there is no process with the referenced
    PID.
    NrN   r   r   r   r   rE     s   rE   c                   @  s   e Zd ZdZdS )r8   z#
    No PID found in PID file.
    NrN   r   r   r   r   r8     s   r8   ) r&   Ú
__future__r   r6   Úosr   r   r   rB   Útypesr   Útypingr   r   r	   Zzope.interfacer
   r   Ztwisted.loggerr   Ztwisted.python.filepathr   r   r'   rL   rM   Ú__annotations__Ú	ExceptionrG   r:   rE   r8   r   r   r   r   Ú<module>   s$   By/