| 1 |
|
| 2 |
|
| 3 |
import re |
| 4 |
import os |
| 5 |
import shutil |
| 6 |
import subprocess |
| 7 |
import sys |
| 8 |
import urllib.request |
| 9 |
import zipfile |
| 10 |
import core_option_translation as t |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
if __name__ == '__main__': |
| 15 |
|
| 16 |
try: |
| 17 |
API_KEY = sys.argv[1] |
| 18 |
CORE_NAME = t.clean_file_name(sys.argv[2]) |
| 19 |
except IndexError as e: |
| 20 |
print('Please provide Crowdin API Token and core name!') |
| 21 |
raise e |
| 22 |
|
| 23 |
DIR_PATH = t.os.path.dirname(t.os.path.realpath(__file__)) |
| 24 |
YAML_PATH = t.os.path.join(DIR_PATH, 'crowdin.yaml') |
| 25 |
|
| 26 |
|
| 27 |
with open(YAML_PATH, 'r') as crowdin_config_file: |
| 28 |
crowdin_config = crowdin_config_file.read() |
| 29 |
crowdin_config = re.sub(r'"api_token": "_secret_"', |
| 30 |
f'"api_token": "{API_KEY}"', |
| 31 |
crowdin_config, 1) |
| 32 |
crowdin_config = re.sub(r'/_core_name_', |
| 33 |
f'/{CORE_NAME}' |
| 34 |
, crowdin_config) |
| 35 |
with open(YAML_PATH, 'w') as crowdin_config_file: |
| 36 |
crowdin_config_file.write(crowdin_config) |
| 37 |
|
| 38 |
try: |
| 39 |
|
| 40 |
jar_name = 'crowdin-cli.jar' |
| 41 |
jar_path = t.os.path.join(DIR_PATH, jar_name) |
| 42 |
crowdin_cli_file = 'crowdin-cli.zip' |
| 43 |
crowdin_cli_url = 'https://downloads.crowdin.com/cli/v3/' + crowdin_cli_file |
| 44 |
crowdin_cli_path = t.os.path.join(DIR_PATH, crowdin_cli_file) |
| 45 |
|
| 46 |
if not os.path.isfile(t.os.path.join(DIR_PATH, jar_name)): |
| 47 |
print('download crowdin-cli.jar') |
| 48 |
urllib.request.urlretrieve(crowdin_cli_url, crowdin_cli_path) |
| 49 |
with zipfile.ZipFile(crowdin_cli_path, 'r') as zip_ref: |
| 50 |
jar_dir = t.os.path.join(DIR_PATH, zip_ref.namelist()[0]) |
| 51 |
for file in zip_ref.namelist(): |
| 52 |
if file.endswith(jar_name): |
| 53 |
jar_file = file |
| 54 |
break |
| 55 |
zip_ref.extract(jar_file, path=DIR_PATH) |
| 56 |
os.rename(t.os.path.join(DIR_PATH, jar_file), jar_path) |
| 57 |
os.remove(crowdin_cli_path) |
| 58 |
shutil.rmtree(jar_dir) |
| 59 |
|
| 60 |
print('upload source *.json') |
| 61 |
subprocess.run(['java', '-jar', jar_path, 'upload', 'sources', '--config', YAML_PATH]) |
| 62 |
|
| 63 |
|
| 64 |
with open(YAML_PATH, 'r') as crowdin_config_file: |
| 65 |
crowdin_config = crowdin_config_file.read() |
| 66 |
crowdin_config = re.sub(r'"api_token": ".*?"', |
| 67 |
'"api_token": "_secret_"', |
| 68 |
crowdin_config, 1) |
| 69 |
|
| 70 |
|
| 71 |
crowdin_config = re.sub(re.escape(f'/{CORE_NAME}'), |
| 72 |
'/_core_name_', |
| 73 |
crowdin_config) |
| 74 |
|
| 75 |
with open(YAML_PATH, 'w') as crowdin_config_file: |
| 76 |
crowdin_config_file.write(crowdin_config) |
| 77 |
|
| 78 |
except Exception as e: |
| 79 |
|
| 80 |
with open(YAML_PATH, 'r') as crowdin_config_file: |
| 81 |
crowdin_config = crowdin_config_file.read() |
| 82 |
crowdin_config = re.sub(r'"api_token": ".*?"', |
| 83 |
'"api_token": "_secret_"', |
| 84 |
crowdin_config, 1) |
| 85 |
|
| 86 |
|
| 87 |
crowdin_config = re.sub(re.escape(f'/{CORE_NAME}'), |
| 88 |
'/_core_name_', |
| 89 |
crowdin_config) |
| 90 |
|
| 91 |
with open(YAML_PATH, 'w') as crowdin_config_file: |
| 92 |
crowdin_config_file.write(crowdin_config) |
| 93 |
raise e |