This repository was archived by the owner on Nov 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.py
More file actions
249 lines (201 loc) · 8.57 KB
/
install.py
File metadata and controls
249 lines (201 loc) · 8.57 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
U校园自动化框架安装脚本
"""
import os
import sys
import subprocess
import platform
from pathlib import Path
def print_banner():
"""打印横幅"""
banner = """
╔══════════════════════════════════════════════════════════════╗
║ ║
║ 🎓 U校园自动化框架安装程序 ║
║ ║
║ Version 1.0.0 ║
║ ║
╚══════════════════════════════════════════════════════════════╝
"""
print(banner)
def check_python_version():
"""检查Python版本"""
print("🔍 检查Python版本...")
version = sys.version_info
if version.major < 3 or (version.major == 3 and version.minor < 8):
print("❌ 错误: 需要Python 3.8或更高版本")
print(f" 当前版本: Python {version.major}.{version.minor}.{version.micro}")
return False
print(f"✅ Python版本检查通过: {version.major}.{version.minor}.{version.micro}")
return True
def check_system():
"""检查系统信息"""
print("🔍 检查系统信息...")
system = platform.system()
architecture = platform.architecture()[0]
print(f" 操作系统: {system}")
print(f" 架构: {architecture}")
supported_systems = ["Windows", "Darwin", "Linux"]
if system not in supported_systems:
print(f"⚠️ 警告: 未测试的操作系统 {system}")
else:
print("✅ 系统兼容性检查通过")
return True
def install_dependencies():
"""安装依赖包"""
print("📦 安装Python依赖包...")
try:
# 升级pip
print(" 升级pip...")
subprocess.run([sys.executable, "-m", "pip", "install", "--upgrade", "pip"],
check=True, capture_output=True)
# 安装依赖
print(" 安装项目依赖...")
subprocess.run([sys.executable, "-m", "pip", "install", "-r", "requirements.txt"],
check=True, capture_output=True)
print("✅ Python依赖安装完成")
return True
except subprocess.CalledProcessError as e:
print(f"❌ 依赖安装失败: {e}")
return False
except FileNotFoundError:
print("❌ 未找到requirements.txt文件")
return False
def install_playwright():
"""安装Playwright浏览器"""
print("🌐 安装Playwright浏览器...")
try:
# 安装浏览器
subprocess.run([sys.executable, "-m", "playwright", "install", "chromium"],
check=True, capture_output=True)
print("✅ Playwright浏览器安装完成")
return True
except subprocess.CalledProcessError as e:
print(f"❌ 浏览器安装失败: {e}")
return False
def create_directories():
"""创建必要的目录"""
print("📁 创建项目目录...")
directories = [
"data",
"logs",
"screenshots",
"config"
]
for directory in directories:
Path(directory).mkdir(exist_ok=True)
print(f" 创建目录: {directory}")
print("✅ 目录创建完成")
return True
def create_env_file():
"""创建环境变量文件"""
print("⚙️ 创建环境配置文件...")
env_file = Path(".env")
if env_file.exists():
print(" .env文件已存在,跳过创建")
return True
env_content = """# U校园自动化框架环境变量配置
# U校园登录凭据
UCAMPUS_USERNAME=your_username
UCAMPUS_PASSWORD=your_password
# 调试模式
DEBUG=false
# 日志级别 (DEBUG, INFO, WARNING, ERROR)
LOG_LEVEL=INFO
# 浏览器设置
BROWSER_HEADLESS=false
BROWSER_SLOW_MO=100
# 视频设置
VIDEO_SPEED=2.0
VIDEO_MUTED=true
# 答题设置
AUTO_SUBMIT=true
MAX_RETRIES=3
"""
try:
with open(env_file, 'w', encoding='utf-8') as f:
f.write(env_content)
print("✅ 环境配置文件创建完成")
print(" 请编辑 .env 文件设置您的用户名和密码")
return True
except Exception as e:
print(f"❌ 创建环境文件失败: {e}")
return False
def test_installation():
"""测试安装"""
print("🧪 测试安装...")
try:
# 测试导入主要模块
import playwright
import yaml
import requests
import loguru
print("✅ 模块导入测试通过")
# 测试配置加载
from src.config.settings import Settings
settings = Settings()
print("✅ 配置加载测试通过")
return True
except ImportError as e:
print(f"❌ 模块导入失败: {e}")
return False
except Exception as e:
print(f"❌ 测试失败: {e}")
return False
def print_usage_instructions():
"""打印使用说明"""
instructions = """
╔══════════════════════════════════════════════════════════════╗
║ 🎉 安装完成! ║
╠══════════════════════════════════════════════════════════════╣
║ ║
║ 📝 下一步操作: ║
║ ║
║ 1. 编辑 .env 文件,设置您的U校园用户名和密码 ║
║ ║
║ 2. 运行程序: ║
║ • GUI模式: python main.py ║
║ • CLI模式: python main.py cli ║
║ • 自动模式: python main.py auto ║
║ • 测试模式: python main.py test ║
║ ║
║ 3. 查看文档: ║
║ • README.md - 详细使用说明 ║
║ • config/config.yaml - 配置文件说明 ║
║ ║
║ 🔧 油猴脚本: ║
║ • tampermonkey/ucampus_helper.js - 完整版脚本 ║
║ • tampermonkey/ucampus_simple.js - 简化版脚本 ║
║ ║
║ 📞 获取帮助: ║
║ • 查看 README.md 中的故障排除部分 ║
║ • 提交 GitHub Issue ║
║ ║
╚══════════════════════════════════════════════════════════════╝
"""
print(instructions)
def main():
"""主函数"""
print_banner()
# 检查步骤
steps = [
("检查Python版本", check_python_version),
("检查系统信息", check_system),
("安装Python依赖", install_dependencies),
("安装Playwright浏览器", install_playwright),
("创建项目目录", create_directories),
("创建环境配置", create_env_file),
("测试安装", test_installation)
]
print("🚀 开始安装...\n")
for step_name, step_func in steps:
print(f"📋 {step_name}")
if not step_func():
print(f"\n❌ 安装失败: {step_name}")
sys.exit(1)
print()
print_usage_instructions()
if __name__ == "__main__":
main()