-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
178 lines (140 loc) · 5.14 KB
/
setup.py
File metadata and controls
178 lines (140 loc) · 5.14 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python3
"""
pdf2othersAPI 安装脚本
"""
import os
import subprocess
import sys
from pathlib import Path
# 加载环境变量
try:
from dotenv import load_dotenv
load_dotenv()
except ImportError:
# 如果没有python-dotenv,尝试手动读取.env文件
env_file = Path('.env')
if env_file.exists():
for line in env_file.read_text().strip().split('\n'):
if line and not line.startswith('#') and '=' in line:
key, value = line.split('=', 1)
os.environ[key.strip()] = value.strip()
def run_command(command, description=""):
"""执行命令并处理错误"""
print(f"🔄 {description}")
try:
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
print(f"✅ {description} - 完成")
return result.stdout
except subprocess.CalledProcessError as e:
print(f"❌ {description} - 失败")
print(f"错误: {e.stderr}")
return None
def check_python_version():
"""检查Python版本"""
if sys.version_info < (3, 8):
print("❌ 需要Python 3.8或更高版本")
sys.exit(1)
print(f"✅ Python版本: {sys.version}")
def install_dependencies():
"""安装Python依赖"""
print("\n📦 安装Python依赖...")
# 检查pip
pip_version = run_command("pip3 --version", "检查pip")
if not pip_version:
print("❌ pip3未找到,请先安装pip")
return False
# 安装依赖
result = run_command("pip3 install -r requirements.txt", "安装Python包")
return result is not None
def install_system_dependencies():
"""安装系统依赖"""
print("\n🔧 检查系统依赖...")
# 检查Pandoc
pandoc_version = run_command("pandoc --version", "检查Pandoc")
if not pandoc_version:
print("📝 Pandoc未安装,尝试安装...")
# 根据操作系统安装Pandoc
import platform
system = platform.system().lower()
if system == "darwin": # macOS
run_command("brew install pandoc", "安装Pandoc (macOS)")
elif system == "linux":
run_command("sudo apt-get update && sudo apt-get install -y pandoc", "安装Pandoc (Linux)")
else:
print("❌ 请手动安装Pandoc: https://pandoc.org/installing.html")
return False
# 检查MinerU
mineru_check = run_command("python3 -c 'import mineru'", "检查MinerU")
if not mineru_check:
print("📝 安装MinerU...")
run_command("pip3 install mineru", "安装MinerU")
return True
def setup_directories():
"""创建必要的目录"""
print("\n📁 创建存储目录...")
# 从环境变量读取目录配置,如果没有则使用默认值
directories = [
Path(os.getenv('PDFS_PATH', '/var/lib/pdf2others/storage')),
Path(os.getenv('TMP_TRS_DIR', '/tmp/pdf2others')),
Path(os.getenv('LOGS_DIR', 'logs'))
]
for directory in directories:
try:
directory.mkdir(parents=True, exist_ok=True)
print(f"✅ 创建目录: {directory}")
except PermissionError:
print(f"❌ 权限不足,无法创建目录: {directory}")
print(f" 请手动创建或使用sudo")
def setup_environment():
"""设置环境配置"""
print("\n⚙️ 设置环境配置...")
env_file = Path(".env")
env_example = Path(".env.example")
if not env_file.exists() and env_example.exists():
# 复制示例配置文件
env_content = env_example.read_text()
env_file.write_text(env_content)
print(f"✅ 创建配置文件: {env_file}")
print("⚠️ 请编辑 .env 文件,设置正确的配置值")
else:
print("ℹ️ .env 文件已存在,跳过创建")
def run_tests():
"""运行测试"""
print("\n🧪 运行测试...")
# 检查pytest
pytest_check = run_command("python3 -c 'import pytest'", "检查pytest")
if not pytest_check:
run_command("pip3 install pytest", "安装pytest")
# 运行测试
test_result = run_command("python3 -m pytest tests/ -v", "运行测试")
return test_result is not None
def main():
"""主安装流程"""
print("🚀 pdf2othersAPI 安装脚本")
print("=" * 50)
# 检查Python版本
check_python_version()
# 安装系统依赖
if not install_system_dependencies():
print("❌ 系统依赖安装失败")
sys.exit(1)
# 安装Python依赖
if not install_dependencies():
print("❌ Python依赖安装失败")
sys.exit(1)
# 创建目录
setup_directories()
# 设置环境
setup_environment()
# 运行测试
if run_tests():
print("\n🎉 安装完成!")
print("\n📖 下一步:")
print("1. 编辑 .env 文件,设置数据库和vLLM配置")
print("2. 启动vLLM服务")
print("3. 运行应用: python3 main.py")
else:
print("\n⚠️ 安装完成,但测试失败")
print("请检查配置和依赖")
if __name__ == "__main__":
main()