/usr/lib/python2.7/dist-packages/sagenb_export/nbextension/start_sagenb_handler.py is in python-sagenb-export 3.2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | import os
import sys
from subprocess import Popen
from tornado.web import authenticated
from notebook.base.handlers import IPythonHandler
from sagenb_export.nbextension.jinja2_env import jinja2_env
class StartSageNBHandler(IPythonHandler):
"""
Start the old Sage Notebook server and send its URL.
"""
@authenticated
def get(self):
self.set_header("Content-Type", "text/plain")
try:
url = self.sagenb_url()
except Exception as E:
self.set_status(500)
self.finish(str(E))
else:
self.finish(url)
def sagenb_url(self):
# Run SageNB in a separate process. We create a pipe through
# which SageNB will communicate the URL.
rfd, wfd = os.pipe()
# SageNB will run the command below "$SAGE_BROWSER url"
# The definition of SAGE_BROWSER below ensures that the url
# (passed as $0) is written to the write end of the pipe and
# that both ends of the pipe are then closed.
env = dict(os.environ)
env["BROWSER"] = "bash -c 'echo >&{1} $0; exec {0}<&- {1}>&-'".format(rfd, wfd)
# Actually start the Sage notebook
cmd = "from sagenb.notebook.notebook_object import notebook; notebook()"
p = Popen([sys.executable, "-c", cmd], stdin=open(os.devnull), env=env)
# Read URL through the pipe
os.close(wfd)
url = os.read(rfd, 1024).strip()
os.close(rfd)
if url:
return url
else:
raise RuntimeError("The Sage Notebook failed to start :-(")
|