o
    tfu                     @   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
BrowserAppZ	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                       s4   e Zd ZdZ fddZ fddZdd Z  ZS )LogErrorHandlerz.A handler that exits with 1 on a logged error.c                    s   t  jtjd d| _d S )N)levelF)super__init__loggingERRORerroredself	__class__ a/var/www/html/software/conda/envs/catlas/lib/python3.10/site-packages/jupyterlab/browser_check.pyr   +      
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   )hasattrr"   
isinstancer	   r
   r   filterr   recordr   r   r    r%   /   s   
zLogErrorHandler.filterc                 C   s   t |jtjd d| _d S )N)fileT)printmsgsysstderrr   r&   r   r   r    emit<   r!   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   currentZspawn_callbackrun_test_async)appfuncr   r   r    run_testA   s   r7   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   r8   Zdisplay_urlinspectiscoroutinefunctionasyncioget_event_loopr   run_in_executorwait	Exceptioncriticalerrorstrr   Zhttp_serverstopZio_looptimesleepos_exit)r5   r6   handlerZ	env_patchurltestloopexecutortaskeresultr   r   r    r4   J   s^   







r4   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 t d}t t |ds4t |stt | tg d|dI dH  tg d|dI dH  tg d|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.browser_testnode_modulesnpminitz-ycwdNrb   installzplaywright@^1.9.2ZnpxZ
playwrightrg   browser-test.jsnode)
ospjoinr   existsrK   makedirsr]   shutilcopyhererN   targetr   r   r    run_browser   s   
rt   c                 C   s   t t d}t t |ds't| tjg d|d tjg d|d tjg d|d t	t t
dt |d tjdd| g|dS )	r^   r_   r`   ra   rd   rf   rh   ri   rj   )rk   rl   r   rm   rK   rn   
subprocesscallro   rp   rq   
check_callrr   r   r   r    run_browser_sync   s   
rx   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 )NZpage_config_dataTZbrowserTestFZbuildAvailableZexposeAppInBrowser)settings
setdefaultr   initialize_settingsr   r   r   r    r~      s
   zBrowserApp.initialize_settingsc                    sB   | j rtndd }tjdkr|tkrt}t| j| t   d S )Nc                 S   s   dS )Nr   r   )rN   r   r   r    <lambda>   s    z0BrowserApp.initialize_handlers.<locals>.<lambda>nt)	test_browserrt   rK   namerx   r7   Z	serverappr   initialize_handlers)r   r6   r   r   r    r      s
   zBrowserApp.initialize_handlers)r.   r/   r0   r1   r   Zopen_browserZserverapp_configr   Zdefault_urlip
test_flagsr   test_aliasesr   r   r   r~   r   r2   r   r   r   r    r      s    c                   C   s   t tdgS )N)moduler5   )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)9r1   r@   r>   r   rK   ro   ru   r+   rI   concurrent.futuresr   r   rk   Zjupyter_server.serverappr   r   Zjupyter_server.utilsr   r   Ztornado.ioloopr   Ztornado.iostreamr	   Ztornado.websocketr
   Z	traitletsr   r   Zlabappr   r   Ztests.test_appr   abspathdirname__file__rq   dictr   r   Handlerr   r7   r4   r]   rt   rx   r   r   r   r.   Zskip_optionsoptionargvr   removeZlaunch_instancer   r   r   r    <module>   sX   	;	
