-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcompile_messages.py
More file actions
executable file
·78 lines (65 loc) · 2.76 KB
/
Copy pathcompile_messages.py
File metadata and controls
executable file
·78 lines (65 loc) · 2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env python3
"""Compile every locale's messages.po into messages.mo.
Usage: python3 scripts/compile_messages.py [--locale=LOCALE] [--module=ModuleName] [--check]
--check runs msgfmt in validating mode first and refuses to write a .mo for any
locale that fails, so a broken .po cannot silently ship a stale .mo.
"""
import os
import subprocess
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import poutil # noqa: E402
def main():
args = sys.argv[1:]
strict = '--check' in args
only = next((a.split('=', 1)[1] for a in args if a.startswith('--locale=')), None)
module = next((a.split('=', 1)[1] for a in args if a.startswith('--module=')), None)
base = (os.path.join(poutil.ROOT, 'lib/Module', module, 'locale') if module
else os.path.join(poutil.ROOT, 'locale'))
if not os.path.isdir(base):
print(f'Error: Locale directory not found: {base}', file=sys.stderr)
return 1
locales = sorted(d for d in os.listdir(base)
if os.path.isdir(os.path.join(base, d)) and 'pot' not in d)
if only:
if only not in locales:
print(f'Error: Locale not found: {only}', file=sys.stderr)
return 1
locales = [only]
compiled = failed = skipped = 0
for locale in locales:
po_file = poutil.po_path(locale, module)
if not os.path.isfile(po_file):
print(f'Compiling {locale} locale')
print(f' Warning: PO file not found: {po_file}')
skipped += 1
continue
print(f'Compiling {locale} locale')
if strict:
check = subprocess.run(['msgfmt', '--check', '-o', os.devnull, po_file],
capture_output=True, text=True)
real = [ln for ln in check.stderr.splitlines() if 'warning:' not in ln]
if check.returncode != 0 and real:
print(f' Refusing to compile, {po_file} has errors:')
for ln in real[:5]:
print(f' {ln}')
failed += 1
continue
mo_file = os.path.join(os.path.dirname(po_file), 'messages.mo')
result = subprocess.run(['msgfmt', '-o', mo_file, po_file], capture_output=True, text=True)
if result.returncode == 0:
print(f' Successfully compiled to {mo_file}')
compiled += 1
else:
print(f' Failed to compile {po_file}')
for ln in result.stderr.splitlines()[:5]:
print(f' {ln}')
failed += 1
print()
if failed:
print(f'Compiled {compiled}, failed {failed}, skipped {skipped}.')
return 1
print('All message files have been compiled successfully.')
return 0
if __name__ == '__main__':
sys.exit(main())