#!/bin/bash # # Create isolated venv for translator service (.venv-translator). # set -euo pipefail PROJECT_ROOT="$(cd "$(dirname "$0")/.." && pwd)" cd "${PROJECT_ROOT}" VENV_DIR="${PROJECT_ROOT}/.venv-translator" TMP_DIR="${TRANSLATOR_PIP_TMPDIR:-${PROJECT_ROOT}/.tmp/translator-pip}" MIN_PYTHON_MAJOR=3 MIN_PYTHON_MINOR=10 python_meets_minimum() { local bin="$1" "${bin}" - <<'PY' "${MIN_PYTHON_MAJOR}" "${MIN_PYTHON_MINOR}" import sys required = tuple(int(value) for value in sys.argv[1:]) sys.exit(0 if sys.version_info[:2] >= required else 1) PY } discover_python_bin() { local candidates=() if [[ -n "${PYTHON_BIN:-}" ]]; then candidates+=("${PYTHON_BIN}") fi candidates+=("python3.12" "python3.11" "python3.10" "python3") local candidate for candidate in "${candidates[@]}"; do if ! command -v "${candidate}" >/dev/null 2>&1; then continue fi if python_meets_minimum "${candidate}"; then echo "${candidate}" return 0 fi done return 1 } if ! PYTHON_BIN="$(discover_python_bin)"; then echo "ERROR: unable to find Python >= ${MIN_PYTHON_MAJOR}.${MIN_PYTHON_MINOR}." >&2 echo "Set PYTHON_BIN to a compatible interpreter and rerun." >&2 exit 1 fi if ! command -v "${PYTHON_BIN}" >/dev/null 2>&1; then echo "ERROR: python not found: ${PYTHON_BIN}" >&2 exit 1 fi if [[ -d "${VENV_DIR}" && ! -f "${VENV_DIR}/bin/activate" ]]; then echo "Found broken venv at ${VENV_DIR}, recreating..." rm -rf "${VENV_DIR}" fi if [[ ! -d "${VENV_DIR}" ]]; then echo "Creating ${VENV_DIR}" "${PYTHON_BIN}" -m venv "${VENV_DIR}" else echo "Reusing ${VENV_DIR}" fi mkdir -p "${TMP_DIR}" export TMPDIR="${TMP_DIR}" PIP_ARGS=(--no-cache-dir) echo "Using Python=${PYTHON_BIN}" echo "Using TMPDIR=${TMPDIR}" "${VENV_DIR}/bin/python" -m pip install "${PIP_ARGS[@]}" --upgrade pip wheel "${VENV_DIR}/bin/python" -m pip install "${PIP_ARGS[@]}" -r requirements_translator_service.txt echo echo "Done." echo "Translator venv: ${VENV_DIR}" echo "Download local models: ./.venv-translator/bin/python scripts/translation/download_translation_models.py --all-local" echo "Start service: ./scripts/start_translator.sh"