important fixes on installation code parts

This commit is contained in:
unknown
2025-01-12 15:42:41 -08:00
parent cb5a9ed510
commit 7280562e87
4 changed files with 577 additions and 568 deletions

0
.cache/.gitkeep Normal file
View File

160
app.py
View File

@@ -1,5 +1,6 @@
import argparse
import os
import re
import socket
import subprocess
import sys
@@ -9,10 +10,12 @@ from lib.lang import install_info, language_mapping, default_language_code
from lib.models import tts_engines, default_xtts_files, default_fine_tuned
def check_virtual_env(script_mode):
if str(sys.prefix) == str(os.path.abspath(os.path.join('.','python_env'))) or script_mode == FULL_DOCKER:
if str(os.path.basename(sys.prefix)) == 'python_env' or script_mode == FULL_DOCKER:
return True
error = f'''***********
Wrong launch! ebook2audiobook must run in its own virtual environment!
NOTE: If you are running a Docker so you are probably using an old version of ebook2audiobook.
To solve this issue go to download the new version at https://github.com/DrewThomasson/ebook2audiobook
If the folder python_env does not exist in the ebook2audiobook root folder,
run your command with "./ebook2audiobook.sh" for Linux and Mac or "ebook2audiobook.cmd" for Windows
to install it all automatically.
@@ -21,7 +24,6 @@ to install it all automatically.
print(error)
return False
def check_python_version():
current_version = sys.version_info[:2] # (major, minor)
if current_version < min_python_version or current_version > max_python_version:
@@ -48,7 +50,6 @@ def check_and_install_requirements(file_path):
missing_packages = []
for package in packages:
# Extract package name without version specifier
import regex as re
pkg_name = re.split(r'[<>=]', package)[0].strip()
try:
installed_version = version(pkg_name)
@@ -67,7 +68,7 @@ def check_and_install_requirements(file_path):
return True
except Exception as e:
raise(f'An error occurred: {e}')
raise SystemExit(f'An error occurred: {e}')
def check_dictionary():
import unidic
@@ -170,90 +171,91 @@ Linux/Mac:
args = vars(parser.parse_args())
if not check_virtual_env(args['script_mode']):
sys.exit(1)
if not check_python_version():
sys.exit(1)
# Check if the port is already in use to prevent multiple launches
if not args['headless'] and is_port_in_use(interface_port):
print(f'Error: Port {interface_port} is already in use. The web interface may already be running.')
sys.exit(1)
args['script_mode'] = args['script_mode'] if args['script_mode'] else NATIVE
args['share'] = args['share'] if args['share'] else False
if args['script_mode'] == NATIVE:
check_pkg = check_and_install_requirements(requirements_file)
if check_pkg:
if not check_dictionary():
sys.exit(1)
else:
print('Some packages could not be installed')
sys.exit(1)
from lib.functions import web_interface, convert_ebook
# Conditions based on the --headless flag
if args['headless']:
args['is_gui_process'] = False
args['audiobooks_dir'] = args['output_dir'] if args['output_dir'] else audiobooks_cli_dir
args['device'] = 'cuda' if args['device'] == 'gpu' else args['device']
# Condition to stop if both --ebook and --ebooks_dir are provided
if args['ebook'] and args['ebooks_dir']:
print('Error: You cannot specify both --ebook and --ebooks_dir in headless mode.')
if not 'help' in args:
if not check_virtual_env(args['script_mode']):
sys.exit(1)
# Condition 1: If --ebooks_dir exists, check value and set 'ebooks_dir'
if args['ebooks_dir']:
new_ebooks_dir = None
if args['ebooks_dir'] == 'default':
print(f'Using the default ebooks_dir: {ebooks_dir}')
new_ebooks_dir = os.path.abspath(ebooks_dir)
else:
# Check if the directory exists
if os.path.exists(args['ebooks_dir']):
new_ebooks_dir = os.path.abspath(args['ebooks_dir'])
else:
print(f'Error: The provided --ebooks_dir "{args["ebooks_dir"]}" does not exist.')
if not check_python_version():
sys.exit(1)
# Check if the port is already in use to prevent multiple launches
if not args['headless'] and is_port_in_use(interface_port):
print(f'Error: Port {interface_port} is already in use. The web interface may already be running.')
sys.exit(1)
args['script_mode'] = args['script_mode'] if args['script_mode'] else NATIVE
args['share'] = args['share'] if args['share'] else False
if args['script_mode'] == NATIVE:
check_pkg = check_and_install_requirements(requirements_file)
if check_pkg:
if not check_dictionary():
sys.exit(1)
if os.path.exists(new_ebooks_dir):
for file in os.listdir(new_ebooks_dir):
# Process files with supported ebook formats
if any(file.endswith(ext) for ext in ebook_formats):
full_path = os.path.join(new_ebooks_dir, file)
print(f'Processing eBook file: {full_path}')
args['ebook'] = full_path
progress_status, audiobook_file = convert_ebook(args)
if audiobook_file is None:
print(f'Conversion failed: {progress_status}')
sys.exit(1)
else:
print(f'Error: The directory {new_ebooks_dir} does not exist.')
print('Some packages could not be installed')
sys.exit(1)
elif args['ebook']:
progress_status, audiobook_file = convert_ebook(args)
if audiobook_file is None:
print(f'Conversion failed: {progress_status}')
from lib.functions import web_interface, convert_ebook
# Conditions based on the --headless flag
if args['headless']:
args['is_gui_process'] = False
args['audiobooks_dir'] = args['output_dir'] if args['output_dir'] else audiobooks_cli_dir
args['device'] = 'cuda' if args['device'] == 'gpu' else args['device']
# Condition to stop if both --ebook and --ebooks_dir are provided
if args['ebook'] and args['ebooks_dir']:
print('Error: You cannot specify both --ebook and --ebooks_dir in headless mode.')
sys.exit(1)
# Condition 1: If --ebooks_dir exists, check value and set 'ebooks_dir'
if args['ebooks_dir']:
new_ebooks_dir = None
if args['ebooks_dir'] == 'default':
print(f'Using the default ebooks_dir: {ebooks_dir}')
new_ebooks_dir = os.path.abspath(ebooks_dir)
else:
# Check if the directory exists
if os.path.exists(args['ebooks_dir']):
new_ebooks_dir = os.path.abspath(args['ebooks_dir'])
else:
print(f'Error: The provided --ebooks_dir "{args["ebooks_dir"]}" does not exist.')
sys.exit(1)
if os.path.exists(new_ebooks_dir):
for file in os.listdir(new_ebooks_dir):
# Process files with supported ebook formats
if any(file.endswith(ext) for ext in ebook_formats):
full_path = os.path.join(new_ebooks_dir, file)
print(f'Processing eBook file: {full_path}')
args['ebook'] = full_path
progress_status, audiobook_file = convert_ebook(args)
if audiobook_file is None:
print(f'Conversion failed: {progress_status}')
sys.exit(1)
else:
print(f'Error: The directory {new_ebooks_dir} does not exist.')
sys.exit(1)
elif args['ebook']:
progress_status, audiobook_file = convert_ebook(args)
if audiobook_file is None:
print(f'Conversion failed: {progress_status}')
sys.exit(1)
else:
print('Error: In headless mode, you must specify either an ebook file using --ebook or an ebook directory using --ebooks_dir.')
sys.exit(1)
else:
print('Error: In headless mode, you must specify either an ebook file using --ebook or an ebook directory using --ebooks_dir.')
sys.exit(1)
else:
args['is_gui_process'] = True
passed_arguments = sys.argv[1:]
allowed_arguments = {'--share', '--script_mode'}
passed_args_set = {arg for arg in passed_arguments if arg.startswith('--')}
if passed_args_set.issubset(allowed_arguments):
web_interface(args)
else:
print('Error: In non-headless mode, no option or only --share can be passed')
sys.exit(1)
args['is_gui_process'] = True
passed_arguments = sys.argv[1:]
allowed_arguments = {'--share', '--script_mode'}
passed_args_set = {arg for arg in passed_arguments if arg.startswith('--')}
if passed_args_set.issubset(allowed_arguments):
web_interface(args)
else:
print('Error: In non-headless mode, no option or only --share can be passed')
sys.exit(1)
if __name__ == '__main__':
main()

View File

@@ -11,11 +11,14 @@ set "FULL_DOCKER=full_docker"
set "SCRIPT_MODE=%NATIVE%"
set "SCRIPT_DIR=%~dp0"
set "TMP=.\.cache"
set "TEMP=.\.cache"
set "PYTHON_VERSION=3.12"
set "DOCKER_UTILS_IMG=utils"
set "PYTHON_ENV=python_env"
set "CURRENT_ENV="
set "PROGRAMS_LIST=calibre ffmpeg"
set "PROGRAMS_LIST=calibre ffmpeg nodejs"
set "CONDA_URL=https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe"
set "CONDA_INSTALLER=%TEMP%\Miniconda3-latest-Windows-x86_64.exe"
@@ -31,256 +34,262 @@ set "DOCKER_BUILD_STATUS=0"
set "CALIBRE_TEMP_DIR=C:\Windows\Temp\Calibre"
if not exist "%CALIBRE_TEMP_DIR%" (
mkdir "%CALIBRE_TEMP_DIR%"
)
set "HELP_FOUND=%ARGS:--help=%"
icacls "%CALIBRE_TEMP_DIR%" /grant Users:(OI)(CI)F /T
if "%HELP_FOUND%" neq "%ARGS%" (
python %SCRIPT_DIR%\app.py --script_mode %SCRIPT_MODE% %ARGS%
} else {
for %%A in (%ARGS%) do (
if "%%A"=="%DOCKER_UTILS%" (
set "SCRIPT_MODE=%DOCKER_UTILS%"
break
)
)
if not exist "%CALIBRE_TEMP_DIR%" (
mkdir "%CALIBRE_TEMP_DIR%"
)
cd /d "%SCRIPT_DIR%"
icacls "%CALIBRE_TEMP_DIR%" /grant Users:(OI)(CI)F /T
:: 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
for %%A in (%ARGS%) do (
if "%%A"=="%DOCKER_UTILS%" (
set "SCRIPT_MODE=%DOCKER_UTILS%"
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
)
cd /d "%SCRIPT_DIR%"
goto conda_check
:: Check if running inside Docker
if defined CONTAINER (
echo Running in %FULL_DOCKER% mode
set "SCRIPT_MODE=%FULL_DOCKER%"
goto main
)
: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
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 (
call :programs_check
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%
goto dispatch
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
)
: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"
)
)
)
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
if not "%missing_prog_array%"=="" (
set "PROGRAMS_CHECK=1"
)
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 pip cache purge
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
: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
:failed
echo ebook2audiobook is not correctly installed or run.
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 cache purge
call python -m pip install --upgrade pip
call python -m pip install --upgrade --no-cache-dir -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
pause

View File

@@ -41,8 +41,10 @@ FULL_DOCKER="full_docker"
SCRIPT_MODE="$NATIVE"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TMPDIR=./.cache
WGET=$(which wget 2>/dev/null)
REQUIRED_PROGRAMS=("calibre" "ffmpeg" "mecab")
REQUIRED_PROGRAMS=("calibre" "ffmpeg" "mecab" "nodejs")
DOCKER_UTILS_IMG="utils"
PYTHON_ENV="python_env"
CURRENT_ENV=""
@@ -94,260 +96,256 @@ else
fi
fi
# Check if running in a Conda or Python virtual environment
if [[ -n "$CONDA_DEFAULT_ENV" ]]; then
CURRENT_ENV="$CONDA_PREFIX"
elif [[ -n "$VIRTUAL_ENV" ]]; then
CURRENT_ENV="$VIRTUAL_ENV"
fi
if [[ -n "${arguments['help']+exists}" && ${arguments['help']} = true ]]; then
python app.py "${ARGS[@]}"
else
# Check if running in a Conda or Python virtual environment
if [[ -n "$CONDA_DEFAULT_ENV" ]]; then
CURRENT_ENV="$CONDA_PREFIX"
elif [[ -n "$VIRTUAL_ENV" ]]; then
CURRENT_ENV="$VIRTUAL_ENV"
fi
# If neither environment variable is set, check Python path
if [[ -z "$CURRENT_ENV" ]]; then
PYTHON_PATH=$(which python 2>/dev/null)
if [[ ( -n "$CONDA_PREFIX" && "$PYTHON_PATH" = "$CONDA_PREFIX/bin/python" ) || ( -n "$VIRTUAL_ENV" && "$PYTHON_PATH" = "$VIRTUAL_ENV/bin/python" ) ]]; then
CURRENT_ENV="${CONDA_PREFIX:-$VIRTUAL_ENV}"
# If neither environment variable is set, check Python path
if [[ -z "$CURRENT_ENV" ]]; then
PYTHON_PATH=$(which python 2>/dev/null)
if [[ ( -n "$CONDA_PREFIX" && "$PYTHON_PATH" = "$CONDA_PREFIX/bin/python" ) || ( -n "$VIRTUAL_ENV" && "$PYTHON_PATH" = "$VIRTUAL_ENV/bin/python" ) ]]; then
CURRENT_ENV="${CONDA_PREFIX:-$VIRTUAL_ENV}"
fi
fi
# Output result if a virtual environment is detected
if [[ -n "$CURRENT_ENV" ]]; then
echo -e "Current python virtual environment detected: $CURRENT_ENV."
echo -e "This script runs with its own virtual env and must be out of any other virtual environment when it's launched."
echo -e "If you are using miniconda then you would type in:"
echo -e "conda deactivate"
exit 1
fi
function required_programs_check {
local programs=("$@")
programs_missing=()
for program in "${programs[@]}"; do
if [ "$program" = "nodejs" ]; then
program="node"
fi
if ! command -v "$program" >/dev/null 2>&1; then
echo -e "\e[33m$program is not installed.\e[0m"
programs_missing+=($program)
fi
done
local count=${#programs_missing[@]}
if [[ $count -eq 0 ]]; then
return 0
else
return 1
fi
}
function install_programs {
echo -e "\e[33mInstalling required programs. NOTE: you must have 'sudo' priviliges or it will fail.\e[0m"
if [[ "$OSTYPE" = "darwin"* ]]; then
PACK_MGR="brew install"
if ! command -v brew &> /dev/null; then
echo -e "\e[33mHomebrew is not installed. Installing Homebrew...\e[0m"
/usr/bin/env bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
fi
mecab_extra="mecab-ipadic"
else
PACK_MGR_OPTIONS=""
if command -v emerge &> /dev/null; then
PACK_MGR="sudo emerge"
mecab_extra="app-text/mecab app-text/mecab-ipadic"
elif command -v dnf &> /dev/null; then
PACK_MGR="sudo dnf install"
PACK_MGR_OPTIONS="-y"
mecab_extra="mecab-devel mecab-ipadic"
elif command -v yum &> /dev/null; then
PACK_MGR="sudo yum install"
PACK_MGR_OPTIONS="-y"
mecab_extra="mecab-devel mecab-ipadic"
elif command -v zypper &> /dev/null; then
PACK_MGR="sudo zypper install"
PACK_MGR_OPTIONS="-y"
mecab_extra="mecab-devel mecab-ipadic"
elif command -v pacman &> /dev/null; then
PACK_MGR="sudo pacman -Sy"
mecab_extra="mecab-devel mecab-ipadic"
elif command -v apt-get &> /dev/null; then
sudo apt-get update
PACK_MGR="sudo apt-get install"
PACK_MGR_OPTIONS="-y"
mecab_extra="libmecab-dev mecab-ipadic-utf8"
elif command -v apk &> /dev/null; then
PACK_MGR="sudo apk add"
mecab_extra="mecab-dev mecab-ipadic"
else
echo "Cannot recognize your applications package manager. Please install the required applications manually."
return 1
fi
fi
if [ -z "$WGET" ]; then
echo -e "\e[33m wget is missing! trying to install it... \e[0m"
result=$(eval "$PACK_MGR wget $PACK_MGR_OPTIONS" 2>&1)
result_code=$?
if [ $result_code -eq 0 ]; then
WGET=$(which wget 2>/dev/null)
else
echo "Cannot 'wget'. Please install 'wget' manually."
return 1
fi
fi
for program in "${programs_missing[@]}"; do
if [ "$program" = "calibre" ];then
# avoid conflict with calibre builtin lxml
pip uninstall lxml -y 2>/dev/null
echo -e "\e[33mInstalling Calibre...\e[0m"
if [[ "$OSTYPE" = "darwin"* ]]; then
eval "sudo $PACK_MGR --cask calibre"
else
$WGET -nv -O- https://download.calibre-ebook.com/linux-installer.sh | sudo sh /dev/stdin
fi
if command -v calibre >/dev/null 2>&1; then
echo -e "\e[32m===============>>> Calibre is installed! <<===============\e[0m"
else
echo "Calibre installation failed."
fi
elif [ "$program" = "mecab" ];then
if command -v emerge &> /dev/null; then
eval "sudo $PACK_MGR $mecab_extra $PACK_MGR_OPTIONS"
else
eval "sudo $PACK_MGR $program $mecab_extra $PACK_MGR_OPTIONS"
fi
if command -v $program >/dev/null 2>&1; then
echo -e "\e[32m===============>>> $program is installed! <<===============\e[0m"
else
echo "$program installation failed."
fi
else
eval "sudo $PACK_MGR $program $PACK_MGR_OPTIONS"
if command -v $program >/dev/null 2>&1; then
echo -e "\e[32m===============>>> $program is installed! <<===============\e[0m"
else
echo "$program installation failed."
fi
fi
done
if required_programs_check "${REQUIRED_PROGRAMS[@]}"; then
return 0
else
echo -e "\e[33mYou can run 'ebook2audiobook.sh --script_mode docker_utils' to avoid to install $REQUIRED_PROGRAMS natively.\e[0m"
return 1
fi
}
function conda_check {
if ! command -v conda &> /dev/null; then
echo -e "\e[33mconda is not installed!\e[0m"
echo -e "\e[33mDownloading conda installer...\e[0m"
wget -O "$CONDA_INSTALLER" "$CONDA_URL"
if [[ -f "$CONDA_INSTALLER" ]]; then
echo -e "\e[33mInstalling Miniconda...\e[0m"
bash "$CONDA_INSTALLER" -u -b -p "$CONDA_INSTALL_DIR"
rm -f "$CONDA_INSTALLER"
if [[ -f "$CONDA_INSTALL_DIR/bin/conda" ]]; then
conda init > /dev/null 2>&1
source $CONDA_ENV
echo -e "\e[32m===============>>> conda is installed! <<===============\e[0m"
else
echo -e "\e[31mconda installation failed.\e[0m"
return 1
fi
else
echo -e "\e[31mFailed to download Miniconda installer.\e[0m"
echo -e "\e[33mI'ts better to use the install.sh to install everything needed.\e[0m"
return 1
fi
fi
if [[ ! -d $SCRIPT_DIR/$PYTHON_ENV ]]; then
# Use this condition to chmod writable folders once
chmod -R 777 ./audiobooks ./tmp ./models
conda init > /dev/null 2>&1
source $CONDA_ENV
conda create --prefix $SCRIPT_DIR/$PYTHON_ENV python=$PYTHON_VERSION -y
source activate $SCRIPT_DIR/$PYTHON_ENV
python -m pip cache purge
python -m pip install --upgrade pip
python -m pip install --upgrade --no-cache-dir -r requirements.txt --progress-bar on
conda deactivate
fi
return 0
}
function docker_check {
if ! command -v docker &> /dev/null; then
echo -e "\e[33m docker is missing! trying to install it... \e[0m"
if [[ "$OSTYPE" = "darwin"* ]]; then
echo "Installing Docker using Homebrew..."
$PACK_MGR --cask docker $PACK_MGR_OPTIONS
else
$WGET -qO get-docker.sh https://get.docker.com && \
sudo sh get-docker.sh
sudo systemctl start docker
sudo systemctl enable docker
docker run hello-world
rm -f get-docker.sh
fi
echo -e "\e[32m===============>>> docker is installed! <<===============\e[0m"
docker_build
else
# Check if Docker service is running
if docker info >/dev/null 2>&1; then
if [[ "$(docker images -q $DOCKER_UTILS_IMG 2> /dev/null)" = "" ]]; then
docker_build
fi
else
echo -e "\e[33mDocker is not running\e[0m"
return 1
fi
fi
return 0
}
function docker_build {
# Check if the Docker socket is accessible
if [[ -e /var/run/docker.sock || -e /run/docker.sock ]]; then
echo -e "\e[33mDocker image '$DOCKER_UTILS_IMG' not found. Trying to build it...\e[0m"
docker build -f DockerfileUtils -t utils .
else
echo -e "\e[33mcannot connect to docker socket. Check if the docker socket is running.\e[0m"
fi
}
if [ "$SCRIPT_MODE" = "$FULL_DOCKER" ]; then
echo -e "\e[33mRunning in $FULL_DOCKER mode\e[0m"
python app.py --script_mode "$SCRIPT_MODE" "${ARGS[@]}"
elif [[ "$SCRIPT_MODE" = "$NATIVE" || "$SCRIPT_MODE" = "$DOCKER_UTILS" ]]; then
pass=true
echo -e "\e[33mRunning in $SCRIPT_MODE mode\e[0m"
if [ "$SCRIPT_MODE" = "$NATIVE" ]; then
if ! required_programs_check "${REQUIRED_PROGRAMS[@]}"; then
if ! install_programs; then
pass=false
fi
fi
fi
if [ $pass = true ]; then
if conda_check; then
conda init > /dev/null 2>&1
source $CONDA_ENV
conda deactivate
conda activate $SCRIPT_DIR/$PYTHON_ENV
python app.py --script_mode "$SCRIPT_MODE" "${ARGS[@]}"
conda deactivate
fi
fi
else
echo -e "\e[33mebook2audiobook is not correctly installed or run.\e[0m"
fi
fi
# Output result if a virtual environment is detected
if [[ -n "$CURRENT_ENV" ]]; then
echo -e "Current python virtual environment detected: $CURRENT_ENV."
echo -e "This script runs with its own virtual env and must be out of any other virtual environment when it's launched."
echo -e "If you are using miniconda then you would type in:"
echo -e "conda deactivate"
exit 1
fi
function required_programs_check {
local programs=("$@")
for program in "${programs[@]}"; do
if ! command -v "$program" >/dev/null 2>&1; then
echo -e "\e[33m$program is not installed.\e[0m"
programs_missing+=($program)
fi
done
local count=${#programs_missing[@]}
if [[ $count -eq 0 ]]; then
return 0
else
return 1
fi
}
function install_programs {
echo -e "\e[33mInstalling required programs. NOTE: you must have 'sudo' priviliges or it will fail.\e[0m"
if [[ "$OSTYPE" = "darwin"* ]]; then
PACK_MGR="brew install"
if ! command -v brew &> /dev/null; then
echo -e "\e[33mHomebrew is not installed. Installing Homebrew...\e[0m"
/usr/bin/env bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
fi
mecab_extra="mecab-ipadic"
else
PACK_MGR_OPTIONS=""
if command -v emerge &> /dev/null; then
PACK_MGR="sudo emerge"
mecab_extra="app-text/mecab app-text/mecab-ipadic"
elif command -v dnf &> /dev/null; then
PACK_MGR="sudo dnf install"
PACK_MGR_OPTIONS="-y"
mecab_extra="mecab-devel mecab-ipadic"
elif command -v yum &> /dev/null; then
PACK_MGR="sudo yum install"
PACK_MGR_OPTIONS="-y"
mecab_extra="mecab-devel mecab-ipadic"
elif command -v zypper &> /dev/null; then
PACK_MGR="sudo zypper install"
PACK_MGR_OPTIONS="-y"
mecab_extra="mecab-devel mecab-ipadic"
elif command -v pacman &> /dev/null; then
PACK_MGR="sudo pacman -Sy"
mecab_extra="mecab-devel mecab-ipadic"
elif command -v apt-get &> /dev/null; then
sudo apt-get update
PACK_MGR="sudo apt-get install"
PACK_MGR_OPTIONS="-y"
mecab_extra="libmecab-dev mecab-ipadic-utf8"
elif command -v apk &> /dev/null; then
PACK_MGR="sudo apk add"
mecab_extra="mecab-dev mecab-ipadic"
else
echo "Cannot recognize your applications package manager. Please install the required applications manually."
return 1
fi
fi
if [ -z "$WGET" ]; then
echo -e "\e[33m wget is missing! trying to install it... \e[0m"
result=$(eval "$PACK_MGR wget $PACK_MGR_OPTIONS" 2>&1)
result_code=$?
if [ $result_code -eq 0 ]; then
WGET=$(which wget 2>/dev/null)
else
echo "Cannot 'wget'. Please install 'wget' manually."
return 1
fi
fi
for program in "${programs_missing[@]}"; do
if [ "$program" = "calibre" ];then
# avoid conflict with calibre builtin lxml
pip uninstall lxml -y 2>/dev/null
echo -e "\e[33mInstalling Calibre...\e[0m"
if [[ "$OSTYPE" = "darwin"* ]]; then
eval "$PACK_MGR --cask calibre"
else
$WGET -nv -O- https://download.calibre-ebook.com/linux-installer.sh | sh /dev/stdin
fi
if command -v calibre >/dev/null 2>&1; then
echo -e "\e[32m===============>>> Calibre is installed! <<===============\e[0m"
else
echo "Calibre installation failed."
fi
elif [ "$program" = "mecab" ];then
if command -v emerge &> /dev/null; then
eval "$PACK_MGR $mecab_extra $PACK_MGR_OPTIONS"
else
eval "$PACK_MGR $program $mecab_extra $PACK_MGR_OPTIONS"
fi
if command -v $program >/dev/null 2>&1; then
echo -e "\e[32m===============>>> $program is installed! <<===============\e[0m"
else
echo "$program installation failed."
fi
else
eval "$PACK_MGR $program $PACK_MGR_OPTIONS"
if command -v $program >/dev/null 2>&1; then
echo -e "\e[32m===============>>> $program is installed! <<===============\e[0m"
else
echo "$program installation failed."
fi
fi
done
if required_programs_check "${REQUIRED_PROGRAMS[@]}"; then
return 0
else
echo -e "\e[33mYou can run 'ebook2audiobook.sh --script_mode docker_utils' to avoid to install $REQUIRED_PROGRAMS natively.\e[0m"
return 1
fi
}
function conda_check {
if ! command -v conda &> /dev/null; then
echo -e "\e[33mconda is not installed!\e[0m"
echo -e "\e[33mDownloading conda installer...\e[0m"
wget -O "$CONDA_INSTALLER" "$CONDA_URL"
if [[ -f "$CONDA_INSTALLER" ]]; then
echo -e "\e[33mInstalling Miniconda...\e[0m"
bash "$CONDA_INSTALLER" -u -b -p "$CONDA_INSTALL_DIR"
rm -f "$CONDA_INSTALLER"
if [[ -f "$CONDA_INSTALL_DIR/bin/conda" ]]; then
conda init > /dev/null 2>&1
source $CONDA_ENV
echo -e "\e[32m===============>>> conda is installed! <<===============\e[0m"
else
echo -e "\e[31mconda installation failed.\e[0m"
return 1
fi
else
echo -e "\e[31mFailed to download Miniconda installer.\e[0m"
echo -e "\e[33mI'ts better to use the install.sh to install everything needed.\e[0m"
return 1
fi
fi
if [[ ! -d $SCRIPT_DIR/$PYTHON_ENV ]]; then
pip cache purge
# Use this condition to chmod writable folders once
chmod -R 777 ./audiobooks ./tmp ./models
conda init > /dev/null 2>&1
source $CONDA_ENV
conda create --prefix $SCRIPT_DIR/$PYTHON_ENV python=$PYTHON_VERSION -y
source activate $SCRIPT_DIR/$PYTHON_ENV
python -m pip install --upgrade pip
python -m pip install --upgrade -r requirements.txt --progress-bar=on
conda deactivate
fi
return 0
}
function docker_check {
if ! command -v docker &> /dev/null; then
echo -e "\e[33m docker is missing! trying to install it... \e[0m"
if [[ "$OSTYPE" = "darwin"* ]]; then
echo "Installing Docker using Homebrew..."
$PACK_MGR --cask docker $PACK_MGR_OPTIONS
else
$WGET -qO get-docker.sh https://get.docker.com && \
sudo sh get-docker.sh
sudo systemctl start docker
sudo systemctl enable docker
docker run hello-world
rm -f get-docker.sh
fi
echo -e "\e[32m===============>>> docker is installed! <<===============\e[0m"
docker_build
else
# Check if Docker service is running
if docker info >/dev/null 2>&1; then
if [[ "$(docker images -q $DOCKER_UTILS_IMG 2> /dev/null)" = "" ]]; then
docker_build
fi
else
echo -e "\e[33mDocker is not running\e[0m"
return 1
fi
fi
return 0
}
function docker_build {
# Check if the Docker socket is accessible
if [[ -e /var/run/docker.sock || -e /run/docker.sock ]]; then
echo -e "\e[33mDocker image '$DOCKER_UTILS_IMG' not found. Trying to build it...\e[0m"
docker build -f DockerfileUtils -t utils .
else
echo -e "\e[33mcannot connect to docker socket. Check if the docker socket is running.\e[0m"
fi
}
if [ "$SCRIPT_MODE" = "$FULL_DOCKER" ]; then
echo -e "\e[33mRunning in $FULL_DOCKER mode\e[0m"
python app.py --script_mode "$SCRIPT_MODE" "${ARGS[@]}"
elif [[ "$SCRIPT_MODE" = "$NATIVE" || "$SCRIPT_MODE" = "$DOCKER_UTILS" ]]; then
pass=true
if [ "$SCRIPT_MODE" = "$NATIVE" ]; then
echo -e "\e[33mRunning in $NATIVE mode\e[0m"
if ! required_programs_check "${REQUIRED_PROGRAMS[@]}"; then
if ! install_programs; then
pass=false
fi
fi
else
echo -e "\e[33mRunning in $DOCKER_UTILS mode\e[0m"
if conda_check; then
if docker_check; then
conda init > /dev/null 2>&1
source $CONDA_ENV
conda deactivate
conda activate $SCRIPT_DIR/$PYTHON_ENV
python app.py --script_mode "$DOCKER_UTILS" "${ARGS[@]}"
conda deactivate
fi
fi
fi
if [ $pass = true ]; then
if conda_check; then
conda init > /dev/null 2>&1
source $CONDA_ENV
conda deactivate
conda activate $SCRIPT_DIR/$PYTHON_ENV
python app.py --script_mode "$SCRIPT_MODE" "${ARGS[@]}"
conda deactivate
fi
fi
else
echo -e "\e[33mebook2audiobook is not correctly installed or run.\e[0m"
fi
exit 0