|
| 1 | +"""Format sphinx-lint diagnostics for the translation team's Telegram alert.""" |
| 2 | + |
| 3 | +import os |
| 4 | +import re |
| 5 | +import subprocess |
| 6 | +import sys |
| 7 | +from pathlib import Path |
| 8 | +from urllib.parse import quote |
| 9 | + |
| 10 | + |
| 11 | +def main(log, output): |
| 12 | + repo = os.environ["PYDOC_REPO"].rstrip("/") |
| 13 | + run = os.environ["GITHUB_RUN_ID"] |
| 14 | + version = os.environ["PYDOC_VERSION"] |
| 15 | + checkout = Path("cpython/Doc/locales") / os.environ["PYDOC_LANGUAGE"] / "LC_MESSAGES" |
| 16 | + revision = subprocess.check_output( |
| 17 | + ["git", "-C", str(checkout), "rev-parse", "HEAD"], universal_newlines=True |
| 18 | + ).strip() |
| 19 | + header = f"❌ *{version} lint* ([execução {run}]({repo}/actions/runs/{run}))" |
| 20 | + footer = "Mais diagnósticos no log da execução." |
| 21 | + message = header |
| 22 | + found = False |
| 23 | + for line in sorted(set(log.read_text(encoding="utf-8").splitlines())): |
| 24 | + match = re.fullmatch(r"(.+?\.po):(\d+):\s+(.+\(.*\))", line) |
| 25 | + if not match: |
| 26 | + continue |
| 27 | + found = True |
| 28 | + filename, lineno, diagnostic = match.groups() |
| 29 | + path = Path(filename) |
| 30 | + if path.is_absolute(): |
| 31 | + path = path.relative_to(checkout.resolve()) |
| 32 | + url = f"{repo}/blob/{revision}/{quote(path.as_posix())}#L{lineno}" |
| 33 | + detail = f"{path}:{lineno}: {diagnostic}".replace("`", "\\`") |
| 34 | + block = f"\n\n```\n{detail}\n```\n[Abrir linha {lineno}]({url})" |
| 35 | + # Count the unparsed text conservatively, including Markdown and emoji. |
| 36 | + if len((message + block + "\n\n" + footer).encode("utf-16-le")) // 2 > 4096: |
| 37 | + message += "\n\n" + footer |
| 38 | + break |
| 39 | + message += block |
| 40 | + if not found: |
| 41 | + sys.exit("No sphinx-lint diagnostics found") |
| 42 | + output.write_text(message, encoding="utf-8") |
| 43 | + |
| 44 | + |
| 45 | +if __name__ == "__main__": |
| 46 | + main(Path(sys.argv[1]), Path(sys.argv[2])) |
0 commit comments