-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlaunch.py
More file actions
191 lines (156 loc) · 6.5 KB
/
Copy pathlaunch.py
File metadata and controls
191 lines (156 loc) · 6.5 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env python3
"""
ViperCapture launcher
---------------------
Run this file directly with Python.
Handles venv setup, dependency install, browser install,
server startup, and opening your browser automatically.
On subsequent runs, dependency checks are skipped unless
requirements.txt has changed (hash-stamped in .venv/).
"""
from __future__ import annotations
import hashlib
from importlib.metadata import version
import os
import sys
import subprocess
import socket
import time
import webbrowser
from pathlib import Path
ROOT = Path(__file__).parent.resolve()
VENV_PYTHON = ROOT / ".venv" / ("Scripts/python.exe" if os.name == "nt" else "bin/python")
HOST = "127.0.0.1"
PORT = 8000
URL = f"http://{HOST}:{PORT}/"
DEPS_STAMP = ROOT / ".venv" / ".deps_stamp"
PLAYWRIGHT_STAMP = ROOT / ".venv" / ".playwright_stamp"
# ── Helpers ───────────────────────────────────────────────────
def port_open() -> bool:
try:
with socket.create_connection((HOST, PORT), timeout=1):
return True
except OSError:
return False
def run(*cmd: str | Path, label: str = "") -> None:
"""Run a subprocess and exit hard if it fails."""
result = subprocess.run([str(c) for c in cmd])
if result.returncode != 0:
tag = f" ({label})" if label else ""
print(f"\n ERROR{tag}: command exited with code {result.returncode}")
print(f" Command: {' '.join(str(c) for c in cmd)}")
wait_and_exit(1)
def wait_and_exit(code: int = 0) -> None:
if sys.stdin.isatty():
input("\n Press Enter to close...")
sys.exit(code)
def _req_hash() -> str:
"""MD5 of requirements.txt — used to detect changes between runs."""
req = ROOT / "requirements.txt"
return hashlib.md5(req.read_bytes()).hexdigest() if req.exists() else ""
# ── Setup steps ───────────────────────────────────────────────
def ensure_venv() -> None:
"""
If the venv doesn't exist yet, create it.
If we're not running from the venv Python, re-launch with it so
all subsequent imports and subprocess calls use the right Python.
"""
if not VENV_PYTHON.exists():
print(" [1/3] Creating Python environment (first run only)...")
run(sys.executable, "-m", "venv", ROOT / ".venv", label="venv creation")
this = Path(sys.executable).resolve()
want = VENV_PYTHON.resolve()
if this != want:
# Hand off to the venv Python — this process becomes just a waiter.
result = subprocess.run([str(VENV_PYTHON), __file__] + sys.argv[1:])
sys.exit(result.returncode)
def ensure_deps() -> None:
"""
Install packages from requirements.txt.
Skipped on subsequent runs unless requirements.txt has changed.
"""
current_hash = _req_hash()
if DEPS_STAMP.exists() and DEPS_STAMP.read_text().strip() == current_hash:
print(" [2/3] Python packages already up to date — skipping.")
return
print(" [2/3] Installing Python packages...")
run(sys.executable, "-m", "pip", "install", "--upgrade", "pip", "-q",
label="pip upgrade")
run(sys.executable, "-m", "pip", "install", "-r", ROOT / "requirements.txt",
label="pip install")
DEPS_STAMP.write_text(current_hash)
def ensure_playwright() -> None:
"""
Install Playwright's Chromium, Firefox, and WebKit browsers.
Skipped when the installed browser matches the Playwright package version.
"""
playwright_stamp = f"{version('playwright')}:chromium,firefox,webkit"
if (
PLAYWRIGHT_STAMP.exists()
and PLAYWRIGHT_STAMP.read_text().strip() == playwright_stamp
):
print(" [3/3] Playwright browsers already installed — skipping.")
return
print(" [3/3] Installing Playwright browsers...")
command = [sys.executable, "-m", "playwright", "install", "--no-shell"]
if sys.platform.startswith("linux"):
command.append("--with-deps")
run(*command, "chromium", "firefox", "webkit", label="playwright install")
PLAYWRIGHT_STAMP.write_text(playwright_stamp)
# ── Main ──────────────────────────────────────────────────────
def main() -> None:
print()
print(" ViperCapture")
print(" ------------")
print()
ensure_venv() # may re-exec this script under the venv Python
ensure_deps()
ensure_playwright()
# Server already running from a previous session?
if port_open():
print(f"\n Server already running. Opening {URL}")
webbrowser.open(URL)
return
# ── Start the server ────────────────────────────────────────
print(f"\n Starting server at {URL}")
print(" Press Ctrl+C here to stop the server.\n")
server = subprocess.Popen(
[sys.executable, "-m", "uvicorn", "vipercapture.main:app",
"--host", HOST, "--port", str(PORT)],
cwd=str(ROOT),
)
# ── Wait for port (1 check/sec, 30s max) ────────────────────
print(" Waiting for server to be ready...", end="", flush=True)
ready = False
for _ in range(30):
if port_open():
ready = True
break
if server.poll() is not None:
# Server process already exited — don't wait the full 30s
break
time.sleep(1)
print(".", end="", flush=True)
print()
if not ready:
print("\n ERROR: Server didn't start.")
print(" Check the output above for details.")
server.terminate()
wait_and_exit(1)
# ── Open browser ────────────────────────────────────────────
webbrowser.open(URL)
print(f"\n Ready! Opened {URL} in your browser.")
print(" Ctrl+C to stop the server.\n")
# Keep this window alive — show server logs until Ctrl+C
try:
server.wait()
except KeyboardInterrupt:
print("\n Stopping server...")
server.terminate()
try:
server.wait(timeout=5)
except subprocess.TimeoutExpired:
server.kill()
print(" Server stopped.")
if __name__ == "__main__":
main()