Beispiel #1
0
    def install_kernelspec(self):
        from ipykernel.kernelspec import write_kernel_spec
        from jupyter_client.kernelspec import KernelSpecManager

        kernel_spec_manager = KernelSpecManager()

        log.info('Writing kernel spec')
        kernel_spec_path = write_kernel_spec(overrides=kernel_json)

        log.info('Installing kernel spec ' + kernel_spec_path)
        try:
            kernel_spec_manager.install_kernel_spec(
                kernel_spec_path,
                kernel_name=kernel_json['name'],
                user=self.user)
        except Exception as e:
            log.error(str(e.args))
            log.error('Failed to install kernel spec')
        else:
            return

        # retry with not self.user
        log.info('Retry install kernel spec')
        try:
            kernel_spec_manager.install_kernel_spec(
                kernel_spec_path,
                kernel_name=kernel_json['name'],
                user=not self.user)
        except Exception as e:
            log.error(str(e.args))
            log.error('Failed to install kernel spec')
Beispiel #2
0
def install_markdown_kernel_spec(args):
    user = False
    prefix = None
    if args.sys_prefix:
        prefix = sys.prefix
    elif args.prefix:
        prefix = args.prefix
    elif args.user or not _is_root():
        user = True

    with TemporaryDirectory() as td:
        os.chmod(td, 0o755)  # Starts off as 700, not user readable
        with open(os.path.join(td, 'kernel.json'), 'w') as f:
            json.dump(kernel_json, f, sort_keys=True)
        # Copy resources once they're specified
        # shutil.copy(os.path.join(os.path.split(__file__)[
        #             0], 'kernel.js'), os.path.join(td, 'kernel.js'))
        # Copy resources once they're specified
        shutil.copy(os.path.join(os.path.split(__file__)[0], 'logo-64x64.png'),
                    os.path.join(td, 'logo-64x64.png'))

        KS = KernelSpecManager()
        KS.log.setLevel(logging.ERROR)
        KS.install_kernel_spec(td, 'markdown', user=user, prefix=prefix)
        print('markdown jupyter kernel spec is installed')
Beispiel #3
0
    def install_kernelspec(self):
        from ipykernel.kernelspec import write_kernel_spec
        from jupyter_client.kernelspec import KernelSpecManager

        kernel_spec_manager = KernelSpecManager()

        log.info('Writing kernel spec')
        kernel_spec_path = write_kernel_spec(overrides=kernel_json)

        log.info('Installing kernel spec ' + kernel_spec_path)
        try:
            kernel_spec_manager.install_kernel_spec(
                kernel_spec_path,
                kernel_name=kernel_json['name'],
                user=self.user)
        except Exception as e:
            log.error(str(e.args))
            log.error('Failed to install kernel spec')
        else:
            return

        # retry with not self.user
        log.info('Retry install kernel spec')
        try:
            kernel_spec_manager.install_kernel_spec(
                kernel_spec_path,
                kernel_name=kernel_json['name'],
                user=not self.user)
        except Exception as e:
            log.error(str(e.args))
            log.error('Failed to install kernel spec')
Beispiel #4
0
        def install_kernelspec(self):
            from ipykernel.kernelspec import write_kernel_spec
            from jupyter_client.kernelspec import KernelSpecManager
            from wolfram_kernel.wolfram_kernel import WolframKernel
            kernel_json = WolframKernel.kernel_json
            kernel_js = WolframKernel.kernel_js
            kernel_spec_manager = KernelSpecManager()
            log.info('Writing kernel spec')
            kernel_spec_path = write_kernel_spec(overrides=kernel_json)
            with open(kernel_spec_path + "/kernel.js", "w") as jsfile:
                jsfile.write(kernel_js)

            log.info('Installing kernel spec')
            try:
                kernel_spec_manager.install_kernel_spec(
                    kernel_spec_path,
                    kernel_name=kernel_json['name'],
                    user=user)
            except:
                log.error('Failed to install kernel spec in ' +
                          kernel_spec_path)
                kernel_spec_manager.install_kernel_spec(
                    kernel_spec_path,
                    kernel_name=kernel_json['name'],
                    user=not user)
