#!/usr/bin/env python3 """ Lab4 Application Setup & Launch Script Handles venv, dependencies, and database initialization """ import os import sys import subprocess import shutil from pathlib import Path def main(): # Get the directory where this script is located script_dir = Path(__file__).resolve().parent os.chdir(script_dir) print("🔧 Lab4 Application Setup & Launch") print("=" * 50) venv_path = script_dir / "venv" requirements_file = script_dir / "requirements.txt" instance_dir = script_dir / "instance" db_file = instance_dir / "accounting.db" # Check and create venv if not exists if not venv_path.exists(): print("📦 Virtual environment not found. Creating venv...") subprocess.run([sys.executable, "-m", "venv", str(venv_path)], check=True) print("✅ venv created") else: print("✅ venv already exists") # Determine the Python executable in venv python_exe = venv_path / "bin" / "python" pip_exe = venv_path / "bin" / "pip" if not python_exe.exists(): print("❌ Failed to locate Python in venv") sys.exit(1) # Install/upgrade requirements if requirements_file.exists(): print("📥 Installing dependencies from requirements.txt...") subprocess.run([str(pip_exe), "install", "-q", "--upgrade", "pip"], check=True) subprocess.run([str(pip_exe), "install", "-q", "-r", str(requirements_file)], check=True) print("✅ Dependencies installed") else: print("❌ requirements.txt not found!") sys.exit(1) # Create instance directory if not exists if not instance_dir.exists(): print("📁 Creating instance directory...") instance_dir.mkdir(parents=True, exist_ok=True) # Remove old database to create fresh one if db_file.exists(): print("🗑️ Removing old database...") db_file.unlink() print() print("=" * 50) print("🚀 Starting Flask Application...") print("=" * 50) print() # Run the Flask app subprocess.run([str(python_exe), "app.py"]) if __name__ == "__main__": main()