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 |
| 6 | # directly to CyanogenMod's Gerrit. |
| 7 | # |
Michael Bestas | 97677e1 | 2015-02-08 13:11:59 +0200 | [diff] [blame] | 8 | # Copyright (C) 2014-2015 The CyanogenMod Project |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 9 | # |
| 10 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 | # you may not use this file except in compliance with the License. |
| 12 | # You may obtain a copy of the License at |
| 13 | # |
| 14 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | # |
| 16 | # Unless required by applicable law or agreed to in writing, software |
| 17 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | # See the License for the specific language governing permissions and |
| 20 | # limitations under the License. |
| 21 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 22 | # ################################# IMPORTS ################################## # |
| 23 | |
| 24 | from __future__ import print_function |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 25 | |
| 26 | import argparse |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 27 | import git |
| 28 | import os |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 29 | import subprocess |
| 30 | import sys |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 31 | |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 32 | from xml.dom import minidom |
| 33 | |
Anthony King | d0d56cf | 2015-06-05 10:48:38 +0100 | [diff] [blame] | 34 | # ################################# GLOBALS ################################## # |
| 35 | |
| 36 | _DIR = os.path.dirname(os.path.realpath(__file__)) |
Tom Powell | 4425685 | 2016-07-06 15:23:25 -0700 | [diff] [blame] | 37 | _COMMITS_CREATED = False |
Anthony King | d0d56cf | 2015-06-05 10:48:38 +0100 | [diff] [blame] | 38 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 39 | # ################################ FUNCTIONS ################################# # |
| 40 | |
| 41 | |
| 42 | def run_subprocess(cmd, silent=False): |
| 43 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 44 | universal_newlines=True) |
| 45 | comm = p.communicate() |
| 46 | exit_code = p.returncode |
| 47 | if exit_code != 0 and not silent: |
| 48 | print("There was an error running the subprocess.\n" |
| 49 | "cmd: %s\n" |
| 50 | "exit code: %d\n" |
| 51 | "stdout: %s\n" |
| 52 | "stderr: %s" % (cmd, exit_code, comm[0], comm[1]), |
| 53 | file=sys.stderr) |
| 54 | return comm, exit_code |
| 55 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 56 | |
Michael Bestas | db69b93 | 2016-03-12 21:08:27 +0200 | [diff] [blame] | 57 | def push_as_commit(base_path, path, name, branch, username, ticket): |
Michael Bestas | 432919a | 2016-03-12 21:16:20 +0200 | [diff] [blame] | 58 | if 'stable/' in base_path: |
| 59 | branch = ''.join(('stable/', branch)) |
| 60 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 61 | print('Committing %s on branch %s' % (name, branch)) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 62 | |
| 63 | # Get path |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 64 | path = os.path.join(base_path, path) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 65 | if not path.endswith('.git'): |
| 66 | path = os.path.join(path, '.git') |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 67 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 68 | # Create repo object |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 69 | repo = git.Repo(path) |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 70 | |
| 71 | # Remove previously deleted files from Git |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 72 | files = repo.git.ls_files(d=True).split('\n') |
| 73 | if files and files[0]: |
| 74 | repo.git.rm(files) |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 75 | |
| 76 | # Add all files to commit |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 77 | repo.git.add('-A') |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 78 | |
| 79 | # Create commit; if it fails, probably empty so skipping |
Michael Bestas | db69b93 | 2016-03-12 21:08:27 +0200 | [diff] [blame] | 80 | if ticket: |
| 81 | message = '''Automatic translation import |
| 82 | |
| 83 | Ticket: %s''' % ticket |
| 84 | else: |
| 85 | message = 'Automatic translation import' |
| 86 | |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 87 | try: |
Michael Bestas | db69b93 | 2016-03-12 21:08:27 +0200 | [diff] [blame] | 88 | repo.git.commit(m=message) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 89 | except: |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 90 | print('Failed to create commit for %s, probably empty: skipping' |
| 91 | % name, file=sys.stderr) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 92 | return |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 93 | |
| 94 | # Push commit |
Michael Bestas | f96f67b | 2014-10-21 00:43:37 +0300 | [diff] [blame] | 95 | try: |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 96 | repo.git.push('ssh://%s@review.cyanogenmod.org:29418/%s' % (username, name), |
| 97 | 'HEAD:refs/for/%s%%topic=translation' % branch) |
| 98 | print('Successfully pushed commit for %s' % name) |
Michael Bestas | f96f67b | 2014-10-21 00:43:37 +0300 | [diff] [blame] | 99 | except: |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 100 | print('Failed to push commit for %s' % name, file=sys.stderr) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 101 | |
Tom Powell | 4425685 | 2016-07-06 15:23:25 -0700 | [diff] [blame] | 102 | _COMMITS_CREATED = True |
| 103 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 104 | |
| 105 | def check_run(cmd): |
Michael Bestas | 97677e1 | 2015-02-08 13:11:59 +0200 | [diff] [blame] | 106 | p = subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr) |
| 107 | ret = p.wait() |
| 108 | if ret != 0: |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 109 | print('Failed to run cmd: %s' % ' '.join(cmd), file=sys.stderr) |
Michael Bestas | 97677e1 | 2015-02-08 13:11:59 +0200 | [diff] [blame] | 110 | sys.exit(ret) |
| 111 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 112 | |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 113 | def find_xml(base_path): |
| 114 | for dp, dn, file_names in os.walk(base_path): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 115 | for f in file_names: |
| 116 | if os.path.splitext(f)[1] == '.xml': |
| 117 | yield os.path.join(dp, f) |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 118 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 119 | # ############################################################################ # |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 120 | |
Michael Bestas | 6b6db12 | 2015-02-08 13:22:22 +0200 | [diff] [blame] | 121 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 122 | def parse_args(): |
| 123 | parser = argparse.ArgumentParser( |
| 124 | description="Synchronising CyanogenMod's translations with Crowdin") |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 125 | parser.add_argument('-u', '--username', help='Gerrit username') |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 126 | parser.add_argument('-b', '--branch', help='CyanogenMod branch', |
| 127 | required=True) |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 128 | parser.add_argument('-c', '--config', help='Custom yaml config') |
Michael Bestas | db69b93 | 2016-03-12 21:08:27 +0200 | [diff] [blame] | 129 | parser.add_argument('-t', '--ticket', help='JIRA ticket') |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 130 | parser.add_argument('--upload-sources', action='store_true', |
| 131 | help='Upload sources to Crowdin') |
| 132 | parser.add_argument('--upload-translations', action='store_true', |
| 133 | help='Upload translations to Crowdin') |
| 134 | parser.add_argument('--download', action='store_true', |
| 135 | help='Download translations from Crowdin') |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 136 | return parser.parse_args() |
Michael Bestas | 6b6db12 | 2015-02-08 13:22:22 +0200 | [diff] [blame] | 137 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 138 | # ################################# PREPARE ################################## # |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 139 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 140 | |
| 141 | def check_dependencies(): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 142 | # Check for Ruby version of crowdin-cli |
| 143 | cmd = ['gem', 'list', 'crowdin-cli', '-i'] |
| 144 | if run_subprocess(cmd, silent=True)[1] != 0: |
| 145 | print('You have not installed crowdin-cli.', file=sys.stderr) |
| 146 | return False |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 147 | return True |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 148 | |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 149 | |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 150 | def load_xml(x): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 151 | try: |
| 152 | return minidom.parse(x) |
| 153 | except IOError: |
| 154 | print('You have no %s.' % x, file=sys.stderr) |
| 155 | return None |
| 156 | except Exception: |
| 157 | # TODO: minidom should not be used. |
| 158 | print('Malformed %s.' % x, file=sys.stderr) |
| 159 | return None |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 160 | |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame] | 161 | |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 162 | def check_files(files): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 163 | for f in files: |
| 164 | if not os.path.isfile(f): |
| 165 | print('You have no %s.' % f, file=sys.stderr) |
| 166 | return False |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 167 | return True |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame] | 168 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 169 | # ################################### MAIN ################################### # |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame] | 170 | |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame] | 171 | |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 172 | def upload_sources_crowdin(branch, config): |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 173 | if config: |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 174 | print('\nUploading sources to Crowdin (custom config)') |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 175 | check_run(['crowdin-cli', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 176 | '--config=%s/config/%s' % (_DIR, config), |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 177 | 'upload', 'sources', '--branch=%s' % branch]) |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 178 | else: |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 179 | print('\nUploading sources to Crowdin (AOSP supported languages)') |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 180 | check_run(['crowdin-cli', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 181 | '--config=%s/config/%s.yaml' % (_DIR, branch), |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 182 | 'upload', 'sources', '--branch=%s' % branch]) |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 183 | |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 184 | print('\nUploading sources to Crowdin (non-AOSP supported languages)') |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 185 | check_run(['crowdin-cli', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 186 | '--config=%s/config/%s_aosp.yaml' % (_DIR, branch), |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 187 | 'upload', 'sources', '--branch=%s' % branch]) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 188 | |
| 189 | |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 190 | def upload_translations_crowdin(branch, config): |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 191 | if config: |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 192 | print('\nUploading translations to Crowdin (custom config)') |
| 193 | check_run(['crowdin-cli', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 194 | '--config=%s/config/%s' % (_DIR, config), |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 195 | 'upload', 'translations', '--branch=%s' % branch, |
| 196 | '--no-import-duplicates', '--import-eq-suggestions', |
| 197 | '--auto-approve-imported']) |
| 198 | else: |
| 199 | print('\nUploading translations to Crowdin ' |
| 200 | '(AOSP supported languages)') |
| 201 | check_run(['crowdin-cli', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 202 | '--config=%s/config/%s.yaml' % (_DIR, branch), |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 203 | 'upload', 'translations', '--branch=%s' % branch, |
| 204 | '--no-import-duplicates', '--import-eq-suggestions', |
| 205 | '--auto-approve-imported']) |
| 206 | |
| 207 | print('\nUploading translations to Crowdin ' |
| 208 | '(non-AOSP supported languages)') |
| 209 | check_run(['crowdin-cli', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 210 | '--config=%s/config/%s_aosp.yaml' % (_DIR, branch), |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 211 | 'upload', 'translations', '--branch=%s' % branch, |
| 212 | '--no-import-duplicates', '--import-eq-suggestions', |
| 213 | '--auto-approve-imported']) |
| 214 | |
| 215 | |
Michael Bestas | db69b93 | 2016-03-12 21:08:27 +0200 | [diff] [blame] | 216 | def download_crowdin(base_path, branch, xml, username, config, ticket): |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 217 | if config: |
| 218 | print('\nDownloading translations from Crowdin (custom config)') |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 219 | check_run(['crowdin-cli', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 220 | '--config=%s/config/%s' % (_DIR, config), |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 221 | 'download', '--branch=%s' % branch]) |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 222 | else: |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 223 | print('\nDownloading translations from Crowdin ' |
| 224 | '(AOSP supported languages)') |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 225 | check_run(['crowdin-cli', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 226 | '--config=%s/config/%s.yaml' % (_DIR, branch), |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 227 | 'download', '--branch=%s' % branch]) |
Michael Bestas | 50579d2 | 2014-08-09 17:49:14 +0300 | [diff] [blame] | 228 | |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 229 | print('\nDownloading translations from Crowdin ' |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 230 | '(non-AOSP supported languages)') |
| 231 | check_run(['crowdin-cli', |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 232 | '--config=%s/config/%s_aosp.yaml' % (_DIR, branch), |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 233 | 'download', '--branch=%s' % branch]) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 234 | |
Michael Bestas | 99f5fce | 2015-06-04 22:07:51 +0300 | [diff] [blame] | 235 | print('\nRemoving useless empty translation files') |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 236 | empty_contents = { |
| 237 | '<resources/>', |
| 238 | '<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>', |
| 239 | ('<resources xmlns:android=' |
| 240 | '"http://schemas.android.com/apk/res/android"/>'), |
| 241 | ('<resources xmlns:android="http://schemas.android.com/apk/res/android"' |
| 242 | ' xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>'), |
| 243 | ('<resources xmlns:tools="http://schemas.android.com/tools"' |
| 244 | ' xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>') |
| 245 | } |
| 246 | xf = None |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 247 | for xml_file in find_xml(base_path): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 248 | xf = open(xml_file).read() |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 249 | for line in empty_contents: |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 250 | if line in xf: |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 251 | print('Removing ' + xml_file) |
| 252 | os.remove(xml_file) |
| 253 | break |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 254 | del xf |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 255 | |
Michael Bestas | 99f5fce | 2015-06-04 22:07:51 +0300 | [diff] [blame] | 256 | print('\nCreating a list of pushable translations') |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 257 | # Get all files that Crowdin pushed |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 258 | paths = [] |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 259 | if config: |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 260 | files = ['%s/config/%s' % (_DIR, config)] |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 261 | else: |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 262 | files = ['%s/config/%s.yaml' % (_DIR, branch), |
| 263 | '%s/config/%s_aosp.yaml' % (_DIR, branch)] |
Michael Bestas | 6c327e6 | 2015-05-02 01:58:01 +0300 | [diff] [blame] | 264 | for c in files: |
Michael Bestas | 44fbb35 | 2015-12-17 02:01:42 +0200 | [diff] [blame] | 265 | cmd = ['crowdin-cli', '--config=%s' % c, 'list', 'project', |
| 266 | '--branch=%s' % branch] |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 267 | comm, ret = run_subprocess(cmd) |
| 268 | if ret != 0: |
| 269 | sys.exit(ret) |
| 270 | for p in str(comm[0]).split("\n"): |
| 271 | paths.append(p.replace('/%s' % branch, '')) |
Michael Bestas | 50579d2 | 2014-08-09 17:49:14 +0300 | [diff] [blame] | 272 | |
Michael Bestas | 99f5fce | 2015-06-04 22:07:51 +0300 | [diff] [blame] | 273 | print('\nUploading translations to Gerrit') |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 274 | items = [x for sub in xml for x in sub.getElementsByTagName('project')] |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 275 | all_projects = [] |
| 276 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 277 | for path in paths: |
| 278 | path = path.strip() |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 279 | if not path: |
| 280 | continue |
| 281 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 282 | if "/res" not in path: |
| 283 | print('WARNING: Cannot determine project root dir of ' |
| 284 | '[%s], skipping.' % path) |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 285 | continue |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 286 | result = path.split('/res')[0].strip('/') |
| 287 | if result == path.strip('/'): |
| 288 | print('WARNING: Cannot determine project root dir of ' |
| 289 | '[%s], skipping.' % path) |
| 290 | continue |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 291 | |
Michael Bestas | c899b8c | 2015-03-03 00:53:19 +0200 | [diff] [blame] | 292 | if result in all_projects: |
Michael Bestas | c899b8c | 2015-03-03 00:53:19 +0200 | [diff] [blame] | 293 | continue |
Michael Bestas | 50579d2 | 2014-08-09 17:49:14 +0300 | [diff] [blame] | 294 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 295 | # When a project has multiple translatable files, Crowdin will |
| 296 | # give duplicates. |
| 297 | # We don't want that (useless empty commits), so we save each |
| 298 | # project in all_projects and check if it's already in there. |
Michael Bestas | c899b8c | 2015-03-03 00:53:19 +0200 | [diff] [blame] | 299 | all_projects.append(result) |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 300 | |
Michael Bestas | 42e25e3 | 2016-03-12 20:18:39 +0200 | [diff] [blame] | 301 | # Search android/default.xml or config/%(branch)_extra_packages.xml |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 302 | # for the project's name |
| 303 | for project in items: |
| 304 | path = project.attributes['path'].value |
| 305 | if not (result + '/').startswith(path +'/'): |
Michael Bestas | c899b8c | 2015-03-03 00:53:19 +0200 | [diff] [blame] | 306 | continue |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 307 | if result != path: |
| 308 | if path in all_projects: |
| 309 | break |
| 310 | result = path |
| 311 | all_projects.append(result) |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 312 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 313 | br = project.getAttribute('revision') or branch |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 314 | |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 315 | push_as_commit(base_path, result, |
Michael Bestas | db69b93 | 2016-03-12 21:08:27 +0200 | [diff] [blame] | 316 | project.getAttribute('name'), br, username, ticket) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 317 | break |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 318 | |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 319 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 320 | def main(): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 321 | args = parse_args() |
| 322 | default_branch = args.branch |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 323 | |
Michael Bestas | 432919a | 2016-03-12 21:16:20 +0200 | [diff] [blame] | 324 | if 'stable/' in default_branch: |
| 325 | base_path_env = 'CM_CROWDIN_STABLE_BASE_PATH' |
| 326 | base_path = os.getenv(base_path_env) |
| 327 | default_branch = default_branch.replace('stable/', '') |
| 328 | else: |
| 329 | base_path_env = 'CM_CROWDIN_BASE_PATH' |
| 330 | base_path = os.getenv(base_path_env) |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 331 | if base_path is None: |
Anthony King | d0d56cf | 2015-06-05 10:48:38 +0100 | [diff] [blame] | 332 | cwd = os.getcwd() |
Michael Bestas | 432919a | 2016-03-12 21:16:20 +0200 | [diff] [blame] | 333 | 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] | 334 | base_path = cwd |
| 335 | else: |
| 336 | base_path = os.path.join(os.path.realpath(base_path), default_branch) |
| 337 | if not os.path.isdir(base_path): |
Michael Bestas | 432919a | 2016-03-12 21:16:20 +0200 | [diff] [blame] | 338 | print('%s + branch is not a real directory: %s' |
| 339 | % (base_path_env, base_path)) |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 340 | sys.exit(1) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 341 | |
Michael Bestas | 99f5fce | 2015-06-04 22:07:51 +0300 | [diff] [blame] | 342 | if not check_dependencies(): |
| 343 | sys.exit(1) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 344 | |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 345 | xml_android = load_xml(x='%s/android/default.xml' % base_path) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 346 | if xml_android is None: |
| 347 | sys.exit(1) |
| 348 | |
Michael Bestas | 42e25e3 | 2016-03-12 20:18:39 +0200 | [diff] [blame] | 349 | xml_extra = load_xml(x='%s/config/%s_extra_packages.xml' |
Anthony King | d0d56cf | 2015-06-05 10:48:38 +0100 | [diff] [blame] | 350 | % (_DIR, default_branch)) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 351 | if xml_extra is None: |
| 352 | sys.exit(1) |
| 353 | |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 354 | if args.config: |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 355 | files = ['%s/config/%s' % (_DIR, args.config)] |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 356 | else: |
Michael Bestas | 03bc705 | 2016-03-12 03:19:10 +0200 | [diff] [blame] | 357 | files = ['%s/config/%s.yaml' % (_DIR, default_branch), |
| 358 | '%s/config/%s_aosp.yaml' % (_DIR, default_branch)] |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame] | 359 | if not check_files(files): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 360 | sys.exit(1) |
| 361 | |
Michael Bestas | fd5d136 | 2015-12-18 20:34:32 +0200 | [diff] [blame] | 362 | if args.download and args.username is None: |
| 363 | print('Argument -u/--username is required for translations download') |
| 364 | sys.exit(1) |
| 365 | |
| 366 | if args.upload_sources: |
| 367 | upload_sources_crowdin(default_branch, args.config) |
| 368 | if args.upload_translations: |
| 369 | upload_translations_crowdin(default_branch, args.config) |
| 370 | if args.download: |
| 371 | download_crowdin(base_path, default_branch, (xml_android, xml_extra), |
Michael Bestas | db69b93 | 2016-03-12 21:08:27 +0200 | [diff] [blame] | 372 | args.username, args.config, args.ticket) |
Tom Powell | 4425685 | 2016-07-06 15:23:25 -0700 | [diff] [blame] | 373 | |
| 374 | if _COMMITS_CREATED: |
| 375 | print('\nDone!') |
| 376 | sys.exit(0) |
Tom Powell | f42586f | 2016-07-11 11:02:54 -0700 | [diff] [blame^] | 377 | else: |
Tom Powell | 4425685 | 2016-07-06 15:23:25 -0700 | [diff] [blame] | 378 | print('\nNothing to commit') |
| 379 | sys.exit(-1) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 380 | |
| 381 | if __name__ == '__main__': |
| 382 | main() |