Beispiel #5
0
def install_kernel(cmd):
    # Install the kernel spec when we install the package
    from ipykernel import kernelspec
    from jupyter_client.kernelspec import KernelSpecManager

    kernel_name = 'geonotebook%i' % sys.version_info[0]

    path = os.path.join(tempfile.mkdtemp(suffix='_kernels'), kernel_name)
    try:
        os.makedirs(path)
    except OSError:
        pass

    kernel_dict = {
        'argv': kernelspec.make_ipkernel_cmd(mod='geonotebook'),
        'display_name': 'Geonotebook (Python %i)' % sys.version_info[0],
        'language': 'python',
    }

    with open(os.path.join(path, 'kernel.json'), 'w') as fh:
        json.dump(kernel_dict, fh, indent=1)

    ksm = KernelSpecManager()
    ksm.install_kernel_spec(path,
                            kernel_name=kernel_name,
                            user=False,
                            prefix=sys.prefix)

    shutil.rmtree(path)
Beispiel #6
0
def install(_ctx) -> None:
    kernel_json = {
        "argv": [
            sys.executable, "-m", "michelson_kernel", "run", "-f",
            "{connection_file}"
        ],
        "display_name":
        "Michelson",
        "language":
        "michelson",
        "codemirror_mode":
        "michelson"
    }
    kernel_js_path = join(dirname(__file__), 'kernel.js')

    kernel_spec = KernelSpecManager()

    with TemporaryDirectory() as td:
        # NOTE: Starts off as 700, not user readable
        os.chmod(td, 0o755)
        shutil.copy(kernel_js_path, join(td, 'kernel.js'))
        with open(os.path.join(td, 'kernel.json'), 'w') as f:
            json.dump(kernel_json, f, sort_keys=True)

        kernel_spec.install_kernel_spec(td, 'michelson', prefix=sys.prefix)
Beispiel #7
0
    def run(self):
        # Regular install
        install.run(self)

        # Post install
        print('Installing Ansible Kernel kernelspec')
        from jupyter_client.kernelspec import KernelSpecManager
        from IPython.utils.tempdir import TemporaryDirectory
        kernel_json = {
            "argv":
            ["python", "-m", "ansible_kernel", "-f", "{connection_file}"],
            "codemirror_mode": "yaml",
            "display_name": "Ansible",
            "language": "ansible"
        }
        with TemporaryDirectory() as td:
            os.chmod(td, 0o755)
            with open(os.path.join(td, 'kernel.json'), 'w') as f:
                json.dump(kernel_json, f, sort_keys=True)
            ksm = KernelSpecManager()
            ksm.install_kernel_spec(td,
                                    'ansible',
                                    user=self.user,
                                    replace=True,
                                    prefix=self.prefix)
Beispiel #8
0
    def run(self):
        install.run(self)

        from jupyter_client.kernelspec import KernelSpecManager
        from tempfile import TemporaryDirectory
        kernel_spec = KernelSpecManager()
        with TemporaryDirectory() as td:
            os.chmod(td, 0o755)  # Starts off as 700, not user readable
            shutil.copy(kernel_js_path, join(td, 'kernel.js'))
            with open(os.path.join(td, 'kernel.json'), 'w') as f:
                json.dump(kernel_json, f, sort_keys=True)

            kernel_spec.install_kernel_spec(td, 'michelson', user=self.user)
Beispiel #9
0
def install_my_kernel_spec(user=True, prefix=None):
    with TemporaryDirectory() as td:
        os.chmod(td, 0o755)  # Starts off as 700, not user readable
        with open(os.path.join(td, 'kernel.json'), 'w') as f:
            json.dump(kernel_json, f, sort_keys=True)

        print('Installing Jupyter kernel spec')
        ksm = KernelSpecManager()
        ksm.install_kernel_spec(td, 'elm', user=user, prefix=prefix)

        install_dir = ksm.get_kernel_spec(kernel_name="elm").resource_dir

        print("Installed to ", install_dir)
