/ scripts / setup.py
setup.py
  1  #!/usr/bin/env python3
  2  """Script to set up the development environment."""
  3  
  4  import os
  5  import sys
  6  import subprocess
  7  from pathlib import Path
  8  from typing import List
  9  
 10  # Project root directory
 11  PROJECT_ROOT = Path(__file__).parent.absolute()
 12  
 13  
 14  def run_command(command: List[str], cwd: str = None) -> int:
 15      """Run a shell command and return the exit code."""
 16      if cwd is None:
 17          cwd = str(PROJECT_ROOT)
 18      
 19      print(f"Running: {' '.join(command)}")
 20      result = subprocess.run(command, cwd=cwd)
 21      return result.returncode
 22  
 23  
 24  def check_environment() -> bool:
 25      """Check if the required environment variables are set."""
 26      required_vars = [
 27          "DATABASE_URL",
 28          "SECRET_KEY",
 29          "ALGORITHM",
 30          "ACCESS_TOKEN_EXPIRE_MINUTES",
 31      ]
 32      
 33      missing_vars = [var for var in required_vars if not os.getenv(var)]
 34      
 35      if missing_vars:
 36          print("Error: The following required environment variables are not set:")
 37          for var in missing_vars:
 38              print(f"  - {var}")
 39          print("\nPlease create a .env file in the project root and set these variables.")
 40          print("You can use .env.example as a template.")
 41          return False
 42      return True
 43  
 44  
 45  def setup_virtualenv() -> bool:
 46      """Set up a Python virtual environment."""
 47      venv_dir = PROJECT_ROOT / "venv"
 48      
 49      if not venv_dir.exists():
 50          print("Creating virtual environment...")
 51          return_code = run_command([sys.executable, "-m", "venv", str(venv_dir)])
 52          if return_code != 0:
 53              return False
 54      
 55      # Activate virtual environment
 56      if os.name == 'nt':  # Windows
 57          activate_script = venv_dir / "Scripts" / "activate"
 58      else:  # Unix/Linux/MacOS
 59          activate_script = venv_dir / "bin" / "activate"
 60      
 61      print(f"\nVirtual environment created at: {venv_dir}")
 62      print("\nTo activate the virtual environment, run:")
 63      if os.name == 'nt':  # Windows
 64          print(f"  .\\venv\\Scripts\\activate")
 65      else:  # Unix/Linux/MacOS
 66          print(f"  source venv/bin/activate")
 67      
 68      return True
 69  
 70  
 71  def install_dependencies() -> bool:
 72      """Install project dependencies."""
 73      print("\nInstalling dependencies...")
 74      return_code = run_command([sys.executable, "-m", "pip", "install", "-r", "requirements/dev.txt"])
 75      if return_code != 0:
 76          return False
 77      
 78      # Install pre-commit hooks
 79      print("\nSetting up pre-commit hooks...")
 80      return_code = run_command([sys.executable, "-m", "pre-commit", "install"])
 81      return return_code == 0
 82  
 83  
 84  def setup_database() -> bool:
 85      """Set up the database."""
 86      print("\nSetting up the database...")
 87      
 88      # Run database migrations
 89      return_code = run_command(["alembic", "upgrade", "head"])
 90      if return_code != 0:
 91          print("Error: Failed to run database migrations.")
 92          return False
 93      
 94      print("\nDatabase setup complete!")
 95      return True
 96  
 97  
 98  def main() -> int:
 99      """Main setup function."""
100      print("=" * 50)
101      print("Setting up Sussro Services Development Environment")
102      print("=" * 50)
103      
104      # Check environment variables
105      if not check_environment():
106          return 1
107      
108      # Set up virtual environment
109      if not setup_virtualenv():
110          return 1
111      
112      # Install dependencies
113      if not install_dependencies():
114          return 1
115      
116      # Set up database
117      if not setup_database():
118          return 1
119      
120      print("\n" + "=" * 50)
121      print("Setup completed successfully!")
122      print("\nTo start the development server, run:")
123      print("  uvicorn sussro_services.main:app --reload")
124      print("\nThen visit http://localhost:8000/docs for the API documentation.")
125      print("=" * 50)
126      
127      return 0
128  
129  
130  if __name__ == "__main__":
131      sys.exit(main())