U
    e]                   
   @  sd  d 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ZddlZddl	Z	ddl
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k
r   d
ZY nX ejjZdejd  ZeeejdZd(dddddddZd)dddddZd*dddddddZdd
eddddfddddddd dd!d"d#Z dd$l!m"Z" G d%d& d&e"Z#e$d'kr`e#%  dS )+z#The IPython kernel spec for Jupyter    )annotationsN)Path)Any)KernelSpecManager)Unicode   )_is_debugpy_availableFzpython%iZ	resourcesipykernel_launcherstrz
str | Nonelist[str] | Nonez	list[str])mod
executableextra_argumentsreturnc                 C  s2   |dkrt j}|pg }|d| ddg}|| |S )a  Build Popen command list for launching an IPython kernel.

    Parameters
    ----------
    mod : str, optional (default 'ipykernel')
        A string of an IPython module whose __main__ starts an IPython kernel
    executable : str, optional (default sys.executable)
        The Python executable to use for the kernel process.
    extra_arguments : list, optional
        A list of extra arguments to pass when executing the launch code.

    Returns
    -------
    A Popen command list
    Nz-mz-fz{connection_file})sysr   extend)r   r   r   Z	arguments r   3lib/python3.8/site-packages/ipykernel/kernelspec.pymake_ipkernel_cmd"   s    
r   zdict[str, Any])r   r   c                 C  s"   t | ddtjd  dddidS )zConstruct dict for kernel.jsonr   zPython %i (ipykernel)r   ZpythondebuggerT)argvdisplay_namelanguageZmetadata)r   r   version_infor   r   r   r   get_kernel_dict?   s
    r   zPath | str | Nonezdict[str, Any] | None)path	overridesr   r   c              	   C  s   | dkrt tjddt } tt|  t |  j}|tj	@ sTt | 
|tj	B  t|}|rj|| tt| dd}tj||dd W 5 Q R X t| S )zWrite a kernel spec directory to `path`

    If `path` is not specified, a temporary directory is created.
    If `overrides` is given, the kernelspec JSON is updated before writing.

    The path to the kernelspec is always returned.
    NZ_kernels)suffixzkernel.jsonwr   )indent)r   tempfileZmkdtempKERNEL_NAMEshutilcopytree	RESOURCESstatst_modeS_IWUSRchmodr   updateopenpjoinjsondumpr
   )r   r   r   maskZkernel_dictfr   r   r   write_kernel_specI   s    

r1   zKernelSpecManager | Noneboolzdict[str, str] | None)kernel_spec_manageruserkernel_namer   prefixprofileenvr   c                 C  s   | dkrt  } |tkr"|dkr"|}i }|r2||d< |rZd|g}|s^dtjd |f |d< nd}|rj||d< t||d}	| j|	|||d}
t|	 |
S )	a^  Install the IPython kernelspec for Jupyter

    Parameters
    ----------
    kernel_spec_manager : KernelSpecManager [optional]
        A KernelSpecManager to use for installation.
        If none provided, a default instance will be created.
    user : bool [default: False]
        Whether to do a user-only install, or system-wide.
    kernel_name : str, optional
        Specify a name for the kernelspec.
        This is needed for having multiple IPython kernels for different environments.
    display_name : str, optional
        Specify the display name for the kernelspec
    profile : str, optional
        Specify a custom profile to be loaded by the kernel.
    prefix : str, optional
        Specify an install prefix for the kernelspec.
        This is needed to install into a non-default location, such as a conda/virtual-env.
    env : dict, optional
        A dictionary of extra environment variables for the kernel.
        These will be added to the current environment variables before the
        kernel is started

    Returns
    -------
    The path where the kernelspec was installed.
    Nr   	--profilezPython %i [profile=%s]r   r8   )r   r   )r5   r4   r6   )r   r"   r   r   r1   Zinstall_kernel_specr#   rmtree)r3   r4   r5   r   r6   r7   r8   r   r   r   destr   r   r   installk   s.    %   
r<   )Applicationc                   @  s8   e Zd ZdZedZddddddZdd	d
dZdS )InstallIPythonKernelSpecAppzDummy app wrapping argparsezipython-kernel-installNr   None)r   r   c                 C  s    |dkrt jdd }|| _dS )zInitialize the app.Nr   )r   r   )selfr   r   r   r   
initialize   s    z&InstallIPythonKernelSpecApp.initialize)r   c              
   C  sZ  ddl }|j| jdd}|jdddd |jd	ttd
d |jdtdd |jdtdd |jdtdd |jddtjddtj d |jdddddd || j	}|j
rt|j
|_
z$t|j|j|j|j|j|j
d}W nb tk
r> } zB|jtjkr,t|tjd |jr"td tjd | d!  W 5 d}~X Y nX td"|j d#|  dS )$zStart the app.r   Nz Install the IPython kernel spec.)progdescriptionz--user
store_truez3Install for the current user instead of system-wide)actionhelpz--namezdSpecify a name for the kernelspec. This is needed to have multiple IPython kernels at the same time.)typedefaultrF   z--display-namezdSpecify the display name for the kernelspec. This is helpful when you have multiple IPython kernels.)rG   rF   r9   z]Specify an IPython profile to load. This can be used to create custom versions of the kernel.z--prefixzSpecify an install prefix for the kernelspec. This is needed to install into a non-default location, such as a conda/virtual-env.z--sys-prefixstore_constr6   z[Install to Python's sys.prefix. Shorthand for --prefix='%s'. For use in conda/virtual-envs.)rE   constr;   rF   z--envappend   )ZENVZVALUEz)Set environment variables for the kernel.)rE   nargsmetavarrF   )r4   r5   r7   r6   r   r8   )filez$Perhaps you want `sudo` or `--user`?r   zInstalled kernelspec z in )argparseArgumentParsernameadd_argumentr
   r"   r   r6   
parse_argsr   r8   dictr<   r4   r7   r   OSErrorerrnoZEACCESprintstderrexit)r@   rP   parserZoptsr;   er   r   r   start   s     

z!InstallIPythonKernelSpecApp.start)N)__name__
__module____qualname____doc__r   rR   rA   r]   r   r   r   r   r>      s   r>   __main__)r	   NN)N)NNN)&ra   Z
__future__r   rW   r-   osr#   r&   r   r!   pathlibr   typingr   Zjupyter_client.kernelspecr   Z	traitletsr   r   r   ImportErrorr   joinr,   r   r"   __file__parentr%   r   r   r1   r<   Ztraitlets.configr=   r>   r^   Zlaunch_instancer   r   r   r   <module>   sP   
      #CU
