|
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' |
@@ -187,8 +188,62 @@ def language_switcher(entry: ResourceLanguageStatistics) -> bool: |
187 | 188 | returnany(entry.name.startswith(prefix)forprefixinlanguage_switcher_resources_prefixes) |
188 | 189 |
|
189 | 190 |
|
| 191 | +defgenerate_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 | +filenameforfilenameinresult.stdout.splitlines()iffilename.endswith('.po') |
| 206 | + ] |
| 207 | + |
| 208 | +forfileinstaged: |
| 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 | +exceptCalledProcessError: |
| 220 | +old_file='' |
| 221 | + |
| 222 | +new_po=pofile(staged_file) |
| 223 | +old_po=pofile(old_file)ifold_fileelsePOFile() |
| 224 | +old_entries= {entry.msgid:entry.msgstrforentryinold_po} |
| 225 | + |
| 226 | +forentryinnew_po: |
| 227 | +ifentry.msgstrand ( |
| 228 | +entry.msgidnotinold_entries |
| 229 | +orold_entries[entry.msgid]!=entry.msgstr |
| 230 | + ): |
| 231 | +translator=new_po.metadata.get('Last-Translator') |
| 232 | +translator=translator.split(',')[0].strip() |
| 233 | +iftranslator: |
| 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) |
|