Grio43's picture
Fix run.bat: convert LF to CRLF line endings so CMD parses commands correctly
c53ebec verified
@echo off
setlocal enabledelayedexpansion
cd /d "%~dp0"
rem Force a UTF-8 console + UTF-8 mode in Python so prints with em-dashes and
rem the like don't crash on machines whose console codepage is cp1252 / cp932.
chcp 65001 >nul 2>nul
set "PYTHONUTF8=1"
set "PYTHONIOENCODING=utf-8"
rem Pick a Python launcher. Order:
rem 1. .pyenv\python.exe — portable Python downloaded by a previous run
rem 2. py / python / python3 — user's system Python
rem 3. Download a portable Python on the fly (no admin, no PATH changes)
set "PYCMD="
if exist "%~dp0.pyenv\python.exe" (
set "PYCMD=%~dp0.pyenv\python.exe"
goto :launch
)
where py >nul 2>nul
if !ERRORLEVEL! EQU 0 set "PYCMD=py"
if not defined PYCMD (
where python >nul 2>nul
if !ERRORLEVEL! EQU 0 set "PYCMD=python"
)
if not defined PYCMD (
where python3 >nul 2>nul
if !ERRORLEVEL! EQU 0 set "PYCMD=python3"
)
if defined PYCMD goto :launch
echo [run.bat] No Python detected on this machine.
echo [run.bat] Downloading a portable Python (~30 MB) into .pyenv\ next to this script.
echo [run.bat] Nothing is installed system-wide; delete .pyenv\ to remove it.
echo.
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
"$ErrorActionPreference='Stop';" ^
"$ver='3.12.7';" ^
"$dest='%~dp0.pyenv';" ^
"$zip=Join-Path $env:TEMP 'oppai-py-embed.zip';" ^
"[void](New-Item -ItemType Directory -Force -Path $dest);" ^
"Invoke-WebRequest -UseBasicParsing -Uri \"https://www.python.org/ftp/python/$ver/python-$ver-embed-amd64.zip\" -OutFile $zip;" ^
"Expand-Archive -Path $zip -DestinationPath $dest -Force;" ^
"Remove-Item $zip -Force"
if !ERRORLEVEL! NEQ 0 (
echo.
echo [run.bat] Failed to download portable Python.
echo [run.bat] Check your internet connection, or install Python 3.10-3.12
echo from https://www.python.org/downloads/ ^(tick "Add Python to PATH"^)
echo and re-run this script.
pause
exit /b 1
)
if not exist "%~dp0.pyenv\python.exe" (
echo [run.bat] Portable Python download finished but python.exe is missing.
echo [run.bat] Try deleting .pyenv\ and re-running, or install Python manually.
pause
exit /b 1
)
set "PYCMD=%~dp0.pyenv\python.exe"
:launch
echo [run.bat] Using !PYCMD! to launch app.py.
echo [run.bat] First run installs ~500 MB of packages; that only happens once.
echo.
"!PYCMD!" "%~dp0app.py" %*
set "EXITCODE=!ERRORLEVEL!"
if !EXITCODE! NEQ 0 (
echo.
echo [run.bat] app.py exited with code !EXITCODE!.
pause
)
endlocal & exit /b %EXITCODE%