Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 1 | #!/usr/bin/env python |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
Michael Bestas | 1ab959b | 2014-07-26 16:01:01 +0300 | [diff] [blame] | 3 | # crowdin_sync.py |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 4 | # |
| 5 | # Updates Crowdin source translations and pushes translations |
Abhisek Devkota | b78def4 | 2016-12-27 13:06:52 -0800 | [diff] [blame] | 6 | # directly to LineageOS' Gerrit. |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 7 | # |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 8 | # Copyright (C) 2014-2016 The CyanogenMod Project |
Michael W | 1bb2f92 | 2019-02-27 17:46:28 +0100 | [diff] [blame] | 9 | # Copyright (C) 2017-2019 The LineageOS Project |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 10 | # |
| 11 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 12 | # you may not use this file except in compliance with the License. |
| 13 | # You may obtain a copy of the License at |
| 14 | # |
| 15 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 16 | # |
| 17 | # Unless required by applicable law or agreed to in writing, software |
| 18 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 19 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 20 | # See the License for the specific language governing permissions and |
| 21 | # limitations under the License. |
| 22 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 23 | # ################################# IMPORTS ################################## # |
| 24 | |
| 25 | from __future__ import print_function |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 26 | |
| 27 | import argparse |
Michael W | 6e0a703 | 2019-02-27 17:04:16 +0100 | [diff] [blame] | 28 | import json |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 29 | import git |
| 30 | import os |
Michael W | bfa2f95 | 2019-03-10 19:53:10 +0100 | [diff] [blame] | 31 | import re |
Michael W | b26b866 | 2019-08-10 23:26:59 +0200 | [diff] [blame] | 32 | import shutil |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 33 | import subprocess |
| 34 | import sys |
Michael W | 1bb2f92 | 2019-02-27 17:46:28 +0100 | [diff] [blame] | 35 | import yaml |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 36 | |
Michael W | 2ae0562 | 2019-02-28 15:27:22 +0100 | [diff] [blame] | 37 | from lxml import etree |
Michael W | e3af0fc | 2020-01-19 12:51:55 +0100 | [diff] [blame] | 38 | from signal import signal, SIGINT |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 39 | |
Anthony King | d0d56cf | 2015-06-05 10:48:38 +0100 | [diff] [blame] | 40 | # ################################# GLOBALS ################################## # |
| 41 | |
| 42 | _DIR = os.path.dirname(os.path.realpath(__file__)) |
Tom Powell | 4425685 | 2016-07-06 15:23:25 -0700 | [diff] [blame] | 43 | _COMMITS_CREATED = False |
Anthony King | d0d56cf | 2015-06-05 10:48:38 +0100 | [diff] [blame] | 44 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 45 | # ################################ FUNCTIONS ################################# # |
| 46 | |
| 47 | |
| 48 | def run_subprocess(cmd, silent=False): |
| 49 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 50 | universal_newlines=True) |
| 51 | comm = p.communicate() |
| 52 | exit_code = p.returncode |
| 53 | if exit_code != 0 and not silent: |
| 54 | print("There was an error running the subprocess.\n" |
| 55 | "cmd: %s\n" |
| 56 | "exit code: %d\n" |
| 57 | "stdout: %s\n" |
| 58 | "stderr: %s" % (cmd, exit_code, comm[0], comm[1]), |
| 59 | file=sys.stderr) |
| 60 | return comm, exit_code |
| 61 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 62 | |
Michael W | 2ae0562 | 2019-02-28 15:27:22 +0100 | [diff] [blame] | 63 | def add_target_paths(config_files, repo, base_path, project_path): |
Michael W | 1bb2f92 | 2019-02-27 17:46:28 +0100 | [diff] [blame] | 64 | # Add or remove the files given in the config files to the commit |
| 65 | count = 0 |
| 66 | file_paths = [] |
| 67 | for f in config_files: |
| 68 | fh = open(f, "r") |
| 69 | try: |
Michael W | 6633d75 | 2020-02-02 13:04:39 +0100 | [diff] [blame] | 70 | config = yaml.safe_load(fh) |
Michael W | 1bb2f92 | 2019-02-27 17:46:28 +0100 | [diff] [blame] | 71 | for tf in config['files']: |
| 72 | if project_path in tf['source']: |
| 73 | target_path = tf['translation'] |
| 74 | lang_codes = tf['languages_mapping']['android_code'] |
| 75 | for l in lang_codes: |
| 76 | lpath = get_target_path(tf['translation'], tf['source'], |
| 77 | lang_codes[l], project_path) |
| 78 | file_paths.append(lpath) |
| 79 | except yaml.YAMLError as e: |
| 80 | print(e, '\n Could not parse YAML.') |
| 81 | exit() |
| 82 | fh.close() |
| 83 | |
Michael W | 2ae0562 | 2019-02-28 15:27:22 +0100 | [diff] [blame] | 84 | # Strip all comments |
| 85 | for f in file_paths: |
Michael W | b26b866 | 2019-08-10 23:26:59 +0200 | [diff] [blame] | 86 | clean_xml_file(base_path, project_path, f, repo) |
Michael W | 2ae0562 | 2019-02-28 15:27:22 +0100 | [diff] [blame] | 87 | |
| 88 | # Modified and untracked files |
| 89 | modified = repo.git.ls_files(m=True, o=True) |
Michael W | 1bb2f92 | 2019-02-27 17:46:28 +0100 | [diff] [blame] | 90 | for m in modified.split('\n'): |
| 91 | if m in file_paths: |
| 92 | repo.git.add(m) |
| 93 | count += 1 |
| 94 | |
| 95 | deleted = repo.git.ls_files(d=True) |
| 96 | for d in deleted.split('\n'): |
| 97 | if d in file_paths: |
| 98 | repo.git.rm(d) |
| 99 | count += 1 |
| 100 | |
| 101 | return count |
| 102 | |
| 103 | |
| 104 | def split_path(path): |
| 105 | # Split the given string to path and filename |
| 106 | if '/' in path: |
| 107 | original_file_name = path[1:][path.rfind("/"):] |
| 108 | original_path = path[:path.rfind("/")] |
| 109 | else: |
| 110 | original_file_name = path |
| 111 | original_path = '' |
| 112 | |
| 113 | return original_path, original_file_name |
| 114 | |
| 115 | |
| 116 | def get_target_path(pattern, source, lang, project_path): |
| 117 | # Make strings like '/%original_path%-%android_code%/%original_file_name%' valid file paths |
| 118 | # based on the source string's path |
| 119 | original_path, original_file_name = split_path(source) |
| 120 | |
| 121 | target_path = pattern #.lstrip('/') |
| 122 | target_path = target_path.replace('%original_path%', original_path) |
| 123 | target_path = target_path.replace('%android_code%', lang) |
| 124 | target_path = target_path.replace('%original_file_name%', original_file_name) |
| 125 | target_path = target_path.replace(project_path, '') |
| 126 | target_path = target_path.lstrip('/') |
| 127 | return target_path |
| 128 | |
| 129 | |
Michael W | b26b866 | 2019-08-10 23:26:59 +0200 | [diff] [blame] | 130 | def clean_xml_file(base_path, project_path, filename, repo): |
Michael W | 2ae0562 | 2019-02-28 15:27:22 +0100 | [diff] [blame] | 131 | path = base_path + '/' + project_path + '/' + filename |
| 132 | |
| 133 | # We don't want to create every file, just work with those already existing |
| 134 | if not os.path.isfile(path): |
| 135 | return |
| 136 | |
| 137 | try: |
| 138 | fh = open(path, 'r+') |
| 139 | except: |
| 140 | print('Something went wrong while opening file %s' % (path)) |
| 141 | return |
| 142 | |
| 143 | XML = fh.read() |
Michael W | b26b866 | 2019-08-10 23:26:59 +0200 | [diff] [blame] | 144 | try: |
| 145 | tree = etree.fromstring(XML) |
| 146 | except etree.XMLSyntaxError as err: |
| 147 | print('%s: XML Error: %s' % (filename, err.error_log)) |
| 148 | filename, ext = os.path.splitext(path) |
| 149 | if ext == '.xml': |
| 150 | reset_file(path, repo) |
| 151 | return |
Michael W | 2ae0562 | 2019-02-28 15:27:22 +0100 | [diff] [blame] | 152 | |
Michael W | b158798 | 2019-06-19 15:29:24 +0200 | [diff] [blame] | 153 | # Remove strings with 'product=*' attribute but no 'product=default' |
| 154 | # This will ensure aapt2 will not throw an error when building these |
| 155 | productStrings = tree.xpath("//string[@product]") |
| 156 | for ps in productStrings: |
| 157 | stringName = ps.get('name') |
| 158 | stringsWithSameName = tree.xpath("//string[@name='{0}']" |
| 159 | .format(stringName)) |
| 160 | |
| 161 | # We want to find strings with product='default' or no product attribute at all |
| 162 | hasProductDefault = False |
| 163 | for string in stringsWithSameName: |
| 164 | product = string.get('product') |
| 165 | if product is None or product == 'default': |
| 166 | hasProductDefault = True |
| 167 | break |
| 168 | |
| 169 | # Every occurance of the string has to be removed when no string with the same name and |
| 170 | # 'product=default' (or no product attribute) was found |
| 171 | if not hasProductDefault: |
| 172 | print("{0}: Found string '{1}' with missing 'product=default' attribute" |
| 173 | .format(path, stringName)) |
| 174 | for string in stringsWithSameName: |
| 175 | tree.remove(string) |
| 176 | productStrings.remove(string) |
| 177 | |
Michael W | 2ae0562 | 2019-02-28 15:27:22 +0100 | [diff] [blame] | 178 | header = '' |
| 179 | comments = tree.xpath('//comment()') |
| 180 | for c in comments: |
| 181 | p = c.getparent() |
| 182 | if p is None: |
| 183 | # Keep all comments in header |
| 184 | header += str(c).replace('\\n', '\n').replace('\\t', '\t') + '\n' |
| 185 | continue |
| 186 | p.remove(c) |
| 187 | |
| 188 | content = '' |
| 189 | |
| 190 | # Take the original xml declaration and prepend it |
| 191 | declaration = XML.split('\n')[0] |
| 192 | if '<?' in declaration: |
| 193 | content = declaration + '\n' |
| 194 | |
| 195 | content += etree.tostring(tree, pretty_print=True, encoding="utf-8", xml_declaration=False) |
| 196 | |
| 197 | if header != '': |
| 198 | content = content.replace('?>\n', '?>\n' + header) |
| 199 | |
| 200 | # Sometimes spaces are added, we don't want them |
| 201 | content = re.sub("[ ]*<\/resources>", "</resources>", content) |
| 202 | |
| 203 | # Overwrite file with content stripped by all comments |
| 204 | fh.seek(0) |
| 205 | fh.write(content) |
| 206 | fh.truncate() |
| 207 | fh.close() |
| 208 | |
| 209 | # Remove files which don't have any translated strings |
Michael W | ed3af93 | 2019-06-19 22:41:09 +0200 | [diff] [blame] | 210 | contentList = list(tree) |
| 211 | if len(contentList) == 0: |
| 212 | print('Removing ' + path) |
| 213 | os.remove(path) |
Michael W | 2ae0562 | 2019-02-28 15:27:22 +0100 | [diff] [blame] | 214 | |
Michael W | b26b866 | 2019-08-10 23:26:59 +0200 | [diff] [blame] | 215 | |
| 216 | # For files we can't process due to errors, create a backup |
| 217 | # and checkout the file to get it back to the previous state |
| 218 | def reset_file(filepath, repo): |
| 219 | backupFile = None |
| 220 | parts = filepath.split("/") |
| 221 | found = False |
| 222 | for s in parts: |
| 223 | curPart = s |
| 224 | if not found and s.startswith("res"): |
| 225 | curPart = s + "_backup" |
| 226 | found = True |
| 227 | if backupFile is None: |
| 228 | backupFile = curPart |
| 229 | else: |
| 230 | backupFile = backupFile + '/' + curPart |
| 231 | |
| 232 | path, filename = os.path.split(backupFile) |
| 233 | if not os.path.exists(path): |
| 234 | os.makedirs(path) |
| 235 | if os.path.exists(backupFile): |
| 236 | i = 1 |
| 237 | while os.path.exists(backupFile + str(i)): |
| 238 | i+=1 |
| 239 | backupFile = backupFile + str(i) |
| 240 | shutil.copy(filepath, backupFile) |
| 241 | repo.git.checkout(filepath) |
| 242 | |
| 243 | |
Michael W | 1bb2f92 | 2019-02-27 17:46:28 +0100 | [diff] [blame] | 244 | def push_as_commit(config_files, base_path, path, name, branch, username): |
Michael W | e3af0fc | 2020-01-19 12:51:55 +0100 | [diff] [blame] | 245 | print('Committing %s on branch %s: ' % (name, branch), end='') |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 246 | |
| 247 | # Get path |
Michael W | 1bb2f92 | 2019-02-27 17:46:28 +0100 | [diff] [blame] | 248 | project_path = path |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 249 | path = os.path.join(base_path, path) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 250 | if not path.endswith('.git'): |
| 251 | path = os.path.join(path, '.git') |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 252 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 253 | # Create repo object |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 254 | repo = git.Repo(path) |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 255 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 256 | # Add all files to commit |
Michael W | 2ae0562 | 2019-02-28 15:27:22 +0100 | [diff] [blame] | 257 | count = add_target_paths(config_files, repo, base_path, project_path) |
Michael W | 1bb2f92 | 2019-02-27 17:46:28 +0100 | [diff] [blame] | 258 | |
| 259 | if count == 0: |
| 260 | print('Nothing to commit') |
| 261 | return |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 262 | |
| 263 | # Create commit; if it fails, probably empty so skipping |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 264 | try: |
Michael Bestas | 80b22ef | 2018-11-14 23:12:33 +0200 | [diff] [blame] | 265 | repo.git.commit(m='Automatic translation import') |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 266 | except: |
Michael W | e3af0fc | 2020-01-19 12:51:55 +0100 | [diff] [blame] | 267 | print('Failed, probably empty: skipping', file=sys.stderr) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 268 | return |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 269 | |
| 270 | # Push commit |
Michael Bestas | f96f67b | 2014-10-21 00:43:37 +0300 | [diff] [blame] | 271 | try: |
Abhisek Devkota | b78def4 | 2016-12-27 13:06:52 -0800 | [diff] [blame] | 272 | repo.git.push('ssh://%s@review.lineageos.org:29418/%s' % (username, name), |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 273 | 'HEAD:refs/for/%s%%topic=translation' % branch) |
Michael W | e3af0fc | 2020-01-19 12:51:55 +0100 | [diff] [blame] | 274 | print('Success') |
Michael Bestas | f96f67b | 2014-10-21 00:43:37 +0300 | [diff] [blame] | 275 | except: |
Michael W | e3af0fc | 2020-01-19 12:51:55 +0100 | [diff] [blame] | 276 | print('Failed', file=sys.stderr) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 277 | |
Tom Powell | 4425685 | 2016-07-06 15:23:25 -0700 | [diff] [blame] | 278 | _COMMITS_CREATED = True |
| 279 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 280 | |
Michael W | d13658a | 2019-01-13 14:05:37 +0100 | [diff] [blame] | 281 | def submit_gerrit(branch, username): |
| 282 | # Find all open translation changes |
| 283 | cmd = ['ssh', '-p', '29418', |
| 284 | '{}@review.lineageos.org'.format(username), |
| 285 | 'gerrit', 'query', |
| 286 | 'status:open', |
| 287 | 'branch:{}'.format(branch), |
| 288 | 'message:"Automatic translation import"', |
| 289 | 'topic:translation', |
Michael W | 6e0a703 | 2019-02-27 17:04:16 +0100 | [diff] [blame] | 290 | '--current-patch-set', |
| 291 | '--format=JSON'] |
| 292 | commits = 0 |
Michael W | d13658a | 2019-01-13 14:05:37 +0100 | [diff] [blame] | 293 | msg, code = run_subprocess(cmd) |
| 294 | if code != 0: |
| 295 | print('Failed: {0}'.format(msg[1])) |
| 296 | return |
| 297 | |
Michael W | 6e0a703 | 2019-02-27 17:04:16 +0100 | [diff] [blame] | 298 | # Each line is one valid JSON object, except the last one, which is empty |
| 299 | for line in msg[0].strip('\n').split('\n'): |
| 300 | js = json.loads(line) |
| 301 | # We get valid JSON, but not every result line is one we want |
| 302 | if not 'currentPatchSet' in js or not 'revision' in js['currentPatchSet']: |
| 303 | continue |
Michael W | d13658a | 2019-01-13 14:05:37 +0100 | [diff] [blame] | 304 | # Add Code-Review +2 and Verified+1 labels and submit |
| 305 | cmd = ['ssh', '-p', '29418', |
| 306 | '{}@review.lineageos.org'.format(username), |
| 307 | 'gerrit', 'review', |
| 308 | '--verified +1', |
| 309 | '--code-review +2', |
Michael W | 6e0a703 | 2019-02-27 17:04:16 +0100 | [diff] [blame] | 310 | '--submit', js['currentPatchSet']['revision']] |
Michael W | d13658a | 2019-01-13 14:05:37 +0100 | [diff] [blame] | 311 | msg, code = run_subprocess(cmd, True) |
Michael W | e3af0fc | 2020-01-19 12:51:55 +0100 | [diff] [blame] | 312 | print('Submitting commit %s: ' % js[url], end='') |
Michael W | d13658a | 2019-01-13 14:05:37 +0100 | [diff] [blame] | 313 | if code != 0: |
| 314 | errorText = msg[1].replace('\n\n', '; ').replace('\n', '') |
Michael W | e3af0fc | 2020-01-19 12:51:55 +0100 | [diff] [blame] | 315 | print('Failed: %s' % errorText) |
Michael W | d13658a | 2019-01-13 14:05:37 +0100 | [diff] [blame] | 316 | else: |
Michael W | e3af0fc | 2020-01-19 12:51:55 +0100 | [diff] [blame] | 317 | print('Success') |
Michael W | 6e0a703 | 2019-02-27 17:04:16 +0100 | [diff] [blame] | 318 | |
| 319 | commits += 1 |
| 320 | |
| 321 | if commits == 0: |
| 322 | print("Nothing to submit!") |
| 323 | return |
Michael W | d13658a | 2019-01-13 14:05:37 +0100 | [diff] [blame] | 324 | |
| 325 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 326 | def check_run(cmd): |
Michael Bestas | 97677e1 | 2015-02-08 13:11:59 +0200 | [diff] [blame] | 327 | p = subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr) |
| 328 | ret = p.wait() |
| 329 | if ret != 0: |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 330 | print('Failed to run cmd: %s' % ' '.join(cmd), file=sys.stderr) |
Michael Bestas | 97677e1 | 2015-02-08 13:11:59 +0200 | [diff] [blame] | 331 | sys.exit(ret) |
| 332 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 333 | |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 334 | def find_xml(base_path): |
| 335 | for dp, dn, file_names in os.walk(base_path): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 336 | for f in file_names: |
| 337 | if os.path.splitext(f)[1] == '.xml': |
| 338 | yield os.path.join(dp, f) |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 339 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 340 | # ############################################################################ # |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 341 | |
Michael Bestas | 6b6db12 | 2015-02-08 13:22:22 +0200 | [diff] [blame] | 342 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 343 | def parse_args(): |
| 344 | parser = argparse.ArgumentParser( |
Abhisek Devkota | b78def4 | 2016-12-27 13:06:52 -0800 | [diff] [blame] | 345 | description="Synchronising LineageOS' translations with Crowdin") |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 346 | parser.add_argument('-u', '--username', help='Gerrit username') |
Abhisek Devkota | b78def4 | 2016-12-27 13:06:52 -0800 | [diff] [blame] | 347 | parser.add_argument('-b', '--branch', help='LineageOS branch', |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 348 | required=True) |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 349 | parser.add_argument('-c', '--config', help='Custom yaml config') |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 350 | parser.add_argument('--upload-sources', action='store_true', |
| 351 | help='Upload sources to Crowdin') |
| 352 | parser.add_argument('--upload-translations', action='store_true', |
| 353 | help='Upload translations to Crowdin') |
| 354 | parser.add_argument('--download', action='store_true', |
| 355 | help='Download translations from Crowdin') |
Michael W | d13658a | 2019-01-13 14:05:37 +0100 | [diff] [blame] | 356 | parser.add_argument('-s', '--submit', action='store_true', |
| 357 | help='Merge open translation commits') |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 358 | return parser.parse_args() |
Michael Bestas | 6b6db12 | 2015-02-08 13:22:22 +0200 | [diff] [blame] | 359 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 360 | # ################################# PREPARE ################################## # |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 361 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 362 | |
| 363 | def check_dependencies(): |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 364 | # Check for Java version of crowdin |
| 365 | cmd = ['dpkg-query', '-W', 'crowdin'] |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 366 | if run_subprocess(cmd, silent=True)[1] != 0: |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 367 | print('You have not installed crowdin.', file=sys.stderr) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 368 | return False |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 369 | return True |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 370 | |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 371 | |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 372 | def load_xml(x): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 373 | try: |
Michael W | da610ba | 2020-03-22 18:38:19 +0100 | [diff] [blame^] | 374 | return etree.parse(x) |
| 375 | except etree.XMLSyntaxError: |
| 376 | print('Malformed %s.' % x, file=sys.stderr) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 377 | return None |
| 378 | except Exception: |
Michael W | da610ba | 2020-03-22 18:38:19 +0100 | [diff] [blame^] | 379 | print('You have no %s.' % x, file=sys.stderr) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 380 | return None |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 381 | |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame] | 382 | |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 383 | def check_files(files): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 384 | for f in files: |
| 385 | if not os.path.isfile(f): |
| 386 | print('You have no %s.' % f, file=sys.stderr) |
| 387 | return False |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 388 | return True |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame] | 389 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 390 | # ################################### MAIN ################################### # |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame] | 391 | |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame] | 392 | |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 393 | def upload_sources_crowdin(branch, config): |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 394 | if config: |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 395 | print('\nUploading sources to Crowdin (custom config)') |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 396 | check_run(['crowdin', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 397 | '--config=%s/config/%s' % (_DIR, config), |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 398 | 'upload', 'sources', '--branch=%s' % branch]) |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 399 | else: |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 400 | print('\nUploading sources to Crowdin (AOSP supported languages)') |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 401 | check_run(['crowdin', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 402 | '--config=%s/config/%s.yaml' % (_DIR, branch), |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 403 | 'upload', 'sources', '--branch=%s' % branch]) |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 404 | |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 405 | print('\nUploading sources to Crowdin (non-AOSP supported languages)') |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 406 | check_run(['crowdin', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 407 | '--config=%s/config/%s_aosp.yaml' % (_DIR, branch), |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 408 | 'upload', 'sources', '--branch=%s' % branch]) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 409 | |
| 410 | |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 411 | def upload_translations_crowdin(branch, config): |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 412 | if config: |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 413 | print('\nUploading translations to Crowdin (custom config)') |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 414 | check_run(['crowdin', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 415 | '--config=%s/config/%s' % (_DIR, config), |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 416 | 'upload', 'translations', '--branch=%s' % branch, |
| 417 | '--no-import-duplicates', '--import-eq-suggestions', |
| 418 | '--auto-approve-imported']) |
| 419 | else: |
| 420 | print('\nUploading translations to Crowdin ' |
| 421 | '(AOSP supported languages)') |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 422 | check_run(['crowdin', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 423 | '--config=%s/config/%s.yaml' % (_DIR, branch), |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 424 | 'upload', 'translations', '--branch=%s' % branch, |
| 425 | '--no-import-duplicates', '--import-eq-suggestions', |
| 426 | '--auto-approve-imported']) |
| 427 | |
| 428 | print('\nUploading translations to Crowdin ' |
| 429 | '(non-AOSP supported languages)') |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 430 | check_run(['crowdin', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 431 | '--config=%s/config/%s_aosp.yaml' % (_DIR, branch), |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 432 | 'upload', 'translations', '--branch=%s' % branch, |
| 433 | '--no-import-duplicates', '--import-eq-suggestions', |
| 434 | '--auto-approve-imported']) |
| 435 | |
| 436 | |
Michael Bestas | 80b22ef | 2018-11-14 23:12:33 +0200 | [diff] [blame] | 437 | def download_crowdin(base_path, branch, xml, username, config): |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 438 | if config: |
| 439 | print('\nDownloading translations from Crowdin (custom config)') |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 440 | check_run(['crowdin', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 441 | '--config=%s/config/%s' % (_DIR, config), |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 442 | 'download', '--branch=%s' % branch]) |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 443 | else: |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 444 | print('\nDownloading translations from Crowdin ' |
| 445 | '(AOSP supported languages)') |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 446 | check_run(['crowdin', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 447 | '--config=%s/config/%s.yaml' % (_DIR, branch), |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 448 | 'download', '--branch=%s' % branch]) |
Michael Bestas | 50579d2 | 2014-08-09 17:49:14 +0300 | [diff] [blame] | 449 | |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 450 | print('\nDownloading translations from Crowdin ' |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 451 | '(non-AOSP supported languages)') |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 452 | check_run(['crowdin', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 453 | '--config=%s/config/%s_aosp.yaml' % (_DIR, branch), |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 454 | 'download', '--branch=%s' % branch]) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 455 | |
Michael Bestas | 99f5fce | 2015-06-04 22:07:51 +0300 | [diff] [blame] | 456 | print('\nCreating a list of pushable translations') |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 457 | # Get all files that Crowdin pushed |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 458 | paths = [] |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 459 | if config: |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 460 | files = ['%s/config/%s' % (_DIR, config)] |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 461 | else: |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 462 | files = ['%s/config/%s.yaml' % (_DIR, branch), |
| 463 | '%s/config/%s_aosp.yaml' % (_DIR, branch)] |
Michael Bestas | 6c327e6 | 2015-05-02 01:58:01 +0300 | [diff] [blame] | 464 | for c in files: |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 465 | cmd = ['crowdin', '--config=%s' % c, 'list', 'project', |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 466 | '--branch=%s' % branch] |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 467 | comm, ret = run_subprocess(cmd) |
| 468 | if ret != 0: |
| 469 | sys.exit(ret) |
| 470 | for p in str(comm[0]).split("\n"): |
| 471 | paths.append(p.replace('/%s' % branch, '')) |
Michael Bestas | 50579d2 | 2014-08-09 17:49:14 +0300 | [diff] [blame] | 472 | |
Michael Bestas | 99f5fce | 2015-06-04 22:07:51 +0300 | [diff] [blame] | 473 | print('\nUploading translations to Gerrit') |
Michael W | da610ba | 2020-03-22 18:38:19 +0100 | [diff] [blame^] | 474 | items = [x for xmlfile in xml for x in xmlfile.findall("//project")] |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 475 | all_projects = [] |
| 476 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 477 | for path in paths: |
| 478 | path = path.strip() |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 479 | if not path: |
| 480 | continue |
| 481 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 482 | if "/res" not in path: |
| 483 | print('WARNING: Cannot determine project root dir of ' |
| 484 | '[%s], skipping.' % path) |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 485 | continue |
Michael W | 67f1493 | 2019-03-11 09:59:32 +0100 | [diff] [blame] | 486 | |
| 487 | # Usually the project root is everything before /res |
| 488 | # but there are special cases where /res is part of the repo name as well |
| 489 | parts = path.split("/res") |
| 490 | if len(parts) == 2: |
| 491 | result = parts[0] |
| 492 | elif len(parts) == 3: |
| 493 | result = parts[0] + '/res' + parts[1] |
| 494 | else: |
| 495 | print('WARNING: Splitting the path not successful for [%s], skipping' % path) |
| 496 | continue |
| 497 | |
| 498 | result = result.strip('/') |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 499 | if result == path.strip('/'): |
| 500 | print('WARNING: Cannot determine project root dir of ' |
| 501 | '[%s], skipping.' % path) |
| 502 | continue |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 503 | |
Michael Bestas | c899b8c | 2015-03-03 00:53:19 +0200 | [diff] [blame] | 504 | if result in all_projects: |
Michael Bestas | c899b8c | 2015-03-03 00:53:19 +0200 | [diff] [blame] | 505 | continue |
Michael Bestas | 50579d2 | 2014-08-09 17:49:14 +0300 | [diff] [blame] | 506 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 507 | # When a project has multiple translatable files, Crowdin will |
| 508 | # give duplicates. |
| 509 | # We don't want that (useless empty commits), so we save each |
| 510 | # project in all_projects and check if it's already in there. |
Michael Bestas | c899b8c | 2015-03-03 00:53:19 +0200 | [diff] [blame] | 511 | all_projects.append(result) |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 512 | |
Michael Bestas | 42e25e3 | 2016-03-12 20:18:39 +0200 | [diff] [blame] | 513 | # Search android/default.xml or config/%(branch)_extra_packages.xml |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 514 | # for the project's name |
Michael W | 1f18776 | 2019-03-11 19:38:00 +0100 | [diff] [blame] | 515 | resultPath = None |
| 516 | resultProject = None |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 517 | for project in items: |
Michael W | da610ba | 2020-03-22 18:38:19 +0100 | [diff] [blame^] | 518 | path = project.get('path') |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 519 | if not (result + '/').startswith(path +'/'): |
Michael Bestas | c899b8c | 2015-03-03 00:53:19 +0200 | [diff] [blame] | 520 | continue |
Michael W | 1f18776 | 2019-03-11 19:38:00 +0100 | [diff] [blame] | 521 | # We want the longest match, so projects in subfolders of other projects are also |
| 522 | # taken into account |
| 523 | if resultPath is None or len(path) > len(resultPath): |
| 524 | resultPath = path |
| 525 | resultProject = project |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 526 | |
Michael W | 1f18776 | 2019-03-11 19:38:00 +0100 | [diff] [blame] | 527 | # Just in case no project was found |
| 528 | if resultPath is None: |
| 529 | continue |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 530 | |
Michael W | 1f18776 | 2019-03-11 19:38:00 +0100 | [diff] [blame] | 531 | if result != resultPath: |
| 532 | if resultPath in all_projects: |
| 533 | continue |
| 534 | result = resultPath |
| 535 | all_projects.append(result) |
| 536 | |
Michael W | da610ba | 2020-03-22 18:38:19 +0100 | [diff] [blame^] | 537 | br = resultProject.get('revision') or branch |
Michael W | 1f18776 | 2019-03-11 19:38:00 +0100 | [diff] [blame] | 538 | |
| 539 | push_as_commit(files, base_path, result, |
Michael W | da610ba | 2020-03-22 18:38:19 +0100 | [diff] [blame^] | 540 | resultProject.get('name'), br, username) |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 541 | |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 542 | |
Michael W | e3af0fc | 2020-01-19 12:51:55 +0100 | [diff] [blame] | 543 | def sig_handler(signal_received, frame): |
| 544 | print('') |
| 545 | print('SIGINT or CTRL-C detected. Exiting gracefully') |
| 546 | exit(0) |
| 547 | |
| 548 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 549 | def main(): |
Michael W | e3af0fc | 2020-01-19 12:51:55 +0100 | [diff] [blame] | 550 | signal(SIGINT, sig_handler) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 551 | args = parse_args() |
| 552 | default_branch = args.branch |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 553 | |
Michael W | d13658a | 2019-01-13 14:05:37 +0100 | [diff] [blame] | 554 | if args.submit: |
| 555 | if args.username is None: |
| 556 | print('Argument -u/--username is required for submitting!') |
| 557 | sys.exit(1) |
| 558 | submit_gerrit(default_branch, args.username) |
| 559 | sys.exit(0) |
| 560 | |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 561 | base_path_branch_suffix = default_branch.replace('-', '_').replace('.', '_').upper() |
| 562 | base_path_env = 'LINEAGE_CROWDIN_BASE_PATH_%s' % base_path_branch_suffix |
| 563 | base_path = os.getenv(base_path_env) |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 564 | if base_path is None: |
Anthony King | d0d56cf | 2015-06-05 10:48:38 +0100 | [diff] [blame] | 565 | cwd = os.getcwd() |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 566 | print('You have not set %s. Defaulting to %s' % (base_path_env, cwd)) |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 567 | base_path = cwd |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 568 | if not os.path.isdir(base_path): |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 569 | print('%s is not a real directory: %s' % (base_path_env, base_path)) |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 570 | sys.exit(1) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 571 | |
Michael Bestas | 99f5fce | 2015-06-04 22:07:51 +0300 | [diff] [blame] | 572 | if not check_dependencies(): |
| 573 | sys.exit(1) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 574 | |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 575 | xml_android = load_xml(x='%s/android/default.xml' % base_path) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 576 | if xml_android is None: |
| 577 | sys.exit(1) |
| 578 | |
Michael Bestas | 42e25e3 | 2016-03-12 20:18:39 +0200 | [diff] [blame] | 579 | xml_extra = load_xml(x='%s/config/%s_extra_packages.xml' |
Anthony King | d0d56cf | 2015-06-05 10:48:38 +0100 | [diff] [blame] | 580 | % (_DIR, default_branch)) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 581 | if xml_extra is None: |
| 582 | sys.exit(1) |
| 583 | |
Michael Bestas | 19dc335 | 2018-02-03 20:24:00 +0200 | [diff] [blame] | 584 | xml_snippet = load_xml(x='%s/android/snippets/lineage.xml' % base_path) |
| 585 | if xml_snippet is None: |
| 586 | xml_snippet = load_xml(x='%s/android/snippets/cm.xml' % base_path) |
| 587 | if xml_snippet is None: |
| 588 | xml_snippet = load_xml(x='%s/android/snippets/hal_cm_all.xml' % base_path) |
| 589 | if xml_snippet is not None: |
| 590 | xml_files = (xml_android, xml_snippet, xml_extra) |
Michael Bestas | 687679f | 2016-12-07 23:20:12 +0200 | [diff] [blame] | 591 | else: |
| 592 | xml_files = (xml_android, xml_extra) |
| 593 | |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 594 | if args.config: |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 595 | files = ['%s/config/%s' % (_DIR, args.config)] |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 596 | else: |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 597 | files = ['%s/config/%s.yaml' % (_DIR, default_branch), |
| 598 | '%s/config/%s_aosp.yaml' % (_DIR, default_branch)] |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 599 | if not check_files(files): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 600 | sys.exit(1) |
| 601 | |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 602 | if args.download and args.username is None: |
| 603 | print('Argument -u/--username is required for translations download') |
| 604 | sys.exit(1) |
| 605 | |
| 606 | if args.upload_sources: |
| 607 | upload_sources_crowdin(default_branch, args.config) |
| 608 | if args.upload_translations: |
| 609 | upload_translations_crowdin(default_branch, args.config) |
| 610 | if args.download: |
Michael Bestas | 687679f | 2016-12-07 23:20:12 +0200 | [diff] [blame] | 611 | download_crowdin(base_path, default_branch, xml_files, |
Michael Bestas | 80b22ef | 2018-11-14 23:12:33 +0200 | [diff] [blame] | 612 | args.username, args.config) |
Tom Powell | 4425685 | 2016-07-06 15:23:25 -0700 | [diff] [blame] | 613 | |
| 614 | if _COMMITS_CREATED: |
| 615 | print('\nDone!') |
| 616 | sys.exit(0) |
Tom Powell | f42586f | 2016-07-11 11:02:54 -0700 | [diff] [blame] | 617 | else: |
Tom Powell | 4425685 | 2016-07-06 15:23:25 -0700 | [diff] [blame] | 618 | print('\nNothing to commit') |
| 619 | sys.exit(-1) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 620 | |
| 621 | if __name__ == '__main__': |
| 622 | main() |