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