Skip to content

Commit bbd73ec

Browse files
authored
Improve RTD speed and efficiency (#45)
1 parent 41007b3 commit bbd73ec

3 files changed

Lines changed: 56 additions & 32 deletions

File tree

.readthedocs.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ build:
1111
- mkdir -p venv/cpython/Doc/locales/fa/LC_MESSAGES
1212
- cp --parents *.po venv/cpython/Doc/locales/fa/LC_MESSAGES/
1313
- find */ -name "*.po" -not -path "venv/*" | xargs -I{} cp --parents {} venv/cpython/Doc/locales/fa/LC_MESSAGES/
14-
- find venv/cpython/Doc/locales/fa/LC_MESSAGES -name "*.po" | xargs python3 scripts/strip_fuzzy.py
15-
- 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
14+
- find venv/cpython/Doc/locales/fa/LC_MESSAGES -name "*.po" | python3 scripts/compile_mo.py
1615
- python -m sphinx -T -j auto -b html
1716
-d $READTHEDOCS_OUTPUT/doctrees
1817
-D language=fa
@@ -21,4 +20,4 @@ build:
2120
-D gettext_allow_fuzzy_translations=1
2221
-D html_theme_options.is_rtl=1
2322
venv/cpython/Doc
24-
$READTHEDOCS_OUTPUT/html
23+
$READTHEDOCS_OUTPUT/html

scripts/compile_mo.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python3
2+
"""Strip stray '#, fuzzy' markers and compile .po files to .mo, in one pass.
3+
4+
CPython's Tools/i18n/msgfmt.py treats a fuzzy flag on the file header as
5+
applying to every entry in the file, silently producing an empty .mo
6+
catalog. Since our translations are complete (not actually rough
7+
drafts), we strip the marker before compiling.
8+
9+
Compilation calls msgfmt.make() directly instead of spawning a fresh
10+
Python interpreter per file, since interpreter startup is the dominant
11+
cost when compiling hundreds of small .po files.
12+
13+
Reads paths from stdin (one per line, e.g. via `find ... | python3
14+
compile_mo.py`).
15+
"""
16+
import sys
17+
import os
18+
19+
# venv/cpython/Tools/i18n/msgfmt.py, relative to repo root.
20+
sys.path.insert(0, os.path.join("venv", "cpython", "Tools", "i18n"))
21+
import msgfmt # noqa: E402
22+
23+
24+
def strip_fuzzy(path: str) -> None:
25+
with open(path, encoding="utf-8") as f:
26+
lines = f.readlines()
27+
28+
kept = [line for line in lines if not line.lstrip().startswith("#, fuzzy")]
29+
30+
if kept != lines:
31+
with open(path, "w", encoding="utf-8") as f:
32+
f.writelines(kept)
33+
34+
35+
def compile_po(path: str) -> None:
36+
msgfmt.MESSAGES = {}
37+
out = path[:-3] + ".mo" if path.endswith(".po") else path + ".mo"
38+
msgfmt.make(path, out)
39+
40+
41+
def process(path: str) -> None:
42+
strip_fuzzy(path)
43+
compile_po(path)
44+
45+
46+
def main() -> None:
47+
for line in sys.stdin:
48+
path = line.strip()
49+
if path:
50+
process(path)
51+
52+
53+
if __name__ == "__main__":
54+
main()

scripts/strip_fuzzy.py

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)