codeyear 0.2.14__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
codeyear/runner.py ADDED
@@ -0,0 +1,304 @@
1
+ """Shared scan and render pipeline for the wizard and the desktop GUI."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import contextlib
6
+ import fnmatch
7
+ import json
8
+ import os
9
+ import subprocess
10
+ from concurrent.futures import ThreadPoolExecutor, as_completed
11
+ from dataclasses import replace
12
+ from datetime import datetime, timedelta
13
+ from pathlib import Path
14
+ from typing import Callable
15
+
16
+ from .card import render as render_card
17
+ from .html_report import render as render_html
18
+ from .html_report import render_en as render_html_en
19
+ from .poster import render as render_poster
20
+ from .poster import render_long
21
+ from .scanner import RepoResult, find_repos, scan_repo
22
+ from .stats import ReportStats, build_stats, compare_summary
23
+
24
+ Progress = Callable[[int, int, str], None]
25
+ ALL_FORMATS = {"json", "html", "png", "long", "card"}
26
+ _NO_WINDOW = subprocess.CREATE_NO_WINDOW if os.name == "nt" else 0
27
+
28
+
29
+ def report_path(written: list[tuple[str, Path]], *, lang: str = "zh") -> Path | None:
30
+ """Pick the report to open: English first when the UI is English."""
31
+ labels = ("HTML-EN", "HTML") if lang == "en" else ("HTML",)
32
+ by_label = dict(written)
33
+ for label in labels:
34
+ if label in by_label:
35
+ return by_label[label]
36
+ return None
37
+
38
+
39
+ def detect_identity() -> tuple[str, str] | None:
40
+ def config(key: str) -> str:
41
+ try:
42
+ proc = subprocess.run(
43
+ ["git", "config", "--global", key],
44
+ capture_output=True, text=True, encoding="utf-8",
45
+ errors="replace", timeout=10, creationflags=_NO_WINDOW,
46
+ )
47
+ except (OSError, subprocess.TimeoutExpired):
48
+ return ""
49
+ return (proc.stdout or "").strip()
50
+
51
+ email, name = config("user.email"), config("user.name")
52
+ if email or name:
53
+ return email, name
54
+ return None
55
+
56
+
57
+ def is_excluded(
58
+ repo_path: Path, root: Path, patterns: list[str] | None
59
+ ) -> bool:
60
+ """Return True if *repo_path* matches any exclude pattern."""
61
+ if not patterns:
62
+ return False
63
+ rel = repo_path.name
64
+ with contextlib.suppress(ValueError):
65
+ rel_str = repo_path.relative_to(root).as_posix()
66
+ if rel_str != ".":
67
+ rel = rel_str
68
+ candidates = {
69
+ repo_path.name.lower(),
70
+ rel.lower(),
71
+ repo_path.as_posix().lower(),
72
+ }
73
+ for raw in patterns:
74
+ for part in str(raw).split(","):
75
+ pat = part.strip().lower()
76
+ if not pat:
77
+ continue
78
+ if any(
79
+ c == pat or fnmatch.fnmatch(c, pat) or pat in c.split("/")
80
+ for c in candidates
81
+ ):
82
+ return True
83
+ return False
84
+
85
+
86
+ def discover(
87
+ roots: list[Path],
88
+ depth: int = 6,
89
+ *,
90
+ exclude: list[str] | None = None,
91
+ ) -> list[tuple[Path, Path]]:
92
+ seen: set[Path] = set()
93
+ found: list[tuple[Path, Path]] = []
94
+ for root in roots:
95
+ for repo in find_repos(root, max_depth=depth):
96
+ resolved = repo.resolve()
97
+ if is_excluded(resolved, root, exclude):
98
+ continue
99
+ if resolved not in seen:
100
+ seen.add(resolved)
101
+ found.append((resolved, root))
102
+ return found
103
+
104
+
105
+ def _root_labels(roots: list[Path]) -> dict[Path, str]:
106
+ labels: dict[Path, str] = {}
107
+ used: set[str] = set()
108
+ for root in roots:
109
+ base = root.name or str(root)
110
+ label = base
111
+ n = 2
112
+ while label in used:
113
+ label = f"{base}-{n}"
114
+ n += 1
115
+ used.add(label)
116
+ labels[root] = label
117
+ return labels
118
+
119
+
120
+ def scan(
121
+ repos: list[tuple[Path, Path]],
122
+ roots: list[Path],
123
+ year: int,
124
+ identity: tuple[str, str] | None,
125
+ *,
126
+ aliases: list[str] | None = None,
127
+ author: str | None = None,
128
+ timeout: int = 300,
129
+ jobs: int = 1,
130
+ progress: Progress | None = None,
131
+ compare: bool = False,
132
+ use_mailmap: bool = True,
133
+ all_branches: bool = False,
134
+ rolling: bool = False,
135
+ ) -> tuple[ReportStats, list[str]]:
136
+ labels = _root_labels(roots)
137
+ results: list[RepoResult] = []
138
+ warnings: list[str] = []
139
+ total = len(repos)
140
+
141
+ until_dt: datetime | None = None
142
+ since_dt: datetime | None = None
143
+ scan_since_dt: datetime | None = None
144
+ if rolling:
145
+ until_dt = datetime.now().replace(
146
+ hour=23, minute=59, second=59, microsecond=0
147
+ )
148
+ since_dt = (until_dt - timedelta(days=364)).replace(
149
+ hour=0, minute=0, second=0, microsecond=0
150
+ )
151
+ scan_since_dt = (
152
+ (since_dt - timedelta(days=365)) if compare else since_dt
153
+ )
154
+
155
+ def prepare(repo: Path, root: Path) -> RepoResult:
156
+ kwargs: dict = {
157
+ "author": author,
158
+ "timeout": timeout,
159
+ "use_mailmap": use_mailmap,
160
+ "all_branches": all_branches,
161
+ }
162
+ if rolling and scan_since_dt is not None and until_dt is not None:
163
+ kwargs["since_dt"] = scan_since_dt
164
+ kwargs["until_dt"] = until_dt
165
+ elif compare:
166
+ kwargs["since_year"] = year - 1
167
+ result = scan_repo(repo, root, year, **kwargs)
168
+ if len(roots) > 1:
169
+ label = labels[root]
170
+ old = result.name
171
+ new = label if old == label or old in {"", "."} else f"{label}/{old}"
172
+ result.name = new
173
+ for commit in result.commits:
174
+ commit.repo = new
175
+ return result
176
+
177
+ if jobs <= 1:
178
+ for i, (repo, root) in enumerate(repos):
179
+ if progress:
180
+ progress(i, total, repo.name)
181
+ result = prepare(repo, root)
182
+ if result.warning:
183
+ warnings.append(f"{result.name}: {result.warning}")
184
+ results.append(result)
185
+ if progress:
186
+ progress(i + 1, total, repo.name)
187
+ else:
188
+ with ThreadPoolExecutor(max_workers=jobs) as pool:
189
+ futures = {
190
+ pool.submit(prepare, repo, root): repo
191
+ for repo, root in repos
192
+ }
193
+ for done, future in enumerate(as_completed(futures), 1):
194
+ repo = futures[future]
195
+ try:
196
+ result = future.result()
197
+ except Exception as exc: # pragma: no cover - worker failure
198
+ warnings.append(f"{repo}: {exc}")
199
+ continue
200
+ if result.warning:
201
+ warnings.append(f"{result.name}: {result.warning}")
202
+ results.append(result)
203
+ if progress:
204
+ progress(done, total, result.name)
205
+
206
+ root_label = " + ".join(str(root) for root in roots)
207
+ if compare:
208
+ if rolling and since_dt is not None:
209
+ current_results = [
210
+ replace(r, commits=[c for c in r.commits if c.dt >= since_dt])
211
+ for r in results
212
+ ]
213
+ prev_results = [
214
+ replace(r, commits=[c for c in r.commits if c.dt < since_dt])
215
+ for r in results
216
+ ]
217
+ else:
218
+ current_results = [
219
+ replace(r, commits=[c for c in r.commits if c.dt.year == year])
220
+ for r in results
221
+ ]
222
+ prev_results = [
223
+ replace(
224
+ r, commits=[c for c in r.commits if c.dt.year == year - 1]
225
+ )
226
+ for r in results
227
+ ]
228
+ stats = build_stats(
229
+ year=year,
230
+ root=root_label,
231
+ repos=current_results,
232
+ warnings=warnings,
233
+ identity=identity,
234
+ aliases=aliases,
235
+ )
236
+ previous = build_stats(
237
+ year=year - 1,
238
+ root=root_label,
239
+ repos=prev_results,
240
+ warnings=[],
241
+ identity=identity,
242
+ aliases=aliases,
243
+ )
244
+ stats.previous = compare_summary(stats, previous)
245
+ else:
246
+ stats = build_stats(
247
+ year=year,
248
+ root=root_label,
249
+ repos=results,
250
+ warnings=warnings,
251
+ identity=identity,
252
+ aliases=aliases,
253
+ )
254
+ return stats, warnings
255
+
256
+
257
+ def render(
258
+ stats: ReportStats,
259
+ out_dir: Path,
260
+ formats: set[str] | None = None,
261
+ *,
262
+ card_theme: str = "paper",
263
+ lang: str = "zh",
264
+ ) -> list[tuple[str, Path]]:
265
+ formats = formats or ALL_FORMATS
266
+ out_dir.mkdir(parents=True, exist_ok=True)
267
+ base = out_dir / f"codeyear-{stats.year}"
268
+ written: list[tuple[str, Path]] = []
269
+
270
+ if "json" in formats:
271
+ path = base.with_suffix(".json")
272
+ path.write_text(
273
+ json.dumps(stats.as_dict(), ensure_ascii=False, indent=2),
274
+ encoding="utf-8",
275
+ )
276
+ written.append(("JSON", path))
277
+ if "html" in formats:
278
+ written.append(("HTML", render_html(stats, base.with_suffix(".html"))))
279
+ written.append(("HTML-EN", render_html_en(
280
+ stats, out_dir / f"codeyear-{stats.year}-en.html")))
281
+ if "png" in formats:
282
+ written.append(("PNG", render_poster(stats, base.with_suffix(".png"))))
283
+ if lang == "en":
284
+ written.append(("PNG-EN", render_poster(
285
+ stats, out_dir / f"codeyear-{stats.year}-en.png", lang="en")))
286
+ if "long" in formats:
287
+ written.append(("LONG", render_long(
288
+ stats, out_dir / f"codeyear-{stats.year}-long.png")))
289
+ if lang == "en":
290
+ written.append(("LONG-EN", render_long(
291
+ stats, out_dir / f"codeyear-{stats.year}-en-long.png",
292
+ lang="en")))
293
+ if "card" in formats:
294
+ cards = render_card(stats, out_dir / f"codeyear-{stats.year}-card.svg",
295
+ theme=card_theme)
296
+ written.append(("CARD", cards[0]))
297
+ written.append(("CARD-DARK", cards[1]))
298
+ en_cards = render_card(
299
+ stats, out_dir / f"codeyear-{stats.year}-card-en.svg",
300
+ theme=card_theme, lang="en",
301
+ )
302
+ written.append(("CARD-EN", en_cards[0]))
303
+ written.append(("CARD-DARK-EN", en_cards[1]))
304
+ return written