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