a
    2b‡<  ã                   @   s  d Z ddlm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mZ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 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„Z(d$dd„Z)d%dd„Z*dd„ Z+dS )&z?Utilities for installing Javascript extensions for the notebooké    )Úprint_functionN)ÚbasenameÚjoinÚnormpath)Újupyter_data_dirÚSYSTEM_JUPYTER_PATHÚENV_JUPYTER_PATH)Úensure_dir_exists)Úget_federated_extensions)ÚArgumentConflicté   )Ú_test_overlapTFc              
   C   s  d}t |||d}	t|	ƒ t| ttfƒr0tdƒ‚|s@tt| ƒƒ}tt|	|ƒƒ}|ržt	j
 |¡rž|rp| d| ¡ t	j
 |¡r”t	j
 |¡s”t |¡ n
t	 |¡ t	jt	j
 |¡dd |r\t	j
 | ¡} t	j
 |¡s@|rê| d|| f ¡ zt	 | |¡ W nB ty< }
 z(t ¡  d¡r&td	ƒ|
‚‚ 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_dirzpath 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: %s -> %sZ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ÚeÚparentÚdirsÚfilesZdest_dirÚ	file_nameÚsrcZ	dest_file© r:   úAlib/python3.9/site-packages/jupyterlab/federated_labextensions.pyÚdevelop_labextension&   s^    $
ÿü


r<   c                 C   sš   t | ƒ\}}tj |j¡d }	g }
|D ]n}tj |	|d ¡}|d }|rZ| d||f ¡ tj |¡srt|	|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   r9   ÚdestzInstalling %s -> %sr   )r0   r(   r   r   r   r1   r   )Ú_get_labextension_metadatar   r   ÚsplitÚ__file__r   r   r'   Úbuild_labextensionr<   Úappend)Úmoduler   r   r0   r(   r   r   ÚmZlabextsÚ	base_pathZ
full_destsr3   r9   r=   r2   r:   r:   r;   Údevelop_labextension_pyˆ   s"    ýrF   c           	      C   sŒ   |du rt  td¡}t  | ¡}|r0| d|  ¡ t||ƒ}d|d||g}|dur^| d|g¡ |rl| d¡ |rz| 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   ÚHEREr&   r   Ú_ensure_builderÚextendrB   Ú
subprocessÚ
check_call)	r   r   ÚdevelopmentZ
static_urlÚ
source_mapÚ	core_pathÚext_pathÚbuilderÚ	argumentsr:   r:   r;   rA   ¦   s    



rA   c                 C   s&  |du rt  td¡}t  | ¡}|r0| d|  ¡ t|ƒ}tt|dƒƒ}t 	|¡}	W d  ƒ n1 sf0    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 pathNrG   rH   úpackage.jsonÚnameT)r   Zext_dirZ
jupyterlabZ	outputDirZstaticrI   rJ   z--watchrK   rL   rM   )rO   r   rP   r&   r   r
   Úopenr   ÚjsonÚloadrF   Úgetr    r!   r"   r   r(   rQ   rB   rS   rT   )r   Zlabextensions_pathr   rU   rV   rW   rX   Zfederated_extensionsÚfidÚext_datar2   Z
output_dirrY   rZ   r:   r:   r;   Úwatch_labextension¼   s,    
(




rc   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tƒ ¡ d¡}|pª| dtƒ ¡ d¡}|du rÀtd| ƒ‚d|v rt t | |d¡ƒ }t |¡ d¡}W d  ƒ n1 s0    Y  t||d	d	d
}|s4td||f ƒ‚t 	t | d¡¡sXt
jdg| d | }t 	t |ddd¡¡sšt |¡|krŒtdƒ‚t |¡}q\t |ddddd¡S )zNEnsure that we can build the extension and return the builder script path
    r[   NZdevDependenciesz@jupyterlab/builderZdependenciesz<Extensions require a devDependency on @jupyterlab/builder@%sú/ÚversionT)Zdrop_prerelease1Zdrop_prerelease2zYExtensions require a devDependency on @jupyterlab/builder@%s, you have a dependency on %sZnode_modulesZjlpmrM   z@jupyterlabrY   z"Could not find @jupyterlab/builderÚlibzbuild-labextension.js)r]   rO   r   r^   r_   r`   Údictr,   r   r'   rS   rT   r%   )	rX   rW   ra   Z	core_datarb   ZdepVersion1ZdepVersion2ZoverlapÚtargetr:   r:   r;   rQ   ã   s.    ((
0rQ   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Úwarnr   ©r9   r=   r   r:   r:   r;   Ú_should_copy  s    rm   c                 C   s4   t | ||dr0|r$| d| |f ¡ 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: %s -> %sN)rm   r   r!   Úcopy2rl   r:   r:   r;   r/   #  s    r/   c                 C   s    d| fd|fd|fd|fg}dd„ |D ƒ}t |ƒdkrJtd d	 |¡¡ƒ‚| r\ttƒ d
ƒ}n@|rpt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 )z{}={!r})Úformat)Ú.0ÚnÚvr:   r:   r;   Ú
<listcomp>K  ó    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   ZshareZjupyter)r.   r   rp   r   r   r   r   r   )r   r   ro   r   ZconflictingZconflicting_setr3   r:   r:   r;   r   6  s,    üÿÿr   c           
      C   sÐ  t  | ¡}t  |¡s"td |¡ƒ‚g }z&t | ¡}t|dƒrJ|| ¡ fW S W n. t	yz } z| 
|¡ W Y d}~n
d}~0 0 z$tjtjddg|d d¡ ¡ }W n" tjyÂ   td | ¡ƒ‚Y n0 d	dl}z| |¡ W n< |j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 qVtd | |¡ƒ‚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 `{}` does not exist.Ú_jupyter_labextension_pathsNzsetup.pyz--namerM   Úutf8zRThe Python package `{}` is not a valid package, it is missing the `setup.py` file.r   z-mZpipÚinstallz-e)Úfind_packagesÚfind_namespace_packagesú-Ú_z6There is no labextension at {}. Errors encountered: {})rO   r&   r'   ÚFileNotFoundErrorrp   Ú	importlibÚimport_moduleÚhasattrrv   Ú	ExceptionrB   rS   Zcheck_outputÚsysÚ
executableÚdecodeÚstripZCalledProcessErrorÚpkg_resourcesZget_distributionZDistributionNotFoundrT   r   ÚinsertZ
setuptoolsry   rz   ÚreplacerR   ÚModuleNotFoundError)
rC   Zmod_pathÚerrorsrD   ÚexcÚpackager†   ry   rz   Zpackage_candidatesr:   r:   r;   r>   ]  sF    



 $ÿ
ÿ
$r>   )TFFNNNF)FFTTNN)NFNFN)NFFN)N)N)FFNN),Ú__doc__Z
__future__r   r~   r^   r   Zos.pathr   rO   r*   r!   rS   r‚   r   r   r   r   Zjupyter_core.pathsr   r   r   Zjupyter_core.utilsr	   Zjupyterlab_server.configr
   Z(jupyter_server.extension.serverextensionr   Zcommandsr   ÚobjectZDEPRECATED_ARGUMENTr&   r%   r@   rP   r<   rF   rA   rc   rQ   rm   r/   r   r>   r:   r:   r:   r;   Ú<module>   s<       ý
b


'$


'