File size: 2,634 Bytes
c53ebec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
@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%