diff --git a/scripts/lint_notification.py b/scripts/lint_notification.py new file mode 100644 index 000000000..cb528a206 --- /dev/null +++ b/scripts/lint_notification.py @@ -0,0 +1,67 @@ +"""Format sphinx-lint diagnostics for the translation team's Telegram alert.""" + +import os +import re +import subprocess +import sys +from pathlib import Path + + +def source_location(po_file, lineno): + """Find the source references for the PO entry containing the diagnostic.""" + try: + lines = po_file.read_text(encoding="utf-8").splitlines() + index = int(lineno) - 1 + if not 0 <= index < len(lines): + return None + start = index + while start > 0 and lines[start - 1].strip(): + start -= 1 + end = index + while end < len(lines) and lines[end].strip(): + end += 1 + references = [] + for line in lines[start:end]: + if line.startswith("#: "): + references.extend(re.findall(r"(?:^|\s)(.+?:\d+)(?=\s|$)", line[3:])) + return ", ".join(dict.fromkeys(references)) or None + except (OSError, UnicodeError): + return None + + +def main(log, output): + repo = os.environ["PYDOC_REPO"].rstrip("/") + run = os.environ["GITHUB_RUN_ID"] + version = os.environ["PYDOC_VERSION"] + checkout = Path("cpython/Doc/locales") / os.environ["PYDOC_LANGUAGE"] / "LC_MESSAGES" + revision = subprocess.check_output( + ["git", "-C", str(checkout), "rev-parse", "HEAD"], universal_newlines=True + ).strip() + header = f"❌ *{version} lint* ([execução {run}]({repo}/actions/runs/{run}))" + footer = "Mais diagnósticos no log da execução." + message = header + f"\n[Revisão das traduções]({repo}/tree/{revision}/)" + found = False + for line in sorted(set(log.read_text(encoding="utf-8").splitlines())): + match = re.fullmatch(r"(.+?\.po):(\d+):\s+(.+\(.*\))", line) + if not match: + continue + found = True + filename, lineno, diagnostic = match.groups() + path = Path(filename) + if path.is_absolute(): + path = path.relative_to(checkout.resolve()) + location = source_location(checkout / path, lineno) or f"{path}:{lineno}" + detail = f"{location}: {diagnostic}".replace("`", "\\`") + block = f"\n\n```\n{detail}\n```" + # Count the unparsed text conservatively, including Markdown and emoji. + if len((message + block + "\n\n" + footer).encode("utf-16-le")) // 2 > 4096: + message += "\n\n" + footer + break + message += block + if not found: + sys.exit("No sphinx-lint diagnostics found") + output.write_text(message, encoding="utf-8") + + +if __name__ == "__main__": + main(Path(sys.argv[1]), Path(sys.argv[2])) diff --git a/scripts/prepmsg.sh b/scripts/prepmsg.sh index db50d9c7c..b00434e1e 100755 --- a/scripts/prepmsg.sh +++ b/scripts/prepmsg.sh @@ -16,6 +16,10 @@ URL="${PYDOC_REPO}/actions/runs/${GITHUB_RUN_ID}" input="$1" output="$2" +if [[ "${GITHUB_JOB}" == "lint" ]]; then + exec python3 "$(dirname "$0")/lint_notification.py" "$input" "$output" +fi + touch aux if [[ "${GITHUB_JOB}" == "build" ]]; then grep 'cpython/Doc/.*WARNING:' "$input" | \ @@ -23,12 +27,6 @@ if [[ "${GITHUB_JOB}" == "build" ]]; then uniq | \ sed 's|^|```\n|;s|$|\n```\n|' \ > aux -elif [[ "${GITHUB_JOB}" == "lint" ]]; then - grep -P '^.*\.po:\d+:\s+.*\(.*\)$' "$input" | \ - sed 's|.*/cpython/Doc/||' | \ - sort -u | \ - sed 's|^|```\n|;s|$|\n```\n|' \ - > aux else die "Unexpected job name ${GITHUB_JOB}" fi