Skip to content

Commit 223233f

Browse files
committedOct 17, 2023
Migrate start-notebook.sh to bash
Based on > Stop using bash, haha 👍 from jupyter#1532. If there's more apetite for this, I'll try to migrate `start.sh` and `start-singleuser.sh` as well - I think they should all be merged together. We can remove the `.sh` suffixes for accuracy, and keep symlinks in so old config still works. Since the shebang is what is used to launch the correct interpreter, the `.sh` doesn't matter. Will help fix jupyter#1532, as I believe all those things are going to be easier to do from python than bash
1 parent 7e1a19a commit 223233f

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed
 
+35-15
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,42 @@
1-
#!/bin/bash
1+
#!/usr/bin/env python -u
22
# Copyright (c) Jupyter Development Team.
33
# Distributed under the terms of the Modified BSD License.
4+
import os
5+
import sys
6+
import shlex
47

5-
set -e
68

7-
# The Jupyter command to launch
8-
# JupyterLab by default
9-
DOCKER_STACKS_JUPYTER_CMD="${DOCKER_STACKS_JUPYTER_CMD:=lab}"
9+
# If we are in a JupyterHub, we pass on to `start-singleuser.sh` instead so it does the right thing
10+
if "JUPYTERHUB_API_TOKEN" not in os.environ:
11+
print(
12+
"WARNING: using start-singleuser.sh instead of start-notebook.sh to start a server associated with JupyterHub.",
13+
file=sys.stderr,
14+
)
15+
os.execvp("/usr/local/bin/start-singleuser.sh", sys.argv[1:])
1016

11-
if [[ -n "${JUPYTERHUB_API_TOKEN}" ]]; then
12-
echo "WARNING: using start-singleuser.sh instead of start-notebook.sh to start a server associated with JupyterHub."
13-
exec /usr/local/bin/start-singleuser.sh "$@"
14-
fi
1517

16-
wrapper=""
17-
if [[ "${RESTARTABLE}" == "yes" ]]; then
18-
wrapper="run-one-constantly"
19-
fi
18+
# Wrap everything in start.sh, no matter what
19+
command = ["/usr/local/bin/start.sh"]
2020

21-
# shellcheck disable=SC1091,SC2086
22-
exec /usr/local/bin/start.sh ${wrapper} jupyter ${DOCKER_STACKS_JUPYTER_CMD} ${NOTEBOOK_ARGS} "$@"
21+
# If we want to survive restarts, tell that to start.sh
22+
if os.environ.get("RESTARTABLE") == "yes":
23+
command.append("run-one-constantly")
24+
25+
# We always launch a jupyter subcommand from this script
26+
command.append("jupyter")
27+
28+
# Launch the configured subcommand. Note that this should be a single string, so we don't split it
29+
# We default to lab
30+
jupyter_command = os.environ.get("DOCKER_STACKS_JUPYTER_CMD", "lab")
31+
command.append(jupyter_command)
32+
33+
# Append any optional NOTEBOOK_ARGS we were passed in. This is supposed to be multiple args passed
34+
# on to the notebook command, so we split it correctly with shlex
35+
command += (
36+
[]
37+
if "NOTEBOOK_ARGS" not in os.environ
38+
else shlex.split(os.environ["NOTEBOOK_ARGS"])
39+
)
40+
41+
# Execute the command!
42+
os.execvp(command[0], command)

0 commit comments

Comments
 (0)