Beispiel #10
0
    def run(self):
        # Regular installation
        install.run(self)

        # Now write the kernelspec
        from jupyter_client.kernelspec import KernelSpecManager
        from tempfile import TemporaryDirectory
        kernel_spec = KernelSpecManager()
        with TemporaryDirectory() as td:
            os.chmod(td, 0o755)  # Starts off as 700, not user readable
            with open(os.path.join(td, 'kernel.json'), 'w') as f:
                json.dump(kernel_json, f, sort_keys=True)
            # TODO: Copy resources once they're specified

            kernel_spec.install_kernel_spec(td, 'postgres', user=self.user)
Beispiel #11
0
def install(kernel_spec_manager=None, user=False, kernel_name=None):
    """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.
    
    Returns
    -------
    
    The path where the kernelspec was installed.
    """
    if kernel_spec_manager is None:
        kernel_spec_manager = KernelSpecManager()
    if kernel_name is None:
        kernel_name = KERNEL_NAME
    path = write_kernel_spec()
    dest = kernel_spec_manager.install_kernel_spec(path,
        kernel_name=kernel_name, user=user)
    # cleanup afterward
    shutil.rmtree(path)
    return dest
Beispiel #12
0
def install(kernel_spec_manager=None, user=False, prefix=None):
    """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.
    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.
    """
    if kernel_spec_manager is None:
        kernel_spec_manager = KernelSpecManager()

    path = write_kernel_spec()
    dest = kernel_spec_manager.install_kernel_spec(path, user=user, prefix=prefix)
    # cleanup afterward
    shutil.rmtree(path)
    return dest
Beispiel #13
0
def install_my_kernel_spec(user=True, prefix=None):
    with TemporaryDirectory() as td:
        td = Path(td)
        td.chmod(0o755)
        with (Path(td) / "kernel.json").open("wt", encoding="utf-8") as f:
            json.dump(kernel_json, f, sort_keys=True, indent="\t")
        # TODO: Copy resources once they're specified

        print("Installing IPython kernel spec")
        m = KernelSpecManager()
        if user:
            m.install_kernel_spec(str(td), "iTTS", user=user)
        elif prefix:
            m.install_kernel_spec(str(td), "iTTS", prefix=str(prefix))
        else:
            return 1
Beispiel #14
0
def install(kernel_spec_manager=None,
            user=False,
            kernel_name=KERNEL_NAME,
            display_name=None,
            prefix=None,
            profile=None):
    """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.
    """
    if kernel_spec_manager is None:
        kernel_spec_manager = KernelSpecManager()

    if (kernel_name != KERNEL_NAME) and (display_name is None):
        # kernel_name is specified and display_name is not
        # default display_name to kernel_name
        display_name = kernel_name
    overrides = {}
    if display_name:
        overrides["display_name"] = display_name
    if profile:
        extra_arguments = ["--profile", profile]
        if not display_name:
            # add the profile to the default display name
            overrides["display_name"] = 'Python %i [profile=%s]' % (
                sys.version_info[0], profile)
    else:
        extra_arguments = None
    path = write_kernel_spec(overrides=overrides,
                             extra_arguments=extra_arguments)
    dest = kernel_spec_manager.install_kernel_spec(path,
                                                   kernel_name=kernel_name,
                                                   user=user,
                                                   prefix=prefix)
    # cleanup afterward
    shutil.rmtree(path)
    return dest
Beispiel #15
0
    def install_kernelspec(self):
        from ipykernel.kernelspec import write_kernel_spec
        from jupyter_client.kernelspec import KernelSpecManager

        kernel_spec_manager = KernelSpecManager()

        log.info('Writing kernel spec')
        kernel_spec_path = write_kernel_spec(overrides=kernel_json)

        log.info('Installing kernel spec')
        try:
            kernel_spec_manager.install_kernel_spec(
                kernel_spec_path,
                kernel_name=kernel_json['name'],
                user=self.user)
        except:
            log.error('Failed to install kernel spec')
