-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
240 lines (204 loc) · 7.7 KB
/
run.py
File metadata and controls
240 lines (204 loc) · 7.7 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
#!/usr/bin/env python3
"""
Git Analytics - 一键运行
扫描本地 Git 仓库,生成个人代码习惯体检报告
"""
import argparse
import os
import shutil
import subprocess
import sys
from pathlib import Path
from generate_report import main as generate_report
from git_analytics import OUTPUT_DATA, OUTPUT_REPORT, main as collect_data
def _open_file(path):
if sys.platform == 'darwin':
subprocess.run(['open', path], check=False)
elif os.name == 'nt':
os.startfile(path) # type: ignore[attr-defined]
else:
subprocess.run(['xdg-open', path], check=False)
def _find_share_card_template():
candidates = [
Path(__file__).resolve().with_name('share-card.html'),
Path(sys.prefix) / 'share' / 'git-analytics' / 'share-card.html',
]
for candidate in candidates:
if candidate.exists():
return candidate
return None
def _is_interactive():
return sys.stdin.isatty() and sys.stdout.isatty()
def _button_choice(title, options, default=1):
print(title)
for idx, (label, desc) in enumerate(options, 1):
suffix = " ← 默认" if idx == default else ""
print(f" [{idx}] {label}{suffix}")
if desc:
print(f" {desc}")
raw = input(f"选择 [{default}]: ").strip()
if not raw:
return default
try:
value = int(raw)
except ValueError:
return default
if 1 <= value <= len(options):
return value
return default
def _confirm(title, default=True):
default_text = "Y/n" if default else "y/N"
raw = input(f"{title} [{default_text}]: ").strip().lower()
if not raw:
return default
return raw in {"y", "yes", "1", "true"}
def _existing_path(path):
expanded = os.path.abspath(os.path.expanduser(path))
return expanded if os.path.isdir(expanded) else None
def _run_wizard(args):
home = os.path.expanduser("~")
current = os.getcwd()
candidates = [
("当前目录", current),
("Desktop", os.path.join(home, "Desktop")),
("Projects", os.path.join(home, "Projects")),
("Code", os.path.join(home, "Code")),
("自定义路径", None),
]
options = []
values = []
for label, path in candidates:
if path is None:
options.append((label, "输入一个或多个目录,用逗号分隔"))
values.append(None)
continue
existing = _existing_path(path)
if existing:
options.append((label, existing))
values.append(existing)
print()
print("Git Analytics Setup")
print("像选开关一样跑一次,不用记命令参数。")
print()
choice = _button_choice("扫描哪里?", options, default=1)
selected = values[choice - 1]
if selected is None:
raw = input("输入扫描目录(多个目录用逗号分隔): ").strip()
scan_dirs = [p.strip() for p in raw.split(",") if p.strip()]
if not scan_dirs:
scan_dirs = [current]
else:
scan_dirs = [selected]
depth_choice = _button_choice(
"扫描深度?",
[
("标准", "max-depth = 3,适合大多数项目目录"),
("更深", "max-depth = 5,适合 repo 放得更散的目录"),
("自定义", "手动输入深度"),
],
default=1,
)
if depth_choice == 1:
max_depth = 3
elif depth_choice == 2:
max_depth = 5
else:
raw = input("输入 max-depth [3]: ").strip()
try:
max_depth = int(raw) if raw else 3
except ValueError:
max_depth = 3
output_raw = input(f"输出目录 [{os.getcwd()}]: ").strip()
output_dir = output_raw or os.getcwd()
args.scan_dir = scan_dirs
args.max_depth = max_depth
args.output_dir = output_dir
args.open = _confirm("生成后自动打开报告?", default=True)
args.share_card = _confirm("同时生成分享卡片设计器?", default=True)
# 显示配置摘要并确认
print()
print("=" * 40)
print("📋 配置摘要")
print("=" * 40)
print("扫描目录:")
for item in scan_dirs:
print(f" - {os.path.abspath(os.path.expanduser(item))}")
print(f"扫描深度: {max_depth}")
print(f"输出目录: {os.path.abspath(os.path.expanduser(output_dir))}")
print(f"自动打开报告: {'是' if args.open else '否'}")
print(f"生成分享卡片: {'是' if args.share_card else '否'}")
print("=" * 40)
confirm = input("[Enter] 开始,输入 q 退出: ").strip().lower()
if confirm in {"q", "quit", "exit"}:
print("已退出。")
sys.exit(0)
print()
return args
def main():
parser = argparse.ArgumentParser(description='Git Analytics - 个人代码习惯体检工具')
parser.add_argument('scan_dir', nargs='*',
help='扫描目录,可传多个;默认扫描当前目录')
parser.add_argument('--since', help='起始日期,如 2024-01-01')
parser.add_argument('--until', help='截止日期,如 2024-12-31')
parser.add_argument('--project', help='只分析指定项目(支持模糊匹配)')
parser.add_argument('--output-dir', default=os.getcwd(), help='输出目录(默认当前目录)')
parser.add_argument('--max-depth', type=int, default=3, help='扫描目录深度(默认 3)')
parser.add_argument('--open', action='store_true', help='生成后自动打开报告')
parser.add_argument('--share-card', action='store_true', help='同时输出 PNG 分享卡片设计器')
parser.add_argument('--no-wizard', action='store_true', help='关闭交互式向导,直接使用默认参数')
args = parser.parse_args()
if not args.scan_dir and not args.no_wizard and _is_interactive():
args = _run_wizard(args)
scan_dirs = args.scan_dir or [os.getcwd()]
output_dir = os.path.abspath(os.path.expanduser(args.output_dir))
data_path = os.path.join(output_dir, OUTPUT_DATA)
report_path = os.path.join(output_dir, OUTPUT_REPORT)
share_card_path = os.path.join(output_dir, 'share-card.html')
print("=" * 60)
print("🔍 Git Analytics - 个人代码习惯体检工具")
print("=" * 60)
print("扫描目录:")
for item in scan_dirs:
print(f" - {os.path.abspath(os.path.expanduser(item))}")
print(f"扫描深度: {args.max_depth}")
print(f"输出目录: {output_dir}")
if args.since or args.until:
print(f"时间范围: {args.since or '起始'} ~ {args.until or '至今'}")
if args.project:
print(f"指定项目: {args.project}")
print()
# 1. 收集数据
print("📊 第一步:收集 Git 仓库数据...")
analysis = collect_data(
scan_dir=scan_dirs,
since=args.since,
until=args.until,
project=args.project,
output_dir=output_dir,
max_depth=args.max_depth,
)
if analysis is None:
raise SystemExit(1)
print()
print("📝 第二步:生成体检报告...")
generate_report(data_path=data_path, output_path=report_path)
if args.share_card:
template = _find_share_card_template()
if template:
shutil.copyfile(template, share_card_path)
else:
print("⚠️ 未找到 share-card.html 模板,跳过分享器输出")
if args.open:
_open_file(report_path)
print()
print("=" * 60)
print("✅ 完成!")
print(f"📄 报告文件: {report_path}")
print(f"📊 数据文件: {data_path}")
if args.share_card and os.path.exists(share_card_path):
print(f"🖼️ 分享器: {share_card_path}")
print()
print("用浏览器打开 report.html 查看你的代码习惯体检报告")
print("=" * 60)
if __name__ == '__main__':
main()