|
1 | 1 | #!/usr/bin/env python3 |
2 | | -"""Fail on broken internal links, drifted entry ids, or unresolved related refs.""" |
| 2 | +"""Fail on broken internal links, drifted entry ids, unresolved related refs, |
| 3 | +or a Wayback replay URL that does not name one capture.""" |
3 | 4 |
|
4 | 5 | from __future__ import annotations |
5 | 6 |
|
| 7 | +import re |
6 | 8 | import sys |
| 9 | +from datetime import datetime |
7 | 10 | from pathlib import Path |
8 | 11 |
|
9 | 12 | TOOLS = Path(__file__).resolve().parent |
|
24 | 27 | ROOT / "MAINTAINERS.md", |
25 | 28 | ] |
26 | 29 |
|
| 30 | +# A Wayback replay URL is /web/<capture id>/<original>. The capture id is |
| 31 | +# YYYYMMDDhhmmss, optionally followed by a rewrite flag such as id_. |
| 32 | +# A shorter id, including a bare 2, is a prefix search. The archive answers |
| 33 | +# HTTP 200 and redirects to whichever capture is currently closest. |
| 34 | +URL = re.compile(r"https?://[^\s<>\]\"']+") |
| 35 | +TRAILING = ".,;:!?" |
| 36 | +WAYBACK_REPLAY = re.compile( |
| 37 | + r"^https?://web\.archive\.org/web/([^/]+)(?:/.*)?$", |
| 38 | + re.IGNORECASE, |
| 39 | +) |
| 40 | +WAYBACK_FLAGS = ("id_", "js_", "cs_", "im_", "if_", "fw_", "mp_", "oe_") |
| 41 | + |
| 42 | + |
| 43 | +def clean_url(url: str) -> str: |
| 44 | + while url and url[-1] in TRAILING: |
| 45 | + url = url[:-1] |
| 46 | + while url.endswith(")") and url.count("(") < url.count(")"): |
| 47 | + url = url[:-1] |
| 48 | + return url |
| 49 | + |
| 50 | + |
| 51 | +def prose_without_fences(text: str) -> str: |
| 52 | + out: list[str] = [] |
| 53 | + inside = False |
| 54 | + for line in text.splitlines(): |
| 55 | + if line.lstrip().startswith("```"): |
| 56 | + inside = not inside |
| 57 | + continue |
| 58 | + if not inside: |
| 59 | + out.append(line) |
| 60 | + return re.sub(r"`[^`]*`", " ", "\n".join(out)) |
| 61 | + |
| 62 | + |
| 63 | +def wayback_timestamp_error(url: str) -> str | None: |
| 64 | + match = WAYBACK_REPLAY.match(url) |
| 65 | + if not match: |
| 66 | + return None |
| 67 | + token = match.group(1) |
| 68 | + digits = token |
| 69 | + for flag in WAYBACK_FLAGS: |
| 70 | + if token.endswith(flag): |
| 71 | + digits = token[: -len(flag)] |
| 72 | + break |
| 73 | + if not re.fullmatch(r"\d{14}", digits): |
| 74 | + return f"wayback capture id must be 14 digits, found '{token}'" |
| 75 | + try: |
| 76 | + datetime.strptime(digits, "%Y%m%d%H%M%S") |
| 77 | + except ValueError: |
| 78 | + return f"wayback capture id is not a calendar time, found '{digits}'" |
| 79 | + return None |
| 80 | + |
| 81 | + |
| 82 | +def wayback_replay_errors(text: str) -> list[str]: |
| 83 | + errors: list[str] = [] |
| 84 | + seen: set[str] = set() |
| 85 | + for raw in URL.findall(prose_without_fences(text)): |
| 86 | + url = clean_url(raw) |
| 87 | + if url in seen: |
| 88 | + continue |
| 89 | + seen.add(url) |
| 90 | + error = wayback_timestamp_error(url) |
| 91 | + if error: |
| 92 | + errors.append(f"{error}: {url}") |
| 93 | + return errors |
| 94 | + |
27 | 95 |
|
28 | 96 | def markdown_files() -> list[Path]: |
29 | 97 | files: list[Path] = [] |
@@ -52,22 +120,32 @@ def main() -> int: |
52 | 120 | for error in errors: |
53 | 121 | print(f" {error}") |
54 | 122 | link_failed = 0 |
| 123 | + wayback_failed = 0 |
55 | 124 | files = markdown_files() |
56 | 125 | for path in files: |
57 | 126 | text = path.read_text(encoding="utf-8", errors="replace") |
58 | 127 | errors = model.markdown_link_errors(path, text, ROOT) |
59 | | - if errors: |
60 | | - link_failed += 1 |
| 128 | + wayback = wayback_replay_errors(text) |
| 129 | + if errors or wayback: |
| 130 | + if errors: |
| 131 | + link_failed += 1 |
| 132 | + if wayback: |
| 133 | + wayback_failed += 1 |
61 | 134 | rel = path.relative_to(ROOT) |
62 | 135 | print(f"FAIL {rel}") |
63 | 136 | for error in errors: |
64 | 137 | print(f" {error}") |
| 138 | + for error in wayback: |
| 139 | + print(f" {error}") |
65 | 140 | entries = len(index.entries) |
66 | 141 | print( |
67 | 142 | f"\n{entries - failed}/{entries} entries pass identity and relationship checks" |
68 | 143 | ) |
69 | 144 | print(f"{len(files) - link_failed}/{len(files)} files pass internal links") |
70 | | - return 1 if failed or link_failed else 0 |
| 145 | + print( |
| 146 | + f"{len(files) - wayback_failed}/{len(files)} files pin every Wayback capture" |
| 147 | + ) |
| 148 | + return 1 if failed or link_failed or wayback_failed else 0 |
71 | 149 |
|
72 | 150 |
|
73 | 151 | if __name__ == "__main__": |
|
0 commit comments