本帖最后由 蜗牛很牛 于 2026-3-16 21:38 编辑
[Python] 纯文本查看 复制代码 import os
import subprocess
import shutil
import sys
import time
def find_executable(name, default_paths):
if shutil.which(name):
return name
for path in default_paths:
if os.path.exists(path):
return path
return None
def install_via_winget(package_id):
print(f" 未检测到对应环境,正在通过 winget 自动静默安装 {package_id} ...")
print(" 这可能需要一段时间下载及安装,请耐心等待。")
try:
subprocess.run(
["winget", "install", "--id", package_id, "-e", "--accept-package-agreements", "--accept-source-agreements", "--silent"],
check=True
)
print(f"[+] {package_id} 安装完成!")
# 停顿一下,等待文件系统刷新
time.sleep(2)
except subprocess.CalledProcessError as e:
print(f"[-] 安装 {package_id} 失败,可能是发生网络连接故障或没有管理员权限。")
print(" 如有异常请考虑手动前往官网进行安装。")
def main():
print("="*60)
print(" 自动环境检查、克隆与部署 CLIProxyAPI & Management Center")
print("="*60)
# 1. 检查并安装基本环境
# Git
git_exec = find_executable("git", [r"C:\Program Files\Git\cmd\git.exe", r"C:\Program Files\Git\bin\git.exe"])
if not git_exec:
install_via_winget("Git.Git")
git_exec = find_executable("git", [r"C:\Program Files\Git\cmd\git.exe", r"C:\Program Files\Git\bin\git.exe"])
if not git_exec:
print("[-] 严重: 无法定位 Git 安装路径。请手动安装后重试。")
sys.exit(1)
print(f"[+] Git 已就绪: {git_exec}")
# Go 语言环境
go_exec = find_executable("go", [r"C:\Program Files\Go\bin\go.exe"])
if not go_exec:
install_via_winget("GoLang.Go")
go_exec = find_executable("go", [r"C:\Program Files\Go\bin\go.exe"])
if not go_exec:
print("[-] 严重: 无法定位 Go 语言环境安装路径。请手动安装后重试。")
sys.exit(1)
print(f"[+] Go (Golang) 已就绪: {go_exec}")
# Node.js 环境
npm_exec = find_executable("npm", [r"C:\Program Files\nodejs\npm.cmd"])
if not npm_exec:
install_via_winget("OpenJS.NodeJS")
npm_exec = find_executable("npm", [r"C:\Program Files\nodejs\npm.cmd"])
if not npm_exec:
print("[-] 严重: 无法定位 npm/Node.js 包安装路径。请手动安装后重试。")
sys.exit(1)
print(f"[+] Node.js (npm) 已就绪: {npm_exec}")
cwd = os.getcwd()
api_dir = os.path.join(cwd, "CLIProxyAPI")
web_dir = os.path.join(cwd, "Cli-Proxy-API-Management-Center")
# 2. 从 GitHub 克隆仓库
print("\n 步骤2: 检查/拉取 GitHub 仓库")
if not os.path.exists(api_dir):
print(" -> 正在克隆后端: CLIProxyAPI ...")
subprocess.run([git_exec, "clone", "https://github.com/router-for-me/CLIProxyAPI.git"], check=True)
else:
print(" -> CLIProxyAPI 后端目录已存在,跳过克隆。")
if not os.path.exists(web_dir):
print(" -> 正在克隆前端: Cli-Proxy-API-Management-Center ...")
subprocess.run([git_exec, "clone", "https://github.com/router-for-me/Cli-Proxy-API-Management-Center.git"], check=True)
else:
print(" -> Cli-Proxy-API-Management-Center 前端目录已存在,跳过克隆。")
# 3. 构造一键启动的 bat 文件,避开 subprocess 和控制台嵌套环境带来的引语问题
print("\n 步骤3: 生成分离启动批处理文件")
api_bat = os.path.join(cwd, "run_api.bat")
with open(api_bat, "w", encoding="utf-8") as f:
f.write(f'''@echo off
title 运行中 - CLIProxyAPI Backend
cd /d "{api_dir}"
echo [Backend] 正在加载 Go 后端... (首次运行可能会自动下载 Go 模块依赖,如网络较慢请耐心等待)
"{go_exec}" run .
pause
''')
web_bat = os.path.join(cwd, "run_web.bat")
with open(web_bat, "w", encoding="utf-8") as f:
f.write(f'''@echo off
title 运行中 - CLIProxyAPI Frontend WebUI
cd /d "{web_dir}"
echo [Frontend] 正在通过 npm 拉取并安装前端依赖,请耐心等待...
call "{npm_exec}" install
echo.
echo [Frontend] 依赖执行完成,即将启动 dev 服务器...
call "{npm_exec}" run dev
pause
''')
# 4. 彻底启动他们
print("\n 步骤4: 正式启动进程")
print(" 正在为您弹窗运行 CLIProxyAPI 后端服务...")
subprocess.Popen(f'start "" "{api_bat}"', shell=True)
time.sleep(1) # 后端先开,稍作缓冲
print(" 正在为您弹窗运行 Management-Center 前端服务...")
subprocess.Popen(f'start "" "{web_bat}"', shell=True)
print("\n[✔] 所有操作已自动触发!")
print(" 您会看到两个新的命令行窗口弹出,分别为前端和后端,如果它们报错请在窗口内查看提示。")
print(" 完成加载后,通常在浏览器打开 http://localhost:5173 即可访问前端界面。")
if __name__ == "__main__":
main()
|