a
    ø£BeüA  ã                   @   sŽ  d Z ddlZddlZddlZddlmZ ddlZddlZddl	Z	ddl
Z
ddlmZ zddlmZmZ W n" eyŠ   ddlmZmZ Y n0 ddlmZmZ ddlmZ ddlmZmZmZ ddlmZ dd	lmZ dd
lmZ zddl m!Z! W n  e�y   ddl"m!Z! Y n0 ddl#m$Z$ e%ƒ Z&e 'e (e)¡¡Z*d"dd„Z+d#dd„Z,d$dd„Z-d%dd„Z.dd„ Z/d&dd„Z0d'dd„Z1d(dd„Z2d d!„ Z3dS ))z?Utilities for installing Javascript extensions for the notebooké    N)ÚPath)ÚPackageNotFoundErrorÚversion)ÚbasenameÚnormpath)Újoin)ÚENV_JUPYTER_PATHÚSYSTEM_JUPYTER_PATHÚjupyter_data_dir)Úensure_dir_exists)ÚArgumentConflict)Úget_federated_extensions)Úloadé   )Ú_test_overlapTFc              
   C   s(  d}t |||d�}	t|	ƒ t| ttfƒr4d}
t|
ƒ‚|sDtt| ƒƒ}tt|	|ƒƒ}|r¢t	j
 |¡r¢|rt| d| ¡ t	j
 |¡r˜t	j
 |¡s˜t |¡ n
t	 |¡ t	jt	j
 |¡dd� |�rht	j
 | ¡} t	j
 |¡�sL|rò| d|› d| › �¡ zt	 | |¡ W nF t�yH } z,t ¡  d	¡�r2d
}
t|
ƒ|‚‚ W Y d}~n
d}~0 0 nt	j
 |¡�s$td| ƒ‚n¼t	j
 | ¡�rtt	j
 | ¡dƒ} t	 | ¡D ]|\}}}t||t| ƒd… ƒ}t	j
 |¡�sÞ|�rÔ| d| ¡ t	 |¡ |D ](}t||ƒ}t||ƒ}t|||d� �qâ�q’n| }t|||d� |S )aZ  Install a prebuilt extension for JupyterLab

    Stages files and/or directories into the labextensions directory.
    By default, this compares modification time, and only stages files that need updating.
    If `overwrite` is specified, matching files are purged before proceeding.

    Parameters
    ----------

    path : path to file, directory, zip or tarball archive, or URL to install
        By default, the file will be installed with its base name, so '/path/to/foo'
        will install to 'labextensions/foo'. See the destination argument below to change this.
        Archives (zip or tarballs) will be extracted into the labextensions directory.
    user : bool [default: False]
        Whether to install to the user's labextensions directory.
        Otherwise do a system-wide install (e.g. /usr/local/share/jupyter/labextensions).
    overwrite : bool [default: False]
        If True, always install the files, regardless of what may already be installed.
    symlink : bool [default: True]
        If True, create a symlink in labextensions, rather than copying files.
        Windows support for symlinks requires a permission bit which only admin users
        have by default, so don't rely on it.
    labextensions_dir : str [optional]
        Specify absolute path of labextensions directory explicitly.
    destination : str [optional]
        name the labextension is installed to.  For example, if destination is 'foo', then
        the source file will be installed to 'labextensions/foo', regardless of the source name.
    logger : Jupyter logger [optional]
        Logger instance to use
    N)ÚuserÚ
