-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapply_review_corrections.py
More file actions
311 lines (266 loc) · 12.9 KB
/
Copy pathapply_review_corrections.py
File metadata and controls
311 lines (266 loc) · 12.9 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/env python3
"""Apply reviewed translation corrections to a catalogue, then verify the write.
Usage: python3 scripts/apply_review_corrections.py <locale> [--dir=DIR] [--plurals]
Reviewers emit JSON rather than editing .po files directly, because parallel
writes to one catalogue lose updates. This applies those files serially behind a
gate, and refuses to write anything unless every correction passes it - a single
failure aborts the whole locale so nothing lands half-applied.
msgstr mode (default) reads DIR/OUT_<locale>_*.json, each {msgid: translation}
--plurals reads DIR/OUT_<locale>_plr*.json, each
{msgid: {index: form}}, and writes msgstr[n]
DIR defaults to ./locale-review.
The gate holds a correction to the same standard the catalogue is checked
against: identical printf placeholders, HTML tags and [TOKEN]s, identical
leading and trailing whitespace, every identifier and IP literal from the source
still present, and every do-not-translate literal from dnt_glossary.json still
present. It also rejects corrections to entries that are untranslated English
fallbacks, since substituting words into English yields a mixed-language hybrid.
"""
import argparse
import glob
import json
import os
import re
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import poutil # noqa: E402
PRINTF = re.compile(r'%(?:\d+\$)?[sdfucxXob]|%%')
TAG = re.compile(r'</?(?:code|strong|em|b|i|u|a|br|p|span|div|ul|ol|li|small|sup|sub)\b[^<>]*>',
re.I)
TOKEN = re.compile(r'\[[A-Z0-9_]+\]')
IDENTIFIER = re.compile(
r'\b[a-z][a-z0-9]*(?:_[a-z0-9]+){1,}\b'
r'|\b(?:dns|api|misc|db|ldap|mail|pdns)\.[a-z_.]+\b'
r'|\b[\w-]+\.(?:php|html|twig|js|css|sql|dat|json|ya?ml|conf|ini)\b'
)
IPV4 = re.compile(r'(?<![0-9A-Za-z.])\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(?:/\d{1,2})?'
r'(?![0-9A-Za-z]|\.[0-9A-Za-z])')
SLASH_SPACE = re.compile(r'\s*/\s*')
# Pass families in application order. Each reviewed the catalogue as the earlier
# ones left it, so it wins any disagreement with them; two files inside one family
# disagreeing is a real problem and still aborts. A family absent from this list
# ranks 0 and can never override, which is what keeps independent slices honest.
ORDER = ['term', 'dnt', 'id', 'cn', 'sty', 'idb', 'cnb', 'idc', 'cnc', 'sup', 'fix']
def load_dnt():
"""Glossary literals with their msgid-side matchers.
'/', '\\', '.' and '-' belong to the token, so the boundaries never split
templates/emails/custom/ or in-addr.arpa. A term carrying an uppercase letter
matches case-sensitively, so the KEY record type does not fire on "key".
"""
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'dnt_glossary.json')
with open(path, encoding='utf-8') as fh:
glossary = json.load(fh)
left, right = r'(?<![A-Za-z0-9_\\/-])', r'(?![A-Za-z0-9_\\/-])'
return [(e['term'],
re.compile(left + re.escape(e['term']) + right,
0 if any(c.isupper() for c in e['term']) else re.I),
set(e.get('except_locales', {})))
for e in glossary['literal']]
def placeholders(text):
return sorted(re.sub(r'\d+\$', '', p) for p in PRINTF.findall(text))
def dnt_dropped(msgid, new, locale, dnt):
"""Glossary literals the source has and the correction does not."""
# Casing and spacing around a slash are not this gate's business, so the
# correction is searched case-insensitively with "SSL / TLS" folded back.
haystack = SLASH_SPACE.sub('/', new).lower()
return [term for term, pat, skip in dnt
if locale not in skip and pat.search(msgid) and term.lower() not in haystack]
def _lead(text):
return text[:len(text) - len(text.lstrip())]
def _trail(text):
return text[len(text.rstrip()):]
def gate(msgid, new, old, locale, dnt):
"""Reasons this correction must not be applied, empty when it is safe."""
bad = []
if placeholders(msgid) != placeholders(new):
bad.append('placeholder set differs from msgid')
if sorted(TAG.findall(msgid)) != sorted(TAG.findall(new)):
bad.append('HTML tags differ from msgid')
if sorted(TOKEN.findall(msgid)) != sorted(TOKEN.findall(new)):
bad.append('[TOKEN]s differ from msgid')
# Exact runs, not merely "is there whitespace": these strings are concatenated
# into sentences, so two leading spaces collapsing to one is real damage.
if _lead(msgid) != _lead(new) or _trail(msgid) != _trail(new):
bad.append('leading/trailing whitespace differs from msgid')
missing = [t for t in set(IDENTIFIER.findall(msgid)) if t not in new]
if missing:
bad.append(f'identifiers dropped: {missing}')
src_ips, new_ips = set(IPV4.findall(msgid)), set(IPV4.findall(new))
if src_ips != new_ips:
bad.append(f'IP literals differ: {sorted(src_ips)} vs {sorted(new_ips)}')
dropped = dnt_dropped(msgid, new, locale, dnt)
if dropped:
bad.append(f'do-not-translate literals dropped: {dropped}')
# old is None for plural forms: replacing an English form wholesale is the
# point there, whereas editing an English msgstr yields a mixed-language hybrid.
if old is not None and old == msgid:
bad.append('entry is an untranslated English fallback')
if not new.strip():
bad.append('correction is empty')
if new == msgid:
bad.append('correction is still the English source')
return bad
def report(problems, total, locale):
print(f'REJECTED {len(problems)} of {total} corrections for {locale}:')
for msgid, bad in problems[:25]:
print(f' {msgid[:70]!r}')
for reason in bad:
print(f' {reason}')
def collect_msgstr(locale, review_dir):
"""Merge every OUT_<locale>_*.json, honouring the family override order."""
def rank(path):
match = re.search(r'_([a-z]+)\d*\.json$', path)
family = match.group(1) if match else None
return ORDER.index(family) + 1 if family in ORDER else 0
paths = sorted(p for p in glob.glob(os.path.join(review_dir, f'OUT_{locale}_*.json'))
if not re.search(r'_plr\d*\.json$', p))
proposals = {}
for path in paths:
with open(path, encoding='utf-8') as fh:
data = json.load(fh)
for msgid, new in data.items():
proposals.setdefault(msgid, []).append((rank(path), path, new))
fixes, overrides = {}, 0
for msgid, offers in proposals.items():
top = max(r for r, _, _ in offers)
winners = {new: path for r, path, new in offers if r == top}
if len(winners) > 1:
# Two files of equal standing reached different answers. A filename
# tiebreak would pick one silently, so this needs a human - either fix
# the input files or record the decision in a later-ranked family.
names = ', '.join(sorted(os.path.basename(p) for p in winners.values()))
raise SystemExit(f'CONFLICT: {names} disagree on {msgid[:60]!r}')
fixes[msgid] = next(iter(winners))
if any(r < top and new != fixes[msgid] for r, _, new in offers):
overrides += 1
if overrides:
print(f'later pass overrode {overrides} earlier corrections')
return fixes
def apply_msgstr(locale, path, entries, review_dir):
by_id = {e.msgid: e for e in entries if not e.obsolete and not e.is_header}
fixes = collect_msgstr(locale, review_dir)
dnt = load_dnt()
problems = []
for msgid, new in fixes.items():
entry = by_id.get(msgid)
if entry is None:
problems.append((msgid, ['msgid not in catalogue']))
elif entry.msgstr != new:
bad = gate(msgid, new, entry.msgstr, locale, dnt)
if bad:
problems.append((msgid, bad))
if problems:
report(problems, len(fixes), locale)
return None
def write():
applied = 0
for msgid, new in fixes.items():
entry = by_id[msgid]
if entry.msgstr == new:
continue
entry.msgstr = new
entry.remove_flags('auto-english-fallback', 'fuzzy')
applied += 1
return applied
return fixes, write
def apply_plurals(locale, path, entries, review_dir):
by_id = {e.msgid: e for e in entries if not e.obsolete and not e.is_header}
rule = poutil.header_plural_rule(entries)
count = poutil.plural_spec(rule)[0] if rule else None
# Plural files carry no family ranking, so any two of them proposing different
# text for the same form is a disagreement between reviewers, not an override.
fixes, source_of = {}, {}
for name in sorted(glob.glob(os.path.join(review_dir, f'OUT_{locale}_plr*.json'))):
with open(name, encoding='utf-8') as fh:
for msgid, forms in json.load(fh).items():
for index, form in forms.items():
key = (msgid, str(index))
if key in source_of and fixes[msgid][index] != form:
raise SystemExit(
f'CONFLICT: {os.path.basename(source_of[key])} and '
f'{os.path.basename(name)} disagree on form {index} of '
f'{msgid[:50]!r}')
fixes.setdefault(msgid, {})[index] = form
source_of[key] = name
dnt = load_dnt()
problems, planned = [], []
for msgid, forms in fixes.items():
entry = by_id.get(msgid)
if entry is None:
problems.append((msgid, ['msgid not in catalogue']))
continue
if not entry.msgid_plural:
problems.append((msgid, ['not a plural entry']))
continue
for index, form in forms.items():
i = int(index)
# Bound below as well as above: set_plural(-1, ...) would write an
# msgstr[-1] that gettext can never select.
if i < 0 or (count is not None and i >= count):
problems.append((msgid, [f'index {i} outside 0..{(count or 1) - 1}']))
continue
# gettext picks by index, so form 0 answers msgid and the rest answer
# msgid_plural. Checking every form against msgid would reject correct
# work wherever the two carry different placeholders.
source = entry.msgid if i == 0 else entry.msgid_plural
bad = gate(source, form, None, locale, dnt)
if bad:
problems.append((msgid, [f'[{i}] {reason}' for reason in bad]))
elif entry.plurals.get(i) != form:
planned.append((entry, i, form))
if problems:
report(problems, len(fixes), locale)
return None
def write():
for entry, i, form in planned:
entry.set_plural(i, form)
entry.remove_flags('auto-english-fallback', 'fuzzy')
return len(planned)
return fixes, write
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('locale')
parser.add_argument('--dir', default='locale-review',
help='directory holding the OUT_<locale>_*.json files')
parser.add_argument('--plurals', action='store_true',
help='apply msgstr[n] forms instead of msgstr')
args = parser.parse_args()
path = poutil.po_path(args.locale)
entries = poutil.parse(path)
handler = apply_plurals if args.plurals else apply_msgstr
result = handler(args.locale, path, entries, os.path.expanduser(args.dir))
if result is None:
return 1
fixes, write = result
# Snapshot immediately before writing rather than diffing HEAD: the working
# tree usually already carries an earlier pass, and diffing HEAD reports those
# as unexpected drift - after the write has already landed.
before = {e.msgid: e for e in poutil.parse(path)
if not e.obsolete and not e.is_header}
applied = write()
poutil.write(path, entries)
print(f'{args.locale}: applied {applied} from {len(fixes)} corrections')
after = {e.msgid: e for e in poutil.parse(path)
if not e.obsolete and not e.is_header}
if set(before) != set(after):
print('FAIL: msgid set drifted')
return 1
if args.plurals:
changed = {m for m in after if before[m].plurals != after[m].plurals}
spilled = [m for m in after if before[m].msgstr != after[m].msgstr]
label = 'msgstr touched'
else:
changed = {m for m in after if before[m].msgstr != after[m].msgstr}
spilled = [m for m in after if before[m].plurals != after[m].plurals
or before[m].msgid_plural != after[m].msgid_plural]
label = 'plural forms touched'
unexpected = changed - set(fixes)
print(f'verify: changed={len(changed)} unexpected={len(unexpected)} '
f'{label}={len(spilled)}')
if unexpected or spilled:
for msgid in (sorted(unexpected) + spilled)[:10]:
print(f' UNEXPECTED {msgid[:70]!r}')
return 1
return 0
if __name__ == '__main__':
sys.exit(main())