Debian Codegraph @

intl/initial_sync.py

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 import re
4 import os
5 import shutil
6 import subprocess
7 import sys
8 import time
9 import urllib.request
10 import zipfile
11 import core_option_translation as t
12
13 # -------------------- MAIN -------------------- #
14
15 if __name__ == '__main__':
16 # Check Crowdin API Token and core name
17 try:
18 API_KEY = sys.argv[1]
19 CORE_NAME = t.clean_file_name(sys.argv[2])
20 except IndexError as e:
21 print('Please provide Crowdin API Token and core name!')
22 raise e
23
24 DIR_PATH = os.path.dirname(os.path.realpath(__file__))
25 YAML_PATH = os.path.join(DIR_PATH, 'crowdin.yaml')
26
27 # Apply Crowdin API Key
28 with open(YAML_PATH, 'r') as crowdin_config_file:
29 crowdin_config = crowdin_config_file.read()
30 crowdin_config = re.sub(r'"api_token": "_secret_"',
31 f'"api_token": "{API_KEY}"',
32 crowdin_config, 1)
33 crowdin_config = re.sub(r'/_core_name_',
34 f'/{CORE_NAME}'
35 , crowdin_config)
36 with open(YAML_PATH, 'w') as crowdin_config_file:
37 crowdin_config_file.write(crowdin_config)
38
39 try:
40 # Download Crowdin CLI
41 jar_name = 'crowdin-cli.jar'
42 jar_path = os.path.join(DIR_PATH, jar_name)
43 crowdin_cli_file = 'crowdin-cli.zip'
44 crowdin_cli_url = 'https://downloads.crowdin.com/cli/v3/' + crowdin_cli_file
45 crowdin_cli_path = os.path.join(DIR_PATH, crowdin_cli_file)
46
47 if not os.path.isfile(os.path.join(DIR_PATH, jar_name)):
48 print('download crowdin-cli.jar')
49 urllib.request.urlretrieve(crowdin_cli_url, crowdin_cli_path)
50 with zipfile.ZipFile(crowdin_cli_path, 'r') as zip_ref:
51 jar_dir = os.path.join(DIR_PATH, zip_ref.namelist()[0])
52 for file in zip_ref.namelist():
53 if file.endswith(jar_name):
54 jar_file = file
55 break
56 zip_ref.extract(jar_file, path=DIR_PATH)
57 os.rename(os.path.join(DIR_PATH, jar_file), jar_path)
58 os.remove(crowdin_cli_path)
59 shutil.rmtree(jar_dir)
60
61 print('upload source & translations *.json')
62 subprocess.run(['java', '-jar', jar_path, 'upload', 'sources', '--config', YAML_PATH])
63 subprocess.run(['java', '-jar', jar_path, 'upload', 'translations', '--config', YAML_PATH])
64
65 print('wait for crowdin server to process data')
66 time.sleep(10)
67
68 print('download translation *.json')
69 subprocess.run(['java', '-jar', jar_path, 'download', '--config', YAML_PATH])
70
71 # Reset Crowdin API Key
72 with open(YAML_PATH, 'r') as crowdin_config_file:
73 crowdin_config = crowdin_config_file.read()
74 crowdin_config = re.sub(r'"api_token": ".*?"', '"api_token": "_secret_"', crowdin_config, 1)
75
76 # TODO this is NOT safe!
77 crowdin_config = re.sub(re.escape(f'/{CORE_NAME}'),
78 '/_core_name_',
79 crowdin_config)
80
81 with open(YAML_PATH, 'w') as crowdin_config_file:
82 crowdin_config_file.write(crowdin_config)
83
84 with open('intl/upload_workflow.py', 'r') as workflow:
85 workflow_config = workflow.read()
86 workflow_config = workflow_config.replace(
87 "subprocess.run(['python3', 'intl/core_option_translation.py', dir_path, core_name])",
88 "subprocess.run(['python3', 'intl/crowdin_prep.py', dir_path, core_name])"
89 )
90 workflow_config = workflow_config.replace(
91 "subprocess.run(['python3', 'intl/initial_sync.py', api_key, core_name])",
92 "subprocess.run(['python3', 'intl/crowdin_source_upload.py', api_key, core_name])"
93 )
94 with open('intl/upload_workflow.py', 'w') as workflow:
95 workflow.write(workflow_config)
96
97 with open('intl/download_workflow.py', 'r') as workflow:
98 workflow_config = workflow.read()
99 workflow_config = workflow_config.replace(
100 "subprocess.run(['python3', 'intl/core_option_translation.py', dir_path, core_name])",
101 "subprocess.run(['python3', 'intl/crowdin_prep.py', dir_path, core_name])"
102 )
103 workflow_config = workflow_config.replace(
104 "subprocess.run(['python3', 'intl/initial_sync.py', api_key, core_name])",
105 "subprocess.run(['python3', 'intl/crowdin_translation_download.py', api_key, core_name])"
106 )
107 with open('intl/download_workflow.py', 'w') as workflow:
108 workflow.write(workflow_config)
109
110 except Exception as e:
111 # Try really hard to reset Crowdin API Key
112 with open(YAML_PATH, 'r') as crowdin_config_file:
113 crowdin_config = crowdin_config_file.read()
114 crowdin_config = re.sub(r'"api_token": ".*?"',
115 '"api_token": "_secret_"',
116 crowdin_config, 1)
117
118 # TODO this is NOT safe!
119 crowdin_config = re.sub(re.escape(f'/{CORE_NAME}'),
120 '/_core_name_',
121 crowdin_config)
122
123 with open(YAML_PATH, 'w') as crowdin_config_file:
124 crowdin_config_file.write(crowdin_config)
125 raise e