kf)ZdZddlmZmZddlZddlZddlZddlZddlZddl Z ddl m Z ddl m Z mZmZmZ en #e$reZYnwxYweZejdeGdd eZGd d eeZGd d eeZGddeeZGddZddZ dZ!dZ"dS)z Daemon runner library. )absolute_importunicode_literalsNpidfile)0_chain_exception_from_existing_exception_context DaemonContext basestringunicodeu@The ‘runner’ module is not a supported API for this library.c(eZdZdZfdZdZxZS)DaemonRunnerErrorz3 Abstract base class for errors from DaemonRunner. cb|tj|i|dSN)_chain_from_contextsuper__init__)selfargskwargs __class__s m/builddir/build/BUILD/imunify360-venv-2.3.5/opt/imunify360/venv/lib/python3.11/site-packages/daemon/runner.pyrzDaemonRunnerError.__init__2s7   """$)&)))))c(t|ddS)NTas_causerrs rrz%DaemonRunnerError._chain_from_context7s8MMMMMMr)__name__ __module__ __qualname____doc__rr __classcell__)rs@rr r /sT==***** NNNNNNNrr ceZdZdZdZdS)DaemonRunnerInvalidActionErrorz; Raised when specified action for DaemonRunner is invalid. c(t|ddS)NFrrrs rrz2DaemonRunnerInvalidActionError._chain_from_context>s8NNNNNNrN)rrr r!rrrr$r$;s.EEOOOOOrr$ceZdZdZdS)DaemonRunnerStartFailureErrorz, Raised when failure starting DaemonRunner. Nrrr r!r&rrr(r(C6666rr(ceZdZdZdS)DaemonRunnerStopFailureErrorz, Raised when failure stopping DaemonRunner. Nr)r&rrr,r,Gr*rr,c`eZdZdZdZdZdZdZddZdZ d Z d Z d Z e e e d Z d ZdZdS) 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. zstarted with pid {pid:d}c|||_t|_||d|_|jt|j|j|_|j|j_dS)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_argsappr daemon_context#_open_streams_from_app_stream_pathsr pidfile_pathmake_pidlockfilepidfile_timeoutrr1s rrzDaemonRunner.__init__Xsz. +oo 00555   '+$c&9;;DL&*l###rct|jd|j_t|jd|j_t|jdd|j_dS)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`. rtzw+tr) bufferingN)open stdin_pathr2stdin stdout_pathstdout stderr_pathstderrr7s rr3z0DaemonRunner._open_streams_from_app_stream_pathszs[%)$>$>!%)#/5%A%A"%)!&5&5&5"""rc tj|d}d}d|j}d||}t|tj |dS)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|zusage: {progname} {usage})prognameusageN) ospathbasenamejoin action_funcskeysformat emit_messagesysexit)rargvrEusage_exit_code action_usagemessages r _usage_exitzDaemonRunner._usage_exits7##DG,,xx 1 6 6 8 899 -44!577W !!!!!rNc| tj}d}t||kr||t |d|_|j|jvr||dSdS)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. NrCr)rOrQlenrUr actionrK)rrQmin_argss rr0zDaemonRunner.parse_argss <8D t99x     T " " "d1g&& ;d/ / /   T " " " " " 0 /rct|jr|j |jn=#t j$r+td|j}|wxYwtj }|j |}t||j dS)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_lockr2r;lockfile AlreadyLockedr(rMrGgetpid start_messagerNr1run)rerrorr[rTs r_startzDaemonRunner._starts DL ) ) & L # # % % %    $ $ & & & &%   1>EE $ F..//EK   ikk$+++44W  s A :Bc|j} tj|tjdS#t $r+}td||}|d}~wwxYw)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})r[excN) rread_pidrGkillsignalSIGTERMOSErrorr,rM)rr[rfrcs r_terminate_daemon_processz&DaemonRunner._terminate_daemon_processsl##%%  GC ( ( ( ( (   08??S@**++EK  s< A1&A,,A1c|js*td|j}|t |jr|jdS|dS)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 lockedrN)r is_lockedr,rMr\r]rl)rrcs r_stopzDaemonRunner._stops|%%'' 0:AA $ B..//EK DL ) ) - L # # % % % % %  * * , , , , ,rcV||dS)z Stop, then start. N)rordrs r_restartzDaemonRunner._restarts"  r)startstoprestartc |j|j}n8#t$r+td|j}|wxYw|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`. zUnknown action: {action!r})rX)rKrXKeyErrorr$rM)rfuncrcs r_get_action_funczDaemonRunner._get_action_funcsm $T[1DD   2077#{8,,--EK    s 5A cD|}||dS)z Perform the requested action. :return: ``None``. The action is specified by the `action` attribute, which is set during `parse_args`. N)rx)rrws r do_actionzDaemonRunner.do_actions&$$&& T rr)rrr r!rarr3rUr0rdrlrorqrKrxrzr&rrr.r.Ks  /M 3 3 3D 5 5 5""" ####42"---&L *     rr.c| tj}|d||dS)z@ Emit a message to the specified stream (default `sys.stderr`). Nz {message} )rT)rOrAwriterMflush)rTstreams rrNrNsB ~ LL%%g%66777 LLNNNNNrc,t|ts%td|}|tj|s%td|}|tj||}|S)z= Make a PIDLockFile instance with the given filesystem path. zNot a filesystem path: {path!r})rHzNot an absolute path: {path!r}) isinstancer ValueErrorrMrGrHisabsrTimeoutPIDLockFile)rHacquire_timeoutrcr^s rr5r5&s dJ ' '<CCD 7==  ;BBC )$@@H Orcd}|j}|\ tj|tjn;#t $rd}Yn-t $r!}|jtjkrd}Yd}~nd}~wwxYw|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) rgrGrhriSIG_DFLProcessLookupErrorrkerrnoESRCH)rresult pidfile_pidrfs rr\r\5sF"'"$$K  GK 0 0 0 0!   FFF   yEK''   Ms5 A- A- A((A-r)#r! __future__rrrrGrirOwarningsr^rdaemonrr r r r NameErrorNotImplementedtype __metaclass__warnDeprecationWarning Exceptionr rr$ RuntimeErrorr(r,r.rNr5r\r&rrrs5;::::::: (((('(  J N N N N N N N NOOOOO%6 OOO77777$5|77777777#4l777PPPPPPPPf   s9AA