@@ -19,6 +19,9 @@ USERNAME=${USERNAME:-"automatic"}
1919UPDATE_RC=${UPDATE_RC:- " true" }
2020USE_ORYX_IF_AVAILABLE=${USE_ORYX_IF_AVAILABLE:- " true" }
2121
22+ INSTALL_JUPYTERLAB=${INSTALL_JUPYTERLAB:- " false" }
23+ CONFIGURE_JUPYTERLAB_ALLOW_ORIGIN=${CONFIGURE_JUPYTERLAB_ALLOW_ORIGIN:- " " }
24+
2225DEFAULT_UTILS=(" pylint" " flake8" " autopep8" " black" " yapf" " mypy" " pydocstyle" " pycodestyle" " bandit" " pipenv" " virtualenv" )
2326PYTHON_SOURCE_GPG_KEYS=" 64E628F8D684696D B26995E310250568 2D347EA6AA65421D FB9921286F5E1540 3A5CA953F73C700D 04C367C218ADD4FF 0EDDC5F26A45C816 6AF053F07D9DC8D2 C9BE28DEE6DF025C 126EB563A74B06BF D9866941EA5BBD71 ED9D77D5"
2427GPG_KEY_SERVERS=" keyserver hkp://keyserver.ubuntu.com:80
@@ -301,6 +304,32 @@ install_using_oryx() {
301304 add_symlink
302305}
303306
307+ sudo_if () {
308+ COMMAND=" $* "
309+ if [ " $( id -u) " -eq 0 ] && [ " $USERNAME " != " root" ]; then
310+ su - " $USERNAME " -c " $COMMAND "
311+ else
312+ " $COMMAND "
313+ fi
314+ }
315+
316+ install_user_package () {
317+ PACKAGE=" $1 "
318+ sudo_if " $INSTALL_PATH /bin/python3" -m pip install --user --upgrade --no-cache-dir " $PACKAGE "
319+ }
320+
321+ add_user_jupyter_config () {
322+ CONFIG_DIR=" /home/$USERNAME /.jupyter"
323+ CONFIG_FILE=" $CONFIG_DIR /jupyter_notebook_config.py"
324+
325+ # Make sure the config file exists or create it with proper permissions
326+ test -d " $CONFIG_DIR " || sudo_if mkdir " $CONFIG_DIR "
327+ test -f " $CONFIG_FILE " || sudo_if touch " $CONFIG_FILE "
328+
329+ # Don't write the same config more than once
330+ grep -q " $1 " " $CONFIG_FILE " || echo " $1 " >> " $CONFIG_FILE "
331+ }
332+
304333# Ensure apt is in non-interactive to avoid prompts
305334export DEBIAN_FRONTEND=noninteractive
306335
@@ -310,7 +339,7 @@ check_packages curl ca-certificates gnupg2 tar make gcc libssl-dev zlib1g-dev li
310339 libxmlsec1-dev libsqlite3-dev libffi-dev liblzma-dev uuid-dev
311340
312341
313- # Install python from source if needed
342+ # Install Python from source if needed
314343if [ " ${PYTHON_VERSION} " != " none" ]; then
315344 CURRENT_PATH=" ${PYTHON_INSTALL_PATH} /current"
316345 # If the os-provided versions are "good enough", detect that and bail out.
@@ -330,53 +359,62 @@ if [ "${PYTHON_VERSION}" != "none" ]; then
330359 updaterc " if [[ \"\$ {PATH}\" != *\" ${CURRENT_PATH} /bin\" * ]]; then export PATH=${CURRENT_PATH} /bin:\$ {PATH}; fi"
331360fi
332361
333- # If not installing python tools, exit
334- if [ " ${INSTALL_PYTHON_TOOLS} " ! = " true" ]; then
335- echo " Done! "
336- exit 0
337- fi
362+ # Install Python tools if needed
363+ if [ " ${INSTALL_PYTHON_TOOLS} " = " true" ]; then
364+ echo ' Installing Python tools... '
365+ export PIPX_BIN_DIR= " ${PIPX_HOME} /bin "
366+ export PATH= " ${CURRENT_PATH} /bin: ${PIPX_BIN_DIR} : ${PATH} "
338367
339- export PIPX_BIN_DIR=" ${PIPX_HOME} /bin"
340- export PATH=" ${CURRENT_PATH} /bin:${PIPX_BIN_DIR} :${PATH} "
368+ # Create pipx group, dir, and set sticky bit
369+ if ! cat /etc/group | grep -e " ^pipx:" > /dev/null 2>&1 ; then
370+ groupadd -r pipx
371+ fi
372+ usermod -a -G pipx ${USERNAME}
373+ umask 0002
374+ mkdir -p ${PIPX_BIN_DIR}
375+ chown :pipx ${PIPX_HOME} ${PIPX_BIN_DIR}
376+ chmod g+s ${PIPX_HOME} ${PIPX_BIN_DIR}
377+
378+ # Update pip if not using os provided python
379+ if [ ${PYTHON_VERSION} != " os-provided" ] && [ ${PYTHON_VERSION} != " system" ] && [ ${PYTHON_VERSION} != " none" ]; then
380+ echo " Updating pip..."
381+ " ${INSTALL_PATH} /bin/python3" -m pip install --no-cache-dir --upgrade pip
382+ fi
341383
342- # Create pipx group, dir, and set sticky bit
343- if ! cat /etc/group | grep -e " ^pipx:" > /dev/null 2>&1 ; then
344- groupadd -r pipx
345- fi
346- usermod -a -G pipx ${USERNAME}
347- umask 0002
348- mkdir -p ${PIPX_BIN_DIR}
349- chown :pipx ${PIPX_HOME} ${PIPX_BIN_DIR}
350- chmod g+s ${PIPX_HOME} ${PIPX_BIN_DIR}
351-
352- # Update pip if not using os provided python
353- if [ ${PYTHON_VERSION} != " os-provided" ] && [ ${PYTHON_VERSION} != " system" ] && [ ${PYTHON_VERSION} != " none" ]; then
354- echo " Updating pip..."
355- ${INSTALL_PATH} /bin/python3 -m pip install --no-cache-dir --upgrade pip
356- fi
384+ # Install tools
385+ echo " Installing Python tools..."
386+ export PYTHONUSERBASE=/tmp/pip-tmp
387+ export PIP_CACHE_DIR=/tmp/pip-tmp/cache
388+ PIPX_DIR=" "
389+ if ! type pipx > /dev/null 2>&1 ; then
390+ pip3 install --disable-pip-version-check --no-cache-dir --user pipx 2>&1
391+ /tmp/pip-tmp/bin/pipx install --pip-args=--no-cache-dir pipx
392+ PIPX_DIR=" /tmp/pip-tmp/bin"
393+ fi
394+ for util in " ${DEFAULT_UTILS[@]} " ; do
395+ if ! type ${util} > /dev/null 2>&1 ; then
396+ " ${PIPX_DIR} /pipx" install --system-site-packages --pip-args ' --no-cache-dir --force-reinstall' ${util}
397+ else
398+ echo " ${util} already installed. Skipping."
399+ fi
400+ done
401+ rm -rf /tmp/pip-tmp
357402
358- # Install tools
359- echo " Installing Python tools..."
360- export PYTHONUSERBASE=/tmp/pip-tmp
361- export PIP_CACHE_DIR=/tmp/pip-tmp/cache
362- pipx_path=" "
363- if ! type pipx > /dev/null 2>&1 ; then
364- pip3 install --disable-pip-version-check --no-cache-dir --user pipx 2>&1
365- /tmp/pip-tmp/bin/pipx install --pip-args=--no-cache-dir pipx
366- pipx_path=" /tmp/pip-tmp/bin/"
403+ updaterc " export PIPX_HOME=\" ${PIPX_HOME} \" "
404+ updaterc " export PIPX_BIN_DIR=\" ${PIPX_BIN_DIR} \" "
405+ updaterc " if [[ \"\$ {PATH}\" != *\"\$ {PIPX_BIN_DIR}\" * ]]; then export PATH=\"\$ {PATH}:\$ {PIPX_BIN_DIR}\" ; fi"
367406fi
368- for util in " ${DEFAULT_UTILS[@]} " ; do
369- if ! type ${util} > /dev/null 2>&1 ; then
370- ${pipx_path} pipx install --system-site-packages --pip-args ' --no-cache-dir --force-reinstall' ${util}
371- else
372- echo " ${util} already installed. Skipping."
407+
408+ # Install JupyterLab if needed
409+ if [ " ${INSTALL_JUPYTERLAB} " = " true" ]; then
410+ install_user_package jupyterlab
411+
412+ # Configure JupyterLab if needed
413+ # TODO: True if it's not empty
414+ if [ -n " ${CONFIGURE_JUPYTERLAB_ALLOW_ORIGIN} " ]; then
415+ add_user_jupyter_config " c.ServerApp.allow_origin = '${CONFIGURE_JUPYTERLAB_ALLOW_ORIGIN} '"
416+ add_user_jupyter_config " c.NotebookApp.allow_origin = '${CONFIGURE_JUPYTERLAB_ALLOW_ORIGIN} '"
373417 fi
374- done
375- rm -rf /tmp/pip-tmp
376-
377- updaterc " $( cat << EOF
378- export PIPX_HOME="${PIPX_HOME} "
379- export PIPX_BIN_DIR="${PIPX_BIN_DIR} "
380- if [[ "\$ {PATH}" != *"\$ {PIPX_BIN_DIR}"* ]]; then export PATH="\$ {PATH}:\$ {PIPX_BIN_DIR}"; fi
381- EOF
382- ) "
418+ fi
419+
420+ echo " Done!"
0 commit comments