B
    U[                 @   s   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	m
Z
 ejjZdejd  ZeejedZddd	Zdd
dZdddZddedddfddZddlmZ G dd deZedkre  dS )z#The IPython kernel spec for Jupyter    )print_functionN)KernelSpecManagerzpython%iZ	resourcesipykernel_launcherc             K   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})sys
executableextend)modr   extra_argumentskwZ	arguments r   3lib/python3.7/site-packages/ipykernel/kernelspec.pymake_ipkernel_cmd   s    
r   c             C   s   t | ddtjd  ddS )zConstruct dict for kernel.json)r	   z	Python %ir   Zpython)argvdisplay_nameZlanguage)r   r   version_info)r	   r   r   r   get_kernel_dict5   s    r   c          	   C   sp   | dkrt jtjddt} tt|  t	|}|r@|
| tt| dd}tj||dd W dQ R X | 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w   )indent)ospathjointempfileZmkdtempKERNEL_NAMEshutilZcopytree	RESOURCESr   updateopenpjoinjsondump)r   	overridesr	   Zkernel_dictfr   r   r   write_kernel_spec>   s    
r$   Fc       
      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}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.

    Returns
    -------
    
    The path where the kernelspec was installed.
    Nr   z	--profilezPython %i [profile=%s]r   )r"   r	   )kernel_nameuserprefix)r   r   r   r   r$   Zinstall_kernel_specr   Zrmtree)
Zkernel_spec_managerr&   r%   r   r'   profiler"   r	   r   destr   r   r   installV   s"    
r*   )Applicationc               @   s&   e Zd ZdZdZdddZdd ZdS )	InstallIPythonKernelSpecAppzDummy app wrapping argparsezipython-kernel-installNc             C   s    |d krt jdd  }|| _d S )Nr   )r   r   )selfr   r   r   r   
initialize   s    z&InstallIPythonKernelSpecApp.initializec          
   C   s(  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	}y t
|j|j|j|j|jd}W n^ tk
r } z>|jtjkrt|tjd |jrtdtjd | d  W d d }~X Y nX td|j|f  d S )Nr   z Install the IPython kernel spec.)progZ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defaultr2   z--display-namezdSpecify the display name for the kernelspec. This is helpful when you have multiple IPython kernels.)r3   r2   z	--profilez]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-prefixZstore_constr'   z[Install to Python's sys.prefix. Shorthand for --prefix='%s'. For use in conda/virtual-envs.)r1   Zconstr)   r2   )r&   r%   r(   r'   r   )filez$Perhaps you want `sudo` or `--user`?r   zInstalled kernelspec %s in %s)argparseArgumentParsernameadd_argumentstrr   r   r'   
parse_argsr   r*   r&   r(   r   OSErrorerrnoZEACCESprintstderrexit)r-   r6   parserZoptsr)   er   r   r   start   s8    

z!InstallIPythonKernelSpecApp.start)N)__name__
__module____qualname____doc__r8   r.   rC   r   r   r   r   r,      s   
r,   __main__)r   NN)N)NNN)rG   Z
__future__r   r=   r    r   r   r   r   Zjupyter_client.kernelspecr   r   r   r   r   r   dirname__file__r   r   r   r$   r*   Ztraitlets.configr+   r,   rD   Zlaunch_instancer   r   r   r   <module>   s(   

	
5,