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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
"""
Installing the SageMath Jupyter Kernel and Extensions
Kernels have to register themselves with Jupyter so that they appear
in the Jupyter notebook's kernel drop-down. This is done by
:class:`SageKernelSpec`.
.. NOTE::
The doctests in this module run in a temporary directory as the involved
directories might be different during runs of the tests and actual
installation and because we might be lacking write permission to places
such as ``/usr/share``.
"""
import os
import errno
from sage.env import (
SAGE_DOC, SAGE_LOCAL, SAGE_EXTCODE,
SAGE_VERSION,
MATHJAX_DIR, JSMOL_DIR, THREEJS_DIR,
)
class SageKernelSpec(object):
def __init__(self, prefix=None):
"""
Utility to manage SageMath kernels and extensions
INPUT:
- ``prefix`` -- (optional, default: ``sys.prefix``)
directory for the installation prefix
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import SageKernelSpec
sage: prefix = tmp_dir()
sage: spec = SageKernelSpec(prefix=prefix)
sage: spec._display_name # random output
'SageMath 6.9'
sage: spec.kernel_dir == SageKernelSpec(prefix=prefix).kernel_dir
True
"""
self._display_name = 'SageMath {0}'.format(SAGE_VERSION)
if prefix is None:
from sys import prefix
jupyter_dir = os.path.join(prefix, "share", "jupyter")
self.nbextensions_dir = os.path.join(jupyter_dir, "nbextensions")
self.kernel_dir = os.path.join(jupyter_dir, "kernels", self.identifier())
self._mkdirs()
def _mkdirs(self):
"""
Create necessary parent directories
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import SageKernelSpec
sage: spec = SageKernelSpec(prefix=tmp_dir())
sage: spec._mkdirs()
sage: os.path.isdir(spec.nbextensions_dir)
True
"""
def mkdir_p(path):
try:
os.makedirs(path)
except OSError:
if not os.path.isdir(path):
raise
mkdir_p(self.nbextensions_dir)
mkdir_p(self.kernel_dir)
@classmethod
def identifier(cls):
"""
Internal identifier for the SageMath kernel
OUTPUT: the string ``"sagemath"``.
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import SageKernelSpec
sage: SageKernelSpec.identifier()
'sagemath'
"""
return 'sagemath'
def symlink(self, src, dst):
"""
Symlink ``src`` to ``dst``
This is not an atomic operation.
Already-existing symlinks will be deleted, already existing
non-empty directories will be kept.
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import SageKernelSpec
sage: spec = SageKernelSpec(prefix=tmp_dir())
sage: path = tmp_dir()
sage: spec.symlink(os.path.join(path, 'a'), os.path.join(path, 'b'))
sage: os.listdir(path)
['b']
"""
if os.path.islink(dst):
origsrc = os.path.join(os.path.dirname(dst), os.readlink(dst))
if os.path.abspath(origsrc) == os.path.abspath(src):
return
try:
os.remove(dst)
except OSError as err:
if err.errno == errno.EEXIST:
return
elif err.errno == errno.ENOENT:
pass
else:
raise
os.symlink(os.path.relpath(src, os.path.dirname(dst)), dst)
def use_local_mathjax(self):
"""
Symlink SageMath's Mathjax install to the Jupyter notebook.
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import SageKernelSpec
sage: spec = SageKernelSpec(prefix=tmp_dir())
sage: spec.use_local_mathjax()
sage: mathjax = os.path.join(spec.nbextensions_dir, 'mathjax')
sage: os.path.isdir(mathjax)
True
"""
src = MATHJAX_DIR
dst = os.path.join(self.nbextensions_dir, 'mathjax')
self.symlink(src, dst)
def use_local_jsmol(self):
"""
Symlink jsmol to the Jupyter notebook.
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import SageKernelSpec
sage: spec = SageKernelSpec(prefix=tmp_dir())
sage: spec.use_local_jsmol()
sage: jsmol = os.path.join(spec.nbextensions_dir, 'jsmol')
sage: os.path.isdir(jsmol)
True
sage: os.path.isfile(os.path.join(jsmol, "JSmol.min.js"))
True
"""
src = os.path.join(JSMOL_DIR)
dst = os.path.join(self.nbextensions_dir, 'jsmol')
self.symlink(src, dst)
def use_local_threejs(self):
"""
Symlink threejs to the Jupyter notebook.
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import SageKernelSpec
sage: spec = SageKernelSpec(prefix=tmp_dir())
sage: spec.use_local_threejs()
sage: threejs = os.path.join(spec.nbextensions_dir, 'threejs')
sage: os.path.isdir(threejs)
True
"""
src = THREEJS_DIR
dst = os.path.join(self.nbextensions_dir, 'threejs')
self.symlink(src, dst)
def _kernel_cmd(self):
"""
Helper to construct the SageMath kernel command.
OUTPUT:
List of strings. The command to start a new SageMath kernel.
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import SageKernelSpec
sage: spec = SageKernelSpec(prefix=tmp_dir())
sage: spec._kernel_cmd()
['/.../sage',
'--python',
'-m',
'sage.repl.ipython_kernel',
'-f',
'{connection_file}']
"""
return [
os.path.join('/usr', 'bin', 'sage'),
'--python',
'-m', 'sage.repl.ipython_kernel',
'-f', '{connection_file}',
]
def kernel_spec(self):
"""
Return the kernel spec as Python dictionary
OUTPUT:
A dictionary. See the Jupyter documentation for details.
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import SageKernelSpec
sage: spec = SageKernelSpec(prefix=tmp_dir())
sage: spec.kernel_spec()
{'argv': ..., 'display_name': 'SageMath ...'}
"""
return dict(
argv=self._kernel_cmd(),
display_name=self._display_name,
)
def _install_spec(self):
"""
Install the SageMath Jupyter kernel
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import SageKernelSpec
sage: spec = SageKernelSpec(prefix=tmp_dir())
sage: spec._install_spec()
"""
jsonfile = os.path.join(self.kernel_dir, "kernel.json")
import json
with open(jsonfile, 'w') as f:
json.dump(self.kernel_spec(), f)
def _symlink_resources(self):
"""
Symlink miscellaneous resources
This method symlinks additional resources (like the SageMath
documentation) into the SageMath kernel directory. This is
necessary to make the help links in the notebook work.
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import SageKernelSpec
sage: spec = SageKernelSpec(prefix=tmp_dir())
sage: spec._install_spec()
sage: spec._symlink_resources()
"""
path = os.path.join(SAGE_EXTCODE, 'notebook-ipython')
for filename in os.listdir(path):
self.symlink(
os.path.join(path, filename),
os.path.join(self.kernel_dir, filename)
)
self.symlink(
os.path.join(SAGE_DOC, 'html', 'en'),
os.path.join(self.kernel_dir, 'doc')
)
@classmethod
def update(cls, *args, **kwds):
"""
Configure the Jupyter notebook for the SageMath kernel
This method does everything necessary to use the SageMath kernel,
you should never need to call any of the other methods
directly.
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import SageKernelSpec
sage: SageKernelSpec.update(prefix=tmp_dir())
"""
instance = cls(*args, **kwds)
instance.use_local_mathjax()
instance.use_local_threejs()
instance._install_spec()
instance._symlink_resources()
def have_prerequisites(debug=True):
"""
Check that we have all prerequisites to run the Jupyter notebook.
In particular, the Jupyter notebook requires OpenSSL whether or
not you are using https. See :trac:`17318`.
INPUT:
``debug`` -- boolean (default: ``True``). Whether to print debug
information in case that prerequisites are missing.
OUTPUT:
Boolean.
EXAMPLES::
sage: from sage.repl.ipython_kernel.install import have_prerequisites
sage: have_prerequisites(debug=False) in [True, False]
True
"""
try:
from notebook.notebookapp import NotebookApp
return True
except ImportError:
if debug:
import traceback
traceback.print_exc()
return False
|