-
-
Notifications
You must be signed in to change notification settings - Fork 570
/
Copy pathebook2audiobook.cmd
executable file
·224 lines (205 loc) · 6.33 KB
/
ebook2audiobook.cmd
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
@echo off
setlocal enabledelayedexpansion
:: Capture all arguments into ARGS
set "ARGS=%*"
set "NATIVE=native"
set "FULL_DOCKER=full_docker"
set "SCRIPT_MODE=%NATIVE%"
set "SCRIPT_DIR=%~dp0"
set "PYTHON_VERSION=3.12"
set "PYTHON_ENV=python_env"
set "CURRENT_ENV="
set "PROGRAMS_LIST=calibre ffmpeg nodejs espeak-ng"
set "TMP=%SCRIPT_DIR%\tmp"
set "TEMP=%SCRIPT_DIR%\tmp"
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 "CONDA_ACTIVATE=%CONDA_INSTALL_DIR%\condabin\conda.bat"
set "ESPEAK_DATA_PATH=%USERPROFILE%\scoop\apps\espeak-ng\current\eSpeak NG\espeak-ng-data"
set "PATH=%PATH%;%CONDA_PATH%;%CONDA_INSTALL_DIR%\condabin;%USERPROFILE%\scoop\shims"
set "PROGRAMS_CHECK=0"
set "CONDA_CHECK_STATUS=0"
set "CONDA_RUN_INIT=0"
set "DOCKER_CHECK_STATUS=0"
set "HELP_FOUND=%ARGS:--help=%"
:: Refresh environment variables (append registry Path to current PATH)
for /f "tokens=2,*" %%A in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path') do (
set "PATH=%%B;%PATH%"
)
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
goto conda_check
:conda_check
set "conda_version="
for /f "tokens=* delims=" %%i in ('conda --version 2^>nul') do (
set "conda_version=%%i"
)
if not defined conda_version (
set "CONDA_CHECK_STATUS=1"
) else (
:: 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 /Q 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
)
call :programs_check
)
goto dispatch
:programs_check
set "missing_prog_array="
for %%p in (%PROGRAMS_LIST%) do (
set "prog=%%p"
if "%%p"=="nodejs" set "prog=node"
where /Q !prog!
if errorlevel 1 (
echo %%p is not installed.
set "missing_prog_array=!missing_prog_array! %%p"
)
)
if not "!missing_prog_array!"=="" (
set "PROGRAMS_CHECK=1"
goto install_components
)
goto dispatch
:docker_check
where /Q docker
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 (
goto dispatch
)
)
goto install_components
:install_components
:: Install Scoop if not already installed
where /Q scoop
if %errorlevel% neq 0 (
echo Scoop is not installed. Installing Scoop...
powershell -NoProfile -ExecutionPolicy Bypass -Command "iwr -useb get.scoop.sh | iex"
set "PATH=%USERPROFILE%\scoop\shims;%PATH%"
call refreshenv
call scoop install git
call scoop bucket add extras
call scoop bucket known
)
:: Install Python if not already installed
where /Q python
if %errorlevel% neq 0 (
echo Python is not installed. Installing Python...
call scoop install python
)
:: Install missing packages if any
if not "%PROGRAMS_CHECK%"=="0" (
echo Installing %missing_prog_array%
call scoop install %missing_prog_array%
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 powershell -Command "Invoke-WebRequest -Uri %CONDA_URL% -OutFile "%CONDA_INSTALLER%"
"%CONDA_INSTALLER%" /InstallationType=JustMe /RegisterPython=0 /AddToPath=1 /S /D=%CONDA_INSTALL_DIR%
if exist "%CONDA_ACTIVATE%" (
echo Conda installed successfully.
set "CONDA_RUN_INIT=1"
set "CONDA_CHECK_STATUS=0"
)
)
:: Install Docker if not already installed
if not "%DOCKER_CHECK_STATUS%"=="0" (
echo Docker is not installed. Installing it now...
call scoop install docker-cli
call scoop install docker-engine
call where /Q docker
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"
)
)
)
goto dispatch
:dispatch
if "%PROGRAMS_CHECK%"=="0" (
if "%CONDA_CHECK_STATUS%"=="0" (
if "%DOCKER_CHECK_STATUS%"=="0" (
goto main
) else (
goto failed
)
)
)
echo PROGRAMS_CHECK: %PROGRAMS_CHECK%
echo CONDA_CHECK_STATUS: %CONDA_CHECK_STATUS%
echo DOCKER_CHECK_STATUS: %DOCKER_CHECK_STATUS%
timeout /t 5 /nobreak >nul
goto install_components
:main
if "%SCRIPT_MODE%"=="%FULL_DOCKER%" (
call python %SCRIPT_DIR%\app.py --script_mode %SCRIPT_MODE% %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 cache purge
call python -m pip install --upgrade pip
for /f %%p in (requirements.txt) do (
echo Installing %%p...
python -m pip install --upgrade --no-cache-dir --progress-bar=on %%p
)
echo ✅ All required packages are installed.
) else (
call conda activate "%SCRIPT_DIR%\%PYTHON_ENV%"
)
call 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