-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlaunch_tgwatch.bat
More file actions
131 lines (118 loc) · 3.73 KB
/
launch_tgwatch.bat
File metadata and controls
131 lines (118 loc) · 3.73 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
@echo off
setlocal
cd /d "%~dp0"
set "USE_CONDA=0"
set "PY_CMD="
if "%TGWATCH_GUI_HOST%"=="" (
set "GUI_HOST=127.0.0.1"
) else (
set "GUI_HOST=%TGWATCH_GUI_HOST%"
)
if "%TGWATCH_GUI_PORT%"=="" (
set "GUI_PORT=8765"
) else (
set "GUI_PORT=%TGWATCH_GUI_PORT%"
)
where conda >nul 2>&1
if %errorlevel% == 0 (
for /f "delims=" %%i in ('conda info --base 2^>nul') do set "CONDA_BASE=%%i"
if defined CONDA_BASE if exist "%CONDA_BASE%\condabin\conda.bat" (
call "%CONDA_BASE%\condabin\conda.bat" env list | findstr /r /c:"^tgwatch " >nul
if errorlevel 1 (
echo [tgwatch] Creating Conda environment: tgwatch
call "%CONDA_BASE%\condabin\conda.bat" create -y -n tgwatch python=3.11
) else (
echo [tgwatch] Using existing Conda environment: tgwatch
)
call "%CONDA_BASE%\condabin\conda.bat" activate tgwatch >nul 2>&1
if not errorlevel 1 (
set "USE_CONDA=1"
set "PY_CMD=python"
%PY_CMD% -c "import sys; raise SystemExit(0 if sys.version_info >= (3, 11) else 1)"
if errorlevel 1 (
echo [tgwatch] Conda environment must use Python 3.11+
exit /b 1
)
) else (
echo [tgwatch] Conda activate failed, falling back to venv
)
)
)
if "%USE_CONDA%"=="1" (
set "INSTALL_MARKER=.conda-tgwatch-installed"
if not exist "%INSTALL_MARKER%" (
%PY_CMD% -m pip install -U pip setuptools wheel
if errorlevel 1 (
echo [tgwatch] Warning: failed to upgrade pip/setuptools/wheel; continuing.
)
%PY_CMD% -m pip install -e .
if errorlevel 1 (
echo [tgwatch] Error: project install failed. Check network/proxy/pip index settings.
exit /b 1
)
type nul > "%INSTALL_MARKER%"
)
echo [tgwatch] Environment source: conda (tgwatch)
) else (
if not exist ".venv\Scripts\python.exe" (
where py >nul 2>&1
if %errorlevel% == 0 (
py -3.11 -m venv .venv
) else (
python -m venv .venv
)
)
".venv\Scripts\python.exe" -c "import sys; raise SystemExit(0 if sys.version_info >= (3, 11) else 1)"
if errorlevel 1 (
echo [tgwatch] venv Python must be 3.11+
exit /b 1
)
set "INSTALL_MARKER=.venv\.tgwatch_installed"
if not exist "%INSTALL_MARKER%" (
".venv\Scripts\python.exe" -m pip install -U pip setuptools wheel
if errorlevel 1 (
echo [tgwatch] Warning: failed to upgrade pip/setuptools/wheel; continuing.
)
".venv\Scripts\python.exe" -m pip install -e .
if errorlevel 1 (
echo [tgwatch] Error: project install failed. Check network/proxy/pip index settings.
exit /b 1
)
type nul > "%INSTALL_MARKER%"
)
set "PY_CMD=.venv\Scripts\python.exe"
echo [tgwatch] Environment source: venv (.venv)
)
if not exist "config.toml" (
copy /y config.example.toml config.toml >nul
)
call :recover_gui_port
if errorlevel 1 exit /b 1
%PY_CMD% -m tgwatch gui --config config.toml --host %GUI_HOST% --port %GUI_PORT%
endlocal
exit /b 0
:recover_gui_port
setlocal EnableDelayedExpansion
set "PIDS="
for /f "tokens=5" %%P in ('netstat -ano -p tcp ^| findstr /r /c:":%GUI_PORT% .*LISTENING"') do (
echo !PIDS! | findstr /w "%%P" >nul || set "PIDS=!PIDS! %%P"
)
if "!PIDS!"=="" (
endlocal & exit /b 0
)
echo [tgwatch] Port %GUI_PORT% is in use. Attempting recovery...
for %%P in (!PIDS!) do (
set "PROC=unknown"
for /f "tokens=1,* delims=:" %%A in ('tasklist /fi "PID eq %%P" /fo list ^| findstr /b /c:"Image Name:"') do (
set "PROC=%%B"
)
echo [tgwatch] Stopping PID %%P!PROC!
taskkill /PID %%P /T /F >nul 2>&1
)
timeout /t 1 /nobreak >nul
for /f "tokens=5" %%P in ('netstat -ano -p tcp ^| findstr /r /c:":%GUI_PORT% .*LISTENING"') do (
echo [tgwatch] Error: port %GUI_PORT% is still in use by PID %%P.
endlocal & exit /b 1
)
echo [tgwatch] Port %GUI_PORT% recovered.
endlocal & exit /b 0