
    Xh                        d 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mZ ddlmZmZ ddlmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZmZ ddlm Z   ejB                   ejD                  e#            Z$ e%e      Z&dddiidfe&d<   dddiidfe&d<   dddiidfe&d<    e%e      Z'de'd<    G d dejP                        Z)d Z*d Z+d Z,d Z-d  Z. G d! de      Z/d" Z0d# Z1e2d$k(  rMd%d&gZ3e3D ]3  Z4e4ejj                  v sd'e/_6        ejj                  jo                  e4       5 e/jq                          yy)(z
This module is meant to run JupyterLab in a headless browser, making sure
the application launches and starts up without errors.
    N)ThreadPoolExecutor)path)aliasesflags)pathname2urlurljoin)IOLoop)StreamClosedError)WebSocketClosedError)BoolUnicode   )LabAppget_app_dir)TestEnv
BrowserApp	core_modeTzStart the app in core mode.z	core-modedev_modezStart the app in dev mode.zdev-modewatchzStart the app in watch mode.zBrowserApp.app_dirzapp-dirc                   6     e Zd ZdZ fdZ fdZ fdZ xZS )LogErrorHandlerz.A handler that exits with 1 on a logged error.c                     t         |   t        j                         | j	                  t
        j                         d| _        y )N)streamF)super__init__sysstderrsetLevelloggingERRORerroredself	__class__s    L/var/www/html/myenv/lib/python3.12/site-packages/jupyterlab/browser_check.pyr   zLogErrorHandler.__init__+   s.    

+gmm$    c                     t        |d      r0|j                  $t        |j                  d   t        t        f      ryt
        |   |      S )Nexc_infor   F)hasattrr(   
isinstancer
   r   r   filterr#   recordr$   s     r%   r+   zLogErrorHandler.filter0   sE     FJ'+6??1-0ACW/XYw~f%%r&   c                 2    d| _         t        | 	  |       y )NT)r!   r   emitr,   s     r%   r/   zLogErrorHandler.emit=   s    Vr&   )__name__
__module____qualname____doc__r   r+   r/   __classcell__r$   s   @r%   r   r   (   s    8
& r&   r   c                 V    t        j                         j                  t        | |       y)a
  Synchronous entry point to run a test function.
    func is a function that accepts an app url as a parameter and returns a result.
    func can be synchronous or asynchronous.  If it is synchronous, it will be run
    in a thread, so asynchronous is preferred.
    N)r	   currentspawn_callbackrun_test_async)appfuncs     r%   run_testr<   B   s     NN##NC>r&   c                   K   t               }| j                  j                  |       t               }|j	                          | j                  j                  d       t        | d      r t        dt        | j                              }n| j                  }t        j                  |      r	 ||      }nb| j                  j                  d       t        j                         }t               }|j!                  |||      }t        j"                  |g      }	 | d{    | j                  j                  d       d}
|j,                  rd	}
| j                  j'                  d
       n| j                  j                  d       | j                  j                  d       	 | j.                  j1                          | j2                  j1                          |j1                          t5        j6                  d       t9        j:                  |
       y7 # t$        $ rJ}	| j                  j'                  d       | j                  j)                  t+        |	             Y d}	~	=d}	~	ww xY w# t$        $ r0}	| j                  j)                  t+        |	             d	}
Y d}	~	d}	~	ww xY w# t5        j6                  d       t9        j:                  |
       w xY ww)zRun a test against the application.
    func is a function that accepts an app url as a parameter and returns a result.
    func can be synchronous or asynchronous.  If it is synchronous, it will be run
    in a thread, so asynchronous is preferred.
    zRunning async testbrowser_open_filezfile:z&Using thread pool executor to run testNz!Caught exception during the test:zTest Completer   r   zExiting with 1 due to errorszExiting normallyzStopping server...   )r   log
addHandlerr   startinfor)   r   r   r>   display_urlinspectiscoroutinefunctionasyncioget_event_loopr   run_in_executorwait	Exceptioncriticalerrorstrr!   http_serverstopio_looptimesleepos_exit)r:   r;   handler	env_patchurltestloopexecutortaskeresults              r%   r9   r9   K   s     GGGw	IOOGGLL%& s'(g|C,A,ABCoo ""4(Cy=>%%'%'##HdC8||TF#


 GGLL!F78'(GGLL%&	
 	

1
1 	 <=c!f$  c!f 	

