
    Mh%                    T   d Z ddl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	Z	ddl
Z
ddlZddlZddlmZ ddlmZ ddlmZ ddlmZ 	 ddlZn# e$ r dZY nw xY we	j        dk    rej        ZnddZdddZ G d d          Z G d de          Z ej        ej                   dS )z!JupyterLab Server process handler    )annotationsN)Logger)which)Any)genwin32cmd_list	list[str]returnstrc                Z    ddl }d                    t          |j        |                     S )zShim for list2cmdline on posix.r   N )shlexjoinmapquote)r	   r   s     Y/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/jupyterlab_server/process.pylist2cmdliner       s)    xxEK22333    commandenvdict[str, str] | Nonec                N   |pt           j        }|                    d          pt           j        }t	          | |          }| dk    r|sd} t	          d|          }|s*| dv rd}t          |          t          dd| z  z             t           j                            |          S )	zGet the full path to a command.

    Parameters
    ----------
    command: str
        The command name or path.
    env: dict, optional
        The environment variables, defaults to `os.environ`.
    PATH)pathnodenodejs)r   r   npmzPlease install Node.js and npm before continuing installation. You may be able to install Node.js from your package manager, from conda, or directly from the Node.js website (https://nodejs.org).z%The command was not found or was not zexecutable: %s.)osenvirongetdefpath_which
ValueErrorr   abspath)r   r   r   command_with_pathmsgs        r   r   r   '   s     
C776??(bjDwT222 &!2"8$777 `/// XCS//!@CTW^C^^___7??,---r   c                      e Zd ZU dZ ej                    Zded<   dZ	 	 	 	 	 d"d#dZ	d$dZ
d$dZej        d%d            Zd&dZed'd            Zd(d!ZdS ))ProcesszA wrapper for a child process.zweakref.WeakSet_procsNFcmdr
   loggerLogger | Nonecwd
str | None
kill_eventthreading.Event | Noner   r   quietboolr   Nonec                   t          |t          t          f          sd}t          |          |r%|                                rd}t          |          |p|                                 | _        d| _        |s(| j                            dt          |                     || _
        i }|rt          j        |d<    | j        d||d|| _        |pt          j                    | _        t$          j                            |            dS )	a)  Start a subprocess that can be run asynchronously.

        Parameters
        ----------
        cmd: list
            The command to run.
        logger: :class:`~logger.Logger`, optional
            The logger instance.
        cwd: string, optional
            The cwd of the process.
        env: dict, optional
            The environment for the process.
        kill_event: :class:`~threading.Event`, optional
            An event used to kill the process operation.
        quiet: bool, optional
            Whether to suppress output.
        zCommand must be given as a listzProcess aborted z> %sstdout)r.   r   N )
isinstancelisttupler$   is_setget_logr,   
_last_lineinfor   r+   
subprocessDEVNULL_create_processproc	threadingEvent_kill_eventr)   r*   add)	selfr+   r,   r.   r0   r   r2   r'   kwargss	            r   __init__zProcess.__init__H   s   4 #e}-- 	"3CS//! 	"*++-- 	"#CS//!. 	8KV\#%6%6777 	2)1F8(D(DScDDVDD	%:):):4     r   intc                r   | j         }|                                $t          j        |j        t
          j                   	 |                    d           nl# t          j	        $ rZ t          j
        dk    rt
          j        }nt
          j        }|                                t          j        |j        |           Y nw xY w| t          j        v rt          j                            |            n3# | t          j        v r t          j                            |            w w xY w|                                S )z/Terminate the process and return the exit code.Ng       @)timeoutnt)rC   pollr   killpidsignalSIGTERMwaitr@   TimeoutExpirednameSIGBREAKSIGKILLr)   r*   remove)rH   rC   sigs      r   	terminatezProcess.terminatey   s   y 99;;GDHfn---	,IIcI""""( 	' 	' 	'w$onyy{{"#&&&	' w~%%%%d+++ w~%%%%d++++ & yy{{s+   A C2 A&C>C2  CC2 20D"c                0   | j         }| j        }|                                a|                                r%|                                  d}t          |          t          j        d           |                                a|                                 S )zhWait for the process to finish.

        Returns
        -------
        The process exit code.
        NProcess was aborted      ?)rC   rF   rO   r<   r[   r$   timesleeprH   rC   r0   r'   s       r   rT   zProcess.wait   s     y%
iikk!  "" &   + oo%JsOOO iikk! ~~r   r   c              #  \  K   | j         }| j        }|                                c|                                r%|                                  d}t          |          t          j        d          V  |                                ct          j        |                                           )z.Asynchronously wait for the process to finish.Nr]   r^   )	rC   rF   rO   r<   r[   r$   r   r`   Returnra   s       r   
wait_asynczProcess.wait_async   s       y%
iikk!  "" &   + oo%)C..    iikk! j))***r   rI   subprocess.Popen[str]c                   t          | j                  }|                    dt          j                   t          |d         |                    d                    |d<   t          j        dk    rd|d<   t          j	        |fi |S )zCreate the process.stderrr   r   rN   Tshell)
r:   r+   
setdefaultr@   STDOUTr   r!   r   rV   Popen)rH   rI   r+   s      r   rB   zProcess._create_process   sx    48nn(J$5666s1vvzz%0011A7d??"F7O..v...r   clstype[Process]c                \    t          | j                  D ]}|                                 dS )z*Clean up the started subprocesses at exit.N)r:   r*   r[   )rl   rC   s     r   _cleanupzProcess._cleanup   s:     $$ 	 	DNN	 	r   r   c                    t          | d          r| j        | j        S t          j        d          | _        | j                            t          j                   | j        S )zGet our logger.r,   N
jupyterlab)hasattrr,   logging	getLoggersetLevelINFO)rH   s    r   r=   zProcess.get_log   sU    4"" 	t{'>;'55W\***{r   )NNNNF)r+   r
   r,   r-   r.   r/   r0   r1   r   r   r2   r3   r   r4   r   rK   )r   r   rI   r   r   re   )rl   rm   r   r4   )r   r   )__name__
