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