File size: 8,111 Bytes
5f0212d |
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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
@echo off
setlocal enabledelayedexpansion
:: Capture all arguments into ARGS
set "ARGS=%*"
set "NATIVE=native"
set "DOCKER_UTILS=docker_utils"
set "FULL_DOCKER=full_docker"
set "SCRIPT_MODE=%NATIVE%"
set "SCRIPT_DIR=%~dp0"
set "PYTHON_VERSION=3.12"
set "DOCKER_UTILS_IMG=utils"
set "PYTHON_ENV=python_env"
set "CURRENT_ENV="
set "PROGRAMS_LIST=calibre ffmpeg"
set "CONDA_URL=https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe"
set "CONDA_INSTALLER=%TEMP%\Miniconda3-latest-Windows-x86_64.exe"
set "CONDA_INSTALL_DIR=%USERPROFILE%\miniconda3"
set "CONDA_PATH=%USERPROFILE%\miniconda3\bin"
set "PATH=%CONDA_PATH%;%PATH%"
set "PROGRAMS_CHECK=0"
set "CONDA_CHECK_STATUS=0"
set "CONDA_RUN_INIT=0"
set "DOCKER_CHECK_STATUS=0"
set "DOCKER_BUILD_STATUS=0"
set "CALIBRE_TEMP_DIR=C:\Windows\Temp\Calibre"
if not exist "%CALIBRE_TEMP_DIR%" (
mkdir "%CALIBRE_TEMP_DIR%"
)
icacls "%CALIBRE_TEMP_DIR%" /grant Users:(OI)(CI)F /T
for %%A in (%ARGS%) do (
if "%%A"=="%DOCKER_UTILS%" (
set "SCRIPT_MODE=%DOCKER_UTILS%"
break
)
)
cd /d "%SCRIPT_DIR%"
:: Check if running inside Docker
if defined CONTAINER (
echo Running in %FULL_DOCKER% mode
set "SCRIPT_MODE=%FULL_DOCKER%"
goto main
)
echo Running in %SCRIPT_MODE% mode
:: Check if running in a Conda environment
if defined CONDA_DEFAULT_ENV (
set "CURRENT_ENV=%CONDA_PREFIX%"
)
:: Check if running in a Python virtual environment
if defined VIRTUAL_ENV (
set "CURRENT_ENV=%VIRTUAL_ENV%"
)
for /f "delims=" %%i in ('where python') do (
if defined CONDA_PREFIX (
if /i "%%i"=="%CONDA_PREFIX%\Scripts\python.exe" (
set "CURRENT_ENV=%CONDA_PREFIX%"
break
)
) else if defined VIRTUAL_ENV (
if /i "%%i"=="%VIRTUAL_ENV%\Scripts\python.exe" (
set "CURRENT_ENV=%VIRTUAL_ENV%"
break
)
)
)
if not "%CURRENT_ENV%"=="" (
echo Current python virtual environment detected: %CURRENT_ENV%.
echo This script runs with its own virtual env and must be out of any other virtual environment when it's launched.
goto failed
)
goto conda_check
:conda_check
where conda >nul 2>&1
if %errorlevel% neq 0 (
set "CONDA_CHECK_STATUS=1"
) else (
if "%SCRIPT_MODE%"=="%DOCKER_UTILS%" (
goto docker_check
exit /b
) else (
call :programs_check
)
)
goto dispatch
exit /b
:programs_check
set "missing_prog_array="
for %%p in (%PROGRAMS_LIST%) do (
set "FOUND="
for /f "delims=" %%i in ('where %%p 2^>nul') do (
set "FOUND=%%i"
)
if not defined FOUND (
echo %%p is not installed.
set "missing_prog_array=!missing_prog_array! %%p"
)
)
if not "%missing_prog_array%"=="" (
set "PROGRAMS_CHECK=1"
)
exit /b
:docker_check
docker --version >nul 2>&1
if %errorlevel% neq 0 (
set "DOCKER_CHECK_STATUS=1"
) else (
:: Verify Docker is running
call docker info >nul 2>&1
if %errorlevel% neq 0 (
set "DOCKER_CHECK_STATUS=1"
) else (
:: Check if the Docker socket is running
set "docker_socket="
if exist \\.\pipe\docker_engine (
set "docker_socket=Windows"
)
if not defined docker_socket (
echo Cannot connect to docker socket. Check if the docker socket is running.
goto failed
exit /b
) else (
:: Check if the Docker image is available
call docker images -q %DOCKER_UTILS_IMG% >nul 2>&1
if %errorlevel% neq 0 (
echo Docker image '%DOCKER_UTILS_IMG%' not found. Installing it now...
set "DOCKER_BUILD_STATUS=1"
) else (
goto dispatch
exit /b
)
)
)
)
goto install_components
exit /b
:install_components
:: Check if running as administrator
net session >nul 2>&1
if %errorlevel% neq 0 (
echo This script needs to be run as administrator.
echo Attempting to restart with administrator privileges...
if defined ARGS (
call powershell -ExecutionPolicy Bypass -Command "Start-Process '%~f0' -ArgumentList '%ARGS%' -WorkingDirectory '%SCRIPT_DIR%' -Verb RunAs"
) else (
call powershell -ExecutionPolicy Bypass -Command "Start-Process '%~f0' -WorkingDirectory '%SCRIPT_DIR%' -Verb RunAs"
)
exit /b
)
:: Install Chocolatey if not already installed
choco -v >nul 2>&1
if %errorlevel% neq 0 (
echo Chocolatey is not installed. Installing Chocolatey...
call powershell -ExecutionPolicy Bypass -Command "Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))"
)
:: Install Python if not already installed
python --version >nul 2>&1
if %errorlevel% neq 0 (
echo Python is not installed. Installing Python...
call choco install python -y
)
:: Install missing packages if any
if not "%PROGRAMS_CHECK%"=="0" (
call choco install %missing_prog_array% -y --force
setx CALIBRE_TEMP_DIR "%CALIBRE_TEMP_DIR%" /M
set "PROGRAMS_CHECK=0"
set "missing_prog_array="
)
:: Install Conda if not already installed
if not "%CONDA_CHECK_STATUS%"=="0" (
echo Installing Conda...
call powershell -Command "[System.Environment]::SetEnvironmentVariable('Path', [System.Environment]::GetEnvironmentVariable('Path','Machine') + ';' + [System.Environment]::GetEnvironmentVariable('Path','User'),'Process')"
echo Downloading Conda installer...
call bitsadmin /transfer "MinicondaDownload" %CONDA_URL% "%CONDA_INSTALLER%"
"%CONDA_INSTALLER%" /InstallationType=JustMe /RegisterPython=0 /AddToPath=1 /S /D=%CONDA_INSTALL_DIR%
if exist "%CONDA_INSTALL_DIR%\condabin\conda.bat" (
echo Conda installed successfully.
set "CONDA_RUN_INIT=1"
set "CONDA_CHECK_STATUS=0"
set "PATH=%CONDA_INSTALL_DIR%\condabin;%PATH%"
)
)
:: Install Docker if not already installed
if not "%DOCKER_CHECK_STATUS%"=="0" (
echo Docker is not installed. Installing it now...
call choco install docker-cli docker-engine -y
call docker --version >nul 2>&1
if %errorlevel% equ 0 (
echo Starting Docker Engine...
net start com.docker.service >nul 2>&1
if %errorlevel% equ 0 (
echo Docker installed and started successfully.
set "DOCKER_CHECK_STATUS=0"
)
)
)
:: Build Docker image if required
if not "%DOCKER_BUILD_STATUS%"=="0" (
call conda activate "%SCRIPT_DIR%\%PYTHON_ENV%"
call python -m pip install -e .
call docker build -f DockerfileUtils -t utils .
call conda deactivate
call docker images -q %DOCKER_UTILS_IMG% >nul 2>&1
if %errorlevel% equ 0 (
set "DOCKER_BUILD_STATUS=0"
)
)
net session >nul 2>&1
if %errorlevel% equ 0 (
echo Restarting in user mode...
start "" /b cmd /c "%~f0" %ARGS%
exit /b
)
goto dispatch
exit /b
:dispatch
if "%PROGRAMS_CHECK%"=="0" (
if "%CONDA_CHECK_STATUS%"=="0" (
if "%DOCKER_CHECK_STATUS%"=="0" (
if "%DOCKER_BUILD_STATUS%"=="0" (
goto main
exit /b
)
) else (
goto failed
exit /b
)
)
)
echo PROGRAMS_CHECK: %PROGRAMS_CHECK%
echo CONDA_CHECK_STATUS: %CONDA_CHECK_STATUS%
echo DOCKER_CHECK_STATUS: %DOCKER_CHECK_STATUS%
echo DOCKER_BUILD_STATUS: %DOCKER_BUILD_STATUS%
timeout /t 5 /nobreak >nul
goto install_components
exit /b
:main
if "%SCRIPT_MODE%"=="%FULL_DOCKER%" (
python %SCRIPT_DIR%\app.py --script_mode %FULL_DOCKER% %ARGS%
) else (
if "%CONDA_RUN_INIT%"=="1" (
call conda init
set "CONDA_RUN_INIT=0"
)
if not exist "%SCRIPT_DIR%\%PYTHON_ENV%" (
call conda create --prefix %SCRIPT_DIR%\%PYTHON_ENV% python=%PYTHON_VERSION% -y
call conda activate %SCRIPT_DIR%\%PYTHON_ENV%
call python -m pip install --upgrade pip
call python -m pip install --upgrade -r requirements.txt --progress-bar=on
) else (
call conda activate %SCRIPT_DIR%\%PYTHON_ENV%
)
python %SCRIPT_DIR%\app.py --script_mode %SCRIPT_MODE% %ARGS%
call conda deactivate
)
exit /b
:failed
echo ebook2audiobook is not correctly installed or run.
exit /b
endlocal
pause |