You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cbmc/codedetect/scripts/setup-env.sh

337 lines
8.5 KiB

#!/bin/bash
# Environment Setup Script for Formal Specification Generation System
# This script initializes the development environment and checks dependencies
set -e # Exit on any error
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_status() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to check Python version
check_python() {
print_status "Checking Python installation..."
if ! command_exists python3; then
print_error "Python 3 is not installed. Please install Python 3.8 or later."
exit 1
fi
PYTHON_VERSION=$(python3 -c 'import sys; print(".".join(map(str, sys.version_info[:2])))')
REQUIRED_VERSION="3.8"
if python3 -c "import sys; exit(0 if sys.version_info >= (3, 8) else 1)"; then
print_success "Python $PYTHON_VERSION found (>= $REQUIRED_VERSION)"
else
print_error "Python $PYTHON_VERSION found, but $REQUIRED_VERSION or later is required"
exit 1
fi
}
# Function to check CBMC installation
check_cbmc() {
print_status "Checking CBMC installation..."
if ! command_exists cbmc; then
print_warning "CBMC is not installed or not in PATH"
print_status "Please install CBMC 5.0 or later:"
echo " - Ubuntu/Debian: sudo apt-get install cbmc"
echo " - Or download from: https://github.com/diffblue/cbmc"
echo " - Make sure cbmc is in your PATH"
return 1
fi
CBMC_VERSION=$(cbmc --version 2>/dev/null | head -n1 || echo "unknown")
print_success "CBMC found: $CBMC_VERSION"
}
# Function to check pip
check_pip() {
print_status "Checking pip installation..."
if ! command_exists pip3; then
print_error "pip3 is not installed. Please install pip for Python 3."
exit 1
fi
print_success "pip3 found"
}
# Function to create virtual environment
create_venv() {
print_status "Creating virtual environment..."
if [ -d "venv" ]; then
print_warning "Virtual environment already exists"
return 0
fi
python3 -m venv venv
print_success "Virtual environment created"
}
# Function to activate virtual environment and install dependencies
install_dependencies() {
print_status "Installing Python dependencies..."
# Activate virtual environment
source venv/bin/activate
# Upgrade pip
pip install --upgrade pip
# Install requirements
if [ -f "requirements.txt" ]; then
pip install -r requirements.txt
print_success "Dependencies installed from requirements.txt"
else
print_error "requirements.txt not found"
exit 1
fi
}
# Function to create necessary directories
create_directories() {
print_status "Creating necessary directories..."
directories=(
"logs"
"uploads"
"config"
"cbmc/proofs"
"cbmc/harnesses"
"cbmc/configs"
"results"
"temp"
)
for dir in "${directories[@]}"; do
if [ ! -d "$dir" ]; then
mkdir -p "$dir"
print_status "Created directory: $dir"
fi
done
print_success "All directories created"
}
# Function to create .env file
create_env_file() {
print_status "Creating environment configuration..."
if [ ! -f ".env" ]; then
if [ -f ".env.template" ]; then
cp .env.template .env
print_success "Created .env file from template"
print_warning "Please edit .env file and add your SiliconFlow API key"
else
print_error ".env.template not found"
exit 1
fi
else
print_warning ".env file already exists"
fi
}
# Function to validate configuration
validate_config() {
print_status "Validating configuration..."
# Check if .env file exists
if [ ! -f ".env" ]; then
print_error ".env file not found. Please create it from .env.template"
return 1
fi
# Source .env file to check environment variables
source .env
# Check required environment variables
required_vars=(
"SILICONFLOW_API_KEY"
)
missing_vars=()
for var in "${required_vars[@]}"; do
if [ -z "${!var}" ]; then
missing_vars+=("$var")
fi
done
if [ ${#missing_vars[@]} -gt 0 ]; then
print_error "Missing required environment variables:"
for var in "${missing_vars[@]}"; do
echo " - $var"
done
print_warning "Please add these variables to your .env file"
return 1
fi
print_success "Configuration validation passed"
}
# Function to run basic tests
run_tests() {
print_status "Running basic tests..."
# Activate virtual environment
source venv/bin/activate
# Run Python import tests
python -c "
import sys
sys.path.insert(0, 'src')
try:
from utils.config import get_config
from utils.logger import get_logger
print('✓ Core utilities import successful')
config = get_config()
print('✓ Configuration loading successful')
logger = get_logger('test')
logger.info('Test message')
print('✓ Logging system working')
except ImportError as e:
print(f'✗ Import error: {e}')
sys.exit(1)
except Exception as e:
print(f'✗ Error: {e}')
sys.exit(1)
"
if [ $? -eq 0 ]; then
print_success "Basic tests passed"
else
print_error "Basic tests failed"
return 1
fi
}
# Function to show usage information
show_usage() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -h, --help Show this help message"
echo " -f, --force Force recreation of virtual environment"
echo " -s, --skip-tests Skip running tests"
echo " -v, --verbose Verbose output"
echo ""
echo "This script sets up the development environment for the Formal"
echo "Specification Generation System."
}
# Main setup function
main() {
local force_recreate=false
local skip_tests=false
local verbose=false
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_usage
exit 0
;;
-f|--force)
force_recreate=true
shift
;;
-s|--skip-tests)
skip_tests=true
shift
;;
-v|--verbose)
verbose=true
set -x # Enable command tracing
shift
;;
*)
print_error "Unknown option: $1"
show_usage
exit 1
;;
esac
done
print_status "Starting environment setup..."
print_status "Project: Formal Specification Generation System"
print_status "=========================================="
# Check dependencies
check_python
check_pip
check_cbmc
# Force recreate virtual environment if requested
if [ "$force_recreate" = true ] && [ -d "venv" ]; then
print_status "Removing existing virtual environment..."
rm -rf venv
fi
# Setup environment
create_venv
install_dependencies
create_directories
create_env_file
# Validate configuration
if ! validate_config; then
print_error "Configuration validation failed"
exit 1
fi
# Run tests unless skipped
if [ "$skip_tests" = false ]; then
if ! run_tests; then
print_error "Setup tests failed"
exit 1
fi
fi
print_success ""
print_success "Environment setup completed successfully!"
print_success ""
print_status "Next steps:"
echo " 1. Edit .env file to add your API keys if needed"
echo " 2. Activate virtual environment: source venv/bin/activate"
echo " 3. (Web UI coming soon) Run the web server: python -m src.ui.web_app"
echo " 4. Open http://localhost:8080 in your browser"
echo ""
print_status "Available now:"
echo " - Use CLI: python -m src.ui.cli (once implemented)"
echo " - Test core utilities: python -c 'from src.utils.config import get_config; print(\"Config loaded successfully\")'"
print_success ""
}
# Run main function
main "$@"