#!/usr/bin/env bash set -euo pipefail echo "[python] starting python devcontainer install" # -------------------------------------------------- # Config # -------------------------------------------------- RUNTIME_ROOT="/opt/zlh/runtime/python" ARTIFACT_ROOT="/opt/zlh/devcontainer/python" VERSION="${RUNTIME_VERSION:-3.12}" SRC_DIR="${ARTIFACT_ROOT}/${VERSION}" DEST_DIR="${RUNTIME_ROOT}/${VERSION}" SYMLINK="${RUNTIME_ROOT}/current" # -------------------------------------------------- # Guards # -------------------------------------------------- if [ ! -d "${SRC_DIR}" ]; then echo "[python][ERROR] artifact directory not found: ${SRC_DIR}" exit 1 fi if [ -x "${DEST_DIR}/bin/python3" ]; then echo "[python] python ${VERSION} already installed" else ARCHIVE="$(ls ${SRC_DIR}/Python-*.tgz 2>/dev/null | head -n1)" if [ -z "${ARCHIVE}" ]; then echo "[python][ERROR] no Python archive found in ${SRC_DIR}" exit 1 fi echo "[python] building python ${VERSION}" mkdir -p "${DEST_DIR}" BUILD_DIR="$(mktemp -d)" tar -xzf "${ARCHIVE}" -C "${BUILD_DIR}" PY_SRC="$(find ${BUILD_DIR} -maxdepth 1 -type d -name 'Python-*' | head -n1)" if [ -z "${PY_SRC}" ]; then echo "[python][ERROR] failed to locate extracted python source" exit 1 fi cd "${PY_SRC}" ./configure \ --prefix="${DEST_DIR}" \ --enable-optimizations \ --with-ensurepip=install make -j"$(nproc)" make install cd / rm -rf "${BUILD_DIR}" fi # -------------------------------------------------- # Symlink current # -------------------------------------------------- ln -sfn "${DEST_DIR}" "${SYMLINK}" # -------------------------------------------------- # Verify # -------------------------------------------------- "${SYMLINK}/bin/python3" --version "${SYMLINK}/bin/pip3" --version echo "[python] python ${VERSION} install complete"