|
11 | 11 | # files.
|
12 | 12 | # * recreate_tx_config: recreate configuration for all resources.
|
13 | 13 | # * warn_about_files_to_delete: lists files that are not available upstream
|
| 14 | +# * generate_commit_msg: generates commit message with co-authors |
14 | 15 |
|
15 | 16 | from argparse import ArgumentParser
|
16 | 17 | import os
|
|
19 | 20 | from difflib import SequenceMatcher
|
20 | 21 | from logging import info
|
21 | 22 | from pathlib import Path
|
22 |
| -from subprocess import call |
| 23 | +from subprocess import call, run, CalledProcessError |
23 | 24 | import sys
|
24 | 25 | from tempfile import TemporaryDirectory
|
25 | 26 | from typing import Self, Generator, Iterable
|
26 | 27 | from warnings import warn
|
27 | 28 |
|
28 |
| -from polib import pofile |
| 29 | +from polib import pofile, POFile |
29 | 30 | from transifex.api import transifex_api
|
30 | 31 |
|
31 | 32 | LANGUAGE = 'pl'
|
@@ -202,8 +203,62 @@ def language_switcher(entry: ResourceLanguageStatistics) -> bool:
|
202 | 203 | )
|
203 | 204 |
|
204 | 205 |
|
| 206 | +def generate_commit_msg(): |
| 207 | + """Generate a commit message |
| 208 | + Parses staged files and generates a commit message with Last-Translator's as |
| 209 | + co-authors. |
| 210 | + """ |
| 211 | + translators: set[str] = set() |
| 212 | + |
| 213 | + result = run( |
| 214 | + ['git', 'diff', '--cached', '--name-only', '--diff-filter=ACM'], |
| 215 | + capture_output=True, |
| 216 | + text=True, |
| 217 | + check=True, |
| 218 | + ) |
| 219 | + staged = [ |
| 220 | + filename for filename in result.stdout.splitlines() if filename.endswith('.po') |
| 221 | + ] |
| 222 | + |
| 223 | + for file in staged: |
| 224 | + staged_file = run( |
| 225 | + ['git', 'show', f':{file}'], capture_output=True, text=True, check=True |
| 226 | + ).stdout |
| 227 | + try: |
| 228 | + old_file = run( |
| 229 | + ['git', 'show', f'HEAD:{file}'], |
| 230 | + capture_output=True, |
| 231 | + text=True, |
| 232 | + check=True, |
| 233 | + ).stdout |
| 234 | + except CalledProcessError: |
| 235 | + old_file = '' |
| 236 | + |
| 237 | + new_po = pofile(staged_file) |
| 238 | + old_po = pofile(old_file) if old_file else POFile() |
| 239 | + old_entries = {entry.msgid: entry.msgstr for entry in old_po} |
| 240 | + |
| 241 | + for entry in new_po: |
| 242 | + if entry.msgstr and ( |
| 243 | + entry.msgid not in old_entries |
| 244 | + or old_entries[entry.msgid] != entry.msgstr |
| 245 | + ): |
| 246 | + translator = new_po.metadata.get('Last-Translator') |
| 247 | + translator = translator.split(',')[0].strip() |
| 248 | + if translator: |
| 249 | + translators.add(f'Co-Authored-By: {translator}') |
| 250 | + break |
| 251 | + |
| 252 | + print('Update translation from Transifex\n\n' + '\n'.join(translators)) |
| 253 | + |
| 254 | + |
205 | 255 | if __name__ == '__main__':
|
206 |
| - RUNNABLE_SCRIPTS = ('fetch', 'recreate_tx_config', 'warn_about_files_to_delete') |
| 256 | + RUNNABLE_SCRIPTS = ( |
| 257 | + 'fetch', |
| 258 | + 'recreate_tx_config', |
| 259 | + 'warn_about_files_to_delete', |
| 260 | + 'generate_commit_msg', |
| 261 | + ) |
207 | 262 |
|
208 | 263 | parser = ArgumentParser()
|
209 | 264 | parser.add_argument('cmd', choices=RUNNABLE_SCRIPTS)
|
|
0 commit comments