a
    ø£Be|  ã                   @   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�rÎd,d-gZ3e3D ]$Z4e4ej5v �r d.e/_6ej5 7e4¡ �q e/ 8¡  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__© ú[/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/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   )Ú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;   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   ÚcurrentZspawn_callbackÚrun_test_async)ÚappÚfuncr   r   r    Úrun_test@   s    r6   c              
   Ã   s¸  t ƒ }| j |¡ tƒ }| ¡  | j d¡ t| dƒrHtdt| j	ƒƒ}n| j
}t |¡rb||ƒ}n4| j d¡ t ¡ }tƒ }| |||¡}t |g¡}z|I dH  W n@ tyä }	 z(| j d¡ | j t|	ƒ¡ W Y d}	~	n
d}	~	0 0 | j d¡ d}
|j�rd	}
| j d
¡ n| j d¡ | j d¡ ztz | j ¡  | j ¡  | ¡  W n: t�y„ }	 z | j t|	ƒ¡ d	}
W Y d}	~	n
d}	~	0 0 W t d¡ t |
¡ nt d¡ t |
¡ 0 dS )zþRun 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   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)r4   r5   ÚhandlerZ	env_patchÚurlÚtestÚloopÚexecutorÚtaskÚeÚresultr   r   r    r3   I   sL    


&


ÿ
r3   c                 Ë   sT   t j| i |¤ŽI dH }| ¡ I dH \}}|jdkrLtt| ƒd t|jƒ ƒ‚||fS )zRun an asynchronous commandNr   z exited with )r?   Úcreate_subprocess_execÚcommunicateÚ
returncodeÚRuntimeErrorrF   )ÚcmdÚkwargsÚprocÚstdoutr+   r   r   r    Úrun_async_process„   s
    
r\   c                 Ã   s´   t  tƒ d¡}t  t  |d¡¡sft  |¡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©ra   Úinstallzplaywright@^1.9.2©ZnpxZ
playwrightrf   úbrowser-test.jsÚnode)
ÚospÚjoinr   ÚexistsrJ   Úmakedirsr\   ÚshutilÚcopyÚhere©rM   Útargetr   r   r    Úrun_browser�   s    
rs   c                 C   s�   t  tƒ d¡}t  t  |d¡¡sNt |¡ 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_   r`   rc   re   rg   rh   ri   )rj   rk   r   rl   rJ   rm   Ú
subprocessÚcallrn   ro   rp   Ú
check_callrq   r   r   r    Úrun_browser_syncš   s    
rw   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   zŠAn 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   )rM   r   r   r    Ú<lambda>½   ó    z0BrowserApp.initialize_handlers.<locals>.<lambda>Únt)	Útest_browserrs   rJ   Únamerw   r6   Z	serverappr   Úinitialize_handlers)r   r5   r   r   r    rƒ   ¼   s
    zBrowserApp.initialize_handlers)r-   r.   r/   r0   r‚   Zopen_browserZserverapp_configr   Zdefault_urlÚipÚ
test_flagsr   Útest_aliasesr   r   r�   r}   rƒ   r1   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)9r0   r?   r=   r   rJ   rn   rt   r*   rH   Úconcurrent.futuresr   r   rj   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__rp   Údictr…   r†   ÚHandlerr   r6   r3   r\   rs   rw   r   rˆ   r‰   r-   Zskip_optionsÚoptionÚargvr�   ÚremoveZlaunch_instancer   r   r   r    Ú<module>   sR   	;	
