
    Xh%                    d   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e	j.                  dk(  rej0                  Zndd	Zddd
Z G d d      Z G d de      Z ej6                  ej8                         y# e$ r dZY `w xY w)z!JupyterLab Server process handler    )annotationsN)Logger)which)Any)genwin32c                T    ddl }dj                  t        |j                  |             S )zShim for list2cmdline on posix.r   N )shlexjoinmapquote)cmd_listr   s     M/var/www/html/myenv/lib/python3.12/site-packages/jupyterlab_server/process.pylist2cmdliner       s    xxEKK233    c                <   |xs t         j                  }|j                  d      xs t         j                  }t	        | |      }| dk(  r|sd} t	        d|      }|s"| dv rd}t        |      t        dd| z  z         t         j                  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)commandenvr   command_with_pathmsgs        r   r   r   '   s     
C776?(bjjDwT2 &!2"8$7// XCS/!@CTW^C^^__77??,--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y)ProcesszA wrapper for a child process.zweakref.WeakSet_procsNc                   t        |t        t        f      sd}t        |      |r|j	                         rd}t        |      |xs | j                         | _        d| _        |s%| j                  j                  dt        |             || _
        i }|rt        j                  |d<    | j                  d||d|| _        |xs t        j                          | _        t$        j&                  j)                  |        y)	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)cwdr!   N )
isinstancelisttupler   is_setget_loglogger
_last_lineinfor   cmd
subprocessDEVNULL_create_processproc	threadingEvent_kill_eventr%   r&   add)	selfr4   r1   r*   
kill_eventr!   quietr#   kwargss	            r   __init__zProcess.__init__H   s    4 #e}-3CS/!*++-#CS/!.KKV\#%67)11F8(D((DScDVD	%:):4 r   c                   | j                   }|j                         .t        j                  |j                  t
        j                         	 |j                  d       | t        j                  v rt        j                  j                  |        |j                         S # t        j                  $ rg t        j                  dk(  rt
        j                  }nt
        j                  }|j                          t        j                  |j                  |       Y w xY w# | t        j                  v r t        j                  j                  |        w w xY w)z/Terminate the process and return the exit code.g       @)timeoutnt)r8   pollr   killpidsignalSIGTERMwaitr5   TimeoutExpirednameSIGBREAKSIGKILLr%   r&   remove)r=   r8   sigs      r   	terminatezProcess.terminatey   s    yy 99;GGDHHfnn-	,IIcI" w~~%%%d+yy{ (( 	'ww$oonnyy{"#&	' w~~%%%d+ &s%   B A7DD DD 4Ec                   | j                   }| j                  }|j                         S|j                         r| j	                          d}t        |      t        j                  d       |j                         S| j	                         S )zhWait for the process to finish.

        Returns
        -------
        The process exit code.
        Process was aborted      ?)r8   r;   rE   r/   rQ   r   timesleepr=   r8   r>   r#   s       r   rJ   zProcess.wait   sq     yy%%
iik!  " + o%JJsO iik! ~~r   c              #  J  K   | j                   }| j                  }|j                         U|j                         r| j	                          d}t        |      t        j                  d       |j                         Ut        j                  | j	                               w)z.Asynchronously wait for the process to finish.rS   rT   )	r8   r;   rE   r/   rQ   r   r   rV   ReturnrW   s       r   
wait_asynczProcess.wait_async   s      yy%%
iik!  " + o%))C.  iik! jj)**s   A=B# #B#c                
   t        | j                        }|j                  dt        j                         t        |d   |j                  d            |d<   t        j                  dk(  rd|d<   t        j                  |fi |S )zCreate the process.stderrr   r!   rD   Tshell)
r-   r4   
setdefaultr5   STDOUTr   r   r   rL   Popen)r=   r@   r4   s      r   r7   zProcess._create_process   sl    488n(J$5$56s1vvzz%01A77d?"F7O.v..r   c                X    t        | j                        D ]  }|j                           y)z*Clean up the started subprocesses at exit.N)r-   r&   rQ   )clsr8   s     r   _cleanupzProcess._cleanup   s&     $ 	DNN	r   c                    t        | d      r| j                  | j                  S t        j                  d      | _        | j                  j	                  t        j
                         | j                  S )zGet our logger.r1   
