225 lines
6.9 KiB
Python
225 lines
6.9 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Setup script to create and configure a Python virtual environment for the MOTM application.
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import subprocess
|
|
import platform
|
|
|
|
def create_virtual_environment():
|
|
"""Create a Python virtual environment for the MOTM application."""
|
|
|
|
print("🐍 Setting up Python virtual environment for MOTM application...")
|
|
print("=" * 60)
|
|
|
|
# Check Python version
|
|
python_version = sys.version_info
|
|
print(f"✓ Python version: {python_version.major}.{python_version.minor}.{python_version.micro}")
|
|
|
|
if python_version < (3, 7):
|
|
print("❌ Python 3.7 or higher is required")
|
|
return False
|
|
|
|
# Determine the virtual environment directory name
|
|
venv_dir = "venv"
|
|
|
|
# Check if virtual environment already exists
|
|
if os.path.exists(venv_dir):
|
|
print(f"⚠ Virtual environment '{venv_dir}' already exists")
|
|
response = input("Do you want to recreate it? (y/N): ").strip().lower()
|
|
if response in ['y', 'yes']:
|
|
print(f"🗑️ Removing existing virtual environment...")
|
|
if platform.system() == "Windows":
|
|
subprocess.run(['rmdir', '/s', '/q', venv_dir], shell=True)
|
|
else:
|
|
subprocess.run(['rm', '-rf', venv_dir])
|
|
else:
|
|
print("Using existing virtual environment...")
|
|
return True
|
|
|
|
# Create virtual environment
|
|
print(f"📦 Creating virtual environment in '{venv_dir}'...")
|
|
try:
|
|
subprocess.run([sys.executable, '-m', 'venv', venv_dir], check=True)
|
|
print("✅ Virtual environment created successfully!")
|
|
except subprocess.CalledProcessError as e:
|
|
print(f"❌ Failed to create virtual environment: {e}")
|
|
return False
|
|
|
|
return True
|
|
|
|
def get_activation_script():
|
|
"""Get the appropriate activation script for the current platform."""
|
|
if platform.system() == "Windows":
|
|
return os.path.join("venv", "Scripts", "activate.bat")
|
|
else:
|
|
return os.path.join("venv", "bin", "activate")
|
|
|
|
def install_dependencies():
|
|
"""Install required dependencies in the virtual environment."""
|
|
|
|
print("\n📚 Installing dependencies...")
|
|
|
|
# Determine the pip command based on platform
|
|
if platform.system() == "Windows":
|
|
pip_cmd = os.path.join("venv", "Scripts", "pip")
|
|
else:
|
|
pip_cmd = os.path.join("venv", "bin", "pip")
|
|
|
|
# Check if requirements.txt exists
|
|
if not os.path.exists("requirements.txt"):
|
|
print("❌ requirements.txt not found")
|
|
return False
|
|
|
|
try:
|
|
# Upgrade pip first
|
|
print("🔄 Upgrading pip...")
|
|
subprocess.run([pip_cmd, "install", "--upgrade", "pip"], check=True)
|
|
|
|
# Install requirements
|
|
print("📦 Installing application dependencies...")
|
|
subprocess.run([pip_cmd, "install", "-r", "requirements.txt"], check=True)
|
|
|
|
print("✅ Dependencies installed successfully!")
|
|
return True
|
|
|
|
except subprocess.CalledProcessError as e:
|
|
print(f"❌ Failed to install dependencies: {e}")
|
|
return False
|
|
|
|
def create_activation_scripts():
|
|
"""Create convenient activation scripts."""
|
|
|
|
print("\n📝 Creating activation scripts...")
|
|
|
|
# Windows batch script
|
|
windows_script = """@echo off
|
|
echo 🐍 Activating MOTM Virtual Environment...
|
|
call venv\\Scripts\\activate.bat
|
|
echo ✅ Virtual environment activated!
|
|
echo.
|
|
echo 🚀 To start the MOTM application, run:
|
|
echo python main.py
|
|
echo.
|
|
echo 🔧 To deactivate, run:
|
|
echo deactivate
|
|
"""
|
|
|
|
# Unix shell script
|
|
unix_script = """#!/bin/bash
|
|
echo "🐍 Activating MOTM Virtual Environment..."
|
|
source venv/bin/activate
|
|
echo "✅ Virtual environment activated!"
|
|
echo ""
|
|
echo "🚀 To start the MOTM application, run:"
|
|
echo " python main.py"
|
|
echo ""
|
|
echo "🔧 To deactivate, run:"
|
|
echo " deactivate"
|
|
"""
|
|
|
|
# Write platform-specific scripts
|
|
if platform.system() == "Windows":
|
|
with open("activate_motm.bat", "w") as f:
|
|
f.write(windows_script)
|
|
print("✓ Created activate_motm.bat for Windows")
|
|
else:
|
|
with open("activate_motm.sh", "w") as f:
|
|
f.write(unix_script)
|
|
# Make it executable
|
|
os.chmod("activate_motm.sh", 0o755)
|
|
print("✓ Created activate_motm.sh for Unix/Linux")
|
|
|
|
return True
|
|
|
|
def create_run_script():
|
|
"""Create a script to run the application directly."""
|
|
|
|
print("📝 Creating run script...")
|
|
|
|
# Windows batch script
|
|
windows_run = """@echo off
|
|
echo 🐍 Starting MOTM Application...
|
|
call venv\\Scripts\\activate.bat
|
|
python main.py
|
|
pause
|
|
"""
|
|
|
|
# Unix shell script
|
|
unix_run = """#!/bin/bash
|
|
echo "🐍 Starting MOTM Application..."
|
|
source venv/bin/activate
|
|
python main.py
|
|
"""
|
|
|
|
if platform.system() == "Windows":
|
|
with open("run_motm.bat", "w") as f:
|
|
f.write(windows_run)
|
|
print("✓ Created run_motm.bat")
|
|
else:
|
|
with open("run_motm.sh", "w") as f:
|
|
f.write(unix_run)
|
|
os.chmod("run_motm.sh", 0o755)
|
|
print("✓ Created run_motm.sh")
|
|
|
|
def print_instructions():
|
|
"""Print setup completion instructions."""
|
|
|
|
print("\n" + "=" * 60)
|
|
print("🎉 MOTM Virtual Environment Setup Complete!")
|
|
print("=" * 60)
|
|
|
|
if platform.system() == "Windows":
|
|
print("\n📋 To use the virtual environment:")
|
|
print(" 1. Activate: activate_motm.bat")
|
|
print(" 2. Run app: run_motm.bat")
|
|
print(" 3. Deactivate: deactivate")
|
|
print("\n🔧 Manual activation:")
|
|
print(" venv\\Scripts\\activate.bat")
|
|
print(" python main.py")
|
|
else:
|
|
print("\n📋 To use the virtual environment:")
|
|
print(" 1. Activate: source activate_motm.sh")
|
|
print(" 2. Run app: ./run_motm.sh")
|
|
print(" 3. Deactivate: deactivate")
|
|
print("\n🔧 Manual activation:")
|
|
print(" source venv/bin/activate")
|
|
print(" python main.py")
|
|
|
|
print("\n🌐 The application will be available at: http://localhost:5000")
|
|
print("\n📚 For development:")
|
|
print(" - Activate the venv before installing new packages")
|
|
print(" - Use 'pip install <package>' to add dependencies")
|
|
print(" - Update requirements.txt with 'pip freeze > requirements.txt'")
|
|
|
|
def main():
|
|
"""Main setup function."""
|
|
|
|
# Change to the script's directory
|
|
script_dir = os.path.dirname(os.path.abspath(__file__))
|
|
os.chdir(script_dir)
|
|
|
|
print("MOTM Flask Application - Virtual Environment Setup")
|
|
print("=" * 60)
|
|
|
|
# Step 1: Create virtual environment
|
|
if not create_virtual_environment():
|
|
sys.exit(1)
|
|
|
|
# Step 2: Install dependencies
|
|
if not install_dependencies():
|
|
sys.exit(1)
|
|
|
|
# Step 3: Create convenience scripts
|
|
create_activation_scripts()
|
|
create_run_script()
|
|
|
|
# Step 4: Print instructions
|
|
print_instructions()
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|