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