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