create_venv.sh 1.52 KB
#!/bin/bash
#
# Create and initialize Python venv for saas-search.
#
# Usage:
#   ./scripts/create_venv.sh
#
set -euo pipefail

PROJECT_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "${PROJECT_ROOT}"

VENV_DIR="${PROJECT_ROOT}/.venv"

PYTHON_BIN="${PYTHON_BIN:-python3.10}"
if ! command -v "${PYTHON_BIN}" >/dev/null 2>&1; then
  PYTHON_BIN="python3"
fi

echo "Using python: $(${PYTHON_BIN} --version)"

if ! "${PYTHON_BIN}" -c "import ensurepip" >/dev/null 2>&1; then
  echo "ERROR: ensurepip is not available for ${PYTHON_BIN}." >&2
  echo "On Ubuntu/Debian, install the venv package first, e.g.:" >&2
  echo "  sudo apt-get update -y && sudo apt-get install -y python3-venv" >&2
  echo "If you are using Python 3.12 specifically, you may need:" >&2
  echo "  sudo apt-get install -y python3.12-venv" >&2
  exit 1
fi

if [[ -d "${VENV_DIR}" ]]; then
  if [[ -f "${VENV_DIR}/bin/activate" ]]; then
    echo "venv already exists at ${VENV_DIR}"
  else
    echo "Found incomplete venv at ${VENV_DIR}, recreating..."
    rm -rf "${VENV_DIR}"
    "${PYTHON_BIN}" -m venv "${VENV_DIR}"
  fi
else
  echo "Creating venv at ${VENV_DIR} ..."
  "${PYTHON_BIN}" -m venv "${VENV_DIR}"
fi

# shellcheck disable=SC1091
source "${VENV_DIR}/bin/activate"

python -m pip install --upgrade pip wheel
python -m pip install -r requirements.txt

if [[ "${INSTALL_ML:-0}" == "1" ]]; then
  echo
  echo "INSTALL_ML=1 detected. Installing optional ML dependencies..."
  python -m pip install -r requirements_ml.txt
fi

echo
echo "Done."
echo "Next:"
echo "  source activate.sh"