
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
U
    {1`)                     @   s  d Z ddlmZmZ ddlZddlZddlZddlZddlZddl	Z	ddl
mZ ddlmZmZmZmZ ze W n ek
r   eZY nX eZede G dd	 d	eZG d
d deeZG dd deeZG dd deeZG dd dZdddZ dd Z!dd Z"dS )z Daemon runner library.
        )absolute_importunicode_literalsN   pidfile)0_chain_exception_from_existing_exception_contextDaemonContext
basestringunicodeu@   The ‘runner’ module is not a supported API for this library.c                       s(   e Zd ZdZ fddZdd Z  ZS )DaemonRunnerErrorz3 Abstract base class for errors from DaemonRunner. c                    s   |    t j|| d S )N)_chain_from_contextsuper__init__)selfargskwargs	__class__ >/opt/alt/python38/lib/python3.8/site-packages/daemon/runner.pyr   2   s    zDaemonRunnerError.__init__c                 C   s   t | dd d S )NTas_causer   r   r   r   r   r   7   s    z%DaemonRunnerError._chain_from_context)__name__
__module____qualname____doc__r   r   __classcell__r   r   r   r   r   /   s   r   c                   @   s   e Zd ZdZdd ZdS )DaemonRunnerInvalidActionErrorz; Raised when specified action for DaemonRunner is invalid. c                 C   s   t | dd d S )NFr   r   r   r   r   r   r   >   s    z2DaemonRunnerInvalidActionError._chain_from_contextN)r   r   r   r   r   r   r   r   r   r   ;   s   r   c                   @   s   e Zd ZdZdS )DaemonRunnerStartFailureErrorz, Raised when failure starting DaemonRunner. Nr   r   r   r   r   r   r   r   r    C   s   r    c                   @   s   e Zd ZdZdS )DaemonRunnerStopFailureErrorz, Raised when failure stopping DaemonRunner. Nr!   r   r   r   r   r"   G   s   r"   c                   @   sr   e Zd ZdZdZdd Zdd Zdd Zdd
dZdd Z	dd Z
dd Zdd Ze	eedZdd Zdd Zd	S )DaemonRunnera4   Controller for a callable running in a separate background process.

        The first command-line argument is the action to take:

        * 'start': Become a daemon and call `app.run()`.
        * 'stop': Exit the daemon process specified in the PID file.
        * 'restart': Stop, then start.

        zstarted with pid {pid:d}c                 C   sN   |    || _t | _| | d| _|jdk	r@t|j|j| _| j| j_dS )a#   Set up the parameters of a new runner.

            :param app: The application instance; see below.
            :return: ``None``.

            The `app` argument must have the following attributes:

            * `stdin_path`, `stdout_path`, `stderr_path`: Filesystem paths
              to open and replace the existing `sys.stdin`, `sys.stdout`,
              `sys.stderr`.

            * `pidfile_path`: Absolute filesystem path to a file that will
              be used as the PID file for the daemon. If ``None``, no PID
              file will be used.

            * `pidfile_timeout`: Used as the default acquisition timeout
              value supplied to the runner's PID lock file.

            * `run`: Callable that will be invoked when the daemon is
              started.

            N)	
parse_argsappr   daemon_context#_open_streams_from_app_stream_pathsr   Zpidfile_pathmake_pidlockfileZpidfile_timeoutr   r%   r   r   r   r   X   s    

 zDaemonRunner.__init__c                 C   s8   t |jd| j_t |jd| j_t |jddd| j_dS )ab   Open the `daemon_context` streams from the paths specified.

            :param app: The application instance.

            Open the `daemon_context` standard streams (`stdin`,
            `stdout`, `stderr`) as stream objects of the appropriate
            types, from each of the corresponding filesystem paths
            from the `app`.
            rtzw+tr   )	bufferingN)openZ
stdin_pathr&   stdinZstdout_pathstdoutZstderr_pathstderrr)   r   r   r   r'   z   s    
  z0DaemonRunner._open_streams_from_app_stream_pathsc                 C   sH   t j|d }d}d| j }dj||d}t| t	| dS )z Emit a usage message, then exit.

            :param argv: The command-line arguments used to invoke the
                program, as a sequence of strings.
            :return: ``None``.

            r      |zusage: {progname} {usage})prognameusageN)
ospathbasenamejoinaction_funcskeysformatemit_messagesysexit)r   argvr2   Zusage_exit_codeaction_usagemessager   r   r   _usage_exit   s     zDaemonRunner._usage_exitNc                 C   sP   |dkrt j}d}t||k r(| | t|d | _| j| jkrL| | dS )a   Parse command-line arguments.

            :param argv: The command-line arguments used to invoke the
                program, as a sequence of strings.

            :return: ``None``.

            The parser expects the first argument as the program name, the
            second argument as the action to perform.

            If the parser fails to parse the arguments, emit a usage
            message and exit the program.

            Nr0   r   )r<   r>   lenrA   r
   actionr8   )r   r>   Zmin_argsr   r   r   r$      s    
zDaemonRunner.parse_argsc                 C   s|   t | jr| j  z| j  W n, tjk
rN   tdj| jd}|Y nX t	
 }| jj|d}t| | j  dS )z Open the daemon context and run the application.

            :return: ``None``.
            :raises DaemonRunnerStartFailureError: If the PID file cannot
                be locked by this process.

            z(PID file {pidfile.path!r} already lockedr   )pidN)is_pidfile_staler   
break_lockr&   r,   lockfileZAlreadyLockedr    r:   r4   getpidstart_messager;   r%   run)r   errorrD   r@   r   r   r   _start   s    


zDaemonRunner._startc              
   C   sZ   | j  }zt|tj W n8 tk
rT } ztdj||d}|W 5 d}~X Y nX dS )z Terminate the daemon process specified in the current PID file.

            :return: ``None``.
            :raises DaemonRunnerStopFailureError: If terminating the daemon
                fails with an OS error.

            z"Failed to terminate {pid:d}: {exc})rD   excN)	r   read_pidr4   killsignalSIGTERMOSErrorr"   r:   )r   rD   rM   rK   r   r   r   _terminate_daemon_process   s    
 z&DaemonRunner._terminate_daemon_processc                 C   sB   | j  s tdj| j d}|t| j r6| j   n|   dS )z Exit the daemon process specified in the current PID file.

            :return: ``None``.
            :raises DaemonRunnerStopFailureError: If the PID file is not
                already locked.

            z$PID file {pidfile.path!r} not lockedr   N)r   Z	is_lockedr"   r:   rE   rF   rS   )r   rK   r   r   r   _stop   s    

zDaemonRunner._stopc                 C   s   |    |   dS )z Stop, then start.
            N)rT   rL   r   r   r   r   _restart   s    zDaemonRunner._restart)startstopZrestartc                 C   s@   z| j | j }W n* tk
r:   tdj| jd}|Y nX |S )ae   Get the function for the specified action.

            :return: The function object corresponding to the specified
                action.
            :raises DaemonRunnerInvalidActionError: if the action is
               unknown.

            The action is specified by the `action` attribute, which is set
            during `parse_args`.

            zUnknown action: {action!r})rC   )r8   rC   KeyErrorr   r:   )r   funcrK   r   r   r   _get_action_func   s    
zDaemonRunner._get_action_funcc                 C   s   |   }||  dS )z Perform the requested action.

            :return: ``None``.

            The action is specified by the `action` attribute, which is set
            during `parse_args`.

            N)rZ   )r   rY   r   r   r   	do_action  s    	zDaemonRunner.do_action)N)r   r   r   r   rI   r   r'   rA   r$   rL   rS   rT   rU   r8   rZ   r[   r   r   r   r   r#   K   s    
"
r#   c                 C   s,   |dkrt j}|dj| d |  dS )z@ Emit a message to the specified stream (default `sys.stderr`). Nz
{message}
)r@   )r<   r/   writer:   flush)r@   streamr   r   r   r;     s    r;   c                 C   sN   t | tstdj| d}|tj| s>tdj| d}|t| |}|S )z= Make a PIDLockFile instance with the given filesystem path. zNot a filesystem path: {path!r})r5   zNot an absolute path: {path!r})	
isinstancer	   
ValueErrorr:   r4   r5   isabsr   ZTimeoutPIDLockFile)r5   Zacquire_timeoutrK   rG   r   r   r   r(   &  s    
r(   c              
   C   st   d}|   }|dk	rpzt|tj W nH tk
r>   d}Y n2 tk
rn } z|jtjkr^d}W 5 d}~X Y nX |S )u    Determine whether a PID file is stale.

        :return: ``True`` iff the PID file is stale; otherwise ``False``.

        The PID file is “stale” if its contents are valid but do not
        match the PID of a currently-running process.

        FNT)	rN   r4   rO   rP   SIG_DFLProcessLookupErrorrR   errnoZESRCH)r   resultZpidfile_pidrM   r   r   r   rE   5  s    	rE   )N)#r   
__future__r   r   rd   r4   rP   r<   warningsrG    r   daemonr   r   r	   r
   rc   	NameErrorNotImplementedtype__metaclass__warnDeprecationWarning	Exceptionr   r`   r   RuntimeErrorr    r"   r#   r;   r(   rE   r   r   r   r   <module>
   s6   
 T
