Skip to content

Commit be14c27

Browse files
authored
[3.10] Add Last-Translators to TX pull commit message (#101)
Backport of #88 to 3.10 branch. Co-authored-by: Stan Ulbrych
1 parent 72df25b commit be14c27

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

manage_translation.py

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,22 @@
1111
# files.
1212
# * recreate_readme: recreate readme to update translation progress.
1313
# * regenerate_tx_config: recreate configuration for all resources.
14+
# * generate_commit_msg: generates commit message with co-authors
1415

1516
from argparse import ArgumentParser
1617
from collections import Counter
1718
import os
1819
from dataclasses import dataclass
1920
from pathlib import Path
2021
from re import match, search
21-
from subprocess import call, run
22+
from subprocess import call, run, CalledProcessError
2223
import sys
2324
from typing import Self
2425
from urllib.parse import unquote
2526
from warnings import warn
2627

28+
from polib import pofile, POFile
29+
2730
LANGUAGE = 'pl'
2831

2932

@@ -271,8 +274,62 @@ def average(averages, weights):
271274
)
272275

273276

277+
def generate_commit_msg():
278+
"""Generate a commit message
279+
Parses staged files and generates a commit message with Last-Translator's as
280+
co-authors.
281+
"""
282+
translators: set[str] = set()
283+
284+
result = run(
285+
['git', 'diff', '--cached', '--name-only', '--diff-filter=ACM'],
286+
capture_output=True,
287+
text=True,
288+
check=True,
289+
)
290+
staged = [
291+
filename for filename in result.stdout.splitlines() if filename.endswith('.po')
292+
]
293+
294+
for file in staged:
295+
staged_file = run(
296+
['git', 'show', f':{file}'], capture_output=True, text=True, check=True
297+
).stdout
298+
try:
299+
old_file = run(
300+
['git', 'show', f'HEAD:{file}'],
301+
capture_output=True,
302+
text=True,
303+
check=True,
304+
).stdout
305+
except CalledProcessError:
306+
old_file = ''
307+
308+
new_po = pofile(staged_file)
309+
old_po = pofile(old_file) if old_file else POFile()
310+
old_entries = {entry.msgid: entry.msgstr for entry in old_po}
311+
312+
for entry in new_po:
313+
if entry.msgstr and (
314+
entry.msgid not in old_entries
315+
or old_entries[entry.msgid] != entry.msgstr
316+
):
317+
translator = new_po.metadata.get('Last-Translator')
318+
translator = translator.split(',')[0].strip()
319+
if translator:
320+
translators.add(f'Co-Authored-By: {translator}')
321+
break
322+
323+
print('Update translation from Transifex\n\n' + '\n'.join(translators))
324+
325+
274326
if __name__ == "__main__":
275-
RUNNABLE_SCRIPTS = ('fetch', 'recreate_tx_config', 'recreate_readme', 'warn_about_files_to_delete')
327+
RUNNABLE_SCRIPTS = (
328+
'fetch',
329+
'recreate_tx_config',
330+
'recreate_readme', 'warn_about_files_to_delete',
331+
'generate_commit_msg',
332+
)
276333

277334
parser = ArgumentParser()
278335
parser.add_argument('cmd', choices=RUNNABLE_SCRIPTS)

0 commit comments

Comments
 (0)