stream-bot/scripts/setup_env.py
Joey Yakimowich-Payne ec1c5958ce Initial working version
2025-05-14 17:45:07 -06:00

113 lines
3.5 KiB
Python

"""
Environment setup helper script
"""
import os
import sys
import shutil
from typing import List, Dict, Any, Optional
import subprocess
import platform
def check_python_version() -> bool:
"""Check if Python version is compatible"""
if sys.version_info < (3, 7):
print(f"Error: Python 3.7 or higher is required. You are using {platform.python_version()}")
return False
print(f"Python version OK: {platform.python_version()}")
return True
def create_env_file() -> bool:
"""Create a .env file from .env.example if it doesn't exist"""
example_path = os.path.join("config", ".env.example")
env_path = ".env"
# Check if .env already exists
if os.path.exists(env_path):
print(f"Info: {env_path} file already exists")
return True
# Check if .env.example exists
if not os.path.exists(example_path):
print(f"Creating default .env file (no example found at {example_path})")
with open(env_path, "w") as f:
f.write("# Twitch API credentials\n")
f.write("TWITCH_USERNAME=your_bot_username\n")
f.write("TWITCH_CLIENT_ID=your_client_id\n")
f.write("TWITCH_CLIENT_SECRET=your_client_secret\n")
f.write("TWITCH_CHANNEL=your_channel\n")
print(f"Created default {env_path} file")
return True
# Copy .env.example to .env
try:
shutil.copy(example_path, env_path)
print(f"Created {env_path} file from {example_path}")
print(f"Please edit {env_path} to set your Twitch API credentials")
return True
except Exception as e:
print(f"Error creating .env file: {e}")
return False
def install_dependencies() -> bool:
"""Install Python dependencies from requirements.txt"""
req_path = "requirements.txt"
if not os.path.exists(req_path):
print(f"Error: {req_path} not found")
return False
try:
print(f"Installing dependencies from {req_path}...")
subprocess.check_call([sys.executable, "-m", "pip", "install", "-r", req_path])
print("Dependencies installed successfully")
return True
except subprocess.CalledProcessError as e:
print(f"Error installing dependencies: {e}")
return False
def create_directories() -> bool:
"""Create necessary directories if they don't exist"""
dirs = [
"data/cache",
"data/sounds"
]
for directory in dirs:
if not os.path.exists(directory):
try:
os.makedirs(directory, exist_ok=True)
print(f"Created directory: {directory}")
except Exception as e:
print(f"Error creating directory {directory}: {e}")
return False
return True
def setup_environment() -> bool:
"""
Run all setup steps
Returns True if all steps succeeded
"""
steps = [
check_python_version,
create_directories,
create_env_file,
install_dependencies
]
success = True
for step in steps:
if not step():
success = False
return success
if __name__ == "__main__":
print("Setting up environment for stream-interact...")
if setup_environment():
print("\nSetup completed successfully!")
print("\nTo run the bot, use: python main.py")
print("To see available options, use: python main.py --help")
else:
print("\nSetup completed with errors. Please check the messages above.")
sys.exit(1)