o
    ^i                     @   s  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!e"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 dej(Z)dd Z*dd Z+d d! Z,d"d# Z-d$d% Z.G d&d deZ/d'd( Z0d)d* Z1e2d+krd,d-gZ3e3D ]Z4e4ej5v rd.e/_6ej57e4 qe/8  dS dS )/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                       s8   e Zd ZdZ fddZ fddZ fddZ  ZS )LogErrorHandlerz.A handler that exits with 1 on a logged error.c                    s&   t  jtjd | tj d| _d S )N)streamF)super__init__sysstderrsetLevelloggingERRORerroredself	__class__ S/home/ubuntu/hpml_nyu/venv/lib/python3.10/site-packages/jupyterlab/browser_check.pyr   +   s   
zLogErrorHandler.__init__c                    s8   t |dr|jd urt|jd ttfrdS t |S )Nexc_infor   F)hasattrr%   
isinstancer	   r
   r   filterr    recordr!   r#   r$   r(   0   s   
zLogErrorHandler.filterc                    s   d| _ t | d S )NT)r   r   emitr)   r!   r#   r$   r+   =   s   zLogErrorHandler.emit)__name__
__module____qualname____doc__r   r(   r+   __classcell__r#   r#   r!   r$   r   (   s
    r   c                 C   s   t  t| | dS )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funcr#   r#   r$   run_testB   s   r6   c              
      s  t  }| j| t }|  | jd t| dr%tdt| j	}n| j
}t|r2||}n| jd t }t }||||}t|g}z|I dH  W n  tys }	 z| jd | jt|	 W Y d}	~	nd}	~	ww | jd d}
|jrd	}
| jd
 n| jd | jd zGz| j  | j  |  W n ty }	 z| jt|	 d	}
W Y d}	~	nd}	~	ww W td t|
 dS W td t|
 dS td t|
 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   r7   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)r4   r5   handler	env_patchurltestloopexecutortaskeresultr#   r#   r$   r3   K   s^   







r3   c                    sV   t j| i |I dH }| I dH \}}|jdkr'tt| d t|j ||fS )zRun an asynchronous commandNr   z exited with )r@   create_subprocess_execcommunicate
returncodeRuntimeErrorrG   )cmdkwargsprocstdoutr   r#   r#   r$   run_async_process   s   
r`   c                    s   t jdd}|dvrd}tt d}tt|dsAt|s+t t| tg d|dI dH  tg d	|dI dH  td
dd|g|dI dH  t	
ttdt|d tdd| g|dI dH  dS )-Run the browser test and return an exit code.JLAB_BROWSER_TYPEchromium   webkitfirefoxrc   browser_testnode_modulesnpminitz-ycwdNrj   installzplaywright@^1.9.2npx
playwrightro   browser-test.jsnode)rM   environgetospjoinr   existsmakedirsr`   shutilcopyhererQ   browsertargetr#   r#   r$   run_browser   s   
r   c                 C   s   t jdd}|dvrd}tt d}tt|ds4t | tj	g d|d tj	g d|d tj	d	d
d|g|d t
ttdt|d tjdd| g|dS )ra   rb   rc   rd   rg   rh   ri   rl   rn   rp   rq   ro   rr   rs   )rM   rt   ru   rv   rw   r   rx   ry   
subprocesscallrz   r{   r|   
check_callr}   r#   r#   r$   run_browser_sync   s   
r   c                       s^   e Zd ZdZe ZdZddiZeddddZd	Z	e
ZeZedZ fd
dZ fddZ  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                    sF   | j di  d| j d d< d| j d d< d| j d d< t   d S )Npage_config_dataTbrowserTestFbuildAvailableexposeAppInBrowser)settings
setdefaultr   initialize_settingsr   r!   r#   r$   r      s
   zBrowserApp.initialize_settingsc                    s:   dd }| j rtjdkrtnt}t| j| t   d S )Nc                  _   s   dS )Nr   r#   )argsr]   r#   r#   r$   r5      s   z,BrowserApp.initialize_handlers.<locals>.funcnt)	test_browserrM   namer   r   r6   	serverappr   initialize_handlers)r    r5   r!   r#   r$   r      s
   zBrowserApp.initialize_handlers)r,   r-   r.   r/   r   open_browserserverapp_configr   default_urlip
test_flagsr   test_aliasesr   r   r   r   r   r0   r#   r#   r!   r$   r      s    c                   C   s   t tdgS )N)moduler4   )r,   r   r#   r#   r#   r$    _jupyter_server_extension_points   s   r   c                   C   s
   ddigS )Nr   zjupyterlab.browser_checkr#   r#   r#   r#   r$   _jupyter_server_extension_paths   s   
r   __main__z--no-browser-testz--no-chrome-testF)9r/   r@   r>   r   rM   rz   r   r   rK   concurrent.futuresr   r   rv   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   r6   r3   r`   r   r   r   r   r   r,   skip_optionsoptionargvr   removelaunch_instancer#   r#   r#   r$   <module>   sX   	;	!
