356 lines
10 KiB
Bash
Executable File
356 lines
10 KiB
Bash
Executable File
#!/bin/bash
|
|
#
|
|
# Project bootstrap script - Sets up Python development environment
|
|
#
|
|
# This script MUST be sourced to properly set up the environment:
|
|
# source bootstrap
|
|
#
|
|
# What it does:
|
|
# 1. Installs pyenv and required system dependencies if needed
|
|
# 2. Initializes pyenv and pyenv-virtualenv in the current shell
|
|
# 3. Creates a Python virtual environment named 'test_env' (or custom name)
|
|
# 4. Activates the virtual environment
|
|
# 5. Installs all project requirements from requirements.txt files
|
|
#
|
|
# With --clean/-c option:
|
|
# - Deletes existing environment before creating a new one
|
|
#
|
|
# Usage:
|
|
# source bootstrap [--pyenv] [-c|--clean] [--env custom_name] [--python 3.x.x]
|
|
# ex. source bootstrap --pyenv -c --env my_env --python 3.10.6
|
|
|
|
# =============================================
|
|
# VARIABLES
|
|
# =============================================
|
|
PYTHON_VERSION="3.13.8" # Default Python version
|
|
export PYENV_NAME="test_env" # Default environment name
|
|
FORCE_PYENV_INSTALL=false # Flag to force pyenv reinstallation
|
|
CLEAN_ENVIRONMENT=false # Flag to delete and recreate environment
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # scripts root directory
|
|
|
|
source "SCRIPT_DIR/src/utils.sh"
|
|
|
|
# Parse command line arguments
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
--pyenv)
|
|
FORCE_PYENV_INSTALL=true
|
|
shift
|
|
;;
|
|
-c|--clean)
|
|
CLEAN_ENVIRONMENT=true
|
|
shift
|
|
;;
|
|
--env)
|
|
export PYENV_NAME="$2"
|
|
shift 2
|
|
;;
|
|
--python)
|
|
PYTHON_VERSION="$2"
|
|
shift 2
|
|
;;
|
|
*)
|
|
echo_error "Unknown option: $1"
|
|
echo_error "Usage: source bootstrap [--pyenv] [-c|--clean] [--env name] [--python version]"
|
|
return 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
activate_environment() {
|
|
# Check if environment exists
|
|
if ! pyenv virtualenvs | grep -q "$PYENV_NAME"; then
|
|
echo_status "Creating virtual environment $PYENV_NAME..."
|
|
pyenv virtualenv "$PYTHON_VERSION" "$PYENV_NAME" || {
|
|
echo_error "Failed to create virtual environment $PYENV_NAME"
|
|
return 1
|
|
}
|
|
fi
|
|
|
|
# Activate the environment
|
|
echo_status "Activating $PYENV_NAME..."
|
|
if pyenv activate "$PYENV_NAME" 2>/dev/null; then
|
|
echo_status "Successfully activated $PYENV_NAME with pyenv"
|
|
return 0
|
|
fi
|
|
|
|
local activate_script="${SCRIPT_DIR}/src/$PYENV_NAME/bin/activate"
|
|
if [ -f "$activate_script" ]; then
|
|
source "$activate_script"
|
|
echo_status "Successfully activated $PYENV_NAME"
|
|
return 0
|
|
fi
|
|
|
|
echo_error "Failed to activate $PYENV_NAME"
|
|
return 1
|
|
}
|
|
|
|
install_requirements() {
|
|
echo_status "Installing requirements..."
|
|
|
|
# Find and install all requirements.txt files
|
|
find "${SCRIPT_DIR}/src" -name "requirements.txt" | while read -r req_file; do
|
|
echo_status "Installing requirements from: $req_file"
|
|
pip install -q -r "$req_file" --break-system-packages || {
|
|
echo_error "Failed to install requirements from $req_file"
|
|
return 1
|
|
}
|
|
done
|
|
}
|
|
|
|
# =============================================
|
|
# CORE FUNCTIONS
|
|
# =============================================
|
|
|
|
check_python_installed() {
|
|
# First check if Python is installed at all
|
|
if ! command -v python &> /dev/null && ! command -v python3 &> /dev/null; then
|
|
echo_error "Python is not installed. Please install Python $PYTHON_VERSION before running this script."
|
|
echo_error "On Ubuntu/Debian: sudo apt-get install python$PYTHON_VERSION"
|
|
echo_error "On macOS: brew install python@$PYTHON_VERSION"
|
|
echo_error "Or download from: https://www.python.org/downloads/"
|
|
return 1
|
|
fi
|
|
|
|
# If pyenv is available, check if the specific version is installed
|
|
if command -v pyenv &> /dev/null; then
|
|
if ! pyenv versions | grep -q "$PYTHON_VERSION"; then
|
|
echo_status "Python $PYTHON_VERSION is not available in pyenv. Installing it now..."
|
|
pyenv install "$PYTHON_VERSION" || {
|
|
echo_error "Failed to install Python $PYTHON_VERSION via pyenv"
|
|
echo_error "You may need to install it manually first"
|
|
return 1
|
|
}
|
|
fi
|
|
fi
|
|
|
|
return 0
|
|
}
|
|
|
|
initialize_pyenv() {
|
|
# Set up pyenv environment variables
|
|
export PYENV_ROOT="$HOME/.pyenv"
|
|
export PATH="$PYENV_ROOT/bin:$PATH"
|
|
|
|
# Check if pyenv is already properly initialized
|
|
if pyenv virtualenvs &> /dev/null && pyenv activate "$PYENV_NAME" &> /dev/null; then
|
|
echo_status "pyenv is already properly initialized"
|
|
return 0
|
|
fi
|
|
|
|
# Force pyenv initialization
|
|
if [ -f "$PYENV_ROOT/bin/pyenv" ]; then
|
|
echo_status "Initializing pyenv..."
|
|
|
|
# Clear any existing pyenv functions
|
|
if declare -F | grep -q 'pyenv$'; then
|
|
unset -f pyenv
|
|
fi
|
|
|
|
# Run the initialization commands directly
|
|
eval "$("$PYENV_ROOT/bin/pyenv" init -)"
|
|
eval "$("$PYENV_ROOT/bin/pyenv" virtualenv-init -)"
|
|
|
|
# Verify initialization
|
|
if pyenv virtualenvs &> /dev/null; then
|
|
echo_status "pyenv initialized successfully"
|
|
return 0
|
|
else
|
|
echo_error "Failed to initialize pyenv"
|
|
return 1
|
|
fi
|
|
else
|
|
echo_error "pyenv not found at $PYENV_ROOT/bin/pyenv"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
install_pyenv_dependencies() {
|
|
# Install pyenv dependencies on Debian-based systems
|
|
if [ -f /etc/debian_version ]; then
|
|
echo_status "Installing pyenv dependencies..."
|
|
sudo apt-get update && sudo apt-get install -y \
|
|
make build-essential libssl-dev zlib1g-dev \
|
|
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
|
|
libncurses-dev xz-utils tk-dev libffi-dev liblzma-dev \
|
|
python3-openssl git || {
|
|
echo_error "Failed to install pyenv dependencies"
|
|
return 1
|
|
}
|
|
fi
|
|
}
|
|
|
|
install_pyenv() {
|
|
echo_status "Installing pyenv..."
|
|
curl https://pyenv.run | bash || {
|
|
echo_error "Failed to install pyenv"
|
|
return 1
|
|
}
|
|
}
|
|
|
|
check_and_install_pyenv() {
|
|
# Check if pyenv is already available
|
|
if command -v pyenv &> /dev/null && [ "$FORCE_PYENV_INSTALL" = false ]; then
|
|
echo_status "pyenv is already available"
|
|
return 0
|
|
fi
|
|
|
|
# Handle existing pyenv directory
|
|
if [ -d "$HOME/.pyenv" ]; then
|
|
if [ "$FORCE_PYENV_INSTALL" = true ]; then
|
|
echo_status "Removing existing pyenv installation..."
|
|
rm -rf "$HOME/.pyenv" || {
|
|
echo_error "Failed to remove existing pyenv installation"
|
|
return 1
|
|
}
|
|
else
|
|
echo_status "Found existing pyenv directory"
|
|
fi
|
|
fi
|
|
|
|
# Install pyenv
|
|
install_pyenv_dependencies || return 1
|
|
install_pyenv || return 1
|
|
|
|
return 0
|
|
}
|
|
|
|
clean_environment() {
|
|
if pyenv virtualenvs | grep -q "$PYENV_NAME"; then
|
|
echo_status "Deleting existing virtual environment $PYENV_NAME..."
|
|
# Redirect both stdout and stderr to /dev/null to suppress messages
|
|
pyenv uninstall -f "$PYENV_NAME" >/dev/null 2>&1 || {
|
|
echo_error "Failed to delete virtual environment $PYENV_NAME"
|
|
return 1
|
|
}
|
|
fi
|
|
}
|
|
|
|
install_python_version() {
|
|
local version="$1"
|
|
echo_status "Installing Python $version using pyenv..."
|
|
pyenv install "$version" || {
|
|
echo_error "Failed to install Python $version"
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
check_environment_exists() {
|
|
if [ "$CLEAN_ENVIRONMENT" = true ]; then
|
|
if ! clean_environment; then
|
|
return 1
|
|
fi
|
|
fi
|
|
|
|
# Check if Python version is available
|
|
if ! pyenv versions | grep -q "$PYTHON_VERSION"; then
|
|
if ! install_python_version "$PYTHON_VERSION"; then
|
|
return 1
|
|
fi
|
|
fi
|
|
|
|
if ! pyenv virtualenvs | grep -q "$PYENV_NAME"; then
|
|
echo_status "Creating virtual environment $PYENV_NAME..."
|
|
pyenv virtualenv "$PYTHON_VERSION" "$PYENV_NAME" || {
|
|
echo_error "Failed to create virtual environment $PYENV_NAME"
|
|
return 1
|
|
}
|
|
else
|
|
echo_status "Virtual environment $PYENV_NAME already exists"
|
|
fi
|
|
|
|
return 0
|
|
}
|
|
|
|
activate_environment() {
|
|
echo_status "Activating $PYENV_NAME..."
|
|
|
|
# Try to activate the environment
|
|
if pyenv activate "$PYENV_NAME"; then
|
|
echo_status "Successfully activated $PYENV_NAME"
|
|
return 0
|
|
else
|
|
echo_error "Failed to activate $PYENV_NAME"
|
|
echo_error "Error: $(pyenv activate "$PYENV_NAME" 2>&1)"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
upgrade_pip() {
|
|
echo_status "Upgrading pip..."
|
|
python -m pip install --upgrade pip || {
|
|
echo_warning "Failed to upgrade pip"
|
|
echo_warning "Continuing with requirements installation..."
|
|
}
|
|
}
|
|
|
|
install_requirements() {
|
|
echo_status "Installing requirements..."
|
|
|
|
# Find and install all requirements.txt files
|
|
find "${SCRIPT_DIR}/src" -name "requirements.txt" | while read -r req_file; do
|
|
echo_status "Installing requirements from: $req_file"
|
|
pip install -r "$req_file" --break-system-packages || {
|
|
echo_error "Failed to install requirements from $req_file"
|
|
return 1
|
|
}
|
|
done
|
|
}
|
|
|
|
# =============================================
|
|
# MAIN EXECUTION
|
|
# =============================================
|
|
echo_status "Starting project bootstrap with environment: $PYENV_NAME"
|
|
if [ "$FORCE_PYENV_INSTALL" = true ]; then
|
|
echo_status "Forcing pyenv reinstallation"
|
|
fi
|
|
|
|
# 1. Check if Python is installed
|
|
if ! check_python_installed; then
|
|
return 1
|
|
fi
|
|
|
|
# 2. Check/install pyenv
|
|
if ! check_and_install_pyenv; then
|
|
return 1
|
|
fi
|
|
|
|
# 2. Initialize pyenv
|
|
if ! initialize_pyenv; then
|
|
return 1
|
|
fi
|
|
|
|
# 3. Check/create virtual environment
|
|
if ! check_environment_exists; then
|
|
return 1
|
|
fi
|
|
|
|
# 4. Activate the environment
|
|
if ! activate_environment; then
|
|
return 1
|
|
fi
|
|
|
|
# 5. Upgrade pip
|
|
upgrade_pip
|
|
|
|
# 7. Install local Python packages in development mode
|
|
if [ -f "${SCRIPT_DIR}/src/install_packages.sh" ]; then
|
|
echo_status "Installing local Python packages in development mode..."
|
|
${SCRIPT_DIR}/src/install_packages.sh || {
|
|
echo_error "Dev packages not installed!"
|
|
return 1
|
|
}
|
|
else
|
|
echo_status "${SCRIPT_DIR}/src/install_packages.sh not found. Skipping local package installation."
|
|
fi
|
|
|
|
# 6. Install requirements
|
|
# install_requirements
|
|
|
|
echo_status "Bootstrap completed successfully!"
|
|
echo_status "Virtual environment '$PYENV_NAME' is active"
|
|
echo_status "Pyenv: $(pyenv version)"
|
|
echo_status "$(python --version) ($(which python))"
|
|
echo_status "Happy coding!"
|