Skip to content

Commit ed359df

Browse files
committed
[3.13] Add Last-Translators to TX pull commit message (#88)
1 parent 41efc04 commit ed359df

File tree

1 file changed

+58
-3
lines changed

1 file changed

+58
-3
lines changed

manage_translation.py

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# files.
1212
# * recreate_tx_config: recreate configuration for all resources.
1313
# * warn_about_files_to_delete: lists files that are not available upstream
14+
# * generate_commit_msg: generates commit message with co-authors
1415

1516
from argparse import ArgumentParser
1617
import os
@@ -19,13 +20,13 @@
1920
from difflib import SequenceMatcher
2021
from logging import info
2122
from pathlib import Path
22-
from subprocess import call
23+
from subprocess import call, run, CalledProcessError
2324
import sys
2425
from tempfile import TemporaryDirectory
2526
from typing import Self, Generator, Iterable
2627
from warnings import warn
2728

28-
from polib import pofile
29+
from polib import pofile, POFile
2930
from transifex.api import transifex_api
3031

3132
LANGUAGE = 'pl'
@@ -187,8 +188,62 @@ def language_switcher(entry: ResourceLanguageStatistics) -> bool:
187188
return any(entry.name.startswith(prefix) for prefix in language_switcher_resources_prefixes)
188189

189190

191+
def generate_commit_msg():
192+
"""Generate a commit message
193+
Parses staged files and generates a commit message with Last-Translator's as
194+
co-authors.
195+
"""
196+
translators: set[str] = set()
197+
198+
result = run(
199+
['git', 'diff', '--cached', '--name-only', '--diff-filter=ACM'],
200+
capture_output=True,
201+
text=True,
202+
check=True,
203+
)
204+
staged = [
205+
filename for filename in result.stdout.splitlines() if filename.endswith('.po')
206+
]
207+
208+
for file in staged:
209+
staged_file = run(
210+
['git', 'show', f':{file}'], capture_output=True, text=True, check=True
211+
).stdout
212+
try:
213+
old_file = run(
214+
['git', 'show', f'HEAD:{file}'],
215+
capture_output=True,
216+
text=True,
217+
check=True,
218+
).stdout
219+
except CalledProcessError:
220+
old_file = ''
221+
222+
new_po = pofile(staged_file)
223+
old_po = pofile(old_file) if old_file else POFile()
224+
old_entries = {entry.msgid: entry.msgstr for entry in old_po}
225+
226+
for entry in new_po:
227+
if entry.msgstr and (
228+
entry.msgid not in old_entries
229+
or old_entries[entry.msgid] != entry.msgstr
230+
):
231+
translator = new_po.metadata.get('Last-Translator')
232+
translator = translator.split(',')[0].strip()
233+
if translator:
234+
translators.add(f'Co-Authored-By: {translator}')
235+
break
236+
237+
print('Update translation from Transifex\n\n' + '\n'.join(translators))
238+
239+
190240
if __name__ == "__main__":
191-
RUNNABLE_SCRIPTS = ('fetch', 'recreate_tx_config', 'warn_about_files_to_delete')
241+
RUNNABLE_SCRIPTS = (
242+
'fetch',
243+
'recreate_tx_config',
244+
'warn_about_files_to_delete',
245+
'generate_commit_msg',
246+
)
192247

193248
parser = ArgumentParser()
194249
parser.add_argument('cmd', choices=RUNNABLE_SCRIPTS)

0 commit comments

Comments
 (0)