Your IP : 216.73.217.79


Current Path : /var/www/cesa.co.za/python/
Upload File :
Current File : /var/www/cesa.co.za/python/setup-env.sh

#!/bin/bash
#
# Setup script for Python virtual environment
# This script creates a virtual environment and installs required packages
#

set -e  # Exit on error

# Get the directory where this script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
VENV_DIR="${SCRIPT_DIR}/venv"
REQUIREMENTS_FILE="${SCRIPT_DIR}/requirements.txt"

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

echo -e "${GREEN}Setting up Python virtual environment...${NC}"

# Check if Python 3 is available
if ! command -v python3 &> /dev/null; then
    echo -e "${RED}Error: python3 is not installed. Please install Python 3 first.${NC}"
    exit 1
fi

# Check if python3-venv is installed (required for venv module)
if ! python3 -m venv --help &> /dev/null; then
    echo -e "${YELLOW}Warning: python3-venv module not found. Attempting to install...${NC}"
    if command -v apt-get &> /dev/null; then
        echo "Please run: sudo apt-get install python3-venv python3-full"
        exit 1
    else
        echo -e "${RED}Error: Cannot create virtual environment. Please install python3-venv.${NC}"
        exit 1
    fi
fi

# Check if virtual environment exists and is valid
if [ ! -d "$VENV_DIR" ]; then
    echo -e "${GREEN}Creating virtual environment in ${VENV_DIR}...${NC}"
    python3 -m venv "$VENV_DIR"
    echo -e "${GREEN}Virtual environment created successfully.${NC}"
elif [ ! -f "${VENV_DIR}/bin/activate" ]; then
    echo -e "${YELLOW}Virtual environment directory exists but is incomplete. Recreating...${NC}"
    rm -rf "$VENV_DIR"
    python3 -m venv "$VENV_DIR"
    echo -e "${GREEN}Virtual environment recreated successfully.${NC}"
else
    echo -e "${YELLOW}Virtual environment already exists at ${VENV_DIR}${NC}"
fi

# Verify activate script exists before sourcing
if [ ! -f "${VENV_DIR}/bin/activate" ]; then
    echo -e "${RED}Error: Virtual environment activation script not found at ${VENV_DIR}/bin/activate${NC}"
    echo -e "${RED}Please remove the venv directory and run this script again:${NC}"
    echo -e "  rm -rf ${VENV_DIR}"
    exit 1
fi

# Activate virtual environment
echo -e "${GREEN}Activating virtual environment...${NC}"
source "${VENV_DIR}/bin/activate"

# Upgrade pip
echo -e "${GREEN}Upgrading pip...${NC}"
pip install --upgrade pip --quiet

# Install requirements if requirements.txt exists
if [ -f "$REQUIREMENTS_FILE" ]; then
    echo -e "${GREEN}Installing requirements from ${REQUIREMENTS_FILE}...${NC}"
    pip install -r "$REQUIREMENTS_FILE"
    echo -e "${GREEN}Requirements installed successfully.${NC}"
else
    echo -e "${YELLOW}Warning: ${REQUIREMENTS_FILE} not found. Skipping requirements installation.${NC}"
fi

echo -e "${GREEN}Setup complete!${NC}"
echo ""
echo -e "${YELLOW}To activate the virtual environment in the future, run:${NC}"
echo -e "  source ${VENV_DIR}/bin/activate"
echo ""
echo -e "${YELLOW}To deactivate, run:${NC}"
echo -e "  deactivate"
echo ""
echo -e "${YELLOW}To run scripts with the virtual environment:${NC}"
echo -e "  ${VENV_DIR}/bin/python ${SCRIPT_DIR}/db-bck.py"