|
11 | 11 | # files.
|
12 | 12 | # * recreate_readme: recreate readme to update translation progress.
|
13 | 13 | # * regenerate_tx_config: recreate configuration for all resources.
|
| 14 | +# * generate_commit_msg: generates commit message with co-authors |
14 | 15 |
|
15 | 16 | from argparse import ArgumentParser
|
16 | 17 | from collections import Counter
|
17 | 18 | import os
|
18 | 19 | from dataclasses import dataclass
|
19 | 20 | from pathlib import Path
|
20 | 21 | from re import match, search
|
21 |
| -from subprocess import call, run |
| 22 | +from subprocess import call, run, CalledProcessError |
22 | 23 | import sys
|
23 | 24 | from urllib.parse import unquote
|
24 | 25 | from warnings import warn
|
25 | 26 |
|
| 27 | +from polib import pofile, POFile |
| 28 | + |
26 | 29 | LANGUAGE = 'pl'
|
27 | 30 |
|
28 | 31 |
|
@@ -273,9 +276,63 @@ def average(averages, weights):
|
273 | 276 | '''
|
274 | 277 | )
|
275 | 278 |
|
| 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() |
276 | 285 |
|
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 | + ) |
279 | 336 |
|
280 | 337 | parser = ArgumentParser()
|
281 | 338 | parser.add_argument('cmd', nargs=1, choices=RUNNABLE_SCRIPTS)
|
|
0 commit comments