jupyterlab)hasattrr1   logging	getLoggersetLevelINFO)r=   s    r   r0   zProcess.get_log   sQ    4"t{{'>;;''5W\\*{{r   )NNNNF)r4   	list[str]r1   Logger | Noner*   
str | Noner>   threading.Event | Noner!   dict[str, str] | Noner?   boolreturnNonerq   int)rq   r   r@   r   rq   zsubprocess.Popen[str])rb   ztype[Process]rq   rr   )rq   r   )__name__
__module____qualname____doc__weakrefWeakSetr&   __annotations___poolrA   rQ   rJ   r   	coroutinerZ   r7   classmethodrc   r0   r+   r   r   r%   r%   B   s    (-goo/FO/E
 !%-1%)/!/! /! 	/!
 +/! #/! /! 
/!b4 " 	]]+ +
/  
r   r%   c                  d     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.c                   t         	|   |||||       t        | j                  j                  | _        	 | j
                  j                         j                  d      }|sd}t        |      t        |j                                t        j                  ||      rnit        j                  | j                  d      | _        | j                   j#                          y)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.
        )r1   r*   r>   r!   Nutf-8zProcess ended improperlyT)targetdaemon)superrA   ptyr8   r)   _stdoutreadlinedecodeRuntimeErrorprintrstriprematchr9   Thread_read_incoming_read_threadstart)
r=   r4   startup_regexr1   r*   r>   r!   liner#   	__class__s
            r   rA   zWatchHelper.__init__   s    4 	VQTU;99++DL<<((*11':D0"3''$++- xxt,  &,,D4G4GPTU!r   c                J   | j                   }|j                         t        j                  dk7  rBt        j                  t        j
                  |j                        t        j                         n.t        j                  |j                  t        j                         	 |j                          | t        j                  v rt        j                  j                  |        |j                  S # | t        j                  v r t        j                  j                  |        w w xY w)zTerminate the process.rD   )r8   rE   r   rL   killpggetpgidrG   rH   rI   rF   rJ   r%   r&   rO   
returncode)r=   r8   s     r   rQ   zWatchHelper.terminate   s    yy99;ww$		"**TXX.?&..1	,IIKw~~%%%d+ w~~%%%d+ &s   !C. .4D"c                
   | j                   j                         }	 	 t        j                  |d      }|syt        |j                  d      d       7# t        $ r&}| j
                  j                  d|       Y d}~yd}~ww xY w)z(Run in a thread to read stdout and printi   zRead incoming error %sNr   r(   )end)	r   filenor   readOSErrorr1   debugr   r   )r=   r   bufes       r   r   zWatchHelper._read_incoming  sv    $$&ggfd+
 #**W%2.   !!":A>s   A 	BA==Bc                   d|d<   t         Bt        j                         \  }}|x|d<   |d<   d|d<   t        j                  |d      | _        nzt
        j                  |d<   t        j                  dk(  rTt        j                         }|xj                  t
        j                  z  c_	        ||d	<   t
        j                  |d
<   d|d<   t        | 4  di |S )z"Create the watcher helper process.r   bufsizer\   r)   Tstart_new_sessionrbrD   startupinfocreationflagsr]   r+   )r   openptyr   fdopenr   r5   PIPErL   STARTUPINFOdwFlagsSTARTF_USESHOWWINDOWCREATE_NEW_PROCESS_GROUPr   r7   )r=   r@   masterslaver   r   s        r   r7   zWatchHelper._create_process  s    y?KKMMFE277F8vh/*.F&'99VT2DL)F8ww$(446##z'F'FF#(3}%*4*M*M'"&ww&000r   )NNNN)r4   rk   r   strr1   rl   r*   rm   r>   rn   r!   ro   rq   rr   rs   )rq   rr   ru   )	rv   rw   rx   ry   rA   rQ   r   r7   __classcell__)r   s   @r   r   r      sr    / !%-1%))")" )" 	)"
 )" +)" #)" 
)"V(/1 1r   r   )r   rk   rq   r   )N)r    r   r!   ro   rq   r   )ry   
__future__r   atexitrg   r   r   rH   r5   sysr9   rU   rz   r   shutilr   r   typingr   tornador   r   ImportErrorplatformr   r%   r   registerrc   r+   r   r   <module>r      s    ' #   	 	   
     "   <<7**L4.6I IXd1' d1P    !{  
Cs   
B% %B/.B/