__module____qualname____doc__weakrefWeakSetr*   __annotations___poolrJ   r[   rT   r   	coroutinerd   rB   classmethodro   r=   r8   r   r   r)   r)   B   s         ((-go//F////E
 !%-1%)/! /! /! /! /!b   4       " 	]+ + + ]+
/ 
/ 
/ 
/    [
     r   r)   c                  J     e Zd ZdZ	 	 	 	 dd fdZddZddZd fdZ xZS )WatchHelperz%A process helper for a watch process.Nr+   r
   startup_regexr   r,   r-   r.   r/   r0   r1   r   r   r   r4   c                   t                                          |||||           t          | j        j        | _        	 | j                                                            d          }|sd}t          |          t          |
                                           t          j        ||          rnwt          j        | j        d          | _        | j                                         dS )a  Initialize the process helper.

        Parameters
        ----------
        cmd: list
            The command to run.
        startup_regex: string
            The regex to wait for at startup.
        logger: :class:`~logger.Logger`, optional
            The logger instance.
        cwd: string, optional
            The cwd of the process.
        env: dict, optional
            The environment for the process.
        kill_event: callable, optional
            A function to call to check if we should abort.
        )r,   r.   r0   r   N   utf-8zProcess ended improperlyT)targetdaemon)superrJ   ptyrC   r7   _stdoutreadlinedecodeRuntimeErrorprintrstriprematchrD   Thread_read_incoming_read_threadstart)
rH   r+   r   r,   r.   r0   r   liner'   	__class__s
            r   rJ   zWatchHelper.__init__   s    4 	VQTUUU;9+DL	<((**11'::D (0"3'''$++--   xt,, 	 &,D4GPTUUU!!!!!r   rK   c                   | j         }|                                kt          j        dk    r7t          j        t          j        |j                  t          j                   n$t          j	        |j        t          j                   	 |
                                 | t          j        v rt          j                            |            n3# | t          j        v r t          j                            |            w w xY w|j        S )zTerminate the process.NrN   )rC   rO   r   rV   killpggetpgidrQ   rR   rS   rP   rT   r)   r*   rY   
returncode)rH   rC   s     r   r[   zWatchHelper.terminate   s    y99;;w$	"*TX..????&.111	,IIKKKw~%%%%d+++ w~%%%%d++++ & s   C
 
0C:c                   | j                                         }	 	 t          j        |d          }n3# t          $ r&}| j                            d|           Y d}~dS d}~ww xY w|sdS t          |                    d          d           s)z(Run in a thread to read stdout and printr   i   zRead incoming error %sNr   r6   )end)	r   filenor   readOSErrorr,   debugr   r   )rH   r   bufes       r   r   zWatchHelper._read_incoming  s    $$&&
	/gfd++   !!":A>>>  #**W%%2....
	/s   2 
A"AA"rI   r   re   c                   d|d<   t           @t          j                    \  }}|x|d<   |d<   d|d<   t          j        |d          | _        net
          j        |d<   t          j        d	k    rFt          j                    }|xj	        t
          j
        z  c_	        ||d
<   t
          j        |d<   d|d<    t                      j        di |S )z"Create the watcher helper process.r   bufsizeNrg   r7   Tstart_new_sessionrbrN   startupinfocreationflagsrh   r8   )r   openptyr   fdopenr   r@   PIPErV   STARTUPINFOdwFlagsSTARTF_USESHOWWINDOWCREATE_NEW_PROCESS_GROUPr   rB   )rH   rI   masterslaver   r   s        r   rB   zWatchHelper._create_process  s    y?KMMMFE277F8vh/*.F&'9VT22DLL)F8w$(466##z'FF##(3}%*4*M'"&w&uww&00000r   )NNNN)r+   r
   r   r   r,   r-   r.   r/   r0   r1   r   r   r   r4   rw   )r   r4   rx   )	ry   rz   r{   r|   rJ   r[   r   rB   __classcell__)r   s   @r   r   r      s        // !%-1%))" )" )" )" )" )" )"V   (/ / / /1 1 1 1 1 1 1 1 1 1r   r   )r	   r
   r   r   )N)r   r   r   r   r   r   )r|   
__future__r   atexitrs   r   r   rR   r@   sysrD   r_   r}   r   shutilr   r#   typingr   tornador   r   ImportErrorplatformr   r)   r   registerro   r8   r   r   <module>r      s   ' ' # " " " " "   				 				      



             " " " " " "            JJJJ   
CCC <7*LL4 4 4 4. . . . .6I I I I I I I IXd1 d1 d1 d1 d1' d1 d1 d1P   ! ! ! ! !s   
A AA