This commit is contained in:
no 2026-06-12 21:15:04 +03:00
parent e37852c8b3
commit d5c0eddba3

482
START.bat
View file

@ -1,242 +1,242 @@
@echo off @echo off
setlocal EnableDelayedExpansion setlocal EnableDelayedExpansion
echo ========================================== echo ==========================================
echo DazedMTLTool Startup Script echo DazedMTLTool Startup Script
echo ========================================== echo ==========================================
echo. echo.
:: Track whether we actually need to create a new venv :: Track whether we actually need to create a new venv
set "NEED_VENV_CREATE=0" set "NEED_VENV_CREATE=0"
:: Determine which venv directory to use (.venv or venv) :: Determine which venv directory to use (.venv or venv)
set "VENV_DIR=" set "VENV_DIR="
if exist ".venv" set "VENV_DIR=.venv" if exist ".venv" set "VENV_DIR=.venv"
if not defined VENV_DIR if exist "venv" set "VENV_DIR=venv" if not defined VENV_DIR if exist "venv" set "VENV_DIR=venv"
set "CREATE_VENV_DIR=" set "CREATE_VENV_DIR="
:: Step 1: Check if a virtual environment exists (.venv or venv) :: Step 1: Check if a virtual environment exists (.venv or venv)
echo [1/4] Checking for a virtual environment... echo [1/4] Checking for a virtual environment...
if defined VENV_DIR ( if defined VENV_DIR (
echo !VENV_DIR! found. Checking its Python version... echo !VENV_DIR! found. Checking its Python version...
if not exist "!VENV_DIR!\Scripts\python.exe" ( if not exist "!VENV_DIR!\Scripts\python.exe" (
echo ERROR: Python executable not found at "!VENV_DIR!\Scripts\python.exe". echo ERROR: Python executable not found at "!VENV_DIR!\Scripts\python.exe".
set "CREATE_VENV_DIR=!VENV_DIR!" set "CREATE_VENV_DIR=!VENV_DIR!"
call :BackupVenv "!VENV_DIR!" call :BackupVenv "!VENV_DIR!"
set "NEED_VENV_CREATE=1" set "NEED_VENV_CREATE=1"
) else ( ) else (
for /f "tokens=2" %%i in ('"!VENV_DIR!\Scripts\python.exe" --version 2^>^&1') do set VENV_PYTHON_VERSION=%%i for /f "tokens=2" %%i in ('"!VENV_DIR!\Scripts\python.exe" --version 2^>^&1') do set VENV_PYTHON_VERSION=%%i
if not defined VENV_PYTHON_VERSION ( if not defined VENV_PYTHON_VERSION (
echo ERROR: Could not determine Python version from "!VENV_DIR!\Scripts\python.exe". echo ERROR: Could not determine Python version from "!VENV_DIR!\Scripts\python.exe".
set "CREATE_VENV_DIR=!VENV_DIR!" set "CREATE_VENV_DIR=!VENV_DIR!"
call :BackupVenv "!VENV_DIR!" call :BackupVenv "!VENV_DIR!"
set "NEED_VENV_CREATE=1" set "NEED_VENV_CREATE=1"
) else ( ) else (
echo Detected Python version: !VENV_PYTHON_VERSION! echo Detected Python version: !VENV_PYTHON_VERSION!
for /f "tokens=1,2 delims=." %%a in ("!VENV_PYTHON_VERSION!") do ( for /f "tokens=1,2 delims=." %%a in ("!VENV_PYTHON_VERSION!") do (
set VENV_MAJOR=%%a set VENV_MAJOR=%%a
set VENV_MINOR=%%b set VENV_MINOR=%%b
) )
if !VENV_MAJOR! EQU 3 if !VENV_MINOR! GEQ 12 if !VENV_MINOR! LSS 15 ( if !VENV_MAJOR! EQU 3 if !VENV_MINOR! GEQ 12 if !VENV_MINOR! LSS 15 (
echo !VENV_DIR! Python version !VENV_PYTHON_VERSION! is compatible ^(^>^=3.12 and ^<3.15^). echo !VENV_DIR! Python version !VENV_PYTHON_VERSION! is compatible ^(^>^=3.12 and ^<3.15^).
goto :activate_venv goto :activate_venv
) else ( ) else (
echo !VENV_DIR! Python version !VENV_PYTHON_VERSION! is not supported ^(requires ^>^=3.12 and ^<3.15^) echo !VENV_DIR! Python version !VENV_PYTHON_VERSION! is not supported ^(requires ^>^=3.12 and ^<3.15^)
echo Backing up !VENV_DIR!... echo Backing up !VENV_DIR!...
set "CREATE_VENV_DIR=!VENV_DIR!" set "CREATE_VENV_DIR=!VENV_DIR!"
call :BackupVenv "!VENV_DIR!" call :BackupVenv "!VENV_DIR!"
set "NEED_VENV_CREATE=1" set "NEED_VENV_CREATE=1"
) )
) )
) )
) else ( ) else (
echo No existing virtual environment found. echo No existing virtual environment found.
set "CREATE_VENV_DIR=.venv" set "CREATE_VENV_DIR=.venv"
set "NEED_VENV_CREATE=1" set "NEED_VENV_CREATE=1"
) )
echo. echo.
:: If we don't need to create a new venv, skip straight to activation :: If we don't need to create a new venv, skip straight to activation
if "%NEED_VENV_CREATE%"=="0" goto :activate_venv if "%NEED_VENV_CREATE%"=="0" goto :activate_venv
:: Step 2: Find suitable global Python and create a virtual environment :: Step 2: Find suitable global Python and create a virtual environment
set "FOUND_PYTHON=" set "FOUND_PYTHON="
for /f "delims=" %%p in ('where python 2^>nul') do ( for /f "delims=" %%p in ('where python 2^>nul') do (
call :CheckPythonVersion "%%p" call :CheckPythonVersion "%%p"
) )
:: Fallback: try 'python' directly if 'where' found nothing (handles Windows Store alias) :: Fallback: try 'python' directly if 'where' found nothing (handles Windows Store alias)
if not defined FOUND_PYTHON ( if not defined FOUND_PYTHON (
python --version >nul 2>&1 python --version >nul 2>&1
if not errorlevel 1 ( if not errorlevel 1 (
call :CheckPythonVersion "python" call :CheckPythonVersion "python"
) )
) )
if not defined FOUND_PYTHON ( if not defined FOUND_PYTHON (
echo ERROR: No suitable Python ^(>=3.12 and <3.15^) found in PATH. echo ERROR: No suitable Python ^(>=3.12 and <3.15^) found in PATH.
echo Please install Python 3.12, 3.13, or 3.14 and ensure it is in your PATH. echo Please install Python 3.12, 3.13, or 3.14 and ensure it is in your PATH.
pause pause
exit /b 1 exit /b 1
) )
:create_venv :create_venv
if not defined CREATE_VENV_DIR set "CREATE_VENV_DIR=.venv" if not defined CREATE_VENV_DIR set "CREATE_VENV_DIR=.venv"
echo Creating new !CREATE_VENV_DIR! using !FOUND_PYTHON! ... echo Creating new !CREATE_VENV_DIR! using !FOUND_PYTHON! ...
"!FOUND_PYTHON!" -m venv !CREATE_VENV_DIR! "!FOUND_PYTHON!" -m venv !CREATE_VENV_DIR!
if errorlevel 1 ( if errorlevel 1 (
echo ERROR: Failed to create virtual environment. echo ERROR: Failed to create virtual environment.
pause pause
exit /b 1 exit /b 1
) )
echo Virtual environment created echo Virtual environment created
echo. echo.
:: Proceed to activation after creating the venv to avoid falling through into subroutines :: Proceed to activation after creating the venv to avoid falling through into subroutines
set "VENV_DIR=!CREATE_VENV_DIR!" set "VENV_DIR=!CREATE_VENV_DIR!"
goto :activate_venv goto :activate_venv
:CheckPythonVersion :CheckPythonVersion
rem -- %1 is the python executable path rem -- %1 is the python executable path
rem -- Skip if we already found a suitable Python rem -- Skip if we already found a suitable Python
if defined FOUND_PYTHON goto :eof if defined FOUND_PYTHON goto :eof
set "PYTHON_VERSION=" set "PYTHON_VERSION="
set "MAJOR=" set "MAJOR="
set "MINOR=" set "MINOR="
for /f "tokens=2" %%i in ('"%~1" --version 2^>nul') do set PYTHON_VERSION=%%i for /f "tokens=2" %%i in ('"%~1" --version 2^>nul') do set PYTHON_VERSION=%%i
if not defined PYTHON_VERSION goto :eof if not defined PYTHON_VERSION goto :eof
for /f "tokens=1,2 delims=." %%a in ("%PYTHON_VERSION%") do ( for /f "tokens=1,2 delims=." %%a in ("%PYTHON_VERSION%") do (
set MAJOR=%%a set MAJOR=%%a
set MINOR=%%b set MINOR=%%b
) )
if not defined MAJOR goto :eof if not defined MAJOR goto :eof
if not defined MINOR goto :eof if not defined MINOR goto :eof
if !MAJOR! EQU 3 if !MINOR! GEQ 12 if !MINOR! LSS 15 ( if !MAJOR! EQU 3 if !MINOR! GEQ 12 if !MINOR! LSS 15 (
set "FOUND_PYTHON=%~1" set "FOUND_PYTHON=%~1"
) )
goto :eof goto :eof
:activate_venv :activate_venv
echo Activating virtual environment... echo Activating virtual environment...
call !VENV_DIR!\Scripts\activate.bat call !VENV_DIR!\Scripts\activate.bat
if errorlevel 1 ( if errorlevel 1 (
echo ERROR: Failed to activate virtual environment at "!VENV_DIR!". echo ERROR: Failed to activate virtual environment at "!VENV_DIR!".
echo Attempting to recreate the virtual environment with a compatible Python... echo Attempting to recreate the virtual environment with a compatible Python...
set "CREATE_VENV_DIR=!VENV_DIR!" set "CREATE_VENV_DIR=!VENV_DIR!"
call :BackupVenv "!VENV_DIR!" call :BackupVenv "!VENV_DIR!"
set "FOUND_PYTHON=" set "FOUND_PYTHON="
for /f "delims=" %%p in ('where python') do ( for /f "delims=" %%p in ('where python') do (
call :CheckPythonVersion "%%p" call :CheckPythonVersion "%%p"
) )
if not defined FOUND_PYTHON ( if not defined FOUND_PYTHON (
echo ERROR: No suitable Python ^(^>^=3.12 and ^<3.15^) found in PATH for recreation. echo ERROR: No suitable Python ^(^>^=3.12 and ^<3.15^) found in PATH for recreation.
pause pause
exit /b 1 exit /b 1
) )
echo Recreating !CREATE_VENV_DIR! using !FOUND_PYTHON! ... echo Recreating !CREATE_VENV_DIR! using !FOUND_PYTHON! ...
"!FOUND_PYTHON!" -m venv !CREATE_VENV_DIR! "!FOUND_PYTHON!" -m venv !CREATE_VENV_DIR!
if errorlevel 1 ( if errorlevel 1 (
echo ERROR: Failed to create virtual environment during recreation. echo ERROR: Failed to create virtual environment during recreation.
pause pause
exit /b 1 exit /b 1
) )
set "VENV_DIR=!CREATE_VENV_DIR!" set "VENV_DIR=!CREATE_VENV_DIR!"
echo Retrying activation... echo Retrying activation...
call !VENV_DIR!\Scripts\activate.bat call !VENV_DIR!\Scripts\activate.bat
if errorlevel 1 ( if errorlevel 1 (
echo ERROR: Activation failed after recreation. echo ERROR: Activation failed after recreation.
pause pause
exit /b 1 exit /b 1
) )
) )
echo Virtual environment activated echo Virtual environment activated
echo. echo.
:: (proceeding to dependency checks and launch) :: (proceeding to dependency checks and launch)
:: Check and install dependencies :: Check and install dependencies
echo Checking dependencies... echo Checking dependencies...
echo Checking if requirements are satisfied... echo Checking if requirements are satisfied...
:: Try importing key packages to see if they're installed :: Try importing key packages to see if they're installed
python -c "import PyQt5; import openai; import dotenv; import PIL; import anthropic; print('All dependencies satisfied')" >nul 2>&1 python -c "import PyQt5; import openai; import dotenv; import PIL; import anthropic; print('All dependencies satisfied')" >nul 2>&1
if errorlevel 1 ( if errorlevel 1 (
echo Upgrading pip... echo Upgrading pip...
python -m pip install --upgrade pip >nul 2>&1 python -m pip install --upgrade pip >nul 2>&1
echo Installing/updating requirements... echo Installing/updating requirements...
pip install -r requirements.txt pip install -r requirements.txt
if errorlevel 1 ( if errorlevel 1 (
echo ERROR: Failed to install requirements. echo ERROR: Failed to install requirements.
pause pause
exit /b 1 exit /b 1
) )
echo Dependencies installed successfully echo Dependencies installed successfully
) else ( ) else (
echo All dependencies are already satisfied echo All dependencies are already satisfied
) )
echo. echo.
:: Launch the GUI :: Launch the GUI
echo ========================================== echo ==========================================
echo Launching DazedMTLTool GUI... echo Launching DazedMTLTool GUI...
echo ========================================== echo ==========================================
echo. echo.
:: Ensure vocab.txt exists (create from example if available) :: Ensure vocab.txt exists (create from example if available)
if not exist "vocab.txt" ( if not exist "vocab.txt" (
if exist "vocab.txt.example" ( if exist "vocab.txt.example" (
echo vocab.txt not found - creating from vocab.txt.example... echo vocab.txt not found - creating from vocab.txt.example...
copy /Y "vocab.txt.example" "vocab.txt" >nul 2>&1 copy /Y "vocab.txt.example" "vocab.txt" >nul 2>&1
if errorlevel 1 ( if errorlevel 1 (
echo ERROR: Failed to copy vocab.txt.example to vocab.txt. echo ERROR: Failed to copy vocab.txt.example to vocab.txt.
) else ( ) else (
echo Created vocab.txt from vocab.txt.example echo Created vocab.txt from vocab.txt.example
) )
) else ( ) else (
echo vocab.txt and vocab.txt.example not found - creating empty vocab.txt to avoid import errors... echo vocab.txt and vocab.txt.example not found - creating empty vocab.txt to avoid import errors...
type NUL > "vocab.txt" type NUL > "vocab.txt"
if errorlevel 1 ( if errorlevel 1 (
echo ERROR: Failed to create empty vocab.txt. echo ERROR: Failed to create empty vocab.txt.
) else ( ) else (
echo Created empty vocab.txt echo Created empty vocab.txt
) )
) )
) )
python start_gui.py python start_gui.py
:: Check if GUI launched successfully :: Check if GUI launched successfully
if errorlevel 1 ( if errorlevel 1 (
echo. echo.
echo ERROR: Failed to launch GUI. echo ERROR: Failed to launch GUI.
echo Check the error messages above. echo Check the error messages above.
pause pause
exit /b 1 exit /b 1
) )
echo. echo.
echo GUI closed successfully. echo GUI closed successfully.
:: End of main flow - prevent falling through into subroutines below :: End of main flow - prevent falling through into subroutines below
goto :eof goto :eof
:: Backup venv subroutine (supports .venv or venv) :: Backup venv subroutine (supports .venv or venv)
:BackupVenv :BackupVenv
set "TARGET_DIR=%~1" set "TARGET_DIR=%~1"
if not defined TARGET_DIR set "TARGET_DIR=.venv" if not defined TARGET_DIR set "TARGET_DIR=.venv"
set BAK_IDX=1 set BAK_IDX=1
:BackupLoop :BackupLoop
if exist "%TARGET_DIR%.bak_!BAK_IDX!" ( if exist "%TARGET_DIR%.bak_!BAK_IDX!" (
set /a BAK_IDX+=1 set /a BAK_IDX+=1
goto BackupLoop goto BackupLoop
) )
move /Y "%TARGET_DIR%" "%TARGET_DIR%.bak_!BAK_IDX!" >nul 2>&1 move /Y "%TARGET_DIR%" "%TARGET_DIR%.bak_!BAK_IDX!" >nul 2>&1
if errorlevel 1 ( if errorlevel 1 (
echo ERROR: Failed to back up %TARGET_DIR% to %TARGET_DIR%.bak_!BAK_IDX!. echo ERROR: Failed to back up %TARGET_DIR% to %TARGET_DIR%.bak_!BAK_IDX!.
echo Please ensure no files are locked and try again. echo Please ensure no files are locked and try again.
goto :eof goto :eof
) )
echo %TARGET_DIR% renamed to %TARGET_DIR%.bak_!BAK_IDX! echo %TARGET_DIR% renamed to %TARGET_DIR%.bak_!BAK_IDX!
goto :eof goto :eof