
    Fh%                    n   S r SSKJr  SSKrSSKrSSKrSSKrSSKrSSKrSSK	r	SSK
r
SSKrSSKrSSKJr  SSKJr  SSKJr  SSKJr   SSKr\	R.                  S:X  a  \R0                  rOSS	 jrSSS
 jjr " S S5      r " S S\5      r\R6                  " \R8                  5        g! \ a    Sr Nef = f)z!JupyterLab Server process handler    )annotationsN)Logger)which)Any)genwin32c                T    SSK nSR                  [        UR                  U 5      5      $ )zShim for list2cmdline on posix.r   N )shlexjoinmapquote)cmd_listr   s     lC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\jupyterlab_server/process.pylist2cmdliner       s    xxEKK233    c                \   U=(       d    [         R                  nUR                  S5      =(       d    [         R                  n[	        XS9nU S:X  a  U(       d  Sn [	        SUS9nU(       d$  U S;   a  Sn[        U5      e[        SSU -  -   5      e[         R                  R                  U5      $ )	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w2 &!2"8$7// XCS/!@CTW^C^^__77??,--r   c                      \ rS rSr% Sr\R                  " 5       rS\S'   Sr	     S             SS jjr
SS jrSS jr\R                  SS	 j5       rSS
 jr\SS j5       rSS jrSrg)ProcessB   zA wrapper for a child process.zweakref.WeakSet_procsNc                B   [        U[        [        45      (       d  Sn[        U5      eU(       a"  UR	                  5       (       a  Sn[        U5      eU=(       d    U R                  5       U l        SU l        U(       d%  U R                  R                  S[        U5      5        Xl
        0 nU(       a  [        R                  US'   U R                  " SX5S.UD6U l        U=(       d    [        R                   " 5       U l        [$        R&                  R)                  U 5        g)	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)	selfr5   r2   r+   
kill_eventr!   quietr#   kwargss	            r   __init__Process.__init__H   s    4 #e}--3CS/!*++--#CS/!.KKV\#%67)11F8((DSDVD	%:):4 r   c                   U R                   nUR                  5       c/  [        R                  " UR                  [
        R                  5         UR                  SS9  U [        R                  ;   a  [        R                  R                  U 5        UR                  5       $ ! [        R                   aj    [        R                  S:X  a  [
        R                  nO[
        R                  nUR                  5       c!  [        R                  " UR                  U5         Nf = f! U [        R                  ;   a   [        R                  R                  U 5        f f = f)z/Terminate the process and return the exit code.g       @)timeoutnt)r9   pollr   killpidsignalSIGTERMwaitr6   TimeoutExpirednameSIGBREAKSIGKILLr%   r'   remove)r>   r9   sigs      r   	terminateProcess.terminatey   s    yy 99;GGDHHfnn-	,IIcI" w~~%%%d+yy{ (( 	'ww$oonnyy{"#&	' w~~%%%d+ &s%   B   A;DD! DD! !6Ec                *   U R                   nU R                  nUR                  5       c[  UR                  5       (       a  U R	                  5         Sn[        U5      e[        R                  " S5        UR                  5       c  M[  U R	                  5       $ )zHWait for the process to finish.

Returns
-------
The process exit code.
Process was aborted      ?)r9   r<   rG   r0   rS   r   timesleepr>   r9   r?   r#   s       r   rL   Process.wait   st     yy%%
iik!  "" + o%JJsO iik! ~~r   c              #  ^  #    U R                   nU R                  nUR                  5       c]  UR                  5       (       a  U R	                  5         Sn[        U5      e[        R                  " S5      v   UR                  5       c  M]  [        R                  " U R	                  5       5      e7f)z.Asynchronously wait for the process to finish.rV   rW   )	r9   r<   rG   r0   rS   r   r   rY   ReturnrZ   s       r   
wait_asyncProcess.wait_async   s      yy%%
iik!  "" + o%))C.  iik! jj)**s   BB-	$B-c                   [        U R                  5      nUR                  S[        R                  5        [        US   UR                  S5      5      US'   [        R                  S:X  a  SUS'   [        R                  " U40 UD6$ )zCreate the process.stderrr   r!   rF   Tshell)
r.   r5   
setdefaultr6   STDOUTr   r   r   rN   Popen)r>   rA   r5   s      r   r8   Process._create_process   sl    488n(J$5$56s1vvzz%01A77d?"F7O.v..r   c                \    [        U R                  5       H  nUR                  5         M     g)z*Clean up the started subprocesses at exit.N)r.   r'   rS   )clsr9   s     r   _cleanupProcess._cleanup   s!     $DNN %r   c                    [        U S5      (       a  U R                  b  U R                  $ [        R                  " S5      U l        U R                  R	                  [        R
                  5        U R                  $ )zGet our logger.r2   
jupyterlab)hasattrr2   logging	getLoggersetLevelINFO)r>   s    r   r1   Process.get_log   sT    4""t{{'>;;''5W\\*{{r   )r<   r3   r5   r2   r9   )NNNNF)r5   	list[str]r2   Logger | Noner+   
str | Noner?   threading.Event | Noner!   dict[str, str] | Noner@   boolreturnNonery   int)ry   r   rA   r   ry   zsubprocess.Popen[str])rh   ztype[Process]ry   rz   )ry   r   )__name__
__module____qualname____firstlineno____doc__weakrefWeakSetr'   __annotations___poolrB   rS   rL   r   	coroutiner^   r8   classmethodri   r1   __static_attributes__r,   r   r   r%   r%   B   s    (%oo/FO/E
 !%-1%)/!/! /! 	/!
 +/! #/! /! 
/!b4 " 	]]+ +
/  
r   r%   c                  z   ^  \ rS rSrSr    S             S	U 4S jjjrS
S jrSS jrSU 4S jjrSr	U =r
$ )WatchHelper   z%A process helper for a watch process.c                  > [         T	U ]  XXEUS9  [        c  U R                  R                  U l         U R
                  R                  5       R                  S5      nU(       d  Sn[        U5      e[        UR                  5       5        [        R                  " X'5      (       a  OMt  [        R                  " U R                  SS9U l        U R                   R#                  5         g)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.
)r2   r+   r?   r!   Nutf-8zProcess ended improperlyT)targetdaemon)superrB   ptyr9   r*   _stdoutreadlinedecodeRuntimeErrorprintrstriprematchr:   Thread_read_incoming_read_threadstart)
r>   r5   startup_regexr2   r+   r?   r!   liner#   	__class__s
            r   rB   WatchHelper.__init__   s    4 	QTU;99++DL<<((*11':D0"3''$++- xx,,  &,,D4G4GPTU!r   c                \   U R                   nUR                  5       c  [        R                  S:w  aD  [        R                  " [        R
                  " UR                  5      [        R                  5        O/[        R                  " UR                  [        R                  5         UR                  5         U [        R                  ;   a  [        R                  R                  U 5        UR                  $ ! U [        R                  ;   a   [        R                  R                  U 5        f f = f)zTerminate the process.rF   )r9   rG   r   rN   killpggetpgidrI   rJ   rK   rH   rL   r%   r'   rQ   
returncode)r>   r9   s     r   rS   WatchHelper.terminate   s    yy99;ww$		"**TXX.?&..1	,IIKw~~%%%d+ w~~%%%d+ &s   &C5 56D+c                   U R                   R                  5       n  [        R                  " US5      nU(       d  g[        UR                  S5      SS9  M;  ! [         a&  nU R
                  R                  SU5         SnAgSnAff = f)z(Run in a thread to read stdout and printi   zRead incoming error %sNr   r)   )end)	r   filenor   readOSErrorr2   debugr   r   )r>   r   bufes       r   r   WatchHelper._read_incoming  sv    $$&ggfd+
 #**W%2.   !!":A>s   A 
B!BBc                  > SUS'   [         bC  [         R                  " 5       u  p#U=US'   US'   SUS'   [        R                  " US5      U l        O{[
        R                  US'   [        R                  S:X  aT  [
        R                  " 5       nU=R                  [
        R                  -  sl	        XAS	'   [
        R                  US
'   SUS'   [        TU ]4  " S0 UD6$ )z"Create the watcher helper process.r   bufsizera   r*   Tstart_new_sessionrbrF   startupinfocreationflagsrb   r,   )r   openptyr   fdopenr   r6   PIPErN   STARTUPINFOdwFlagsSTARTF_USESHOWWINDOWCREATE_NEW_PROCESS_GROUPr   r8   )r>   rA   masterslaver   r   s        r   r8   WatchHelper._create_process  s    y?KKMMF277F8vh/*.F&'99VT2DL)F8ww$(446##z'F'FF#(3}%*4*M*M'"&ww&000r   )r   r   )NNNN)r5   rs   r   strr2   rt   r+   ru   r?   rv   r!   rw   ry   rz   r{   )ry   rz   r}   )r~   r   r   r   r   rB   rS   r   r8   r   __classcell__)r   s   @r   r   r      sw    / !%-1%))")" )" 	)"
 )" +)" #)" 
)" )"V(/1 1r   r   )r   rs   ry   r   )N)r    r   r!   rw   ry   r   )r   
__future__r   atexitrn   r   r   rJ   r6   sysr:   rX   r   r   shutilr   r   typingr   tornador   r   ImportErrorplatformr   r%   r   registerri   r,   r   r   <module>r      s    ' #   	 	   
     "   <<7**L4.6I IXd1' d1P    !{  
Cs   
B) )B43B4