-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmanage.py
More file actions
244 lines (191 loc) · 6.57 KB
/
manage.py
File metadata and controls
244 lines (191 loc) · 6.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
nmTeam Documentation Management Tool
管理脚本,提供开发和构建指令
"""
import os
import sys
import time
import subprocess
import threading
from pathlib import Path
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from generate import generate
class DocumentationHandler(FileSystemEventHandler):
"""文档变更监听器"""
def __init__(self):
self.last_modified = {}
def on_modified(self, event):
if event.is_directory:
return
# 只监听 .md 文件和配置文件
if not (event.src_path.endswith('.md') or
event.src_path.endswith('.yml') or
event.src_path.endswith('.yaml')):
return
# 防止频繁触发
now = time.time()
if event.src_path in self.last_modified:
if now - self.last_modified[event.src_path] < 1:
return
self.last_modified[event.src_path] = now
print(f"检测到文件变更: {event.src_path}")
self.rebuild_docs()
def rebuild_docs(self):
"""重新构建文档"""
try:
print("🔄 重新生成文档...")
generate()
print("✅ 文档重新生成完成")
except Exception as e:
print(f"❌ 文档生成失败: {e}")
def run_mkdocs_serve():
"""运行 MkDocs 开发服务器"""
try:
# 首先生成文档
print("🚀 启动开发服务器...")
generate()
# 启动 MkDocs 服务器
process = subprocess.Popen(
[sys.executable, "-m", "mkdocs", "serve", "--dev-addr", "127.0.0.1:8000"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1,
universal_newlines=True
)
# 实时输出日志
for line in iter(process.stdout.readline, ''):
print(line.rstrip())
except KeyboardInterrupt:
print("\n🛑 开发服务器已停止")
if 'process' in locals():
process.terminate()
except Exception as e:
print(f"❌ 启动开发服务器失败: {e}")
def dev_command():
"""开发模式:启动文件监听和开发服务器"""
print("🔥 启动开发模式...")
print("📁 监听 docs/ 目录变化...")
# 设置文件监听
event_handler = DocumentationHandler()
observer = Observer()
observer.schedule(event_handler, "docs", recursive=True)
observer.schedule(event_handler, "mkdocs-template.yml", recursive=False)
# 启动监听
observer.start()
try:
# 在单独线程中运行 MkDocs 服务器
server_thread = threading.Thread(target=run_mkdocs_serve)
server_thread.daemon = True
server_thread.start()
print("✅ 开发环境已启动")
print("🌐 访问 http://127.0.0.1:8000 查看文档")
print("📝 修改 docs/ 目录下的文件将自动重新生成")
print("💡 按 Ctrl+C 停止服务")
# 保持主线程运行
server_thread.join()
except KeyboardInterrupt:
print("\n🛑 停止开发模式...")
finally:
observer.stop()
observer.join()
def build_command():
"""构建模式:生成文档并构建静态站点"""
print("🏗️ 开始构建...")
try:
# 步骤1: 生成文档
print("📝 第1步: 生成文档结构...")
generate()
print("✅ 文档生成完成")
# 步骤2: 构建静态站点
print("🔨 第2步: 构建静态站点...")
result = subprocess.run(
[sys.executable, "-m", "mkdocs", "build", "--clean"],
capture_output=True,
text=True
)
if result.returncode == 0:
print("✅ 静态站点构建完成")
print("📁 输出目录: site/")
else:
print(f"❌ 构建失败: {result.stderr}")
return False
except Exception as e:
print(f"❌ 构建过程出错: {e}")
return False
return True
def clean_command():
"""清理生成的文件"""
print("🧹 清理中...")
dirs_to_clean = ['cache', 'generated', 'site']
for dir_name in dirs_to_clean:
if os.path.exists(dir_name):
import shutil
shutil.rmtree(dir_name)
print(f"🗑️ 已删除: {dir_name}/")
print("✅ 清理完成")
def install_command():
"""安装依赖"""
print("📦 安装依赖...")
try:
# 更新 requirements.txt 以包含 watchdog
requirements_path = "requirements.txt"
with open(requirements_path, 'r', encoding='utf-8') as f:
content = f.read()
if 'watchdog' not in content:
with open(requirements_path, 'a', encoding='utf-8') as f:
f.write('watchdog\n')
print("📝 已更新 requirements.txt")
# 安装依赖
result = subprocess.run(
[sys.executable, "-m", "pip", "install", "-r", "requirements.txt"],
capture_output=True,
text=True
)
if result.returncode == 0:
print("✅ 依赖安装完成")
else:
print(f"❌ 依赖安装失败: {result.stderr}")
except Exception as e:
print(f"❌ 安装过程出错: {e}")
def show_help():
"""显示帮助信息"""
help_text = """
nmTeam Documentation Management Tool
用法:
python manage.py <command>
可用命令:
dev 启动开发模式 (文件监听 + 热更新服务器)
build 构建静态站点 (生成文档 + 构建)
clean 清理生成的文件
install 安装依赖包
help 显示此帮助信息
示例:
python manage.py dev # 启动开发服务器
python manage.py build # 构建生产版本
python manage.py clean # 清理临时文件
"""
print(help_text)
def main():
"""主函数"""
if len(sys.argv) < 2:
show_help()
return
command = sys.argv[1].lower()
commands = {
'dev': dev_command,
'build': build_command,
'clean': clean_command,
'install': install_command,
'help': show_help,
}
if command in commands:
commands[command]()
else:
print(f"❌ 未知命令: {command}")
show_help()
if __name__ == "__main__":
main()