Files
douyi_guaji_main/scripts/一键启动WebGUI.bat
T
2026-06-18 16:49:38 +08:00

171 lines
5.8 KiB
Batchfile

@echo off
REM ==========================================
REM Douyin Web GUI Launcher (Stable Version)
REM ==========================================
REM Save original code page
for /f "tokens=2 delims=:" %%a in ('chcp') do set "_ORIG_CP=%%a"
set "_ORIG_CP=%_ORIG_CP: =%"
chcp 65001 >nul 2>&1
setlocal enabledelayedexpansion
REM Change to project root directory (parent of scripts)
cd /d "%~dp0.."
if errorlevel 1 (
echo [ERROR] Cannot change to project root directory
pause
goto :cleanup
)
REM Save current project root directory to variable (for delayed expansion)
set "WORK_DIR=%CD%"
REM Create log file
set "LOG_FILE=%~dp0webgui_startup.log"
(
echo ========================================
echo Start time: %date% %time%
echo ========================================
) >> "%LOG_FILE%" 2>&1
echo.
echo ========================================
echo Douyin Web GUI Launcher
echo ========================================
echo.
REM [1/6] Check Python environment
echo [1/6] Checking Python environment...
python --version >> "%LOG_FILE%" 2>&1
if errorlevel 1 (
echo [ERROR] Python not detected, please install Python first
echo Download: https://www.python.org/downloads/
echo [ERROR] Python not installed >> "%LOG_FILE%"
pause
goto :cleanup
)
echo [1/6] Python environment check... OK
echo.
REM [2/6] Check required files
echo [2/6] Checking required files...
if not exist "gui\web_gui_entry.py" (
echo [ERROR] web_gui_entry.py not found
echo Current directory: !WORK_DIR!
echo Expected path: !WORK_DIR!\gui\web_gui_entry.py
echo [ERROR] web_gui_entry.py does not exist >> "%LOG_FILE%"
pause
goto :cleanup
)
echo [2/6] File check... OK
echo.
REM [3/6] Stop old processes
echo [3/6] Stopping old processes...
REM Silently stop old processes, redirect all output to nul to avoid garbled text
powershell -NoProfile -ExecutionPolicy Bypass -Command "$ErrorActionPreference='SilentlyContinue'; $root=(Resolve-Path '.').Path; Get-CimInstance Win32_Process -ErrorAction SilentlyContinue | Where-Object {($_.Name -like 'python*') -and ($_.CommandLine -like ('*' + $root + '*')) -and (($_.CommandLine -like '*web_gui.py*') -or ($_.CommandLine -like '*web_gui_entry.py*'))} | ForEach-Object {Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue}" >nul 2>&1
powershell -NoProfile -Command "Start-Sleep -Seconds 2" >nul 2>&1
echo [3/6] Stopping old processes... OK
echo.
REM [4/6] Port configuration
set "WEB_PORT="
for /f "usebackq delims=" %%p in (`python -c "from douyin.core.path_config import get_web_gui_port; print(get_web_gui_port())" 2^>nul`) do set "WEB_PORT=%%p"
if not defined WEB_PORT set "WEB_PORT=8000"
echo [4/6] Port configuration: !WEB_PORT!
echo.
REM [5/6] Start service
echo [5/6] Starting Web GUI service (Port: !WEB_PORT!)...
echo.
REM Set Python environment variables
set PYTHONIOENCODING=utf-8
set PYTHONUNBUFFERED=1
REM Determine Python executable path
set "PYTHON_EXE="
set "USE_VENV=0"
REM Prefer virtual environment Python
if exist ".venv\Scripts\python.exe" (
set "PYTHON_EXE=.venv\Scripts\python.exe"
set "USE_VENV=1"
echo Virtual environment detected, using venv Python
) else if exist ".venv\Scripts\activate.bat" (
REM If python.exe not found, try using activate.bat
set "USE_VENV=2"
echo Virtual environment detected, using activate.bat
) else (
set "PYTHON_EXE=python"
set "USE_VENV=0"
echo Using system Python
)
REM Start service (in new window, keep window open)
set "SERVICE_LOG=%~dp0webgui_service.log"
if exist "!SERVICE_LOG!" del /f /q "!SERVICE_LOG!" >nul 2>&1
if !USE_VENV! equ 1 (
REM Use virtual environment python.exe directly (use full path)
set "PYTHON_EXE=!WORK_DIR!\.venv\Scripts\python.exe"
) else (
REM Use system Python or activated Python through PATH
set "PYTHON_EXE=python"
)
REM Start command with stable quoting and a service log
powershell -NoProfile -ExecutionPolicy Bypass -Command "$root = '%WORK_DIR%'; $py = '%PYTHON_EXE%'; $log = '%SERVICE_LOG%'; $cmd = 'cd /d \"' + $root + '\" && echo Starting Web GUI service... && echo. && \"' + $py + '\" gui\web_gui_entry.py >> \"' + $log + '\" 2>&1'; Start-Process -FilePath 'cmd.exe' -ArgumentList @('/k', $cmd) -WorkingDirectory $root -WindowStyle Normal"
REM Wait for service to start
echo Starting service, please wait...
set /a count=0
:wait_loop
powershell -NoProfile -Command "Start-Sleep -Seconds 2" >nul 2>&1
set /a count+=1
REM Check if port is listening (use temporary variable to pass port number)
set "CHECK_PORT=!WEB_PORT!"
powershell -NoProfile -Command "$listener = Get-NetTCPConnection -LocalPort !CHECK_PORT! -State Listen -ErrorAction SilentlyContinue; if ($listener) { exit 0 } else { exit 1 }" >nul 2>&1
if not errorlevel 1 (
echo [5/6] Web GUI service started
goto :open_browser
)
REM Timeout check (wait up to 30 seconds)
if !count! geq 15 (
echo [WARNING] Service startup timeout (waited 30 seconds)
echo Please check the error message in the new "Douyin Web GUI" window
echo Or check log file: %LOG_FILE%
goto :open_browser
)
goto :wait_loop
:open_browser
REM [6/6] Open browser
echo.
echo [6/6] Opening browser...
powershell -NoProfile -Command "Start-Sleep -Seconds 1" >nul 2>&1
start "" "http://127.0.0.1:!WEB_PORT!"
echo.
echo ========================================
echo Startup Complete!
echo ========================================
echo.
echo Web GUI URL: http://127.0.0.1:!WEB_PORT!
echo Log file: %LOG_FILE%
echo.
echo Notes:
echo - Web GUI service is running in a new window, do not close that window
echo - Closing this window will not stop the Web GUI service
echo - To stop the service, close the "Douyin Web GUI" window
echo - Or run "scripts\停止WebGUI服务.bat"
echo.
echo Service log: !SERVICE_LOG!
:cleanup
endlocal
if defined _ORIG_CP chcp %_ORIG_CP% >nul 2>&1
exit /b 0