sys_prefixÚlabextensions_dirz�path must be a string pointing to a single extension to install; call this function multiple times to install multiple extensionszRemoving: %sT)Úexist_okzSymlinking: ú -> ÚWindowszýSymlinks can be activated on Windows 10 for Python version 3.8 or higher by activating the 'Developer Mode'. That may not be allowed by your administrators.
See https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-developmentz%s exists and is not a symlinkÚ zMaking directory: %s©Úlogger)Ú_get_labextension_dirr   Ú
isinstanceÚlistÚtupleÚ	TypeErrorr   r   ÚpjoinÚosÚpathÚlexistsÚinfoÚisdirÚislinkÚshutilÚrmtreeÚremoveÚmakedirsÚdirnameÚabspathÚexistsÚsymlinkÚOSErrorÚplatformÚ
startswithÚ
ValueErrorÚwalkÚlenÚ_maybe_copy)r!   r-   Ú	overwriter   r   Údestinationr   r   Ú	full_destÚlabextÚmsgÚeÚparentÚ_ÚfilesÚdest_dirÚ	file_nameÚsrcÚ	dest_file© rB   úe/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jupyterlab/federated_labextensions.pyÚdevelop_labextension/   s`    )ÿ
ÿ



rD   c                 C   sž   t | ƒ\}}tj |j¡d }	g }
|D ]r}tj |	|d ¡}|d }|r^| d|› d|› �¡ tj |¡svt|	|d� t	||||||||d�}|
 
|¡ q&|
S )z¢Develop a labextension bundled in a Python package.

    Returns a list of installed/updated directories.

    See develop_labextension for parameter information.r   r@   ÚdestzInstalling r   r   )r5   r-   r   r   r   r6   r   )Ú_get_labextension_metadatar    r!   ÚsplitÚ__file__r   r#   r,   Úbuild_labextensionrD   Úappend)Úmoduler   r   r5   r-   r   r   ÚmZlabextsÚ	base_pathZ
full_destsr8   r@   rE   r7   rB   rB   rC   Údevelop_labextension_pyš   s,    ø
rN   c           	      C   s¢   |du rt  td¡ntt|ƒ ¡ ƒ}tt| ƒ ¡ ƒ}|rF| d|  ¡ t||ƒ}d|d||g}|durt| d|g¡ |r‚| 	d¡ |r�| 	d¡ t
j||d	� dS )
z&Build a labextension in the given pathNÚstagingúBuilding extension in %sÚnodeú--core-pathz--static-urlú--developmentú--source-map©Úcwd)Úospr   ÚHEREÚstrr   Úresolver#   Ú_ensure_builderÚextendrJ   Ú
subprocessÚ
check_call)	r!   r   ÚdevelopmentZ
static_urlÚ
source_mapÚ	core_pathÚext_pathÚbuilderÚ	argumentsrB   rB   rC   rI   Å   s    $


rI   c                 C   s<  |du rt  td¡ntt|ƒ ¡ ƒ}tt| ƒ ¡ ƒ}|rF| d|  ¡ t|ƒ}tt	|dƒƒ�}t
 |¡}	W d  ƒ n1 s|0    Y  |	d |vr t|dd� nPt	||	d  d |	d ƒ}
t	||	d	  d
d¡ƒ}t  |
¡sðt |
¡ t ||
¡ t||ƒ}d|d|d|g}|�r| d¡ |�r*| d¡ tj||d� dS )z$Watch a labextension in a given pathNrO   rP   úpackage.jsonÚnameT)r   Zext_dirZ
jupyterlabZ	outputDirZstaticrQ   rR   z--watchrS   rT   rU   )rW   r   rX   rY   r   rZ   r#   r   Úopenr   Újsonr   rN   Úgetr%   r&   r'   r    r-   r[   rJ   r]   r^   )r!   Zlabextensions_pathr   r_   r`   ra   rb   Zfederated_extensionsÚfidÚext_datar7   Ú
output_dirrc   rd   rB   rB   rC   Úwatch_labextensionÝ   s*    $(




rm   c           
      C   s  t t |d¡ƒ�}t |¡}W d  ƒ n1 s00    Y  t t | d¡ƒ�}t |¡}W d  ƒ n1 sj0    Y  |d d }| di ¡ d¡}|p¦| di ¡ d¡}|du r¼td| ƒ‚d|v �rt t | |d¡ƒ� }t |¡ d¡}W d  ƒ n1 sþ0    Y  t t | d	¡¡�s,tj	d
g| d� | }t t |d	dd¡¡�srt 
|¡|k�rdd}t|ƒ‚t 
|¡}�q0t||ddd�}	|	�sàt t |d	ddd¡ƒ� }t |¡ d¡}W d  ƒ n1 �sÆ0    Y  t||ddd�}	|	�sþd|› d|› �}t|ƒ‚t |d	dddd¡S )zIEnsure that we can build the extension and return the builder script pathre   NZdevDependenciesz@jupyterlab/builderÚdependenciesz<Extensions require a devDependency on @jupyterlab/builder@%sú/r   Znode_modulesZjlpmrU   z@jupyterlabrc   z"Could not find @jupyterlab/builderT)Zdrop_prerelease1Zdrop_prerelease2z:Extensions require a devDependency on @jupyterlab/builder@z, you have a dependency on Úlibzbuild-labextension.js)rg   rW   r   rh   r   ri   r1   r,   r]   r^   r*   r   )
rb   ra   rj   Z	core_datark   Zdep_version1Zdep_version2Útargetr9   ÚoverlaprB   rB   rC   r[     sP    ((ÿ
.ÿÿ0ÿÿr[   c                 C   sX   t j |¡sdS t  | ¡jt  |¡j dkrB|r>| d| ¡ dS |rT| d| ¡ dS )am  Should a file be copied, if it doesn't exist, or is newer?

    Returns whether the file needs to be updated.

    Parameters
    ----------

    src : string
        A path that should exist from which to copy a file
    src : string
        A path that might exist to which to copy a file
    logger : Jupyter logger [optional]
        Logger instance to use
    Tg�íµ ÷Æ°>zOut of date: %szUp to date: %sF)r    r!   r,   ÚstatÚst_mtimeÚwarningr#   ©r@   rE   r   rB   rB   rC   Ú_should_copy:  s    rw   c                 C   s8   t | ||d�r4|r(| d| › d|› �¡ t | |¡ dS )a!  Copy a file if it needs updating.

    Parameters
    ----------

    src : string
        A path that should exist from which to copy a file
    src : string
        A path that might exist to which to copy a file
    logger : Jupyter logger [optional]
        Logger instance to use
    r   z	Copying: r   N)rw   r#   r&   Úcopy2rv   rB   rB   rC   r4   V  s    r4   c                 C   s¤   d| fd|fd|fd|fg}dd„ |D ƒ}t |ƒdkrNd d	 |¡¡}t|ƒ‚| r`ttƒ d
ƒ}n@|rtttd d
ƒ}n,|rˆt|ddd
ƒ}n|r’|}nttd d
ƒ}|S )a‘  Return the labextension directory specified

    Parameters
    ----------

    user : bool [default: False]
        Get the user's .jupyter/labextensions directory
    sys_prefix : bool [default: False]
        Get sys.prefix, i.e. ~/.envs/my-env/share/jupyter/labextensions
    prefix : str [optional]
        Get custom prefix
    labextensions_dir : str [optional]
        Get what you put in
    r   Úprefixr   r   c                 S   s"   g | ]\}}|r|› d |›�‘qS )ú=rB   )Ú.0ÚnÚvrB   rB   rC   Ú
