Skip to content
This repository was archived by the owner on Nov 30, 2023. It is now read-only.

Commit a5358ec

Browse files
authored
Refactor Python package installer (#1437)
* Remove python-package script * Install JupyterLab with feature script * Install packages using private script * Remove duplicate install * Install Python packages from one script
1 parent ba0cb7a commit a5358ec

File tree

8 files changed

+89
-187
lines changed

8 files changed

+89
-187
lines changed

containers/codespaces-linux/.devcontainer/base.Dockerfile

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ ENV SHELL=/bin/bash \
3737
ENV PATH="${NVM_DIR}/current/bin:${NPM_GLOBAL}/bin:${PYTHON_ROOT}/current/bin:${ORIGINAL_PATH}:${DOTNET_ROOT}:${DOTNET_ROOT}/tools:${SDKMAN_DIR}/bin:${SDKMAN_DIR}/candidates/gradle/current/bin:${SDKMAN_DIR}/candidates/java/current/bin:/opt/maven/lts:${GOROOT}/bin:${GOPATH}/bin:${PIPX_BIN_DIR}:/opt/conda/condabin:${JAVA_ROOT}/current/bin:${NODE_ROOT}/current/bin:${PHP_ROOT}/current/bin:${RUBY_ROOT}/current/bin:${MAVEN_ROOT}/current/bin:${HUGO_ROOT}/current/bin:${JUPYTERLAB_PATH}:${ORYX_PATHS}"
3838

3939
# Install needed utilities and setup non-root user. Use a separate RUN statement to add your own dependencies.
40-
COPY library-scripts/* setup-user.sh first-run-notice.txt /tmp/scripts/
40+
COPY library-scripts/* setup-user.sh setup-python-tools.sh first-run-notice.txt /tmp/scripts/
4141
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
4242
# Restore man command
4343
&& yes | unminimize 2>&1 \
@@ -72,17 +72,8 @@ RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
7272
RUN bash /tmp/scripts/python-debian.sh "none" "/opt/python/latest" "${PIPX_HOME}" "${USERNAME}" "true" \
7373
# Install JupyterLab and common machine learning packages
7474
&& PYTHON_BINARY="${PYTHON_ROOT}/current/bin/python" \
75-
&& bash /tmp/scripts/python-package-debian.sh "jupyterlab" "latest" $PYTHON_BINARY \
76-
&& bash /tmp/scripts/python-package-debian.sh "numpy" "latest" $PYTHON_BINARY \
77-
&& bash /tmp/scripts/python-package-debian.sh "pandas" "latest" $PYTHON_BINARY \
78-
&& bash /tmp/scripts/python-package-debian.sh "scipy" "latest" $PYTHON_BINARY \
79-
&& bash /tmp/scripts/python-package-debian.sh "matplotlib" "latest" $PYTHON_BINARY \
80-
&& bash /tmp/scripts/python-package-debian.sh "seaborn" "latest" $PYTHON_BINARY \
81-
&& bash /tmp/scripts/python-package-debian.sh "scikit-learn" "latest" $PYTHON_BINARY \
82-
&& bash /tmp/scripts/python-package-debian.sh "tensorflow" "latest" $PYTHON_BINARY \
83-
&& bash /tmp/scripts/python-package-debian.sh "keras" "latest" $PYTHON_BINARY \
84-
&& bash /tmp/scripts/python-package-debian.sh "torch" "latest" $PYTHON_BINARY \
85-
&& bash /tmp/scripts/python-package-debian.sh "requests" "latest" $PYTHON_BINARY \
75+
&& bash /tmp/scripts/jupyterlab-debian.sh "latest" "automatic" ${PYTHON_BINARY} \
76+
&& bash /tmp/scripts/setup-python-tools.sh ${PYTHON_BINARY} \
8677
# Install rvm, rbenv, any missing base gems
8778
&& chown -R ${USERNAME} /opt/ruby/* \
8879
&& bash /tmp/scripts/ruby-debian.sh "none" "${USERNAME}" "true" "true" \

containers/codespaces-linux/.devcontainer/library-scripts/jupyterlab-debian.sh

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ set -e
1313

1414
VERSION=${1:-"latest"}
1515
USERNAME=${2:-"automatic"}
16+
PYTHON=${3:-"python"}
1617

1718
# If in automatic mode, determine if a user already exists, if not use vscode
1819
if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
@@ -33,28 +34,25 @@ elif [ "${USERNAME}" = "none" ]; then
3334
USER_GID=0
3435
fi
3536

36-
# Use sudo to run as non-root user is not already running
37-
sudoUserIf()
38-
{
37+
# Make sure we run the command as non-root user
38+
sudoUserIf() {
3939
if [ "$(id -u)" -eq 0 ] && [ "${USERNAME}" != "root" ]; then
4040
sudo -u ${USERNAME} "$@"
4141
else
4242
"$@"
4343
fi
4444
}
4545

46-
# If we don't yet have Python, install it now.
47-
if ! python --version > /dev/null ; then
46+
# Make sure that Python is available
47+
if ! ${PYTHON} --version > /dev/null ; then
4848
echo "You need to install Python before installing JupyterLab."
4949
exit 1
5050
fi
5151

52-
# If we don't already have JupyterLab installed, install it now.
53-
if ! jupyter-lab --version > /dev/null ; then
54-
echo "Installing JupyterLab..."
55-
if [ "${VERSION}" = "latest" ]; then
56-
sudoUserIf pip install jupyterlab
57-
else
58-
sudoUserIf pip install jupyterlab=="${VERSION}" --no-cache-dir
59-
fi
52+
# pip skips installation if JupyterLab is already installed
53+
echo "Installing JupyterLab..."
54+
if [ "${VERSION}" = "latest" ]; then
55+
sudoUserIf ${PYTHON} -m pip install jupyterlab --no-cache-dir
56+
else
57+
sudoUserIf ${PYTHON} -m pip install jupyterlab=="${VERSION}" --no-cache-dir
6058
fi

containers/codespaces-linux/.devcontainer/library-scripts/python-package-debian.sh

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
PYTHON=${1:-"python"}
6+
USERNAME=${2-"automatic"}
7+
8+
# Make sure we run the command as non-root user
9+
sudoUserIf() {
10+
if [ "$(id -u)" -eq 0 ] && [ "${USERNAME}" != "root" ]; then
11+
sudo -u ${USERNAME} "$@"
12+
else
13+
"$@"
14+
fi
15+
}
16+
17+
installPythonPackage() {
18+
PACKAGE=${1:-""}
19+
VERSION=${2:-"latest"}
20+
21+
# pip skips installation if the package is already installed
22+
echo "Installing $PACKAGE..."
23+
if [ "${VERSION}" = "latest" ]; then
24+
sudoUserIf ${PYTHON} -m pip install ${PACKAGE} --no-cache-dir
25+
else
26+
sudoUserIf ${PYTHON} -m pip install ${PACKAGE}=="${VERSION}" --no-cache-dir
27+
fi
28+
}
29+
30+
# If in automatic mode, determine if a user already exists, if not use vscode
31+
if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
32+
USERNAME=""
33+
POSSIBLE_USERS=("vscode" "node" "codespace" "$(awk -v val=1000 -F ":" '$3==val{print $1}' /etc/passwd)")
34+
for CURRENT_USER in ${POSSIBLE_USERS[@]}; do
35+
if id -u ${CURRENT_USER} > /dev/null 2>&1; then
36+
USERNAME=${CURRENT_USER}
37+
break
38+
fi
39+
done
40+
if [ "${USERNAME}" = "" ]; then
41+
USERNAME=vscode
42+
fi
43+
elif [ "${USERNAME}" = "none" ]; then
44+
USERNAME=root
45+
USER_UID=0
46+
USER_GID=0
47+
fi
48+
49+
# Make sure that Python is available
50+
if ! ${PYTHON} --version > /dev/null ; then
51+
echo "You need to install Python before installing packages"
52+
exit 1
53+
fi
54+
55+
installPythonPackage "numpy" "latest"
56+
installPythonPackage "pandas" "latest"
57+
installPythonPackage "scipy" "latest"
58+
installPythonPackage "matplotlib" "latest"
59+
installPythonPackage "seaborn" "latest"
60+
installPythonPackage "scikit-learn" "latest"
61+
installPythonPackage "tensorflow" "latest"
62+
installPythonPackage "keras" "latest"
63+
installPythonPackage "torch" "latest"
64+
installPythonPackage "requests" "latest"

script-library/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ Some scripts have special installation instructions (like `desktop-lite-debian.s
3535
| [Node.js Install Script](docs/node.md) | `node-debian.sh` | VS Code and GitHub Codespaces teams|
3636
| [PowerShell Install Script](docs/powershell.md) | `powershell-debian.sh` | VS Code and GitHub Codespaces teams|
3737
| [Python Install Script](docs/python.md) | `python-debian.sh` | VS Code and GitHub Codespaces teams|
38-
| [Python Package Install Script](docs/python-package.md) | `python-package-debian.sh` | VS Code and GitHub Codespaces teams|
3938
| [Ruby Install Script](docs/ruby.md) | `ruby-debian.sh` | VS Code and GitHub Codespaces teams|
4039
| [Rust (rustlang) Install Script](docs/rust.md) | `rust-debian.sh` | VS Code and GitHub Codespaces teams|
4140
| [SSH Server Install Script](docs/sshd.md) | `sshd-debian.sh` | VS Code and GitHub Codespaces teams|

script-library/docs/python-package.md

Lines changed: 0 additions & 30 deletions
This file was deleted.

script-library/jupyterlab-debian.sh

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ set -e
1313

1414
VERSION=${1:-"latest"}
1515
USERNAME=${2:-"automatic"}
16+
PYTHON=${3:-"python"}
1617

1718
# If in automatic mode, determine if a user already exists, if not use vscode
1819
if [ "${USERNAME}" = "auto" ] || [ "${USERNAME}" = "automatic" ]; then
@@ -33,28 +34,25 @@ elif [ "${USERNAME}" = "none" ]; then
3334
USER_GID=0
3435
fi
3536

36-
# Use sudo to run as non-root user is not already running
37-
sudoUserIf()
38-
{
37+
# Make sure we run the command as non-root user
38+
sudoUserIf() {
3939
if [ "$(id -u)" -eq 0 ] && [ "${USERNAME}" != "root" ]; then
4040
sudo -u ${USERNAME} "$@"
4141
else
4242
"$@"
4343
fi
4444
}
4545

46-
# If we don't yet have Python, install it now.
47-
if ! python --version > /dev/null ; then
46+
# Make sure that Python is available
47+
if ! ${PYTHON} --version > /dev/null ; then
4848
echo "You need to install Python before installing JupyterLab."
4949
exit 1
5050
fi
5151

52-
# If we don't already have JupyterLab installed, install it now.
53-
if ! jupyter-lab --version > /dev/null ; then
54-
echo "Installing JupyterLab..."
55-
if [ "${VERSION}" = "latest" ]; then
56-
sudoUserIf pip install jupyterlab
57-
else
58-
sudoUserIf pip install jupyterlab=="${VERSION}" --no-cache-dir
59-
fi
52+
# pip skips installation if JupyterLab is already installed
53+
echo "Installing JupyterLab..."
54+
if [ "${VERSION}" = "latest" ]; then
55+
sudoUserIf ${PYTHON} -m pip install jupyterlab --no-cache-dir
56+
else
57+
sudoUserIf ${PYTHON} -m pip install jupyterlab=="${VERSION}" --no-cache-dir
6058
fi

script-library/python-package-debian.sh

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)