- Refactor codebase into src/ (preprocessing, translation, assembly) - Add pipeline/ scripts for individual stages - Externalize configuration to config/config.yaml - Fix Cover Image preservation - Update documentation and manuals
132 lines
3.5 KiB
Python
132 lines
3.5 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
快速安装和测试脚本
|
|
"""
|
|
|
|
import subprocess
|
|
import sys
|
|
import os
|
|
from pathlib import Path
|
|
|
|
|
|
def run_command(cmd, description):
|
|
"""运行命令并显示结果"""
|
|
print(f"\n🔄 {description}...")
|
|
try:
|
|
result = subprocess.run(cmd, shell=True, check=True, capture_output=True, text=True)
|
|
print(f"✅ {description}完成")
|
|
return True
|
|
except subprocess.CalledProcessError as e:
|
|
print(f"❌ {description}失败: {e}")
|
|
if e.stdout:
|
|
print(f"输出: {e.stdout}")
|
|
if e.stderr:
|
|
print(f"错误: {e.stderr}")
|
|
return False
|
|
|
|
|
|
def check_python_version():
|
|
"""检查 Python 版本"""
|
|
version = sys.version_info
|
|
if version.major < 3 or (version.major == 3 and version.minor < 9):
|
|
print(f"❌ Python 版本过低: {version.major}.{version.minor}")
|
|
print("需要 Python 3.9 或更高版本")
|
|
return False
|
|
print(f"✅ Python 版本: {version.major}.{version.minor}.{version.micro}")
|
|
return True
|
|
|
|
|
|
def setup_environment():
|
|
"""设置环境"""
|
|
print("🚀 EPUB 双语翻译程序 - 快速设置")
|
|
|
|
# 检查 Python 版本
|
|
if not check_python_version():
|
|
return False
|
|
|
|
# 检查 uv 是否安装
|
|
if not run_command("uv --version", "检查 uv"):
|
|
print("正在安装 uv...")
|
|
if not run_command("curl -LsSf https://astral.sh/uv/install.sh | sh", "安装 uv"):
|
|
print("❌ uv 安装失败,请手动安装")
|
|
return False
|
|
|
|
# 创建虚拟环境
|
|
if not Path(".venv").exists():
|
|
if not run_command("uv venv", "创建虚拟环境"):
|
|
return False
|
|
|
|
# 安装依赖
|
|
if not run_command("uv pip install -r requirements.txt", "安装依赖"):
|
|
return False
|
|
|
|
# 创建必要目录
|
|
for dir_name in ["output", "logs"]:
|
|
Path(dir_name).mkdir(exist_ok=True)
|
|
|
|
# 创建 .env 文件
|
|
if not Path(".env").exists():
|
|
with open(".env", "w") as f:
|
|
f.write("OPENROUTER_API_KEY=your_openrouter_api_key_here\n")
|
|
print("✅ 已创建 .env 文件")
|
|
|
|
return True
|
|
|
|
|
|
def test_installation():
|
|
"""测试安装"""
|
|
print("\n🧪 测试安装...")
|
|
|
|
# 测试导入
|
|
test_imports = [
|
|
"ebooklib",
|
|
"beautifulsoup4",
|
|
"lxml",
|
|
"openai",
|
|
"aiohttp",
|
|
"pydantic",
|
|
"loguru",
|
|
"rich"
|
|
]
|
|
|
|
for module in test_imports:
|
|
try:
|
|
if module == "beautifulsoup4":
|
|
import bs4
|
|
else:
|
|
__import__(module)
|
|
print(f"✅ {module}")
|
|
except ImportError:
|
|
print(f"❌ {module} 导入失败")
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
def main():
|
|
"""主函数"""
|
|
if not setup_environment():
|
|
print("\n❌ 环境设置失败")
|
|
return 1
|
|
|
|
if not test_installation():
|
|
print("\n❌ 安装测试失败")
|
|
return 1
|
|
|
|
print("\n🎉 安装完成!")
|
|
print("\n📋 下一步:")
|
|
print("1. 编辑 .env 文件,设置你的 OpenRouter API Key")
|
|
print("2. 运行测试: python main.py your_book.epub --test")
|
|
print("3. 查看帮助: python main.py --help")
|
|
|
|
# 检查是否有示例 EPUB 文件
|
|
epub_files = list(Path(".").glob("*.epub"))
|
|
if epub_files:
|
|
print(f"\n📚 发现 EPUB 文件: {epub_files[0].name}")
|
|
print(f"可以运行: python main.py '{epub_files[0].name}' --test")
|
|
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main()) |