-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.py
More file actions
203 lines (176 loc) · 7.22 KB
/
Copy pathsetup.py
File metadata and controls
203 lines (176 loc) · 7.22 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python3
"""
期末一键复习(Final Review)环境引导脚本。
Detects Python environment and installs all dependencies.
If Python isn't available, this script can't run — but the skill's
Tier 0 mode handles that gracefully without Python.
Usage:
python setup.py # Check + install dependencies
python setup.py --check # Check only, don't install
python setup.py --install-agents # Also run multi-agent install
For users without Python:
Windows: winget install Python.Python.3.12
macOS: brew install python@3.12
Linux: sudo apt install python3 python3-pip
"""
import subprocess
import sys
import os
import platform as pf
# Ensure UTF-8 output on Windows
try:
sys.stdout.reconfigure(encoding='utf-8')
except Exception:
pass
REQUIREMENTS_FILE = os.path.join(os.path.dirname(__file__), "requirements.txt")
def run(cmd, **kwargs):
"""Run a command and return (success, output)."""
try:
result = subprocess.run(
cmd, shell=True, capture_output=True, text=True, **kwargs
)
return result.returncode == 0, result.stdout + result.stderr
except Exception as e:
return False, str(e)
def check_python():
"""Check Python version."""
version = sys.version_info
print(f" Python {version.major}.{version.minor}.{version.micro} — {'✅' if version >= (3, 9) else '⚠️ 需要 3.9+'}")
def check_pip_package(package_name: str, import_name: str = None) -> bool:
"""Check if a Python package is importable."""
if import_name is None:
import_name = package_name.split(">=")[0].split("[")[0]
try:
__import__(import_name.replace("-", "_"))
return True
except ImportError:
return False
def check_dependencies():
"""Check which dependencies are installed."""
print("\n Dependency check:")
packages = {
"python-docx": "docx",
"PyPDF2": "PyPDF2",
"python-pptx": "pptx",
"markdownify": "markdownify",
"ebooklib": "ebooklib",
"chardet": "chardet",
"beautifulsoup4": "bs4",
}
optional = {
"easyocr": "easyocr",
"pytesseract": "pytesseract",
"pdf2image": "pdf2image",
"weasyprint": "weasyprint",
"jinja2": "jinja2",
}
all_ok = True
for pkg, imp in packages.items():
ok = check_pip_package(pkg, imp)
all_ok = all_ok and ok
print(f" {'✅' if ok else '❌'} {pkg}")
print(" Optional:")
for pkg, imp in optional.items():
ok = check_pip_package(pkg, imp)
print(f" {'✅' if ok else '⬜'} {pkg}")
return all_ok
def check_ocr_engines():
"""Check OCR engine availability."""
print("\n OCR engines:")
ok = check_pip_package("easyocr", "easyocr")
print(f" {'✅' if ok else '⬜'} EasyOCR (GPU加速,中英混合,推荐)")
try:
import pytesseract
pytesseract.get_tesseract_version()
print(" ✅ Tesseract (系统已安装)")
except ImportError:
print(" ⬜ Tesseract Python 绑定 (pip install pytesseract)")
except Exception:
print(" ⬜ Tesseract 引擎 (需系统安装: brew install tesseract / apt install tesseract-ocr)")
def install_dependencies():
"""Install all dependencies from requirements.txt."""
if not os.path.exists(REQUIREMENTS_FILE):
print(" ⚠️ requirements.txt not found. Skipping.")
return False
print(f"\n Installing dependencies from {REQUIREMENTS_FILE}...")
ok, output = run(f"{sys.executable} -m pip install -r {REQUIREMENTS_FILE}")
if ok:
print(" ✅ Dependencies installed.")
else:
print(f" ⚠️ Some packages may have failed:\n{output[-500:]}")
return ok
def install_agents():
"""Run multi-agent install if install.py exists."""
install_py = os.path.join(os.path.dirname(__file__), "install.py")
if os.path.exists(install_py):
print("\n Running multi-agent install...")
ok, output = run(f"{sys.executable} {install_py}")
if ok:
print(" ✅ Agents configured.")
else:
print(f" ⚠️ Agent install had issues:\n{output[-300:]}")
else:
print(" ⬜ install.py not found. Skipping agent setup.")
def print_tier_info():
"""Print tier capability summary."""
print(f"""
┌─ Tier 0: 零依赖 (当前可用) ─────────────────────────────┐
│ • 文本文件 (.md .txt .html) 直接处理 │
│ • 资料类型智能分类 │
│ • 课程目录创建 │
│ • Markdown 输出 + 复习资料生成 │
│ • 文件发现 (bash find/ls) │
└────────────────────────────────────────────────────────┘
""")
all_core = check_dependencies()
if all_core:
print(f"""
┌─ Tier 1: pip 依赖 (当前可用) ───────────────────────────┐
│ • .docx .pptx .pdf .epub 文本提取 │
│ • 幕布 JSON → Markdown │
│ • HTML → 纯净文本 │
│ • 大文件智能分段 │
│ • MD/HTML/DOCX 多格式输出 │
└────────────────────────────────────────────────────────┘
""")
else:
print(f"""
┌─ Tier 1: pip 依赖 ⬜ 未满足 ────────────────────────────┐
│ 运行: pip install -r requirements.txt │
│ 或: python setup.py --install │
└────────────────────────────────────────────────────────┘
""")
def main():
print("=" * 60)
print(" 期末一键复习 — 环境检测与依赖安装")
print("=" * 60)
if "--check" in sys.argv:
check_python()
check_dependencies()
check_ocr_engines()
return
if "--install-agents" in sys.argv:
check_python()
check_dependencies()
install_agents()
return
# Default: full check + info
check_python()
print_tier_info()
check_ocr_engines()
if "--install" in sys.argv or "--full" in sys.argv:
install_dependencies()
print(f"""
Quick start:
pip install -r requirements.txt # Tier 1
pip install easyocr # Tier 2 (OCR)
python install.py # Deploy to AI agents
Without Python? The skill works in Tier 0 mode —
Claude handles text files natively.
To install Python:
Windows: winget install Python.Python.3.12
macOS: brew install python@3.12
Linux: sudo apt install python3 python3-pip
""")
if __name__ == "__main__":
main()