Beispiel #16
0
def install_my_kernel_spec(user=True, prefix=None):
    if "python2" in sys.executable:
        print("I think this needs python3")
    with TemporaryDirectory() as td:
        os.chmod(td, 0o755)  # Starts off as 700, not user readable
        with open(os.path.join(td, 'kernel.json'), 'w') as f:
            json.dump(kernel_json, f, sort_keys=True)
        # TODO: Copy resources once they're specified

        print('Installing IPython kernel spec of micropython')
        k = KernelSpecManager()
        k.install_kernel_spec(td,
                              'Micropython-upydevice',
                              user=user,
                              prefix=prefix)

        h = k.get_kernel_spec("micropython-upydevice")
        print("...into", h.resource_dir)
Beispiel #17
0
def install_kernelspec():
    from ipykernel.kernelspec import write_kernel_spec
    from jupyter_client.kernelspec import KernelSpecManager

    kernel_spec_manager = KernelSpecManager()

    print('Writing kernel spec')
    kernel_spec_path = write_kernel_spec(overrides=kernel_json)

    print('Installing kernel spec ' + kernel_spec_path)
    try:
        kernel_spec_manager.install_kernel_spec(
            kernel_spec_path, kernel_name=kernel_json['name'], user=True)
    except Exception as e:
        print(str(e.args))
        print('Failed to install kernel spec')
    else:
        return
Beispiel #18
0
def install(kernel_spec_manager=None, user=False):
    """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.
    """
    if kernel_spec_manager is None:
        kernel_spec_manager = KernelSpecManager()
    path = write_kernel_spec()
    kernel_spec_manager.install_kernel_spec(path,
        kernel_name=KERNEL_NAME, user=user, replace=True)
    # cleanup afterward
    shutil.rmtree(path)
Beispiel #19
0
def install(
    kernel_spec_manager=None,
    user=False,
    kernel_name=KERNEL_NAME,
    display_name=None,
    prefix=None,
):
    """Install the Picky 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.
        Names are needed to have multiple Picky kernels for different environments.
    display_name: str, optional
        Specify the display name for the kernelspec
    prefix: str, optional
        Specify an install prefix for the kernelspec.
        This is needed to install into a non-default location,
        such as a conda or a virtual environment.

    Returns
    -------
    
    The path where the kernelspec was installed.
    """
    if kernel_spec_manager is None:
        kernel_spec_manager = KernelSpecManager()

    if (kernel_name != KERNEL_NAME) and (display_name is None):
        # kernel_name is specified and display_name is not
        # default display_name to kernel_name
        display_name = kernel_name
    overrides = {}
    if display_name:
        overrides["display_name"] = display_name
    else:
        extra_arguments = None
    path = write_kernel_spec(overrides=overrides,
                             extra_arguments=extra_arguments)
    dest = kernel_spec_manager.install_kernel_spec(path,
                                                   kernel_name=kernel_name,
                                                   user=user,
                                                   prefix=prefix)
    # cleanup the temporary path afterward
    shutil.rmtree(path)
    return dest
Beispiel #20
0
    def _post_install(self):
        print("install attributes: {}".format(self.__dict__))
        if not hasattr(self, "user"):
            self.user = False
        if not hasattr(self, "prefix"):
            self.prefix = None

        print('Installing Jupyter kernelspec')
        from jupyter_client.kernelspec import KernelSpecManager
        from IPython.utils.tempdir import TemporaryDirectory
        kernel_json = {
            "argv": ["python", "-m", "imongo", "-f", "{connection_file}"],
            "codemirror_mode": "shell",
            "display_name": "IMongo"
        }
        with TemporaryDirectory() as td:
            os.chmod(td, 0o755)
            with open(os.path.join(td, 'kernel.json'), 'w') as f:
                json.dump(kernel_json, f, sort_keys=True)
            ksm = KernelSpecManager()
            ksm.install_kernel_spec(td, 'imongo', replace=True,
                                    user=self.user, prefix=self.prefix)
