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 |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 31 | import subprocess |
| 32 | import sys |
Michael W | 1bb2f92 | 2019-02-27 17:46:28 +0100 | [diff] [blame] | 33 | import yaml |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 34 | |
Michael W | 2ae0562 | 2019-02-28 15:27:22 +0100 | [diff] [blame^] | 35 | from lxml import etree |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 36 | from xml.dom import minidom |
| 37 | |
Anthony King | d0d56cf | 2015-06-05 10:48:38 +0100 | [diff] [blame] | 38 | # ################################# GLOBALS ################################## # |
| 39 | |
| 40 | _DIR = os.path.dirname(os.path.realpath(__file__)) |
Tom Powell | 4425685 | 2016-07-06 15:23:25 -0700 | [diff] [blame] | 41 | _COMMITS_CREATED = False |
Anthony King | d0d56cf | 2015-06-05 10:48:38 +0100 | [diff] [blame] | 42 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 43 | # ################################ FUNCTIONS ################################# # |
| 44 | |
| 45 | |
| 46 | def run_subprocess(cmd, silent=False): |
| 47 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 48 | universal_newlines=True) |
| 49 | comm = p.communicate() |
| 50 | exit_code = p.returncode |
| 51 | if exit_code != 0 and not silent: |
| 52 | print("There was an error running the subprocess.\n" |
| 53 | "cmd: %s\n" |
| 54 | "exit code: %d\n" |
| 55 | "stdout: %s\n" |
| 56 | "stderr: %s" % (cmd, exit_code, comm[0], comm[1]), |
| 57 | file=sys.stderr) |
| 58 | return comm, exit_code |
| 59 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 60 | |
Michael W | 2ae0562 | 2019-02-28 15:27:22 +0100 | [diff] [blame^] | 61 | def add_target_paths(config_files, repo, base_path, project_path): |
Michael W | 1bb2f92 | 2019-02-27 17:46:28 +0100 | [diff] [blame] | 62 | # Add or remove the files given in the config files to the commit |
| 63 | count = 0 |
| 64 | file_paths = [] |
| 65 | for f in config_files: |
| 66 | fh = open(f, "r") |
| 67 | try: |
| 68 | config = yaml.load(fh) |
| 69 | for tf in config['files']: |
| 70 | if project_path in tf['source']: |
| 71 | target_path = tf['translation'] |
| 72 | lang_codes = tf['languages_mapping']['android_code'] |
| 73 | for l in lang_codes: |
| 74 | lpath = get_target_path(tf['translation'], tf['source'], |
| 75 | lang_codes[l], project_path) |
| 76 | file_paths.append(lpath) |
| 77 | except yaml.YAMLError as e: |
| 78 | print(e, '\n Could not parse YAML.') |
| 79 | exit() |
| 80 | fh.close() |
| 81 | |
Michael W | 2ae0562 | 2019-02-28 15:27:22 +0100 | [diff] [blame^] | 82 | # Strip all comments |
| 83 | for f in file_paths: |
| 84 | clean_file(base_path, project_path, f) |
| 85 | |
| 86 | # Modified and untracked files |
| 87 | modified = repo.git.ls_files(m=True, o=True) |
Michael W | 1bb2f92 | 2019-02-27 17:46:28 +0100 | [diff] [blame] | 88 | for m in modified.split('\n'): |
| 89 | if m in file_paths: |
| 90 | repo.git.add(m) |
| 91 | count += 1 |
| 92 | |
| 93 | deleted = repo.git.ls_files(d=True) |
| 94 | for d in deleted.split('\n'): |
| 95 | if d in file_paths: |
| 96 | repo.git.rm(d) |
| 97 | count += 1 |
| 98 | |
| 99 | return count |
| 100 | |
| 101 | |
| 102 | def split_path(path): |
| 103 | # Split the given string to path and filename |
| 104 | if '/' in path: |
| 105 | original_file_name = path[1:][path.rfind("/"):] |
| 106 | original_path = path[:path.rfind("/")] |
| 107 | else: |
| 108 | original_file_name = path |
| 109 | original_path = '' |
| 110 | |
| 111 | return original_path, original_file_name |
| 112 | |
| 113 | |
| 114 | def get_target_path(pattern, source, lang, project_path): |
| 115 | # Make strings like '/%original_path%-%android_code%/%original_file_name%' valid file paths |
| 116 | # based on the source string's path |
| 117 | original_path, original_file_name = split_path(source) |
| 118 | |
| 119 | target_path = pattern #.lstrip('/') |
| 120 | target_path = target_path.replace('%original_path%', original_path) |
| 121 | target_path = target_path.replace('%android_code%', lang) |
| 122 | target_path = target_path.replace('%original_file_name%', original_file_name) |
| 123 | target_path = target_path.replace(project_path, '') |
| 124 | target_path = target_path.lstrip('/') |
| 125 | return target_path |
| 126 | |
| 127 | |
Michael W | 2ae0562 | 2019-02-28 15:27:22 +0100 | [diff] [blame^] | 128 | def clean_file(base_path, project_path, filename): |
| 129 | path = base_path + '/' + project_path + '/' + filename |
| 130 | |
| 131 | # We don't want to create every file, just work with those already existing |
| 132 | if not os.path.isfile(path): |
| 133 | return |
| 134 | |
| 135 | try: |
| 136 | fh = open(path, 'r+') |
| 137 | except: |
| 138 | print('Something went wrong while opening file %s' % (path)) |
| 139 | return |
| 140 | |
| 141 | XML = fh.read() |
| 142 | tree = etree.fromstring(XML) |
| 143 | |
| 144 | header = '' |
| 145 | comments = tree.xpath('//comment()') |
| 146 | for c in comments: |
| 147 | p = c.getparent() |
| 148 | if p is None: |
| 149 | # Keep all comments in header |
| 150 | header += str(c).replace('\\n', '\n').replace('\\t', '\t') + '\n' |
| 151 | continue |
| 152 | p.remove(c) |
| 153 | |
| 154 | content = '' |
| 155 | |
| 156 | # Take the original xml declaration and prepend it |
| 157 | declaration = XML.split('\n')[0] |
| 158 | if '<?' in declaration: |
| 159 | content = declaration + '\n' |
| 160 | |
| 161 | content += etree.tostring(tree, pretty_print=True, encoding="utf-8", xml_declaration=False) |
| 162 | |
| 163 | if header != '': |
| 164 | content = content.replace('?>\n', '?>\n' + header) |
| 165 | |
| 166 | # Sometimes spaces are added, we don't want them |
| 167 | content = re.sub("[ ]*<\/resources>", "</resources>", content) |
| 168 | |
| 169 | # Overwrite file with content stripped by all comments |
| 170 | fh.seek(0) |
| 171 | fh.write(content) |
| 172 | fh.truncate() |
| 173 | fh.close() |
| 174 | |
| 175 | # Remove files which don't have any translated strings |
| 176 | empty_contents = { |
| 177 | '<resources/>', |
| 178 | '<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>', |
| 179 | ('<resources xmlns:android=' |
| 180 | '"http://schemas.android.com/apk/res/android"/>'), |
| 181 | ('<resources xmlns:android="http://schemas.android.com/apk/res/android"' |
| 182 | ' xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>'), |
| 183 | ('<resources xmlns:tools="http://schemas.android.com/tools"' |
| 184 | ' xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>'), |
| 185 | '<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">\n</resources>', |
| 186 | '<resources>\n</resources>' |
| 187 | } |
| 188 | for line in empty_contents: |
| 189 | if line in content: |
| 190 | print('Removing ' + path) |
| 191 | os.remove(path) |
| 192 | break |
| 193 | |
Michael W | 1bb2f92 | 2019-02-27 17:46:28 +0100 | [diff] [blame] | 194 | def push_as_commit(config_files, base_path, path, name, branch, username): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 195 | print('Committing %s on branch %s' % (name, branch)) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 196 | |
| 197 | # Get path |
Michael W | 1bb2f92 | 2019-02-27 17:46:28 +0100 | [diff] [blame] | 198 | project_path = path |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 199 | path = os.path.join(base_path, path) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 200 | if not path.endswith('.git'): |
| 201 | path = os.path.join(path, '.git') |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 202 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 203 | # Create repo object |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 204 | repo = git.Repo(path) |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 205 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 206 | # Add all files to commit |
Michael W | 2ae0562 | 2019-02-28 15:27:22 +0100 | [diff] [blame^] | 207 | count = add_target_paths(config_files, repo, base_path, project_path) |
Michael W | 1bb2f92 | 2019-02-27 17:46:28 +0100 | [diff] [blame] | 208 | |
| 209 | if count == 0: |
| 210 | print('Nothing to commit') |
| 211 | return |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 212 | |
| 213 | # Create commit; if it fails, probably empty so skipping |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 214 | try: |
Michael Bestas | 80b22ef | 2018-11-14 23:12:33 +0200 | [diff] [blame] | 215 | repo.git.commit(m='Automatic translation import') |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 216 | except: |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 217 | print('Failed to create commit for %s, probably empty: skipping' |
| 218 | % name, file=sys.stderr) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 219 | return |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 220 | |
| 221 | # Push commit |
Michael Bestas | f96f67b | 2014-10-21 00:43:37 +0300 | [diff] [blame] | 222 | try: |
Abhisek Devkota | b78def4 | 2016-12-27 13:06:52 -0800 | [diff] [blame] | 223 | repo.git.push('ssh://%s@review.lineageos.org:29418/%s' % (username, name), |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 224 | 'HEAD:refs/for/%s%%topic=translation' % branch) |
| 225 | print('Successfully pushed commit for %s' % name) |
Michael Bestas | f96f67b | 2014-10-21 00:43:37 +0300 | [diff] [blame] | 226 | except: |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 227 | print('Failed to push commit for %s' % name, file=sys.stderr) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 228 | |
Tom Powell | 4425685 | 2016-07-06 15:23:25 -0700 | [diff] [blame] | 229 | _COMMITS_CREATED = True |
| 230 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 231 | |
Michael W | d13658a | 2019-01-13 14:05:37 +0100 | [diff] [blame] | 232 | def submit_gerrit(branch, username): |
| 233 | # Find all open translation changes |
| 234 | cmd = ['ssh', '-p', '29418', |
| 235 | '{}@review.lineageos.org'.format(username), |
| 236 | 'gerrit', 'query', |
| 237 | 'status:open', |
| 238 | 'branch:{}'.format(branch), |
| 239 | 'message:"Automatic translation import"', |
| 240 | 'topic:translation', |
Michael W | 6e0a703 | 2019-02-27 17:04:16 +0100 | [diff] [blame] | 241 | '--current-patch-set', |
| 242 | '--format=JSON'] |
| 243 | commits = 0 |
Michael W | d13658a | 2019-01-13 14:05:37 +0100 | [diff] [blame] | 244 | msg, code = run_subprocess(cmd) |
| 245 | if code != 0: |
| 246 | print('Failed: {0}'.format(msg[1])) |
| 247 | return |
| 248 | |
Michael W | 6e0a703 | 2019-02-27 17:04:16 +0100 | [diff] [blame] | 249 | # Each line is one valid JSON object, except the last one, which is empty |
| 250 | for line in msg[0].strip('\n').split('\n'): |
| 251 | js = json.loads(line) |
| 252 | # We get valid JSON, but not every result line is one we want |
| 253 | if not 'currentPatchSet' in js or not 'revision' in js['currentPatchSet']: |
| 254 | continue |
Michael W | d13658a | 2019-01-13 14:05:37 +0100 | [diff] [blame] | 255 | # Add Code-Review +2 and Verified+1 labels and submit |
| 256 | cmd = ['ssh', '-p', '29418', |
| 257 | '{}@review.lineageos.org'.format(username), |
| 258 | 'gerrit', 'review', |
| 259 | '--verified +1', |
| 260 | '--code-review +2', |
Michael W | 6e0a703 | 2019-02-27 17:04:16 +0100 | [diff] [blame] | 261 | '--submit', js['currentPatchSet']['revision']] |
Michael W | d13658a | 2019-01-13 14:05:37 +0100 | [diff] [blame] | 262 | msg, code = run_subprocess(cmd, True) |
| 263 | if code != 0: |
| 264 | errorText = msg[1].replace('\n\n', '; ').replace('\n', '') |
Michael W | 6e0a703 | 2019-02-27 17:04:16 +0100 | [diff] [blame] | 265 | print('Submitting commit {0} failed: {1}'.format(js['url'], errorText)) |
Michael W | d13658a | 2019-01-13 14:05:37 +0100 | [diff] [blame] | 266 | else: |
Michael W | 6e0a703 | 2019-02-27 17:04:16 +0100 | [diff] [blame] | 267 | print('Success when submitting commit {0}'.format(js['url'])) |
| 268 | |
| 269 | commits += 1 |
| 270 | |
| 271 | if commits == 0: |
| 272 | print("Nothing to submit!") |
| 273 | return |
Michael W | d13658a | 2019-01-13 14:05:37 +0100 | [diff] [blame] | 274 | |
| 275 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 276 | def check_run(cmd): |
Michael Bestas | 97677e1 | 2015-02-08 13:11:59 +0200 | [diff] [blame] | 277 | p = subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr) |
| 278 | ret = p.wait() |
| 279 | if ret != 0: |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 280 | print('Failed to run cmd: %s' % ' '.join(cmd), file=sys.stderr) |
Michael Bestas | 97677e1 | 2015-02-08 13:11:59 +0200 | [diff] [blame] | 281 | sys.exit(ret) |
| 282 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 283 | |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 284 | def find_xml(base_path): |
| 285 | for dp, dn, file_names in os.walk(base_path): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 286 | for f in file_names: |
| 287 | if os.path.splitext(f)[1] == '.xml': |
| 288 | yield os.path.join(dp, f) |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 289 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 290 | # ############################################################################ # |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 291 | |
Michael Bestas | 6b6db12 | 2015-02-08 13:22:22 +0200 | [diff] [blame] | 292 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 293 | def parse_args(): |
| 294 | parser = argparse.ArgumentParser( |
Abhisek Devkota | b78def4 | 2016-12-27 13:06:52 -0800 | [diff] [blame] | 295 | description="Synchronising LineageOS' translations with Crowdin") |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 296 | parser.add_argument('-u', '--username', help='Gerrit username') |
Abhisek Devkota | b78def4 | 2016-12-27 13:06:52 -0800 | [diff] [blame] | 297 | parser.add_argument('-b', '--branch', help='LineageOS branch', |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 298 | required=True) |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 299 | parser.add_argument('-c', '--config', help='Custom yaml config') |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 300 | parser.add_argument('--upload-sources', action='store_true', |
| 301 | help='Upload sources to Crowdin') |
| 302 | parser.add_argument('--upload-translations', action='store_true', |
| 303 | help='Upload translations to Crowdin') |
| 304 | parser.add_argument('--download', action='store_true', |
| 305 | help='Download translations from Crowdin') |
Michael W | d13658a | 2019-01-13 14:05:37 +0100 | [diff] [blame] | 306 | parser.add_argument('-s', '--submit', action='store_true', |
| 307 | help='Merge open translation commits') |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 308 | return parser.parse_args() |
Michael Bestas | 6b6db12 | 2015-02-08 13:22:22 +0200 | [diff] [blame] | 309 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 310 | # ################################# PREPARE ################################## # |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 311 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 312 | |
| 313 | def check_dependencies(): |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 314 | # Check for Java version of crowdin |
| 315 | cmd = ['dpkg-query', '-W', 'crowdin'] |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 316 | if run_subprocess(cmd, silent=True)[1] != 0: |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 317 | print('You have not installed crowdin.', file=sys.stderr) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 318 | return False |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 319 | return True |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 320 | |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 321 | |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 322 | def load_xml(x): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 323 | try: |
| 324 | return minidom.parse(x) |
| 325 | except IOError: |
| 326 | print('You have no %s.' % x, file=sys.stderr) |
| 327 | return None |
| 328 | except Exception: |
| 329 | # TODO: minidom should not be used. |
| 330 | print('Malformed %s.' % x, file=sys.stderr) |
| 331 | return None |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 332 | |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame] | 333 | |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 334 | def check_files(files): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 335 | for f in files: |
| 336 | if not os.path.isfile(f): |
| 337 | print('You have no %s.' % f, file=sys.stderr) |
| 338 | return False |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 339 | return True |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame] | 340 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 341 | # ################################### MAIN ################################### # |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame] | 342 | |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame] | 343 | |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 344 | def upload_sources_crowdin(branch, config): |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 345 | if config: |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 346 | print('\nUploading sources to Crowdin (custom config)') |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 347 | check_run(['crowdin', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 348 | '--config=%s/config/%s' % (_DIR, config), |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 349 | 'upload', 'sources', '--branch=%s' % branch]) |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 350 | else: |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 351 | print('\nUploading sources to Crowdin (AOSP supported languages)') |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 352 | check_run(['crowdin', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 353 | '--config=%s/config/%s.yaml' % (_DIR, branch), |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 354 | 'upload', 'sources', '--branch=%s' % branch]) |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 355 | |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 356 | print('\nUploading sources to Crowdin (non-AOSP supported languages)') |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 357 | check_run(['crowdin', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 358 | '--config=%s/config/%s_aosp.yaml' % (_DIR, branch), |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 359 | 'upload', 'sources', '--branch=%s' % branch]) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 360 | |
| 361 | |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 362 | def upload_translations_crowdin(branch, config): |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 363 | if config: |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 364 | print('\nUploading translations to Crowdin (custom config)') |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 365 | check_run(['crowdin', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 366 | '--config=%s/config/%s' % (_DIR, config), |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 367 | 'upload', 'translations', '--branch=%s' % branch, |
| 368 | '--no-import-duplicates', '--import-eq-suggestions', |
| 369 | '--auto-approve-imported']) |
| 370 | else: |
| 371 | print('\nUploading translations to Crowdin ' |
| 372 | '(AOSP supported languages)') |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 373 | check_run(['crowdin', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 374 | '--config=%s/config/%s.yaml' % (_DIR, branch), |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 375 | 'upload', 'translations', '--branch=%s' % branch, |
| 376 | '--no-import-duplicates', '--import-eq-suggestions', |
| 377 | '--auto-approve-imported']) |
| 378 | |
| 379 | print('\nUploading translations to Crowdin ' |
| 380 | '(non-AOSP supported languages)') |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 381 | check_run(['crowdin', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 382 | '--config=%s/config/%s_aosp.yaml' % (_DIR, branch), |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 383 | 'upload', 'translations', '--branch=%s' % branch, |
| 384 | '--no-import-duplicates', '--import-eq-suggestions', |
| 385 | '--auto-approve-imported']) |
| 386 | |
| 387 | |
Michael Bestas | 80b22ef | 2018-11-14 23:12:33 +0200 | [diff] [blame] | 388 | def download_crowdin(base_path, branch, xml, username, config): |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 389 | if config: |
| 390 | print('\nDownloading translations from Crowdin (custom config)') |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 391 | check_run(['crowdin', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 392 | '--config=%s/config/%s' % (_DIR, config), |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 393 | 'download', '--branch=%s' % branch]) |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 394 | else: |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 395 | print('\nDownloading translations from Crowdin ' |
| 396 | '(AOSP supported languages)') |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 397 | check_run(['crowdin', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 398 | '--config=%s/config/%s.yaml' % (_DIR, branch), |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 399 | 'download', '--branch=%s' % branch]) |
Michael Bestas | 50579d2 | 2014-08-09 17:49:14 +0300 | [diff] [blame] | 400 | |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 401 | print('\nDownloading translations from Crowdin ' |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 402 | '(non-AOSP supported languages)') |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 403 | check_run(['crowdin', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 404 | '--config=%s/config/%s_aosp.yaml' % (_DIR, branch), |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 405 | 'download', '--branch=%s' % branch]) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 406 | |
Michael Bestas | 99f5fce | 2015-06-04 22:07:51 +0300 | [diff] [blame] | 407 | print('\nCreating a list of pushable translations') |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 408 | # Get all files that Crowdin pushed |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 409 | paths = [] |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 410 | if config: |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 411 | files = ['%s/config/%s' % (_DIR, config)] |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 412 | else: |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 413 | files = ['%s/config/%s.yaml' % (_DIR, branch), |
| 414 | '%s/config/%s_aosp.yaml' % (_DIR, branch)] |
Michael Bestas | 6c327e6 | 2015-05-02 01:58:01 +0300 | [diff] [blame] | 415 | for c in files: |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 416 | cmd = ['crowdin', '--config=%s' % c, 'list', 'project', |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 417 | '--branch=%s' % branch] |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 418 | comm, ret = run_subprocess(cmd) |
| 419 | if ret != 0: |
| 420 | sys.exit(ret) |
| 421 | for p in str(comm[0]).split("\n"): |
| 422 | paths.append(p.replace('/%s' % branch, '')) |
Michael Bestas | 50579d2 | 2014-08-09 17:49:14 +0300 | [diff] [blame] | 423 | |
Michael Bestas | 99f5fce | 2015-06-04 22:07:51 +0300 | [diff] [blame] | 424 | print('\nUploading translations to Gerrit') |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 425 | items = [x for sub in xml for x in sub.getElementsByTagName('project')] |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 426 | all_projects = [] |
| 427 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 428 | for path in paths: |
| 429 | path = path.strip() |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 430 | if not path: |
| 431 | continue |
| 432 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 433 | if "/res" not in path: |
| 434 | print('WARNING: Cannot determine project root dir of ' |
| 435 | '[%s], skipping.' % path) |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 436 | continue |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 437 | result = path.split('/res')[0].strip('/') |
| 438 | if result == path.strip('/'): |
| 439 | print('WARNING: Cannot determine project root dir of ' |
| 440 | '[%s], skipping.' % path) |
| 441 | continue |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 442 | |
Michael Bestas | c899b8c | 2015-03-03 00:53:19 +0200 | [diff] [blame] | 443 | if result in all_projects: |
Michael Bestas | c899b8c | 2015-03-03 00:53:19 +0200 | [diff] [blame] | 444 | continue |
Michael Bestas | 50579d2 | 2014-08-09 17:49:14 +0300 | [diff] [blame] | 445 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 446 | # When a project has multiple translatable files, Crowdin will |
| 447 | # give duplicates. |
| 448 | # We don't want that (useless empty commits), so we save each |
| 449 | # project in all_projects and check if it's already in there. |
Michael Bestas | c899b8c | 2015-03-03 00:53:19 +0200 | [diff] [blame] | 450 | all_projects.append(result) |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 451 | |
Michael Bestas | 42e25e3 | 2016-03-12 20:18:39 +0200 | [diff] [blame] | 452 | # Search android/default.xml or config/%(branch)_extra_packages.xml |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 453 | # for the project's name |
| 454 | for project in items: |
| 455 | path = project.attributes['path'].value |
| 456 | if not (result + '/').startswith(path +'/'): |
Michael Bestas | c899b8c | 2015-03-03 00:53:19 +0200 | [diff] [blame] | 457 | continue |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 458 | if result != path: |
| 459 | if path in all_projects: |
| 460 | break |
| 461 | result = path |
| 462 | all_projects.append(result) |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 463 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 464 | br = project.getAttribute('revision') or branch |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 465 | |
Michael W | 1bb2f92 | 2019-02-27 17:46:28 +0100 | [diff] [blame] | 466 | push_as_commit(files, base_path, result, |
Michael Bestas | 80b22ef | 2018-11-14 23:12:33 +0200 | [diff] [blame] | 467 | project.getAttribute('name'), br, username) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 468 | break |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 469 | |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 470 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 471 | def main(): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 472 | args = parse_args() |
| 473 | default_branch = args.branch |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 474 | |
Michael W | d13658a | 2019-01-13 14:05:37 +0100 | [diff] [blame] | 475 | if args.submit: |
| 476 | if args.username is None: |
| 477 | print('Argument -u/--username is required for submitting!') |
| 478 | sys.exit(1) |
| 479 | submit_gerrit(default_branch, args.username) |
| 480 | sys.exit(0) |
| 481 | |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 482 | base_path_branch_suffix = default_branch.replace('-', '_').replace('.', '_').upper() |
| 483 | base_path_env = 'LINEAGE_CROWDIN_BASE_PATH_%s' % base_path_branch_suffix |
| 484 | base_path = os.getenv(base_path_env) |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 485 | if base_path is None: |
Anthony King | d0d56cf | 2015-06-05 10:48:38 +0100 | [diff] [blame] | 486 | cwd = os.getcwd() |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 487 | 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] | 488 | base_path = cwd |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 489 | if not os.path.isdir(base_path): |
Michael Bestas | eb4629a | 2018-11-14 23:03:18 +0200 | [diff] [blame] | 490 | 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] | 491 | sys.exit(1) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 492 | |
Michael Bestas | 99f5fce | 2015-06-04 22:07:51 +0300 | [diff] [blame] | 493 | if not check_dependencies(): |
| 494 | sys.exit(1) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 495 | |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 496 | xml_android = load_xml(x='%s/android/default.xml' % base_path) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 497 | if xml_android is None: |
| 498 | sys.exit(1) |
| 499 | |
Michael Bestas | 42e25e3 | 2016-03-12 20:18:39 +0200 | [diff] [blame] | 500 | xml_extra = load_xml(x='%s/config/%s_extra_packages.xml' |
Anthony King | d0d56cf | 2015-06-05 10:48:38 +0100 | [diff] [blame] | 501 | % (_DIR, default_branch)) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 502 | if xml_extra is None: |
| 503 | sys.exit(1) |
| 504 | |
Michael Bestas | 19dc335 | 2018-02-03 20:24:00 +0200 | [diff] [blame] | 505 | xml_snippet = load_xml(x='%s/android/snippets/lineage.xml' % base_path) |
| 506 | if xml_snippet is None: |
| 507 | xml_snippet = load_xml(x='%s/android/snippets/cm.xml' % base_path) |
| 508 | if xml_snippet is None: |
| 509 | xml_snippet = load_xml(x='%s/android/snippets/hal_cm_all.xml' % base_path) |
| 510 | if xml_snippet is not None: |
| 511 | xml_files = (xml_android, xml_snippet, xml_extra) |
Michael Bestas | 687679f | 2016-12-07 23:20:12 +0200 | [diff] [blame] | 512 | else: |
| 513 | xml_files = (xml_android, xml_extra) |
| 514 | |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 515 | if args.config: |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 516 | files = ['%s/config/%s' % (_DIR, args.config)] |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 517 | else: |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 518 | files = ['%s/config/%s.yaml' % (_DIR, default_branch), |
| 519 | '%s/config/%s_aosp.yaml' % (_DIR, default_branch)] |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 520 | if not check_files(files): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 521 | sys.exit(1) |
| 522 | |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 523 | if args.download and args.username is None: |
| 524 | print('Argument -u/--username is required for translations download') |
| 525 | sys.exit(1) |
| 526 | |
| 527 | if args.upload_sources: |
| 528 | upload_sources_crowdin(default_branch, args.config) |
| 529 | if args.upload_translations: |
| 530 | upload_translations_crowdin(default_branch, args.config) |
| 531 | if args.download: |
Michael Bestas | 687679f | 2016-12-07 23:20:12 +0200 | [diff] [blame] | 532 | download_crowdin(base_path, default_branch, xml_files, |
Michael Bestas | 80b22ef | 2018-11-14 23:12:33 +0200 | [diff] [blame] | 533 | args.username, args.config) |
Tom Powell | 4425685 | 2016-07-06 15:23:25 -0700 | [diff] [blame] | 534 | |
| 535 | if _COMMITS_CREATED: |
| 536 | print('\nDone!') |
| 537 | sys.exit(0) |
Tom Powell | f42586f | 2016-07-11 11:02:54 -0700 | [diff] [blame] | 538 | else: |
Tom Powell | 4425685 | 2016-07-06 15:23:25 -0700 | [diff] [blame] | 539 | print('\nNothing to commit') |
| 540 | sys.exit(-1) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 541 | |
| 542 | if __name__ == '__main__': |
| 543 | main() |