1
sz   DKH HH  A=KAI% "+KH 	I"?IKI""K%	J.&JJ! JJ! !,KKc                   K   t        j                  | i | d{   }|j                          d{   \  }}|j                  dk7  r-t	        t        |       dz   t        |j                        z         ||fS 7 _7 Iw)zRun an asynchronous commandNr   z exited with )rG   create_subprocess_execcommunicate
returncodeRuntimeErrorrN   )cmdkwargsprocstdoutr   s        r%   run_async_processrh      su     //???D++--NFF!3s8o5DOO8LLMM6>	 @-s    A?A;A?A=AA?=A?c                   K   t         j                  j                  dd      }|dvrd}t        j                  t               d      }t        j                  t        j                  |d            skt        j                  |      s(t        j                  t        j                  |             t        g d|       d{    t        g d	|       d{    t        d
dd|g|       d{    t        j                  t        j                  t        d      t        j                  |d             t        dd| g|       d{    y7 7 z7 c7 w)-Run the browser test and return an exit code.JLAB_BROWSER_TYPEchromium>   webkitfirefoxrl   browser_testnode_modulesnpminitz-ycwdNrr   installzplaywright@^1.9.2npx
playwrightrw   browser-test.jsnode)rT   environgetospjoinr   existsmakedirsrh   shutilcopyhererX   browsertargets      r%   run_browserr      s     jjnn0*=G77XXkm^4F::chhv~67zz&!KK() 56BBB GVTTT
UL)WE6
RRR
KK01388FDU3VW
V%6<&
III	 	CTRIsI   B=E?E EEE/E0AE	E
EEEEc                 X   t         j                  j                  dd      }|dvrd}t        j                  t               d      }t        j                  t        j                  |d            sGt        j                  |       t        j                  g d|       t        j                  g d|       t        j                  d	d
d|g|       t        j                  t        j                  t        d      t        j                  |d             t        j                  dd| g|      S )rj   rk   rl   >   rm   rn   rl   ro   rp   rq   rt   rv   rx   ry   rw   rz   r{   )rT   r|   r}   r~   r   r   r   r   
subprocesscallr   r   r   
check_callr   s      r%   run_browser_syncr      s    jjnn0*=G77XXkm^4F::chhv~67
F-6:?VLOOUL)W=6J
KK01388FDU3VW  &*;S!AvNNr&   c                   n     e Zd ZdZe ZdZddiZ eddd      Zd	Z	e
ZeZ ed      Z fd
Z fdZ xZS )r   zAn app the launches JupyterLab and waits for it to start up, checking for
    JS console errors, JS errors, and Python logged errors.
    Fbase_urlz/foo/z
/lab?resetTz'The default URL to redirect to from `/`)confighelpz	127.0.0.1c                     | j                   j                  di        d| j                   d   d<   d| j                   d   d<   d| j                   d   d<   t        |           y )Npage_config_dataTbrowserTestFbuildAvailableexposeAppInBrowser)settings
setdefaultr   initialize_settingsr"   s    r%   r   zBrowserApp.initialize_settings   sa      !3R8;?()-8>C()*:;BF()*>?#%r&   c                     d }| j                   rt        j                  dk(  rt        nt        }t        | j                  |       t        | !          y )Nc                       y)Nr    )argsre   s     r%   r;   z,BrowserApp.initialize_handlers.<locals>.func   s    r&   nt)	test_browserrT   namer   r   r<   	serverappr   initialize_handlers)r#   r;   r$   s     r%   r   zBrowserApp.initialize_handlers   s>    	 ')ww$#KD&#%r&   )r0   r1   r2   r3   r   open_browserserverapp_configr   default_urlip
test_flagsr   test_aliasesr   r   r   r   r   r4   r5   s   @r%   r   r      sU     DL"G,,t:cdK	BEG:L&& &r&   c                      t         t        dgS )N)moduler:   )r0   r   r   r&   r%    _jupyter_server_extension_pointsr      s    
344r&   c                      ddigS )Nr   zjupyterlab.browser_checkr   r   r&   r%   _jupyter_server_extension_pathsr      s    1233r&   __main__z--no-browser-testz--no-chrome-testF)9r3   rG   rE   r   rT   r   r   r   rR   concurrent.futuresr   r   r~   jupyter_server.serverappr   r   jupyter_server.utilsr   r   tornado.ioloopr	   tornado.iostreamr
   tornado.websocketr   	traitletsr   r   labappr   r   tests.test_appr   abspathdirname__file__r   dictr   r   StreamHandlerr   r<   r9   rh   r   r   r   r   r   r0   skip_optionsoptionargvr   removelaunch_instancer   r&   r%   <module>r      sf  
    	   
  1  3 6 ! . 2 # ' #s{{;3;;x()%[
(;*=>@]^
; '*d);<>Z[
: $wo68VW
7 G}.Y g++ 4?8vJ"O & &B54 z');<L $SXX&+J#HHOOF#$
   r&   