Skip to content

Commit 0d2fc3a

Browse files
[python] Change the sudo_if function to avoid issues with Bash (#694)
* [python] Updated `sudo_if` function - Remove double quotes to avoid issues with string tokenization * Add test scenario * Revert "Add test scenario" This reverts commit 9e62a37. * Update `jupyterlab git` package name * Bump feature version * Test: Install jupyterlab under root user * Refactor changes * Bump patch version * Address review points
1 parent e7f7d19 commit 0d2fc3a

File tree

7 files changed

+97
-14
lines changed

7 files changed

+97
-14
lines changed

src/python/devcontainer-feature.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "python",
3-
"version": "1.1.0",
3+
"version": "1.2.0",
44
"name": "Python",
55
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/python",
66
"description": "Installs the provided version of Python, as well as PIPX, and other common Python utilities. JupyterLab is conditionally installed with the python feature. Note: May require source code compilation.",

src/python/install.sh

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -304,25 +304,31 @@ sudo_if() {
304304
if [ "$(id -u)" -eq 0 ] && [ "$USERNAME" != "root" ]; then
305305
su - "$USERNAME" -c "$COMMAND"
306306
else
307-
"$COMMAND"
307+
$COMMAND
308308
fi
309309
}
310310

311311
install_user_package() {
312-
PACKAGE="$1"
313-
sudo_if "${PYTHON_SRC}" -m pip install --user --upgrade --no-cache-dir "$PACKAGE"
312+
INSTALL_UNDER_ROOT="$1"
313+
PACKAGE="$2"
314+
315+
if [ "$INSTALL_UNDER_ROOT" = true ]; then
316+
sudo_if "${PYTHON_SRC}" -m pip install --upgrade --no-cache-dir "$PACKAGE"
317+
else
318+
sudo_if "${PYTHON_SRC}" -m pip install --user --upgrade --no-cache-dir "$PACKAGE"
319+
fi
314320
}
315321

316322
add_user_jupyter_config() {
317-
CONFIG_DIR="/home/$USERNAME/.jupyter"
318-
CONFIG_FILE="$CONFIG_DIR/jupyter_server_config.py"
323+
CONFIG_DIR="$1"
324+
CONFIG_FILE="$2"
319325

320326
# Make sure the config file exists or create it with proper permissions
321327
test -d "$CONFIG_DIR" || sudo_if mkdir "$CONFIG_DIR"
322328
test -f "$CONFIG_FILE" || sudo_if touch "$CONFIG_FILE"
323329

324330
# Don't write the same config more than once
325-
grep -q "$1" "$CONFIG_FILE" || echo "$1" >> "$CONFIG_FILE"
331+
grep -q "$3" "$CONFIG_FILE" || echo "$3" >> "$CONFIG_FILE"
326332
}
327333

328334
install_python() {
@@ -461,13 +467,26 @@ if [ "${INSTALL_JUPYTERLAB}" = "true" ]; then
461467
exit 1
462468
fi
463469

464-
install_user_package jupyterlab
465-
install_user_package jupyterlab-git
470+
INSTALL_UNDER_ROOT=true
471+
if [ "$(id -u)" -eq 0 ] && [ "$USERNAME" != "root" ]; then
472+
INSTALL_UNDER_ROOT=false
473+
fi
474+
475+
install_user_package $INSTALL_UNDER_ROOT jupyterlab
476+
install_user_package $INSTALL_UNDER_ROOT jupyterlab-git
466477

467478
# Configure JupyterLab if needed
468479
if [ -n "${CONFIGURE_JUPYTERLAB_ALLOW_ORIGIN}" ]; then
469-
add_user_jupyter_config "c.ServerApp.allow_origin = '${CONFIGURE_JUPYTERLAB_ALLOW_ORIGIN}'"
470-
add_user_jupyter_config "c.NotebookApp.allow_origin = '${CONFIGURE_JUPYTERLAB_ALLOW_ORIGIN}'"
480+
# Resolve config directory
481+
CONFIG_DIR="/root/.jupyter"
482+
if [ "$INSTALL_UNDER_ROOT" = false ]; then
483+
CONFIG_DIR="/home/$USERNAME/.jupyter"
484+
fi
485+
486+
CONFIG_FILE="$CONFIG_DIR/jupyter_server_config.py"
487+
488+
add_user_jupyter_config $CONFIG_DIR $CONFIG_FILE "c.ServerApp.allow_origin = '${CONFIGURE_JUPYTERLAB_ALLOW_ORIGIN}'"
489+
add_user_jupyter_config $CONFIG_DIR $CONFIG_FILE "c.NotebookApp.allow_origin = '${CONFIGURE_JUPYTERLAB_ALLOW_ORIGIN}'"
471490
fi
472491
fi
473492

test/python/install_additional_jupyterlab.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ packages="$(python3 -m pip list)"
1717
check "location" grep jupyter <<< "$packages"
1818

1919
# Check for git extension
20-
check "jupyterlab-git" grep jupyterlab-git <<< "$packages"
20+
check "jupyterlab_git" grep jupyterlab_git <<< "$packages"
2121

2222
# Check for correct JupyterLab configuration
2323
check "config" grep ".*.allow_origin = '*'" /home/vscode/.jupyter/jupyter_server_config.py

test/python/install_jupyterlab.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ packages="$(python3 -m pip list)"
1717
check "location" grep jupyter <<< "$packages"
1818

1919
# Check for git extension
20-
check "jupyterlab-git" grep jupyterlab-git <<< "$packages"
20+
check "jupyterlab_git" grep jupyterlab_git <<< "$packages"
2121

2222
# Check for correct JupyterLab configuration
2323
check "config" grep ".*.allow_origin = '*'" /home/vscode/.jupyter/jupyter_server_config.py
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Optional: Import test library
6+
source dev-container-features-test-lib
7+
8+
# Check for an installation of JupyterLab
9+
check "version" jupyter lab --version
10+
11+
# Check location of JupyterLab installation
12+
packages="$(python3 -m pip list)"
13+
check "location" grep jupyter <<< "$packages"
14+
15+
# Check for git extension
16+
check "jupyterlab_git" grep jupyterlab_git <<< "$packages"
17+
18+
# Check for correct JupyterLab configuration
19+
check "config" grep ".*.allow_origin = '*'" /root/.jupyter/jupyter_server_config.py
20+
21+
# Report result
22+
reportResults
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Optional: Import test library
6+
source dev-container-features-test-lib
7+
8+
# Check for an installation of JupyterLab
9+
check "version" jupyter lab --version
10+
11+
# Check location of JupyterLab installation
12+
packages="$(python3 -m pip list)"
13+
check "location" grep jupyter <<< "$packages"
14+
15+
# Check for git extension
16+
check "jupyterlab_git" grep jupyterlab_git <<< "$packages"
17+
18+
# Check for correct JupyterLab configuration
19+
check "config" grep ".*.allow_origin = '*'" /root/.jupyter/jupyter_server_config.py
20+
21+
# Report result
22+
reportResults

test/python/scenarios.json

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,25 @@
5353
"features": {
5454
"python": "3.10"
5555
}
56+
},
57+
"install_jupyterlab_debian": {
58+
"image": "debian:bullseye-slim",
59+
"features": {
60+
"python": {
61+
"version": "3.11",
62+
"installJupyterlab": true,
63+
"configureJupyterlabAllowOrigin": "*"
64+
}
65+
}
66+
},
67+
"install_jupyterlab_ubuntu": {
68+
"image": "ubuntu:focal",
69+
"features": {
70+
"python": {
71+
"version": "3.11",
72+
"installJupyterlab": true,
73+
"configureJupyterlabAllowOrigin": "*"
74+
}
75+
}
5676
}
57-
}
77+
}

0 commit comments

Comments
 (0)