Skip to content

Commit e299dae

Browse files
authored
[3.11] Add Last-Translators to TX pull commit message (#100)
Backport of #88 to 3.11 branch.
1 parent 4439c0a commit e299dae

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
@@ -10,6 +10,7 @@
1010
# * fetch: fetch translations from transifex.com and strip source lines from the
1111
# files.
1212
# * recreate_tx_config: recreate configuration for all resources.
13+
# * generate_commit_msg: generates commit message with co-authors
1314

1415
from argparse import ArgumentParser
1516
import os
@@ -18,13 +19,13 @@
1819
from difflib import SequenceMatcher
1920
from itertools import combinations
2021
from pathlib import Path
21-
from subprocess import call
22+
from subprocess import call, run, CalledProcessError
2223
import sys
2324
from tempfile import TemporaryDirectory
2425
from typing import Self, Generator, Iterable
2526
from warnings import warn
2627

27-
from polib import pofile
28+
from polib import pofile, POFile
2829
from transifex.api import transifex_api
2930

3031
LANGUAGE = 'pl'
@@ -190,8 +191,62 @@ def language_switcher(entry: ResourceLanguageStatistics) -> bool:
190191
return any(entry.name.startswith(prefix) for prefix in language_switcher_resources_prefixes)
191192

192193

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

196251
parser = ArgumentParser()
197252
parser.add_argument('cmd', choices=RUNNABLE_SCRIPTS)

0 commit comments

Comments
 (0)