Files
grok-keygen/start.bat
T
chaos 4feac70c91 Make start scripts auto-install missing runtime deps
Bootstrap uv, Python 3.13, project deps, and browser when possible so first-run setup works without manual environment setup.
2026-07-12 00:28:05 +08:00

287 lines
7.7 KiB
Batchfile
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
@echo off
chcp 65001 >nul 2>&1
setlocal EnableExtensions EnableDelayedExpansion
title Grok 注册机 - 一键启动
echo ============================================
echo Grok 注册机 - 一键启动 (Windows)
echo ============================================
echo.
echo 首次运行会自动:
echo 1. 安装 uv(如缺失)
echo 2. 安装 Python 3.13(如缺失,经 uv 管理)
echo 3. 同步 Python 依赖
echo 4. 尝试安装 Chrome(如缺失且可用 winget)
echo 5. 从模板复制 .env / config.json(如缺失)
echo.
cd /d "%~dp0"
:: 预置 uv 常见路径
set "PATH=%USERPROFILE%\.local\bin;%USERPROFILE%\.cargo\bin;%LOCALAPPDATA%\uv;%USERPROFILE%\.uv\bin;%PATH%"
:: ── 1. 检测/安装 uv ──
echo [1/5] 检测/安装 uv 包管理器...
where uv >nul 2>&1
if !errorlevel! equ 0 (
for /f "tokens=*" %%V in ('uv --version 2^>nul') do echo [OK] uv: %%V
goto :uv_ok
)
echo [!] 未检测到 uv,正在自动安装...
powershell -NoProfile -ExecutionPolicy Bypass -Command "irm https://astral.sh/uv/install.ps1 | iex"
if !errorlevel! neq 0 (
echo [X] uv 安装失败,请手动执行:
echo powershell -ExecutionPolicy Bypass -c "irm https://astral.sh/uv/install.ps1 | iex"
pause
exit /b 1
)
call :refresh_path
where uv >nul 2>&1
if !errorlevel! equ 0 (
for /f "tokens=*" %%V in ('uv --version 2^>nul') do echo [OK] uv 安装成功: %%V
) else (
echo [X] uv 安装后仍未找到,请关闭本窗口后重新运行 start.bat
pause
exit /b 1
)
:uv_ok
echo.
:: ── 2. 检测/安装 Python 3.13 ──
echo [2/5] 检测/安装 Python 3.13...
set "PYTHON_OK=0"
:: 系统 / py launcher
for %%P in (
"%LOCALAPPDATA%\Programs\Python\Python313\python.exe"
"%LOCALAPPDATA%\Programs\Python\Python314\python.exe"
"%ProgramFiles%\Python313\python.exe"
"python"
"python3"
"py"
) do (
if !PYTHON_OK! equ 0 (
call :try_python "%%~P"
)
)
:: 项目 venv
if !PYTHON_OK! equ 0 (
if exist ".venv\Scripts\python.exe" (
call :try_python ".venv\Scripts\python.exe"
)
)
:: uv 已管理的 3.13
if !PYTHON_OK! equ 0 (
for /f "delims=" %%P in ('uv python find 3.13 2^>nul') do (
if exist "%%~P" (
call :try_python "%%~P"
)
)
)
if !PYTHON_OK! equ 1 (
echo [OK] Python 3.13+ 已可用
goto :python_ok
)
echo [!] 未找到 Python 3.13+,正在通过 uv 安装...
uv python install 3.13
if !errorlevel! neq 0 (
echo [X] uv python install 3.13 失败
echo 也可尝试: winget install Python.Python.3.13
pause
exit /b 1
)
call :refresh_path
for /f "delims=" %%P in ('uv python find 3.13 2^>nul') do (
if exist "%%~P" (
call :try_python "%%~P"
)
)
if !PYTHON_OK! equ 1 (
echo [OK] Python 3.13 已由 uv 安装完成
) else (
:: uv run 仍可能解析到托管 Python
uv run --python 3.13 python -c "import sys; raise SystemExit(0 if sys.version_info >= (3,13) else 1)" >nul 2>&1
if !errorlevel! equ 0 (
set "PYTHON_OK=1"
echo [OK] Python 3.13 已由 uv 管理(将通过 uv run 调用)
) else (
echo [X] 无法安装或定位 Python 3.13
pause
exit /b 1
)
)
:python_ok
echo.
:: ── 3. 同步依赖 ──
echo [3/5] 同步 Python 依赖...
uv sync --python 3.13
if !errorlevel! neq 0 (
echo [!] uv sync --python 3.13 失败,尝试默认 uv sync...
uv sync
if !errorlevel! neq 0 (
echo [X] 依赖同步失败
pause
exit /b 1
)
)
echo [OK] 依赖同步完成
echo.
:: ── 4. 检测/尝试安装浏览器 ──
echo [4/5] 检测/安装浏览器...
set "BROWSER_FOUND=0"
set "BROWSER_PATH="
for %%B in (
"%ProgramFiles%\Google\Chrome\Application\chrome.exe"
"%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe"
"%LOCALAPPDATA%\Google\Chrome\Application\chrome.exe"
"%ProgramFiles%\Chromium\Application\chrome.exe"
"%LOCALAPPDATA%\Chromium\Application\chrome.exe"
) do (
if !BROWSER_FOUND! equ 0 (
if exist "%%~B" (
set "BROWSER_FOUND=1"
set "BROWSER_PATH=%%~B"
)
)
)
if !BROWSER_FOUND! equ 1 (
echo [OK] 浏览器: !BROWSER_PATH!
goto :browser_ok
)
where chrome >nul 2>&1
if !errorlevel! equ 0 (
for /f "delims=" %%C in ('where chrome 2^>nul') do (
echo [OK] 浏览器: %%C
set "BROWSER_FOUND=1"
goto :browser_ok
)
)
echo [!] 未检测到 Chrome,尝试自动安装...
:: 优先 winget
where winget >nul 2>&1
if !errorlevel! equ 0 (
echo → 使用 winget 安装 Google Chrome...
winget install -e --id Google.Chrome --accept-package-agreements --accept-source-agreements
if !errorlevel! equ 0 (
echo [OK] winget 安装命令已完成
) else (
echo [!] winget 安装可能未成功(权限/策略限制)
)
) else (
echo [!] 未找到 winget,跳过自动安装
echo 请手动安装: https://www.google.com/chrome/
)
:: 再扫一次
set "BROWSER_FOUND=0"
for %%B in (
"%ProgramFiles%\Google\Chrome\Application\chrome.exe"
"%ProgramFiles(x86)%\Google\Chrome\Application\chrome.exe"
"%LOCALAPPDATA%\Google\Chrome\Application\chrome.exe"
) do (
if !BROWSER_FOUND! equ 0 (
if exist "%%~B" (
set "BROWSER_FOUND=1"
echo [OK] 浏览器已就绪: %%~B
)
)
)
if !BROWSER_FOUND! equ 0 (
echo [!] 仍未检测到 Chrome
echo DrissionPage 需要 Chrome;协议 mint 不强制浏览器,回退流程需要。
echo 脚本将继续,注册可能因缺少浏览器而失败。
)
:browser_ok
echo.
:: ── 5. 配置文件模板 ──
echo [5/5] 检查配置文件...
if not exist ".env" (
if exist ".env.example" (
copy /Y ".env.example" ".env" >nul
echo [!] 已从 .env.example 创建 .env,请编辑填入真实配置
) else (
echo [!] 无 .env / .env.example(可直接写 config.json)
)
) else (
echo [OK] .env 已存在
)
if not exist "config.json" (
if exist "config.example.json" (
copy /Y "config.example.json" "config.json" >nul
echo [!] 已从 config.example.json 创建 config.json,请编辑填入真实配置
) else (
echo [X] 缺少 config.json 与 config.example.json
pause
exit /b 1
)
) else (
echo [OK] config.json 已存在
)
echo.
echo ============================================
echo 环境就绪,启动 register_cli.py
echo ============================================
echo.
:: 默认不限数量(--count 0),除非用户显式传参
if "%~1"=="" (
echo [*] 默认模式:不限数量,持续注册(Ctrl+C 停止)
echo.
uv run --python 3.13 python -u register_cli.py --count 0
) else (
uv run --python 3.13 python -u register_cli.py %*
)
set "EXIT_CODE=!errorlevel!"
echo.
if !EXIT_CODE! neq 0 (
echo [X] 进程退出码: !EXIT_CODE!
) else (
echo [OK] 已结束
)
pause
exit /b !EXIT_CODE!
:: ── 辅助函数 ──
:try_python
:: 检查指定解释器是否 >= 3.13
set "PY_CAND=%~1"
if "%PY_CAND%"=="" exit /b 0
where "%PY_CAND%" >nul 2>&1
if errorlevel 1 (
if not exist "%PY_CAND%" exit /b 0
)
"%PY_CAND%" -c "import sys; raise SystemExit(0 if sys.version_info >= (3,13) else 1)" >nul 2>&1
if !errorlevel! equ 0 (
set "PYTHON_OK=1"
for /f "tokens=*" %%V in ('"%PY_CAND%" --version 2^>^&1') do echo [OK] %%V ^(%PY_CAND%^)
)
exit /b 0
:refresh_path
:: 合并用户/机器 PATH,并补上 uv 默认目录
for /f "usebackq delims=" %%A in (`powershell -NoProfile -Command "[Environment]::GetEnvironmentVariable('Path','User') + ';' + [Environment]::GetEnvironmentVariable('Path','Machine')"`) do set "PATH=%%A"
set "PATH=%USERPROFILE%\.local\bin;%USERPROFILE%\.cargo\bin;%LOCALAPPDATA%\uv;%USERPROFILE%\.uv\bin;%PATH%"
exit /b 0