-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
executable file
·107 lines (89 loc) · 3.19 KB
/
Copy pathrun.py
File metadata and controls
executable file
·107 lines (89 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
"""
QPDS Quick Start Script
Starts the QPDS backend server
"""
import os
import shutil
import sys
import subprocess
from datetime import datetime
def venv_executables(venv_path):
"""Return the platform-specific Python and pip paths for a venv."""
scripts_dir = "Scripts" if sys.platform == "win32" else "bin"
return (
os.path.join(venv_path, scripts_dir, "python"),
os.path.join(venv_path, scripts_dir, "pip"),
)
def venv_is_usable(python):
"""Check that an existing venv still points to a runnable interpreter."""
if not os.path.isfile(python):
return False
result = subprocess.run(
[python, "--version"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=False,
)
return result.returncode == 0
def dependencies_are_available(python):
"""Avoid network/package work when runtime dependencies already import."""
result = subprocess.run(
[python, "-c", "import flask, flask_cors, numpy"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=False,
)
return result.returncode == 0
def main():
print("=" * 50)
print("🎯 QPDS - Quantitative Poker Decision System")
print("=" * 50)
print()
# Check Python version
if sys.version_info < (3, 8):
print("❌ Error: Python 3.8+ is required")
sys.exit(1)
print(f"✅ Python {sys.version.split()[0]} detected")
# Check whether the existing venv is usable, not just whether it exists.
venv_path = "venv"
python, pip = venv_executables(venv_path)
if os.path.exists(venv_path) and not venv_is_usable(python):
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
backup_path = f"{venv_path}.broken-{timestamp}"
print(f"Existing virtual environment is unusable; moving it to {backup_path}")
shutil.move(venv_path, backup_path)
if not os.path.exists(venv_path):
print("📦 Creating virtual environment...")
subprocess.run([sys.executable, "-m", "venv", venv_path], check=True)
python, pip = venv_executables(venv_path)
if not os.path.exists(pip):
print("📦 Bootstrapping pip inside virtual environment...")
subprocess.run([python, "-m", "ensurepip", "--upgrade"], check=True)
if dependencies_are_available(python):
print("Runtime dependencies already installed")
else:
print("📦 Installing dependencies...")
subprocess.run(
[python, "-m", "pip", "install", "-q", "-r", "requirements.txt"],
check=True,
)
port = os.environ.get("PORT", "8080")
env = os.environ.copy()
env["PORT"] = port
print()
print("🚀 Starting QPDS Backend...")
print("=" * 50)
print(f"API: http://localhost:{port}")
print(f"Health Check: http://localhost:{port}/health")
print()
print("Open frontend/index.html in your browser to use the UI")
print("Press Ctrl+C to stop the server")
print("=" * 50)
# Start the backend
try:
subprocess.run([python, "-m", "backend.api.app"], env=env)
except KeyboardInterrupt:
print("\n👋 QPDS stopped")
if __name__ == "__main__":
main()