Skip to content

Commit e2c7057

Browse files
authored
[3.9] Add Last-Translators to TX pull commit message (#102)
Backport of #88 to 3.9 branch. Co-authored-by: Stan Ulbrych
1 parent d5cc2d8 commit e2c7057

File tree

1 file changed

+60
-3
lines changed

1 file changed

+60
-3
lines changed

manage_translation.py

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,21 @@
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 urllib.parse import unquote
2425
from warnings import warn
2526

27+
from polib import pofile, POFile
28+
2629
LANGUAGE = 'pl'
2730

2831

@@ -273,9 +276,63 @@ def average(averages, weights):
273276
'''
274277
)
275278

279+
def generate_commit_msg():
280+
"""Generate a commit message
281+
Parses staged files and generates a commit message with Last-Translator's as
282+
co-authors.
283+
"""
284+
translators: set[str] = set()
276285

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

280337
parser = ArgumentParser()
281338
parser.add_argument('cmd', nargs=1, choices=RUNNABLE_SCRIPTS)

0 commit comments

Comments
 (0)