
Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­Â�Â­
<!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