Beispiel #21
0
    def install_jupyter_kernelspec(self):
        from ipykernel.kernelspec import write_kernel_spec
        from jupyter_client.kernelspec import KernelSpecManager

        kernel_json = {
            'argv':
            [sys.executable, '-m', 'imathics', '-f', '{connection_file}'],
            'display_name': 'mathics',
            'language': 'Mathematica',
            'name': 'mathics',
        }

        kernel_spec_manager = KernelSpecManager()

        log.info('Writing kernel spec')
        kernel_spec_path = write_kernel_spec(overrides=kernel_json)

        log.info('Installing kernel spec ' + kernel_spec_path)
        try:
            kernel_spec_manager.install_kernel_spec(
                kernel_spec_path,
                kernel_name=kernel_json['name'],
                user=self.user)
        except Exception as e:
            log.error(str(e.args))
            log.error('Failed to install kernel spec')
        else:
            return

        # retry with not self.user
        log.info('Retry install kernel spec')
        try:
            kernel_spec_manager.install_kernel_spec(
                kernel_spec_path,
                kernel_name=kernel_json['name'],
                user=not self.user)
        except Exception as e:
            log.error(str(e.args))
            log.error('Failed to install kernel spec')
Beispiel #22
0
def install(kernel_spec_manager=None, user=False, kernel_name=KERNEL_NAME, display_name=None,
            prefix=None, profile=None):
    """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.
    """
    if kernel_spec_manager is None:
        kernel_spec_manager = KernelSpecManager()

    if (kernel_name != KERNEL_NAME) and (display_name is None):
        # kernel_name is specified and display_name is not
        # default display_name to kernel_name
        display_name = kernel_name
    overrides = {}
    if display_name:
        overrides["display_name"] = display_name
    if profile:
        extra_arguments = ["--profile", profile]
        if not display_name:
            # add the profile to the default display name
            overrides["display_name"] = 'Python %i [profile=%s]' % (sys.version_info[0], profile)
    else:
        extra_arguments = None
    path = write_kernel_spec(overrides=overrides, extra_arguments=extra_arguments)
    dest = kernel_spec_manager.install_kernel_spec(
        path, kernel_name=kernel_name, user=user, prefix=prefix)
    # cleanup afterward
    shutil.rmtree(path)
    return dest
def install(kernel_spec_manager=None,
            user=False,
            kernel_name=None,
            display_name=None,
            prefix=None):
    """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.
    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.
    display_name: str, optional
        Specify the display name for the kernelspec
    
    Returns
    -------
    
    The path where the kernelspec was installed.
    """
    if kernel_spec_manager is None:
        kernel_spec_manager = KernelSpecManager()
    if kernel_name is None:
        kernel_name = KERNEL_NAME
    if display_name:
        overrides = dict(display_name=display_name)
    else:
        overrides = None
    path = write_kernel_spec(overrides=overrides)
    dest = kernel_spec_manager.install_kernel_spec(path,
                                                   kernel_name=kernel_name,
                                                   user=user,
                                                   prefix=prefix)
    # cleanup afterward
    shutil.rmtree(path)
    return dest
Beispiel #24
0
def install_kernel():
    from jupyter_client.kernelspec import KernelSpecManager
    logging.info('Installing Qudi kernel.')

    try:
        # prepare temporary kernelspec folder
        tempdir = tempfile.mkdtemp(suffix='_kernels')
        path = os.path.join(tempdir, 'qudi')
        resourcepath = os.path.join(path, 'resources')
        kernelpath = os.path.abspath(__file__)
        os.mkdir(path)
        os.mkdir(resourcepath)

        kernel_dict = {
            'argv': [sys.executable, kernelpath, '{connection_file}'],
            'display_name': 'Qudi',
            'language': 'python',
        }
        # write the kernelspe file
        with open(os.path.join(path, 'kernel.json'), 'w') as f:
            json.dump(kernel_dict, f, indent=1)

        # copy logo
        logopath = os.path.abspath(
            os.path.join(os.path.dirname(kernelpath), '..', 'artwork', 'logo'))
        shutil.copy(os.path.join(logopath, 'logo-qudi-32x32.png'),
                    os.path.join(resourcepath, 'logo-32x32.png'))
        shutil.copy(os.path.join(logopath, 'logo-qudi-32x32.png'),
                    os.path.join(resourcepath, 'logo-32x32.png'))

        # install kernelspec folder
        kernel_spec_manager = KernelSpecManager()
        dest = kernel_spec_manager.install_kernel_spec(path,
                                                       kernel_name='qudi',
                                                       user=True)
        logging.info('Installed kernelspec qudi in {}'.format(dest))
    except OSError as e:
        if e.errno == errno.EACCES:
            print(e, file=sys.stderr)
            sys.exit(1)
    finally:
        if os.path.isdir(tempdir):
            shutil.rmtree(tempdir)