<listcomp>~  ó    z)_get_labextension_dir.<locals>.<listcomp>r   z[cannot specify more than one of user, sys_prefix, prefix, or labextensions_dir, but got: {}z, Zlabextensionsr   ÚshareÚjupyter)r3   Úformatr   r   r   r
   r   r	   )r   r   ry   r   ÚconflictingZconflicting_setr9   r8   rB   rB   rC   r   i  s*    üÿr   c                 C   sJ  t  | ¡}t  |¡s(d|› d�}t|ƒ‚g }z&t | ¡}t|dƒrP|| ¡ fW S W n. ty€ } z| 	|¡ W Y d}~n
d}~0 0 d}t
j t
j |d¡¡rêtt
j |d¡dƒ�}t|ƒ}W d  ƒ n1 sÎ0    Y  | di ¡ d¡}|�sBz$tjtjd	d
g|d� d¡ ¡ }W n, tj�y@   d| › d�}t|ƒd‚Y n0 zt|ƒ W n: t�yˆ   t tjdddd|g¡ tj d|¡ Y n0 ddlm}	m}
 | dd¡g}| |
|ƒ¡ | |	|ƒ¡ |D ]d}z,t |¡}t|dƒ�rö|| ¡ fW   S W n0 t�y( } z| 	|¡ W Y d}~n
d}~0 0 �qÈd| › d|› �}t |ƒ‚dS )af  Get the list of labextension paths associated with a Python module.

    Returns a tuple of (the module path,             [{
        'src': 'mockextension',
        'dest': '_mockdestination'
    }])

    Parameters
    ----------

    module : str
        Importable Python module exposing the
        magic-named `_jupyter_labextension_paths` function
    z
The path `z` does not exist.Ú_jupyter_labextension_pathsNzpyproject.tomlÚrbÚprojectrf   zsetup.pyz--namerU   Úutf8zThe Python package `z<` is not a valid package, it is missing the `setup.py` file.z-mÚpipÚinstallz-er   )Úfind_namespace_packagesÚfind_packagesú-r<   zThere is no labextension at z. Errors encountered: )!rW   r+   r,   ÚFileNotFoundErrorÚ	importlibÚimport_moduleÚhasattrr„   Ú	ExceptionrJ   r    r!   r   rg   r   ri   r]   Úcheck_outputÚsysÚ
executableÚdecodeÚstripÚCalledProcessErrorr   r   r^   ÚinsertÚ
setuptoolsrŠ   r‹   Úreplacer\   ÚModuleNotFoundError)rK   Zmod_pathr9   ÚerrorsrL   ÚexcÚpackagerj   ÚdatarŠ   r‹   Zpackage_candidatesrB   rB   rC   rF   ‘  sb    



 &ÿýÿ
ÿ
ÿÿ
$rF   )TFFNNNF)FFTTNN)NFNFN)NFFN)N)N)FFNN)4Ú__doc__rŽ   rh   r    Úos.pathr!   rW   r/   r&   r]   r“   Úpathlibr   Úimportlib.metadatar   r   ÚImportErrorÚimportlib_metadatar   r   r   r   Zjupyter_core.pathsr   r	   r
   Zjupyter_core.utilsr   Z(jupyter_server.extension.serverextensionr   Zjupyterlab_server.configr   Ztomllibr   ÚtomliÚcommandsr   ÚobjectZDEPRECATED_ARGUMENTr+   r*   rH   rX   rD   rN   rI   rm   r[   rw   r4   r   rF   rB   rB   rB   rC   Ú<module>   sd   
       ø
m      ù
, ÿ
 ÿ
(5


(