|
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 typing import Self
|
24 | 25 | from urllib.parse import unquote
|
25 | 26 | from warnings import warn
|
26 | 27 |
|
| 28 | +from polib import pofile, POFile |
| 29 | + |
27 | 30 | LANGUAGE = 'pl'
|
28 | 31 |
|
29 | 32 |
|
@@ -271,8 +274,62 @@ def average(averages, weights):
|
271 | 274 | )
|
272 | 275 |
|
273 | 276 |
|
| 277 | +def generate_commit_msg(): |
| 278 | + """Generate a commit message |
| 279 | + Parses staged files and generates a commit message with Last-Translator's as |
| 280 | + co-authors. |
| 281 | + """ |
| 282 | + translators: set[str] = set() |
| 283 | + |
| 284 | + result = run( |
| 285 | + ['git', 'diff', '--cached', '--name-only', '--diff-filter=ACM'], |
| 286 | + capture_output=True, |
| 287 | + text=True, |
| 288 | + check=True, |
| 289 | + ) |
| 290 | + staged = [ |
| 291 | + filename for filename in result.stdout.splitlines() if filename.endswith('.po') |
| 292 | + ] |
| 293 | + |
| 294 | + for file in staged: |
| 295 | + staged_file = run( |
| 296 | + ['git', 'show', f':{file}'], capture_output=True, text=True, check=True |
| 297 | + ).stdout |
| 298 | + try: |
| 299 | + old_file = run( |
| 300 | + ['git', 'show', f'HEAD:{file}'], |
| 301 | + capture_output=True, |
| 302 | + text=True, |
| 303 | + check=True, |
| 304 | + ).stdout |
| 305 | + except CalledProcessError: |
| 306 | + old_file = '' |
| 307 | + |
| 308 | + new_po = pofile(staged_file) |
| 309 | + old_po = pofile(old_file) if old_file else POFile() |
| 310 | + old_entries = {entry.msgid: entry.msgstr for entry in old_po} |
| 311 | + |
| 312 | + for entry in new_po: |
| 313 | + if entry.msgstr and ( |
| 314 | + entry.msgid not in old_entries |
| 315 | + or old_entries[entry.msgid] != entry.msgstr |
| 316 | + ): |
| 317 | + translator = new_po.metadata.get('Last-Translator') |
| 318 | + translator = translator.split(',')[0].strip() |
| 319 | + if translator: |
| 320 | + translators.add(f'Co-Authored-By: {translator}') |
| 321 | + break |
| 322 | + |
| 323 | + print('Update translation from Transifex\n\n' + '\n'.join(translators)) |
| 324 | + |
| 325 | + |
274 | 326 | if __name__ == "__main__":
|
275 |
| - RUNNABLE_SCRIPTS = ('fetch', 'recreate_tx_config', 'recreate_readme', 'warn_about_files_to_delete') |
| 327 | + RUNNABLE_SCRIPTS = ( |
| 328 | + 'fetch', |
| 329 | + 'recreate_tx_config', |
| 330 | + 'recreate_readme', 'warn_about_files_to_delete', |
| 331 | + 'generate_commit_msg', |
| 332 | + ) |
276 | 333 |
|
277 | 334 | parser = ArgumentParser()
|
278 | 335 | parser.add_argument('cmd', choices=RUNNABLE_SCRIPTS)
|
|
0 commit comments