File: install.py

package info (click to toggle)
macaulay2-jupyter-kernel 0.6.7~beta-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 3,492 kB
  • sloc: python: 21,611; javascript: 51; makefile: 9
file content (71 lines) | stat: -rw-r--r-- 2,412 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import argparse
import json
import os
import shutil
import sys

from jupyter_client.kernelspec import KernelSpecManager
from IPython.utils.tempdir import TemporaryDirectory
from notebook import __path__ as notebook_dir
from notebook.nbextensions import install_nbextension

""" Macaulay2 Jupyter Kernel: standard jupyter kernel spec installation
"""

kernel_json = {
    "argv": [sys.executable, "-m", "m2_kernel", "-f", "{connection_file}"],
    "display_name": "M2",
    "language": "text/x-macaulay2",
    "codemirror_mode": "macaulay2",
}


def install_kernel_assets(user=True, prefix=None):
    """
    """
    assets_dir = '{}/assets'.format(os.path.dirname(__file__))

    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, indent=2, sort_keys=False)
        shutil.copy('{}/m2-spec/kernel.js'.format(assets_dir), td)
        print('Installing kernel spec ...')
        KernelSpecManager().install_kernel_spec(td, kernel_name='m2', user=user, prefix=prefix)

    print("Installing nbextension for syntax highlighting ...")
    install_nbextension('{}/m2-mode'.format(assets_dir),
            nbextensions_dir=os.path.realpath(
                '{}/static/components/codemirror/mode/'.format(
                    notebook_dir[0])).replace('/usr', prefix),
            destination='macaulay2', overwrite=True, symlink=False)


def _is_root():
    try:
        return os.geteuid() == 0
    except AttributeError:
        return False  # assume not an admin on non-Unix platforms


def main(argv=None):
    ap = argparse.ArgumentParser()
    ap.add_argument('--user', action='store_true',
                    help="Install to the per-user kernels registry. Default if not root.")
    ap.add_argument('--sys-prefix', action='store_true',
                    help="Install to sys.prefix (e.g. a virtualenv or conda env)")
    ap.add_argument('--prefix',
                    help="Install to the given prefix."
                         "Kernelspec will be installed in {PREFIX}/share/jupyter/kernels/")
    args = ap.parse_args(argv)

    if args.sys_prefix:
        args.prefix = sys.prefix
    if not args.prefix and not _is_root():
        args.user = True

    install_kernel_assets(user=args.user, prefix=args.prefix)


if __name__ == '__main__':
    main()