|
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 | fromargparseimportArgumentParser |
16 | 17 | importos |
|
19 | 20 | fromdifflibimportSequenceMatcher |
20 | 21 | fromloggingimportinfo |
21 | 22 | frompathlibimportPath |
22 | | -fromsubprocessimportcall |
| 23 | +fromsubprocessimportcall,run,CalledProcessError |
23 | 24 | importsys |
24 | 25 | fromtempfileimportTemporaryDirectory |
25 | 26 | fromtypingimportSelf,Generator,Iterable |
26 | 27 | fromwarningsimportwarn |
27 | 28 |
|
28 | | -frompolibimportpofile |
| 29 | +frompolibimportpofile,POFile |
29 | 30 | fromtransifex.apiimporttransifex_api |
30 | 31 |
|
31 | 32 | LANGUAGE='pl' |
@@ -202,8 +203,62 @@ def language_switcher(entry: ResourceLanguageStatistics) -> bool: |
202 | 203 | ) |
203 | 204 |
|
204 | 205 |
|
| 206 | +defgenerate_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 | +filenameforfilenameinresult.stdout.splitlines()iffilename.endswith('.po') |
| 221 | + ] |
| 222 | + |
| 223 | +forfileinstaged: |
| 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 | +exceptCalledProcessError: |
| 235 | +old_file='' |
| 236 | + |
| 237 | +new_po=pofile(staged_file) |
| 238 | +old_po=pofile(old_file)ifold_fileelsePOFile() |
| 239 | +old_entries= {entry.msgid:entry.msgstrforentryinold_po} |
| 240 | + |
| 241 | +forentryinnew_po: |
| 242 | +ifentry.msgstrand ( |
| 243 | +entry.msgidnotinold_entries |
| 244 | +orold_entries[entry.msgid]!=entry.msgstr |
| 245 | + ): |
| 246 | +translator=new_po.metadata.get('Last-Translator') |
| 247 | +translator=translator.split(',')[0].strip() |
| 248 | +iftranslator: |
| 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) |
|