Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6fe5fe3

Browse files
committed
[3.12] Add Last-Translators to TX pull commit message
1 parent 31f9930 commit 6fe5fe3

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'
@@ -182,8 +183,62 @@ def language_switcher(entry: ResourceLanguageStatistics) -> bool:
182183
return any(entry.name.startswith(prefix) for prefix in language_switcher_resources_prefixes)
183184

184185

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

188243
parser = ArgumentParser()
189244
parser.add_argument('cmd', choices=RUNNABLE_SCRIPTS)

0 commit comments

Comments
 (0)