diff --git a/gui/gui_dashboard_app_entry.py b/gui/gui_dashboard_app_entry.py index 32edf84..b64d91b 100644 --- a/gui/gui_dashboard_app_entry.py +++ b/gui/gui_dashboard_app_entry.py @@ -1,5 +1,13 @@ from __future__ import annotations +import sys +from pathlib import Path + + +_BASE_DIR = Path(__file__).resolve().parent.parent +if str(_BASE_DIR) not in sys.path: + sys.path.insert(0, str(_BASE_DIR)) + from douyin.updater.bootstrap import run_startup_update_check_early @@ -10,4 +18,3 @@ from gui.dashboard_app.app import main # noqa: E402 if __name__ == "__main__": main() - diff --git a/scripts/一键启动WebGUI.bat b/scripts/一键启动WebGUI.bat index 17ae541..b2a5267 100644 --- a/scripts/一键启动WebGUI.bat +++ b/scripts/一键启动WebGUI.bat @@ -17,9 +17,8 @@ if errorlevel 1 ( goto :cleanup ) -REM Save current directory to variable (for delayed expansion) -set "WORK_DIR=%~dp0.." -set "WORK_DIR=!WORK_DIR:~0,-1!" +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" @@ -50,11 +49,11 @@ echo. REM [2/6] Check required files echo [2/6] Checking required files... -if not exist "gui\web_gui.py" ( - echo [ERROR] web_gui.py not found +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.py - echo [ERROR] web_gui.py does not exist >> "%LOG_FILE%" + echo Expected path: !WORK_DIR!\gui\web_gui_entry.py + echo [ERROR] web_gui_entry.py does not exist >> "%LOG_FILE%" pause goto :cleanup ) @@ -64,15 +63,15 @@ 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'; Get-CimInstance Win32_Process -ErrorAction SilentlyContinue | Where-Object {$_.CommandLine -like '*web_gui.py*' -and $_.Name -like 'python*'} | ForEach-Object {Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue}" >nul 2>&1 -timeout /t 2 /nobreak >nul +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=8000" -if defined PORT set "WEB_PORT=!PORT!" -if defined WEB_GUI_PORT set "WEB_PORT=!WEB_GUI_PORT!" +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. @@ -104,30 +103,29 @@ if exist ".venv\Scripts\python.exe" ( ) 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_CMD=!WORK_DIR!\.venv\Scripts\python.exe gui\web_gui.py" -) else if !USE_VENV! equ 2 ( - REM Use activate.bat - set "PYTHON_CMD=call !WORK_DIR!\.venv\Scripts\activate.bat && python gui\web_gui.py" + set "PYTHON_EXE=!WORK_DIR!\.venv\Scripts\python.exe" ) else ( - REM Use system Python - set "PYTHON_CMD=python gui\web_gui.py" + REM Use system Python or activated Python through PATH + set "PYTHON_EXE=python" ) -REM Start command with proper quoting -start "Douyin Web GUI" /D "!WORK_DIR!" cmd /k "cd /d !WORK_DIR! && echo Starting Web GUI service... && echo. && !PYTHON_CMD!" +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 -timeout /t 2 /nobreak >nul +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 +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 @@ -147,7 +145,7 @@ goto :wait_loop REM [6/6] Open browser echo. echo [6/6] Opening browser... -timeout /t 1 /nobreak >nul +powershell -NoProfile -Command "Start-Sleep -Seconds 1" >nul 2>&1 start "" "http://127.0.0.1:!WEB_PORT!" echo. @@ -162,16 +160,9 @@ 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 "StopWebGUIService.bat" +echo - Or run "scripts\停止WebGUI服务.bat" echo. - -REM Ask if view log -set /p view_log="View startup log? (Y/N): " -if /i "!view_log!"=="Y" ( - if exist "%LOG_FILE%" ( - notepad "%LOG_FILE%" - ) -) +echo Service log: !SERVICE_LOG! :cleanup endlocal diff --git a/scripts/停止WebGUI服务.bat b/scripts/停止WebGUI服务.bat index 0f1a566..04dad56 100644 --- a/scripts/停止WebGUI服务.bat +++ b/scripts/停止WebGUI服务.bat @@ -11,8 +11,8 @@ echo. echo 正在查找Web GUI进程... -REM 查找并停止web_gui.py进程 -powershell -NoProfile -Command "$ps = Get-CimInstance Win32_Process | Where-Object { ($_.CommandLine -like '*web_gui.py*') -and ($_.Name -like 'python*') }; if ($ps) { $ps | ForEach-Object { try { $pid = $_.ProcessId; Stop-Process -Id $pid -Force -ErrorAction SilentlyContinue; Write-Host \"[已停止] PID=$pid\" } catch { Write-Host \"[停止失败] PID=$($_.ProcessId): $_\" } } } else { Write-Host \"[未找到] 没有运行中的Web GUI进程\" }" +REM 查找并停止 web_gui.py / web_gui_entry.py 进程 +powershell -NoProfile -Command "$root=(Resolve-Path (Join-Path '%~dp0' '..')).Path; $ps = Get-CimInstance Win32_Process | Where-Object { ($_.Name -like 'python*') -and ($_.CommandLine -like ('*' + $root + '*')) -and (($_.CommandLine -like '*web_gui.py*') -or ($_.CommandLine -like '*web_gui_entry.py*')) }; if ($ps) { $ps | ForEach-Object { try { $targetPid = $_.ProcessId; Stop-Process -Id $targetPid -Force -ErrorAction SilentlyContinue; Write-Host \"[已停止] PID=$targetPid\" } catch { Write-Host \"[停止失败] PID=$($_.ProcessId): $_\" } } } else { Write-Host \"[未找到] 没有运行中的Web GUI进程\" }" echo. echo 操作完成!