-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathscript_deps.py
More file actions
executable file
·448 lines (372 loc) · 14 KB
/
Copy pathscript_deps.py
File metadata and controls
executable file
·448 lines (372 loc) · 14 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/usr/bin/env -S uv run --script
#
# /// script
# requires-python = ">=3.14"
# dependencies = [
# "packaging>=26.3",
# "rich>=15.0.0",
# "typer>=0.27.1",
# ]
# ///
from __future__ import annotations
import json
import os
import re
import subprocess
import sys
from dataclasses import dataclass, field
from pathlib import Path
from typing import Annotated, Any
from urllib.error import HTTPError, URLError
from urllib.parse import quote
from urllib.request import urlopen
from packaging.requirements import InvalidRequirement, Requirement
from packaging.utils import canonicalize_name
from packaging.version import InvalidVersion, Version
from rich.console import Console
from rich.table import Table
import typer
console = Console()
app = typer.Typer(add_completion=False, no_args_is_help=False)
SCRIPT_BLOCK_RE = re.compile(r"(?m)^# /// script\s*\n(?P<body>(?:#.*\n)*?)^# ///\s*$")
@dataclass(frozen=True)
class DependencyOccurrence:
path: Path
raw: str
requirement: Requirement | None
error: str | None = None
@dataclass
class PackageReport:
name: str
latest: str | None = None
latest_error: str | None = None
occurrences: list[DependencyOccurrence] = field(default_factory=list)
@property
def constraints(self) -> list[str]:
values = {
str(occurrence.requirement.specifier) or "(unbounded)"
for occurrence in self.occurrences
if occurrence.requirement is not None
}
return sorted(values)
@property
def files(self) -> list[str]:
return sorted({str(occurrence.path) for occurrence in self.occurrences})
@dataclass(frozen=True)
class UpgradeAction:
path: Path
package: str
requirement: str
def read_script_metadata(path: Path) -> dict[str, Any] | None:
"""读取 PEP 723 script metadata。"""
try:
text = path.read_text(encoding="utf-8")
except UnicodeDecodeError:
return None
match = SCRIPT_BLOCK_RE.search(text)
if match is None:
return None
body = []
for line in match.group("body").splitlines():
if line.startswith("# "):
body.append(line[2:])
elif line.startswith("#"):
body.append(line[1:])
else:
body.append(line)
import tomllib
return tomllib.loads("\n".join(body))
def list_python_files(root: Path) -> list[Path]:
"""优先扫描 Git 跟踪文件,避免虚拟环境和缓存目录。"""
try:
result = subprocess.run(
["git", "ls-files", "*.py"],
cwd=root,
check=True,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
text=True,
)
except (OSError, subprocess.CalledProcessError):
return sorted(
path
for path in root.rglob("*.py")
if ".git" not in path.relative_to(root).parts
and ".venv" not in path.relative_to(root).parts
)
return sorted(root / line for line in result.stdout.splitlines() if line)
def collect_dependencies(root: Path) -> tuple[dict[str, PackageReport], list[str]]:
"""收集所有 uv script 依赖声明。"""
reports: dict[str, PackageReport] = {}
errors: list[str] = []
for path in list_python_files(root):
metadata = read_script_metadata(path)
if metadata is None:
continue
for raw in metadata.get("dependencies", []):
rel_path = path.relative_to(root)
try:
requirement = Requirement(raw)
except InvalidRequirement as exc:
package = raw.split(";", 1)[0].split("[", 1)[0].strip() or raw
name = canonicalize_name(package)
reports.setdefault(name, PackageReport(name=name)).occurrences.append(
DependencyOccurrence(rel_path, raw, None, str(exc))
)
errors.append(f"{rel_path}: invalid requirement {raw!r}: {exc}")
continue
name = canonicalize_name(requirement.name)
reports.setdefault(name, PackageReport(name=name)).occurrences.append(
DependencyOccurrence(rel_path, raw, requirement)
)
return reports, errors
def fetch_latest_version(package: str, timeout: float) -> tuple[str | None, str | None]:
"""从 PyPI JSON API 读取最新版本。"""
url = f"https://pypi.org/pypi/{quote(package)}/json"
try:
with urlopen(url, timeout=timeout) as response:
payload = json.load(response)
except (HTTPError, URLError, TimeoutError, OSError) as exc:
return None, str(exc)
version = payload.get("info", {}).get("version")
if not isinstance(version, str) or not version:
return None, "missing info.version in PyPI response"
return version, None
def declared_versions(report: PackageReport) -> list[Version]:
"""提取声明中的可比较版本下限或精确版本。"""
versions: list[Version] = []
for occurrence in report.occurrences:
requirement = occurrence.requirement
if requirement is None:
continue
for specifier in requirement.specifier:
if specifier.operator not in {"==", "===", ">=", ">", "~="}:
continue
try:
versions.append(Version(specifier.version))
except InvalidVersion:
continue
return versions
def package_status(report: PackageReport) -> str:
"""判断依赖声明是否值得关注。"""
if any(occurrence.error for occurrence in report.occurrences):
return "invalid requirement"
if report.latest_error:
return "lookup failed"
if not report.latest:
return "unknown"
if "(unbounded)" in report.constraints:
return "unbounded"
versions = declared_versions(report)
if not versions:
return "no comparable floor"
try:
latest = Version(report.latest)
except InvalidVersion:
return "unknown"
constraints_are_mixed = len(report.constraints) > 1
if max(versions) < latest:
return (
"minimum behind latest; mixed constraints"
if constraints_are_mixed
else "minimum behind latest"
)
if constraints_are_mixed:
return "mixed constraints"
return "ok"
def needs_attention(status: str) -> bool:
return status != "ok"
def build_reports(root: Path, timeout: float) -> tuple[list[PackageReport], list[str]]:
reports, errors = collect_dependencies(root)
for report in reports.values():
report.latest, report.latest_error = fetch_latest_version(report.name, timeout)
return sorted(reports.values(), key=lambda item: item.name), errors
def upgrade_requirement(report: PackageReport, requirement: Requirement) -> str | None:
"""构造保留 extras 和 marker 的最新下限声明。"""
if not report.latest or report.latest_error or requirement.url:
return None
extras = f"[{','.join(sorted(requirement.extras))}]" if requirement.extras else ""
marker = f"; {requirement.marker}" if requirement.marker else ""
return f"{requirement.name}{extras}>={report.latest}{marker}"
def collect_upgrade_actions(
root: Path, reports: list[PackageReport]
) -> tuple[list[UpgradeAction], list[str]]:
"""生成 uv add --script 升级动作。"""
actions: dict[tuple[Path, str], UpgradeAction] = {}
skipped: list[str] = []
for report in reports:
for occurrence in report.occurrences:
if occurrence.requirement is None:
skipped.append(
f"{occurrence.path}: invalid requirement {occurrence.raw!r}"
)
continue
requirement = upgrade_requirement(report, occurrence.requirement)
if requirement is None:
skipped.append(
f"{occurrence.path}: cannot upgrade {occurrence.raw!r} automatically"
)
continue
if occurrence.raw == requirement:
continue
path = root / occurrence.path
key = (path, report.name)
actions[key] = UpgradeAction(
path=path,
package=report.name,
requirement=requirement,
)
return sorted(actions.values(), key=lambda item: (item.path, item.package)), skipped
def run_upgrade_actions(actions: list[UpgradeAction], *, dry_run: bool) -> None:
"""调用 uv add --script 更新依赖声明。"""
if not actions:
console.print("[green]No upgrade actions needed.[/green]")
return
for action in actions:
command = [
"uv",
"add",
"--script",
str(action.path),
action.requirement,
]
console.print(f"{'Would run' if dry_run else 'Running'}: {' '.join(command)}")
if not dry_run:
subprocess.run(command, check=True)
def report_to_payload(
reports: list[PackageReport], errors: list[str]
) -> dict[str, Any]:
packages = []
for report in reports:
status = package_status(report)
packages.append(
{
"name": report.name,
"latest": report.latest,
"latest_error": report.latest_error,
"constraints": report.constraints,
"files": report.files,
"status": status,
"needs_attention": needs_attention(status),
}
)
return {
"package_count": len(packages),
"attention_count": sum(1 for package in packages if package["needs_attention"]),
"packages": packages,
"errors": errors,
}
def render_table(reports: list[PackageReport], *, only_attention: bool) -> None:
table = Table(title="uv script dependency report")
table.add_column("package")
table.add_column("latest")
table.add_column("declared")
table.add_column("scripts", justify="right")
table.add_column("status")
for report in reports:
status = package_status(report)
if only_attention and not needs_attention(status):
continue
table.add_row(
report.name,
report.latest or "-",
", ".join(report.constraints) or "-",
str(len(report.files)),
status,
)
console.print(table)
def markdown_escape(value: str) -> str:
return value.replace("|", "\\|").replace("\n", " ")
def render_markdown(reports: list[PackageReport], errors: list[str]) -> str:
attention_count = sum(
1 for report in reports if needs_attention(package_status(report))
)
lines = [
"# uv script dependency report",
"",
f"- Packages scanned: {len(reports)}",
f"- Packages needing attention: {attention_count}",
"",
"| Package | Latest | Declared | Scripts | Status |",
"| --- | --- | --- | ---: | --- |",
]
for report in reports:
lines.append(
"| "
+ " | ".join(
[
markdown_escape(report.name),
markdown_escape(report.latest or "-"),
markdown_escape(", ".join(report.constraints) or "-"),
str(len(report.files)),
markdown_escape(package_status(report)),
]
)
+ " |"
)
if errors:
lines.extend(["", "## Parse errors", ""])
lines.extend(f"- {markdown_escape(error)}" for error in errors)
return "\n".join(lines) + "\n"
@app.command(help="检查或升级仓库内 PEP 723 uv script 依赖声明。")
def main(
root: Annotated[Path, typer.Option("--root", "-C", help="仓库根目录")] = Path("."),
timeout: Annotated[float, typer.Option(help="PyPI 请求超时时间,单位秒")] = 10.0,
json_output: Annotated[
bool, typer.Option("--json", "-j", help="输出 JSON")
] = False,
github_summary: Annotated[
bool,
typer.Option("--github-summary", help="写入 GitHub Actions step summary"),
] = False,
only_attention: Annotated[
bool,
typer.Option("--only-attention", help="终端表格只显示需要关注的依赖"),
] = False,
fail_on_attention: Annotated[
bool,
typer.Option("--fail-on-attention", help="发现需要关注的依赖时返回非 0"),
] = False,
upgrade: Annotated[
bool,
typer.Option("--upgrade", help="使用 uv add --script 自动升级依赖声明下限"),
] = False,
dry_run: Annotated[
bool,
typer.Option("--dry-run", help="配合 --upgrade 使用,只输出将执行的命令"),
] = False,
) -> None:
resolved_root = root.expanduser().resolve()
reports, errors = build_reports(resolved_root, timeout)
payload = report_to_payload(reports, errors)
if dry_run and not upgrade:
raise typer.BadParameter("--dry-run 必须和 --upgrade 一起使用")
if upgrade:
actions, skipped = collect_upgrade_actions(resolved_root, reports)
run_upgrade_actions(actions, dry_run=dry_run)
if skipped:
console.print("[yellow]Skipped automatic upgrades:[/yellow]")
for item in skipped:
console.print(f"- {item}")
if not dry_run:
reports, errors = build_reports(resolved_root, timeout)
payload = report_to_payload(reports, errors)
if json_output:
print(json.dumps(payload, ensure_ascii=False, indent=2))
else:
render_table(reports, only_attention=only_attention)
if github_summary:
summary = render_markdown(reports, errors)
summary_path = os.environ.get("GITHUB_STEP_SUMMARY")
if summary_path:
Path(summary_path).write_text(summary, encoding="utf-8")
else:
console.print(summary)
if errors or (fail_on_attention and payload["attention_count"]):
raise typer.Exit(code=1)
if __name__ == "__main__":
try:
app()
except KeyboardInterrupt:
console.print("[red]Interrupted[/red]", file=sys.stderr)
raise typer.Exit(code=130)