|
10 | 10 | # * fetch: fetch translations from transifex.com and strip source lines from the |
11 | 11 | # files. |
12 | 12 | # * recreate_tx_config: recreate configuration for all resources. |
| 13 | +# * generate_commit_msg: generates commit message with co-authors |
13 | 14 |
|
14 | 15 | fromargparseimportArgumentParser |
15 | 16 | importos |
|
18 | 19 | fromdifflibimportSequenceMatcher |
19 | 20 | fromitertoolsimportcombinations |
20 | 21 | frompathlibimportPath |
21 | | -fromsubprocessimportcall |
| 22 | +fromsubprocessimportcall,run,CalledProcessError |
22 | 23 | importsys |
23 | 24 | fromtempfileimportTemporaryDirectory |
24 | 25 | fromtypingimportSelf,Generator,Iterable |
25 | 26 | fromwarningsimportwarn |
26 | 27 |
|
27 | | -frompolibimportpofile |
| 28 | +frompolibimportpofile,POFile |
28 | 29 | fromtransifex.apiimporttransifex_api |
29 | 30 |
|
30 | 31 | LANGUAGE='pl' |
@@ -190,8 +191,62 @@ def language_switcher(entry: ResourceLanguageStatistics) -> bool: |
190 | 191 | returnany(entry.name.startswith(prefix)forprefixinlanguage_switcher_resources_prefixes) |
191 | 192 |
|
192 | 193 |
|
| 194 | +defgenerate_commit_msg(): |
| 195 | +"""Generate a commit message |
| 196 | + Parses staged files and generates a commit message with Last-Translator's as |
| 197 | + co-authors. |
| 198 | + """ |
| 199 | +translators:set[str]=set() |
| 200 | + |
| 201 | +result=run( |
| 202 | + ['git','diff','--cached','--name-only','--diff-filter=ACM'], |
| 203 | +capture_output=True, |
| 204 | +text=True, |
| 205 | +check=True, |
| 206 | + ) |
| 207 | +staged= [ |
| 208 | +filenameforfilenameinresult.stdout.splitlines()iffilename.endswith('.po') |
| 209 | + ] |
| 210 | + |
| 211 | +forfileinstaged: |
| 212 | +staged_file=run( |
| 213 | + ['git','show',f':{file}'],capture_output=True,text=True,check=True |
| 214 | + ).stdout |
| 215 | +try: |
| 216 | +old_file=run( |
| 217 | + ['git','show',f'HEAD:{file}'], |
| 218 | +capture_output=True, |
| 219 | +text=True, |
| 220 | +check=True, |
| 221 | + ).stdout |
| 222 | +exceptCalledProcessError: |
| 223 | +old_file='' |
| 224 | + |
| 225 | +new_po=pofile(staged_file) |
| 226 | +old_po=pofile(old_file)ifold_fileelsePOFile() |
| 227 | +old_entries= {entry.msgid:entry.msgstrforentryinold_po} |
| 228 | + |
| 229 | +forentryinnew_po: |
| 230 | +ifentry.msgstrand ( |
| 231 | +entry.msgidnotinold_entries |
| 232 | +orold_entries[entry.msgid]!=entry.msgstr |
| 233 | + ): |
| 234 | +translator=new_po.metadata.get('Last-Translator') |
| 235 | +translator=translator.split(',')[0].strip() |
| 236 | +iftranslator: |
| 237 | +translators.add(f'Co-Authored-By:{translator}') |
| 238 | +break |
| 239 | + |
| 240 | +print('Update translation from Transifex\n\n'+'\n'.join(translators)) |
| 241 | + |
| 242 | + |
193 | 243 | if__name__=="__main__": |
194 | | -RUNNABLE_SCRIPTS= ('fetch','recreate_tx_config','warn_about_files_to_delete') |
| 244 | +RUNNABLE_SCRIPTS= ( |
| 245 | +'fetch', |
| 246 | +'recreate_tx_config', |
| 247 | +'warn_about_files_to_delete', |
| 248 | +'generate_commit_msg', |
| 249 | + ) |
195 | 250 |
|
196 | 251 | parser=ArgumentParser() |
197 | 252 | parser.add_argument('cmd',choices=RUNNABLE_SCRIPTS) |
|