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__)) |
| 37 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 38 | # ################################ FUNCTIONS ################################# # |
| 39 | |
| 40 | |
| 41 | def run_subprocess(cmd, silent=False): |
| 42 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, |
| 43 | universal_newlines=True) |
| 44 | comm = p.communicate() |
| 45 | exit_code = p.returncode |
| 46 | if exit_code != 0 and not silent: |
| 47 | print("There was an error running the subprocess.\n" |
| 48 | "cmd: %s\n" |
| 49 | "exit code: %d\n" |
| 50 | "stdout: %s\n" |
| 51 | "stderr: %s" % (cmd, exit_code, comm[0], comm[1]), |
| 52 | file=sys.stderr) |
| 53 | return comm, exit_code |
| 54 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 55 | |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 56 | def push_as_commit(base_path, path, name, branch, username): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 57 | print('Committing %s on branch %s' % (name, branch)) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 58 | |
| 59 | # Get path |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 60 | path = os.path.join(base_path, path) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 61 | if not path.endswith('.git'): |
| 62 | path = os.path.join(path, '.git') |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 63 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 64 | # Create repo object |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 65 | repo = git.Repo(path) |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 66 | |
| 67 | # Remove previously deleted files from Git |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 68 | files = repo.git.ls_files(d=True).split('\n') |
| 69 | if files and files[0]: |
| 70 | repo.git.rm(files) |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 71 | |
| 72 | # Add all files to commit |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 73 | repo.git.add('-A') |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 74 | |
| 75 | # Create commit; if it fails, probably empty so skipping |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 76 | try: |
| 77 | repo.git.commit(m='Automatic translation import') |
| 78 | except: |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 79 | print('Failed to create commit for %s, probably empty: skipping' |
| 80 | % name, file=sys.stderr) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 81 | return |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 82 | |
| 83 | # Push commit |
Michael Bestas | f96f67b | 2014-10-21 00:43:37 +0300 | [diff] [blame] | 84 | try: |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 85 | repo.git.push('ssh://%s@review.cyanogenmod.org:29418/%s' % (username, name), |
| 86 | 'HEAD:refs/for/%s%%topic=translation' % branch) |
| 87 | print('Successfully pushed commit for %s' % name) |
Michael Bestas | f96f67b | 2014-10-21 00:43:37 +0300 | [diff] [blame] | 88 | except: |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 89 | print('Failed to push commit for %s' % name, file=sys.stderr) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 90 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 91 | |
| 92 | def check_run(cmd): |
Michael Bestas | 97677e1 | 2015-02-08 13:11:59 +0200 | [diff] [blame] | 93 | p = subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr) |
| 94 | ret = p.wait() |
| 95 | if ret != 0: |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 96 | print('Failed to run cmd: %s' % ' '.join(cmd), file=sys.stderr) |
Michael Bestas | 97677e1 | 2015-02-08 13:11:59 +0200 | [diff] [blame] | 97 | sys.exit(ret) |
| 98 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 99 | |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 100 | def find_xml(base_path): |
| 101 | for dp, dn, file_names in os.walk(base_path): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 102 | for f in file_names: |
| 103 | if os.path.splitext(f)[1] == '.xml': |
| 104 | yield os.path.join(dp, f) |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 105 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 106 | # ############################################################################ # |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 107 | |
Michael Bestas | 6b6db12 | 2015-02-08 13:22:22 +0200 | [diff] [blame] | 108 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 109 | def parse_args(): |
| 110 | parser = argparse.ArgumentParser( |
| 111 | description="Synchronising CyanogenMod's translations with Crowdin") |
| 112 | sync = parser.add_mutually_exclusive_group() |
| 113 | parser.add_argument('-u', '--username', help='Gerrit username', |
| 114 | required=True) |
| 115 | parser.add_argument('-b', '--branch', help='CyanogenMod branch', |
| 116 | required=True) |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame^] | 117 | parser.add_argument('-c', '--config', help='Custom yaml config') |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 118 | sync.add_argument('--no-upload', action='store_true', |
| 119 | help='Only download CM translations from Crowdin') |
| 120 | sync.add_argument('--no-download', action='store_true', |
| 121 | help='Only upload CM source translations to Crowdin') |
| 122 | return parser.parse_args() |
Michael Bestas | 6b6db12 | 2015-02-08 13:22:22 +0200 | [diff] [blame] | 123 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 124 | # ################################# PREPARE ################################## # |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 125 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 126 | |
| 127 | def check_dependencies(): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 128 | # Check for Ruby version of crowdin-cli |
| 129 | cmd = ['gem', 'list', 'crowdin-cli', '-i'] |
| 130 | if run_subprocess(cmd, silent=True)[1] != 0: |
| 131 | print('You have not installed crowdin-cli.', file=sys.stderr) |
| 132 | return False |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 133 | return True |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 134 | |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 135 | |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 136 | def load_xml(x): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 137 | try: |
| 138 | return minidom.parse(x) |
| 139 | except IOError: |
| 140 | print('You have no %s.' % x, file=sys.stderr) |
| 141 | return None |
| 142 | except Exception: |
| 143 | # TODO: minidom should not be used. |
| 144 | print('Malformed %s.' % x, file=sys.stderr) |
| 145 | return None |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 146 | |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame] | 147 | |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame^] | 148 | def check_files(files): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 149 | for f in files: |
| 150 | if not os.path.isfile(f): |
| 151 | print('You have no %s.' % f, file=sys.stderr) |
| 152 | return False |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 153 | return True |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame] | 154 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 155 | # ################################### MAIN ################################### # |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame] | 156 | |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame] | 157 | |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame^] | 158 | def upload_crowdin(branch, config, no_upload=False): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 159 | if no_upload: |
| 160 | print('Skipping source translations upload') |
| 161 | return |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 162 | |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame^] | 163 | if config: |
| 164 | print('\nUploading Crowdin source translations (custom config)') |
| 165 | check_run(['crowdin-cli', |
| 166 | '--config=%s/crowdin/%s' % (_DIR, config), |
| 167 | 'upload', 'sources']) |
| 168 | else: |
| 169 | print('\nUploading Crowdin source translations ' |
| 170 | '(AOSP supported languages)') |
| 171 | check_run(['crowdin-cli', |
| 172 | '--config=%s/crowdin/crowdin_%s.yaml' % (_DIR, branch), |
| 173 | 'upload', 'sources']) |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 174 | |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame^] | 175 | print('\nUploading Crowdin source translations ' |
| 176 | '(non-AOSP supported languages)') |
| 177 | check_run(['crowdin-cli', |
| 178 | '--config=%s/crowdin/crowdin_%s_aosp.yaml' % (_DIR, branch), |
| 179 | 'upload', 'sources']) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 180 | |
| 181 | |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame^] | 182 | def download_crowdin(base_path, branch, xml, username, config, |
| 183 | no_download=False): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 184 | if no_download: |
| 185 | print('Skipping translations download') |
| 186 | return |
| 187 | |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame^] | 188 | if config: |
| 189 | print('\nDownloading Crowdin translations (custom config)') |
| 190 | check_run(['crowdin-cli', |
| 191 | '--config=%s/crowdin/%s' % (_DIR, config), |
| 192 | 'download', '--ignore-match']) |
| 193 | else: |
| 194 | print('\nDownloading Crowdin translations (AOSP supported languages)') |
| 195 | check_run(['crowdin-cli', |
| 196 | '--config=%s/crowdin/crowdin_%s.yaml' % (_DIR, branch), |
| 197 | 'download', '--ignore-match']) |
Michael Bestas | 50579d2 | 2014-08-09 17:49:14 +0300 | [diff] [blame] | 198 | |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame^] | 199 | print('\nDownloading Crowdin translations ' |
| 200 | '(non-AOSP supported languages)') |
| 201 | check_run(['crowdin-cli', |
| 202 | '--config=%s/crowdin/crowdin_%s_aosp.yaml' % (_DIR, branch), |
| 203 | 'download', '--ignore-match']) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 204 | |
Michael Bestas | 99f5fce | 2015-06-04 22:07:51 +0300 | [diff] [blame] | 205 | print('\nRemoving useless empty translation files') |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 206 | empty_contents = { |
| 207 | '<resources/>', |
| 208 | '<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>', |
| 209 | ('<resources xmlns:android=' |
| 210 | '"http://schemas.android.com/apk/res/android"/>'), |
| 211 | ('<resources xmlns:android="http://schemas.android.com/apk/res/android"' |
| 212 | ' xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>'), |
| 213 | ('<resources xmlns:tools="http://schemas.android.com/tools"' |
| 214 | ' xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>') |
| 215 | } |
| 216 | xf = None |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 217 | for xml_file in find_xml(base_path): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 218 | xf = open(xml_file).read() |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 219 | for line in empty_contents: |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 220 | if line in xf: |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 221 | print('Removing ' + xml_file) |
| 222 | os.remove(xml_file) |
| 223 | break |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 224 | del xf |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 225 | |
Michael Bestas | 99f5fce | 2015-06-04 22:07:51 +0300 | [diff] [blame] | 226 | print('\nCreating a list of pushable translations') |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 227 | # Get all files that Crowdin pushed |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 228 | paths = [] |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame^] | 229 | if config: |
| 230 | files = ['%s/crowdin/%s' % (_DIR, config)] |
| 231 | else: |
| 232 | files = ['%s/crowdin/crowdin_%s.yaml' % (_DIR, branch), |
| 233 | '%s/crowdin/crowdin_%s_aosp.yaml' % (_DIR, branch) |
| 234 | ] |
Michael Bestas | 6c327e6 | 2015-05-02 01:58:01 +0300 | [diff] [blame] | 235 | for c in files: |
| 236 | cmd = ['crowdin-cli', '--config=%s' % c, 'list', 'sources'] |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 237 | comm, ret = run_subprocess(cmd) |
| 238 | if ret != 0: |
| 239 | sys.exit(ret) |
| 240 | for p in str(comm[0]).split("\n"): |
| 241 | paths.append(p.replace('/%s' % branch, '')) |
Michael Bestas | 50579d2 | 2014-08-09 17:49:14 +0300 | [diff] [blame] | 242 | |
Michael Bestas | 99f5fce | 2015-06-04 22:07:51 +0300 | [diff] [blame] | 243 | print('\nUploading translations to Gerrit') |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 244 | items = [x for sub in xml for x in sub.getElementsByTagName('project')] |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 245 | all_projects = [] |
| 246 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 247 | for path in paths: |
| 248 | path = path.strip() |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 249 | if not path: |
| 250 | continue |
| 251 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 252 | if "/res" not in path: |
| 253 | print('WARNING: Cannot determine project root dir of ' |
| 254 | '[%s], skipping.' % path) |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 255 | continue |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 256 | result = path.split('/res')[0].strip('/') |
| 257 | if result == path.strip('/'): |
| 258 | print('WARNING: Cannot determine project root dir of ' |
| 259 | '[%s], skipping.' % path) |
| 260 | continue |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 261 | |
Michael Bestas | c899b8c | 2015-03-03 00:53:19 +0200 | [diff] [blame] | 262 | if result in all_projects: |
Michael Bestas | c899b8c | 2015-03-03 00:53:19 +0200 | [diff] [blame] | 263 | continue |
Michael Bestas | 50579d2 | 2014-08-09 17:49:14 +0300 | [diff] [blame] | 264 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 265 | # When a project has multiple translatable files, Crowdin will |
| 266 | # give duplicates. |
| 267 | # We don't want that (useless empty commits), so we save each |
| 268 | # project in all_projects and check if it's already in there. |
Michael Bestas | c899b8c | 2015-03-03 00:53:19 +0200 | [diff] [blame] | 269 | all_projects.append(result) |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 270 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 271 | # Search android/default.xml or crowdin/extra_packages_%(branch).xml |
| 272 | # for the project's name |
| 273 | for project in items: |
| 274 | path = project.attributes['path'].value |
| 275 | if not (result + '/').startswith(path +'/'): |
Michael Bestas | c899b8c | 2015-03-03 00:53:19 +0200 | [diff] [blame] | 276 | continue |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 277 | if result != path: |
| 278 | if path in all_projects: |
| 279 | break |
| 280 | result = path |
| 281 | all_projects.append(result) |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 282 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 283 | br = project.getAttribute('revision') or branch |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 284 | |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 285 | push_as_commit(base_path, result, |
| 286 | project.getAttribute('name'), br, username) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 287 | break |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 288 | |
Anthony King | 69a9538 | 2015-02-08 18:44:10 +0000 | [diff] [blame] | 289 | |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 290 | def main(): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 291 | args = parse_args() |
| 292 | default_branch = args.branch |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 293 | |
| 294 | base_path = os.getenv('CM_CROWDIN_BASE_PATH') |
| 295 | if base_path is None: |
Anthony King | d0d56cf | 2015-06-05 10:48:38 +0100 | [diff] [blame] | 296 | cwd = os.getcwd() |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 297 | print('You have not set CM_CROWDIN_BASE_PATH. Defaulting to %s' % cwd) |
| 298 | base_path = cwd |
| 299 | else: |
| 300 | base_path = os.path.join(os.path.realpath(base_path), default_branch) |
| 301 | if not os.path.isdir(base_path): |
| 302 | print('CM_CROWDIN_BASE_PATH + branch is not a real directory: %s' |
| 303 | % base_path) |
| 304 | sys.exit(1) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 305 | |
Michael Bestas | 99f5fce | 2015-06-04 22:07:51 +0300 | [diff] [blame] | 306 | if not check_dependencies(): |
| 307 | sys.exit(1) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 308 | |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 309 | xml_android = load_xml(x='%s/android/default.xml' % base_path) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 310 | if xml_android is None: |
| 311 | sys.exit(1) |
| 312 | |
Michael Bestas | 118fcaf | 2015-06-04 23:02:20 +0300 | [diff] [blame] | 313 | xml_extra = load_xml(x='%s/crowdin/extra_packages_%s.xml' |
Anthony King | d0d56cf | 2015-06-05 10:48:38 +0100 | [diff] [blame] | 314 | % (_DIR, default_branch)) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 315 | if xml_extra is None: |
| 316 | sys.exit(1) |
| 317 | |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame^] | 318 | if args.config: |
| 319 | files = ['%s/crowdin/%s' % (_DIR, args.config)] |
| 320 | else: |
| 321 | files = ['%s/crowdin/crowdin_%s.yaml' % (_DIR, default_branch), |
| 322 | '%s/crowdin/crowdin_%s_aosp.yaml' % (_DIR, default_branch) |
| 323 | ] |
| 324 | if not check_files(files): |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 325 | sys.exit(1) |
| 326 | |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame^] | 327 | upload_crowdin(default_branch, args.config, args.no_upload) |
Anthony King | d0d56cf | 2015-06-05 10:48:38 +0100 | [diff] [blame] | 328 | download_crowdin(base_path, default_branch, (xml_android, xml_extra), |
Michael Bestas | 2f8c4a5 | 2015-08-05 21:33:50 +0300 | [diff] [blame^] | 329 | args.username, args.config, args.no_download) |
Anthony King | b860763 | 2015-05-01 22:06:37 +0300 | [diff] [blame] | 330 | print('\nDone!') |
| 331 | |
| 332 | if __name__ == '__main__': |
| 333 | main() |