U
    
W[S                     @   s   d Z ddlZddlmZmZmZ ddlmZm	Z	 ddl
mZ G dd deZe	eG dd	 d	eZe	eG d
d deZe Z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)	Interfaceimplementer)Loggerc                   @   s@   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.
    c                   C   s   dS )aB  
        Read the process ID stored in this PID file.

        @return: The contained process ID.
        @rtype: L{int}

        @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   s    zIPIDFile.readc                   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.removec                   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.
        @rtype: L{bool}

        @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
   	isRunning4   s    zIPIDFile.isRunningc                   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__D   s    zIPIDFile.__enter__c                 C   s   dS )zS
        Exit a context using this PIDFile.

        Removes the PID file.
        Nr	   )excTypeexcValue	tracebackr	   r	   r
   __exit__O   s    zIPIDFile.__exit__N)
__name__
__module____qualname____doc__r   r   r   r   r   r   r	   r	   r	   r
   r      s   r   c                   @   sn   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} based on C{IFilePath}.

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

        @param pid: A process ID.
        @type pid: int

        @return: Formatted PID file contents.
        @rtype: L{bytes}
        z{}
zutf-8)formatintencodepidr	   r	   r
   _formate   s    zPIDFile._formatc                 C   s
   || _ dS )zi
        @param filePath: The path to the PID file on disk.
        @type filePath: L{IFilePath}
        N)filePath)selfr   r	   r	   r
   __init__s   s    zPIDFile.__init__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})
r   openOSErrorerrnoENOENT
NoPIDFoundr   
ValueErrorInvalidPIDFileErrorr   )r    Z	pidStringZfher	   r	   r
   r   {   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.
        @type pid: L{int}

        @raise EnvironmentError: If this PID file cannot be written.
        r   N)r   Z
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      s    zPIDFile.removec                 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   r(   SYSTEM_NAME_pidIsRunningPOSIXNotImplementedErrorr   r,   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 )a"  
        POSIX implementation for running process check.

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

        @return: True if the given PID is currently running; false otherwise.
        @rtype: L{bool}

        @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   r%   r&   ZESRCHStalePIDFileErrorEPERM)r   r+   r	   r	   r
   r2      s    
zPIDFile._pidIsRunningPOSIXc                 C   s@   z|   rt W n  tk
r2   | jd Y nX |   | S )Nz&Replacing stale PID file: {log_source})r   AlreadyRunningErrorr4   _loginfor   r/   r	   r	   r
   r      s    
zPIDFile.__enter__c                 C   s   |    d S r.   )r   r    r   r   r   r	   r	   r
   r      s    zPIDFile.__exit__N)r   r   r   r   r   r7   staticmethodr   r!   r   r-   r   r   r   r2   r   r   r	   r	   r	   r
   r   X   s   


r   c                   @   sP   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.
    c                 C   s   d S r.   r	   r/   r	   r	   r
   r!      s    zNonePIDFile.__init__c                 C   s   t dd S )Nr#   )r(   r/   r	   r	   r
   r      s    zNonePIDFile.readc                 C   s   t tjddS )z
        Store a PID in this PID file.

        @param pid: A PID to store.
        @type pid: L{int}

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

        @note: This implementation always raises an L{EnvironmentError}.
        zOperation not permittedN)r%   r&   r5   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)r%   r&   r'   r/   r	   r	   r
   r     s    zNonePIDFile.removec                 C   s   dS )NFr	   r/   r	   r	   r
   r     s    zNonePIDFile.isRunningc                 C   s   | S r.   r	   r/   r	   r	   r
   r   	  s    zNonePIDFile.__enter__c                 C   s   d S r.   r	   r9   r	   r	   r
   r     s    zNonePIDFile.__exit__N)r   r   r   r   r!   r   r-   r   r   r   r   r   r	   r	   r	   r
   r;      s   r;   c                   @   s   e Zd ZdZdS )r6   z%
    Process is already running.
    Nr   r   r   r   r	   r	   r	   r
   r6     s   r6   c                   @   s   e Zd ZdZdS )r*   z(
    PID file contents are invalid.
    Nr<   r	   r	   r	   r
   r*     s   r*   c                   @   s   e Zd ZdZdS )r4   z[
    PID file contents are valid, but there is no process with the referenced
    PID.
    Nr<   r	   r	   r	   r
   r4   $  s   r4   c                   @   s   e Zd ZdZdS )r(   z#
    No PID found in PID file.
    Nr<   r	   r	   r	   r
   r(   ,  s   r(   )r   r&   osr   r   r   r1   Zzope.interfacer   r   Ztwisted.loggerr   r   objectr   r;   ZnonePIDFile	Exceptionr6   r*   r4   r(   r	   r	   r	   r
   <module>   s   F 3