def install(kernel_spec_manager=None, user=False, kernel_name=None, display_name=None, prefix=None):
    """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.
    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.
    display_name: str, optional
        Specify the display name for the kernelspec
    
    Returns
    -------
    
    The path where the kernelspec was installed.
    """
    if kernel_spec_manager is None:
        kernel_spec_manager = KernelSpecManager()
    if kernel_name is None:
        kernel_name = KERNEL_NAME
    elif display_name is None:
        # kernel_name is specified and display_name is not
        # default display_name to kernel_name
        display_name = kernel_name
    if display_name:
        overrides = dict(display_name=display_name)
    else:
        overrides = None
    path = write_kernel_spec(overrides=overrides)
    dest = kernel_spec_manager.install_kernel_spec(path,
        kernel_name=kernel_name, user=user, prefix=prefix)
    # cleanup afterward
    shutil.rmtree(path)
    return dest
Beispiel #26
0
def install_kernel():
        from jupyter_client.kernelspec import KernelSpecManager
        logging.info('Installing Qudi kernel.')

        try:
            # prepare temporary kernelspec folder
            tempdir = tempfile.mkdtemp(suffix='_kernels')
            path = os.path.join(tempdir, 'qudi')
            resourcepath = os.path.join(path, 'resources')
            kernelpath = os.path.abspath(__file__)
            os.mkdir(path)
            os.mkdir(resourcepath)

            kernel_dict = {
                'argv': [sys.executable, kernelpath, '{connection_file}'],
                'display_name': 'Qudi',
                'language': 'python',
                }
            # write the kernelspe file
            with open(os.path.join(path, 'kernel.json'), 'w') as f:
                json.dump(kernel_dict, f, indent=1)

            # copy logo
            logopath = os.path.abspath(os.path.join(os.path.dirname(kernelpath), '..', 'artwork', 'logo'))
            shutil.copy(os.path.join(logopath, 'logo-qudi-32x32.png'), os.path.join(resourcepath, 'logo-32x32.png'))
            shutil.copy(os.path.join(logopath, 'logo-qudi-32x32.png'), os.path.join(resourcepath, 'logo-32x32.png'))

            # install kernelspec folder
            kernel_spec_manager = KernelSpecManager()
            dest = kernel_spec_manager.install_kernel_spec(path, kernel_name='qudi', user=True)
            logging.info('Installed kernelspec qudi in {}'.format(dest))
        except OSError as e:
            if e.errno == errno.EACCES:
                print(e, file=sys.stderr)
                sys.exit(1)
        finally:
            if os.path.isdir(tempdir):
                shutil.rmtree(tempdir)
