From 2bfd60e9abf410527cae875a97eb8f4787bc8b5b Mon Sep 17 00:00:00 2001 From: Sepehr Rasouli Date: Tue, 18 Aug 2026 11:02:27 +0330 Subject: [PATCH] Improve RTD speed and efficiency --- .readthedocs.yml | 5 ++-- scripts/compile_mo.py | 54 ++++++++++++++++++++++++++++++++++++++++++ scripts/strip_fuzzy.py | 29 ----------------------- 3 files changed, 56 insertions(+), 32 deletions(-) create mode 100644 scripts/compile_mo.py delete mode 100644 scripts/strip_fuzzy.py diff --git a/.readthedocs.yml b/.readthedocs.yml index fafcca811..bd0d3adf0 100644 --- a/.readthedocs.yml +++ b/.readthedocs.yml @@ -11,8 +11,7 @@ build: - mkdir -p venv/cpython/Doc/locales/fa/LC_MESSAGES - cp --parents *.po venv/cpython/Doc/locales/fa/LC_MESSAGES/ - find */ -name "*.po" -not -path "venv/*" | xargs -I{} cp --parents {} venv/cpython/Doc/locales/fa/LC_MESSAGES/ - - find venv/cpython/Doc/locales/fa/LC_MESSAGES -name "*.po" | xargs python3 scripts/strip_fuzzy.py - - find venv/cpython/Doc/locales/fa/LC_MESSAGES -name "*.po" | while read f; do python venv/cpython/Tools/i18n/msgfmt.py -o "${f%.po}.mo" "$f"; done + - find venv/cpython/Doc/locales/fa/LC_MESSAGES -name "*.po" | python3 scripts/compile_mo.py - python -m sphinx -T -j auto -b html -d $READTHEDOCS_OUTPUT/doctrees -D language=fa @@ -21,4 +20,4 @@ build: -D gettext_allow_fuzzy_translations=1 -D html_theme_options.is_rtl=1 venv/cpython/Doc - $READTHEDOCS_OUTPUT/html \ No newline at end of file + $READTHEDOCS_OUTPUT/html diff --git a/scripts/compile_mo.py b/scripts/compile_mo.py new file mode 100644 index 000000000..9eb26df4d --- /dev/null +++ b/scripts/compile_mo.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +"""Strip stray '#, fuzzy' markers and compile .po files to .mo, in one pass. + +CPython's Tools/i18n/msgfmt.py treats a fuzzy flag on the file header as +applying to every entry in the file, silently producing an empty .mo +catalog. Since our translations are complete (not actually rough +drafts), we strip the marker before compiling. + +Compilation calls msgfmt.make() directly instead of spawning a fresh +Python interpreter per file, since interpreter startup is the dominant +cost when compiling hundreds of small .po files. + +Reads paths from stdin (one per line, e.g. via `find ... | python3 +compile_mo.py`). +""" +import sys +import os + +# venv/cpython/Tools/i18n/msgfmt.py, relative to repo root. +sys.path.insert(0, os.path.join("venv", "cpython", "Tools", "i18n")) +import msgfmt # noqa: E402 + + +def strip_fuzzy(path: str) -> None: + with open(path, encoding="utf-8") as f: + lines = f.readlines() + + kept = [line for line in lines if not line.lstrip().startswith("#, fuzzy")] + + if kept != lines: + with open(path, "w", encoding="utf-8") as f: + f.writelines(kept) + + +def compile_po(path: str) -> None: + msgfmt.MESSAGES = {} + out = path[:-3] + ".mo" if path.endswith(".po") else path + ".mo" + msgfmt.make(path, out) + + +def process(path: str) -> None: + strip_fuzzy(path) + compile_po(path) + + +def main() -> None: + for line in sys.stdin: + path = line.strip() + if path: + process(path) + + +if __name__ == "__main__": + main() diff --git a/scripts/strip_fuzzy.py b/scripts/strip_fuzzy.py deleted file mode 100644 index 1af9342b7..000000000 --- a/scripts/strip_fuzzy.py +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env python3 -"""Remove '#, fuzzy' marker lines from .po files. - -CPython's Tools/i18n/msgfmt.py treats a stray fuzzy flag on the file -header as applying to every entry in the file, silently producing an -empty .mo catalog. Since our translations are complete (not actually -rough drafts), we strip the marker before compiling. -""" -import sys - - -def strip_fuzzy(path: str) -> None: - with open(path, encoding="utf-8") as f: - lines = f.readlines() - - kept = [line for line in lines if not line.lstrip().startswith("#, fuzzy")] - - if kept != lines: - with open(path, "w", encoding="utf-8") as f: - f.writelines(kept) - - -def main() -> None: - for path in sys.argv[1:]: - strip_fuzzy(path) - - -if __name__ == "__main__": - main()