Beispiel #27
0
def _jupyter_kernel(args):
    """Make xonsh available as a Jupyter kernel."""
    try:
        from jupyter_client.kernelspec import KernelSpecManager, NoSuchKernel
    except ImportError as e:
        raise ImportError(
            "Jupyter not found in current Python environment") from e

    ksm = KernelSpecManager()

    root = args.root
    prefix = args.prefix if args.prefix else sys.prefix
    user = args.user
    spec = {
        "argv": [
            sys.executable,
            "-m",
            "xonsh.jupyter_kernel",
            "-f",
            "{connection_file}",
        ],
        "display_name":
        "Xonsh",
        "language":
        "xonsh",
        "codemirror_mode":
        "shell",
    }

    if root and prefix:
        # os.path.join isn't used since prefix is probably absolute
        prefix = root + prefix

    try:
        old_jup_kernel = ksm.get_kernel_spec(XONSH_JUPYTER_KERNEL)
        if not old_jup_kernel.resource_dir.startswith(prefix):
            print("Removing existing Jupyter kernel found at {0}".format(
                old_jup_kernel.resource_dir))
        ksm.remove_kernel_spec(XONSH_JUPYTER_KERNEL)
    except NoSuchKernel:
        pass

    if sys.platform == "win32":
        # Ensure that conda-build detects the hard coded prefix
        spec["argv"][0] = spec["argv"][0].replace(os.sep, os.altsep)
        prefix = prefix.replace(os.sep, os.altsep)

    with tempfile.TemporaryDirectory() as d:
        os.chmod(d, 0o755)  # Starts off as 700, not user readable
        with open(os.path.join(d, "kernel.json"), "w") as f:
            json.dump(spec, f, sort_keys=True)

        print("Installing Jupyter kernel spec:")
        print("  root: {0!r}".format(root))
        if user:
            print("  as user: {0}".format(user))
        elif root and prefix:
            print("  combined prefix {0!r}".format(prefix))
        else:
            print("  prefix: {0!r}".format(prefix))
        ksm.install_kernel_spec(d,
                                XONSH_JUPYTER_KERNEL,
                                user=user,
                                prefix=(None if user else prefix))
        return 0
Beispiel #28
0
def _jupyter_kernel(
    user=False,
    prefix=None,
    root=None,
):
    """Generate xonsh kernel for jupyter.

    Parameters
    ----------
    user : -u, --user
        Install kernel spec in user config directory.
    prefix : -p, --prefix
        Installation prefix for bin, lib, etc.
    root : -r, --root
        Install relative to this alternate root directory.
    """
    try:
        from jupyter_client.kernelspec import KernelSpecManager, NoSuchKernel
    except ImportError as e:
        raise ImportError("Jupyter not found in current Python environment") from e

    ksm = KernelSpecManager()

    prefix = prefix or sys.prefix
    spec = {
        "argv": [
            sys.executable,
            "-m",
            "xonsh.jupyter_kernel",
            "-f",
            "{connection_file}",
        ],
        "display_name": "Xonsh",
        "language": "xonsh",
        "codemirror_mode": "shell",
    }

    if root and prefix:
        # os.path.join isn't used since prefix is probably absolute
        prefix = root + prefix

    try:
        old_jup_kernel = ksm.get_kernel_spec(XONSH_JUPYTER_KERNEL)
        if not old_jup_kernel.resource_dir.startswith(prefix):
            print(
                "Removing existing Jupyter kernel found at {}".format(
                    old_jup_kernel.resource_dir
                )
            )
        ksm.remove_kernel_spec(XONSH_JUPYTER_KERNEL)
    except NoSuchKernel:
        pass

    if sys.platform == "win32":
        # Ensure that conda-build detects the hard coded prefix
        spec["argv"][0] = spec["argv"][0].replace(os.sep, os.altsep)
        prefix = prefix.replace(os.sep, os.altsep)

    with tempfile.TemporaryDirectory() as d:
        os.chmod(d, 0o755)  # Starts off as 700, not user readable
        with open(os.path.join(d, "kernel.json"), "w") as f:
            json.dump(spec, f, sort_keys=True)

        print("Installing Jupyter kernel spec:")
        print(f"  root: {root!r}")
        if user:
            print(f"  as user: {user}")
        elif root and prefix:
            print(f"  combined prefix {prefix!r}")
        else:
            print(f"  prefix: {prefix!r}")
        ksm.install_kernel_spec(
            d, XONSH_JUPYTER_KERNEL, user=user, prefix=(None if user else prefix)
        )
        return 0