blob: 2817fb21f0d418ca5dff01caac83a175be2a369a [file] [log] [blame]
Anthony Kingb8607632015-05-01 22:06:37 +03001#!/usr/bin/env python
Marco Brohetcb5cdb42014-07-11 22:41:53 +02002# -*- coding: utf-8 -*-
Michael Bestas1ab959b2014-07-26 16:01:01 +03003# crowdin_sync.py
Marco Brohetcb5cdb42014-07-11 22:41:53 +02004#
5# Updates Crowdin source translations and pushes translations
Abhisek Devkotab78def42016-12-27 13:06:52 -08006# directly to LineageOS' Gerrit.
Marco Brohetcb5cdb42014-07-11 22:41:53 +02007#
Michael Bestaseb4629a2018-11-14 23:03:18 +02008# Copyright (C) 2014-2016 The CyanogenMod Project
9# Copyright (C) 2017-2018 The LineageOS Project
Marco Brohetcb5cdb42014-07-11 22:41:53 +020010#
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 Kingb8607632015-05-01 22:06:37 +030023# ################################# IMPORTS ################################## #
24
25from __future__ import print_function
Marco Brohet6b6b4e52014-07-20 00:05:16 +020026
27import argparse
Marco Brohetcb5cdb42014-07-11 22:41:53 +020028import git
29import os
Marco Brohetcb5cdb42014-07-11 22:41:53 +020030import subprocess
31import sys
Anthony Kingb8607632015-05-01 22:06:37 +030032
Marco Brohetcb5cdb42014-07-11 22:41:53 +020033from xml.dom import minidom
34
Anthony Kingd0d56cf2015-06-05 10:48:38 +010035# ################################# GLOBALS ################################## #
36
37_DIR = os.path.dirname(os.path.realpath(__file__))
Tom Powell44256852016-07-06 15:23:25 -070038_COMMITS_CREATED = False
Anthony Kingd0d56cf2015-06-05 10:48:38 +010039
Anthony Kingb8607632015-05-01 22:06:37 +030040# ################################ FUNCTIONS ################################# #
41
42
43def run_subprocess(cmd, silent=False):
44 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
45 universal_newlines=True)
46 comm = p.communicate()
47 exit_code = p.returncode
48 if exit_code != 0 and not silent:
49 print("There was an error running the subprocess.\n"
50 "cmd: %s\n"
51 "exit code: %d\n"
52 "stdout: %s\n"
53 "stderr: %s" % (cmd, exit_code, comm[0], comm[1]),
54 file=sys.stderr)
55 return comm, exit_code
56
Marco Brohet6b6b4e52014-07-20 00:05:16 +020057
Michael Bestas80b22ef2018-11-14 23:12:33 +020058def push_as_commit(base_path, path, name, branch, username):
Anthony Kingb8607632015-05-01 22:06:37 +030059 print('Committing %s on branch %s' % (name, branch))
Marco Brohetcb5cdb42014-07-11 22:41:53 +020060
61 # Get path
Michael Bestas118fcaf2015-06-04 23:02:20 +030062 path = os.path.join(base_path, path)
Anthony Kingb8607632015-05-01 22:06:37 +030063 if not path.endswith('.git'):
64 path = os.path.join(path, '.git')
Marco Brohetcb5cdb42014-07-11 22:41:53 +020065
Marco Brohet6b6b4e52014-07-20 00:05:16 +020066 # Create repo object
Marco Brohetcb5cdb42014-07-11 22:41:53 +020067 repo = git.Repo(path)
Marco Brohet6b6b4e52014-07-20 00:05:16 +020068
69 # Remove previously deleted files from Git
Anthony Kingb8607632015-05-01 22:06:37 +030070 files = repo.git.ls_files(d=True).split('\n')
71 if files and files[0]:
72 repo.git.rm(files)
Marco Brohet6b6b4e52014-07-20 00:05:16 +020073
74 # Add all files to commit
Marco Brohetcb5cdb42014-07-11 22:41:53 +020075 repo.git.add('-A')
Marco Brohet6b6b4e52014-07-20 00:05:16 +020076
77 # Create commit; if it fails, probably empty so skipping
Marco Brohetcb5cdb42014-07-11 22:41:53 +020078 try:
Michael Bestas80b22ef2018-11-14 23:12:33 +020079 repo.git.commit(m='Automatic translation import')
Marco Brohetcb5cdb42014-07-11 22:41:53 +020080 except:
Anthony Kingb8607632015-05-01 22:06:37 +030081 print('Failed to create commit for %s, probably empty: skipping'
82 % name, file=sys.stderr)
Marco Brohetcb5cdb42014-07-11 22:41:53 +020083 return
Marco Brohet6b6b4e52014-07-20 00:05:16 +020084
85 # Push commit
Michael Bestasf96f67b2014-10-21 00:43:37 +030086 try:
Abhisek Devkotab78def42016-12-27 13:06:52 -080087 repo.git.push('ssh://%s@review.lineageos.org:29418/%s' % (username, name),
Anthony Kingb8607632015-05-01 22:06:37 +030088 'HEAD:refs/for/%s%%topic=translation' % branch)
89 print('Successfully pushed commit for %s' % name)
Michael Bestasf96f67b2014-10-21 00:43:37 +030090 except:
Anthony Kingb8607632015-05-01 22:06:37 +030091 print('Failed to push commit for %s' % name, file=sys.stderr)
Marco Brohetcb5cdb42014-07-11 22:41:53 +020092
Tom Powell44256852016-07-06 15:23:25 -070093 _COMMITS_CREATED = True
94
Anthony Kingb8607632015-05-01 22:06:37 +030095
Michael Wd13658a2019-01-13 14:05:37 +010096def submit_gerrit(branch, username):
97 # Find all open translation changes
98 cmd = ['ssh', '-p', '29418',
99 '{}@review.lineageos.org'.format(username),
100 'gerrit', 'query',
101 'status:open',
102 'branch:{}'.format(branch),
103 'message:"Automatic translation import"',
104 'topic:translation',
105 '--current-patch-set']
106 commits = []
107 msg, code = run_subprocess(cmd)
108 if code != 0:
109 print('Failed: {0}'.format(msg[1]))
110 return
111
112 for line in msg[0].split('\n'):
113 if "revision:" not in line:
114 continue;
115 elements = line.split(': ');
116 if len(elements) != 2:
117 print('Unexpected line found: {0}'.format(line))
118 commits.append(elements[1])
119
120 if len(commits) == 0:
121 print("Nothing to submit!")
122 return
123
124 for commit in commits:
125 # Add Code-Review +2 and Verified+1 labels and submit
126 cmd = ['ssh', '-p', '29418',
127 '{}@review.lineageos.org'.format(username),
128 'gerrit', 'review',
129 '--verified +1',
130 '--code-review +2',
131 '--submit', commit]
132 msg, code = run_subprocess(cmd, True)
133 if code != 0:
134 errorText = msg[1].replace('\n\n', '; ').replace('\n', '')
135 print('Submitting commit {0} failed: {1}'.format(commit, errorText))
136 else:
137 print('Success when submitting commit {0}'.format(commit))
138
139
Anthony Kingb8607632015-05-01 22:06:37 +0300140def check_run(cmd):
Michael Bestas97677e12015-02-08 13:11:59 +0200141 p = subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr)
142 ret = p.wait()
143 if ret != 0:
Anthony Kingb8607632015-05-01 22:06:37 +0300144 print('Failed to run cmd: %s' % ' '.join(cmd), file=sys.stderr)
Michael Bestas97677e12015-02-08 13:11:59 +0200145 sys.exit(ret)
146
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200147
Michael Bestas118fcaf2015-06-04 23:02:20 +0300148def find_xml(base_path):
149 for dp, dn, file_names in os.walk(base_path):
Anthony Kingb8607632015-05-01 22:06:37 +0300150 for f in file_names:
151 if os.path.splitext(f)[1] == '.xml':
152 yield os.path.join(dp, f)
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200153
Anthony Kingb8607632015-05-01 22:06:37 +0300154# ############################################################################ #
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200155
Michael Bestas6b6db122015-02-08 13:22:22 +0200156
Anthony Kingb8607632015-05-01 22:06:37 +0300157def parse_args():
158 parser = argparse.ArgumentParser(
Abhisek Devkotab78def42016-12-27 13:06:52 -0800159 description="Synchronising LineageOS' translations with Crowdin")
Michael Bestasfd5d1362015-12-18 20:34:32 +0200160 parser.add_argument('-u', '--username', help='Gerrit username')
Abhisek Devkotab78def42016-12-27 13:06:52 -0800161 parser.add_argument('-b', '--branch', help='LineageOS branch',
Anthony Kingb8607632015-05-01 22:06:37 +0300162 required=True)
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300163 parser.add_argument('-c', '--config', help='Custom yaml config')
Michael Bestasfd5d1362015-12-18 20:34:32 +0200164 parser.add_argument('--upload-sources', action='store_true',
165 help='Upload sources to Crowdin')
166 parser.add_argument('--upload-translations', action='store_true',
167 help='Upload translations to Crowdin')
168 parser.add_argument('--download', action='store_true',
169 help='Download translations from Crowdin')
Michael Wd13658a2019-01-13 14:05:37 +0100170 parser.add_argument('-s', '--submit', action='store_true',
171 help='Merge open translation commits')
Anthony Kingb8607632015-05-01 22:06:37 +0300172 return parser.parse_args()
Michael Bestas6b6db122015-02-08 13:22:22 +0200173
Anthony Kingb8607632015-05-01 22:06:37 +0300174# ################################# PREPARE ################################## #
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200175
Anthony Kingb8607632015-05-01 22:06:37 +0300176
177def check_dependencies():
Michael Bestaseb4629a2018-11-14 23:03:18 +0200178 # Check for Java version of crowdin
179 cmd = ['dpkg-query', '-W', 'crowdin']
Anthony Kingb8607632015-05-01 22:06:37 +0300180 if run_subprocess(cmd, silent=True)[1] != 0:
Michael Bestaseb4629a2018-11-14 23:03:18 +0200181 print('You have not installed crowdin.', file=sys.stderr)
Anthony Kingb8607632015-05-01 22:06:37 +0300182 return False
Anthony Kingb8607632015-05-01 22:06:37 +0300183 return True
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200184
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200185
Michael Bestas118fcaf2015-06-04 23:02:20 +0300186def load_xml(x):
Anthony Kingb8607632015-05-01 22:06:37 +0300187 try:
188 return minidom.parse(x)
189 except IOError:
190 print('You have no %s.' % x, file=sys.stderr)
191 return None
192 except Exception:
193 # TODO: minidom should not be used.
194 print('Malformed %s.' % x, file=sys.stderr)
195 return None
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200196
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300197
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300198def check_files(files):
Anthony Kingb8607632015-05-01 22:06:37 +0300199 for f in files:
200 if not os.path.isfile(f):
201 print('You have no %s.' % f, file=sys.stderr)
202 return False
Anthony Kingb8607632015-05-01 22:06:37 +0300203 return True
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300204
Anthony Kingb8607632015-05-01 22:06:37 +0300205# ################################### MAIN ################################### #
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300206
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300207
Michael Bestasfd5d1362015-12-18 20:34:32 +0200208def upload_sources_crowdin(branch, config):
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300209 if config:
Michael Bestasfd5d1362015-12-18 20:34:32 +0200210 print('\nUploading sources to Crowdin (custom config)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200211 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200212 '--config=%s/config/%s' % (_DIR, config),
Michael Bestas44fbb352015-12-17 02:01:42 +0200213 'upload', 'sources', '--branch=%s' % branch])
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300214 else:
Michael Bestasfd5d1362015-12-18 20:34:32 +0200215 print('\nUploading sources to Crowdin (AOSP supported languages)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200216 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200217 '--config=%s/config/%s.yaml' % (_DIR, branch),
Michael Bestas44fbb352015-12-17 02:01:42 +0200218 'upload', 'sources', '--branch=%s' % branch])
Anthony King69a95382015-02-08 18:44:10 +0000219
Michael Bestasfd5d1362015-12-18 20:34:32 +0200220 print('\nUploading sources to Crowdin (non-AOSP supported languages)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200221 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200222 '--config=%s/config/%s_aosp.yaml' % (_DIR, branch),
Michael Bestas44fbb352015-12-17 02:01:42 +0200223 'upload', 'sources', '--branch=%s' % branch])
Anthony Kingb8607632015-05-01 22:06:37 +0300224
225
Michael Bestasfd5d1362015-12-18 20:34:32 +0200226def upload_translations_crowdin(branch, config):
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300227 if config:
Michael Bestasfd5d1362015-12-18 20:34:32 +0200228 print('\nUploading translations to Crowdin (custom config)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200229 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200230 '--config=%s/config/%s' % (_DIR, config),
Michael Bestasfd5d1362015-12-18 20:34:32 +0200231 'upload', 'translations', '--branch=%s' % branch,
232 '--no-import-duplicates', '--import-eq-suggestions',
233 '--auto-approve-imported'])
234 else:
235 print('\nUploading translations to Crowdin '
236 '(AOSP supported languages)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200237 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200238 '--config=%s/config/%s.yaml' % (_DIR, branch),
Michael Bestasfd5d1362015-12-18 20:34:32 +0200239 'upload', 'translations', '--branch=%s' % branch,
240 '--no-import-duplicates', '--import-eq-suggestions',
241 '--auto-approve-imported'])
242
243 print('\nUploading translations to Crowdin '
244 '(non-AOSP supported languages)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200245 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200246 '--config=%s/config/%s_aosp.yaml' % (_DIR, branch),
Michael Bestasfd5d1362015-12-18 20:34:32 +0200247 'upload', 'translations', '--branch=%s' % branch,
248 '--no-import-duplicates', '--import-eq-suggestions',
249 '--auto-approve-imported'])
250
251
Michael Bestas80b22ef2018-11-14 23:12:33 +0200252def download_crowdin(base_path, branch, xml, username, config):
Michael Bestasfd5d1362015-12-18 20:34:32 +0200253 if config:
254 print('\nDownloading translations from Crowdin (custom config)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200255 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200256 '--config=%s/config/%s' % (_DIR, config),
Michael Bestas44fbb352015-12-17 02:01:42 +0200257 'download', '--branch=%s' % branch])
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300258 else:
Michael Bestasfd5d1362015-12-18 20:34:32 +0200259 print('\nDownloading translations from Crowdin '
260 '(AOSP supported languages)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200261 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200262 '--config=%s/config/%s.yaml' % (_DIR, branch),
Michael Bestas44fbb352015-12-17 02:01:42 +0200263 'download', '--branch=%s' % branch])
Michael Bestas50579d22014-08-09 17:49:14 +0300264
Michael Bestasfd5d1362015-12-18 20:34:32 +0200265 print('\nDownloading translations from Crowdin '
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300266 '(non-AOSP supported languages)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200267 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200268 '--config=%s/config/%s_aosp.yaml' % (_DIR, branch),
Michael Bestas44fbb352015-12-17 02:01:42 +0200269 'download', '--branch=%s' % branch])
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200270
Michael Bestas99f5fce2015-06-04 22:07:51 +0300271 print('\nCreating a list of pushable translations')
Michael Bestas919053f2014-10-20 23:30:54 +0300272 # Get all files that Crowdin pushed
Anthony Kingb8607632015-05-01 22:06:37 +0300273 paths = []
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300274 if config:
Michael Bestas03bc7052016-03-12 03:19:10 +0200275 files = ['%s/config/%s' % (_DIR, config)]
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300276 else:
Michael Bestas03bc7052016-03-12 03:19:10 +0200277 files = ['%s/config/%s.yaml' % (_DIR, branch),
278 '%s/config/%s_aosp.yaml' % (_DIR, branch)]
Michael Bestas6c327e62015-05-02 01:58:01 +0300279 for c in files:
Michael Bestaseb4629a2018-11-14 23:03:18 +0200280 cmd = ['crowdin', '--config=%s' % c, 'list', 'project',
Michael Bestas44fbb352015-12-17 02:01:42 +0200281 '--branch=%s' % branch]
Anthony Kingb8607632015-05-01 22:06:37 +0300282 comm, ret = run_subprocess(cmd)
283 if ret != 0:
284 sys.exit(ret)
285 for p in str(comm[0]).split("\n"):
286 paths.append(p.replace('/%s' % branch, ''))
Michael Bestas50579d22014-08-09 17:49:14 +0300287
Michael Bestas99f5fce2015-06-04 22:07:51 +0300288 print('\nUploading translations to Gerrit')
Anthony Kingb8607632015-05-01 22:06:37 +0300289 items = [x for sub in xml for x in sub.getElementsByTagName('project')]
Michael Bestas919053f2014-10-20 23:30:54 +0300290 all_projects = []
291
Anthony Kingb8607632015-05-01 22:06:37 +0300292 for path in paths:
293 path = path.strip()
Michael Bestas919053f2014-10-20 23:30:54 +0300294 if not path:
295 continue
296
Anthony Kingb8607632015-05-01 22:06:37 +0300297 if "/res" not in path:
298 print('WARNING: Cannot determine project root dir of '
299 '[%s], skipping.' % path)
Anthony King69a95382015-02-08 18:44:10 +0000300 continue
Anthony Kingb8607632015-05-01 22:06:37 +0300301 result = path.split('/res')[0].strip('/')
302 if result == path.strip('/'):
303 print('WARNING: Cannot determine project root dir of '
304 '[%s], skipping.' % path)
305 continue
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200306
Michael Bestasc899b8c2015-03-03 00:53:19 +0200307 if result in all_projects:
Michael Bestasc899b8c2015-03-03 00:53:19 +0200308 continue
Michael Bestas50579d22014-08-09 17:49:14 +0300309
Anthony Kingb8607632015-05-01 22:06:37 +0300310 # When a project has multiple translatable files, Crowdin will
311 # give duplicates.
312 # We don't want that (useless empty commits), so we save each
313 # project in all_projects and check if it's already in there.
Michael Bestasc899b8c2015-03-03 00:53:19 +0200314 all_projects.append(result)
Anthony King69a95382015-02-08 18:44:10 +0000315
Michael Bestas42e25e32016-03-12 20:18:39 +0200316 # Search android/default.xml or config/%(branch)_extra_packages.xml
Anthony Kingb8607632015-05-01 22:06:37 +0300317 # for the project's name
318 for project in items:
319 path = project.attributes['path'].value
320 if not (result + '/').startswith(path +'/'):
Michael Bestasc899b8c2015-03-03 00:53:19 +0200321 continue
Anthony Kingb8607632015-05-01 22:06:37 +0300322 if result != path:
323 if path in all_projects:
324 break
325 result = path
326 all_projects.append(result)
Anthony King69a95382015-02-08 18:44:10 +0000327
Anthony Kingb8607632015-05-01 22:06:37 +0300328 br = project.getAttribute('revision') or branch
Anthony King69a95382015-02-08 18:44:10 +0000329
Michael Bestas118fcaf2015-06-04 23:02:20 +0300330 push_as_commit(base_path, result,
Michael Bestas80b22ef2018-11-14 23:12:33 +0200331 project.getAttribute('name'), br, username)
Anthony Kingb8607632015-05-01 22:06:37 +0300332 break
Anthony King69a95382015-02-08 18:44:10 +0000333
Anthony King69a95382015-02-08 18:44:10 +0000334
Anthony Kingb8607632015-05-01 22:06:37 +0300335def main():
Anthony Kingb8607632015-05-01 22:06:37 +0300336 args = parse_args()
337 default_branch = args.branch
Michael Bestas118fcaf2015-06-04 23:02:20 +0300338
Michael Wd13658a2019-01-13 14:05:37 +0100339 if args.submit:
340 if args.username is None:
341 print('Argument -u/--username is required for submitting!')
342 sys.exit(1)
343 submit_gerrit(default_branch, args.username)
344 sys.exit(0)
345
Michael Bestaseb4629a2018-11-14 23:03:18 +0200346 base_path_branch_suffix = default_branch.replace('-', '_').replace('.', '_').upper()
347 base_path_env = 'LINEAGE_CROWDIN_BASE_PATH_%s' % base_path_branch_suffix
348 base_path = os.getenv(base_path_env)
Michael Bestas118fcaf2015-06-04 23:02:20 +0300349 if base_path is None:
Anthony Kingd0d56cf2015-06-05 10:48:38 +0100350 cwd = os.getcwd()
Michael Bestaseb4629a2018-11-14 23:03:18 +0200351 print('You have not set %s. Defaulting to %s' % (base_path_env, cwd))
Michael Bestas118fcaf2015-06-04 23:02:20 +0300352 base_path = cwd
Michael Bestas118fcaf2015-06-04 23:02:20 +0300353 if not os.path.isdir(base_path):
Michael Bestaseb4629a2018-11-14 23:03:18 +0200354 print('%s is not a real directory: %s' % (base_path_env, base_path))
Michael Bestas118fcaf2015-06-04 23:02:20 +0300355 sys.exit(1)
Anthony Kingb8607632015-05-01 22:06:37 +0300356
Michael Bestas99f5fce2015-06-04 22:07:51 +0300357 if not check_dependencies():
358 sys.exit(1)
Anthony Kingb8607632015-05-01 22:06:37 +0300359
Michael Bestas118fcaf2015-06-04 23:02:20 +0300360 xml_android = load_xml(x='%s/android/default.xml' % base_path)
Anthony Kingb8607632015-05-01 22:06:37 +0300361 if xml_android is None:
362 sys.exit(1)
363
Michael Bestas42e25e32016-03-12 20:18:39 +0200364 xml_extra = load_xml(x='%s/config/%s_extra_packages.xml'
Anthony Kingd0d56cf2015-06-05 10:48:38 +0100365 % (_DIR, default_branch))
Anthony Kingb8607632015-05-01 22:06:37 +0300366 if xml_extra is None:
367 sys.exit(1)
368
Michael Bestas19dc3352018-02-03 20:24:00 +0200369 xml_snippet = load_xml(x='%s/android/snippets/lineage.xml' % base_path)
370 if xml_snippet is None:
371 xml_snippet = load_xml(x='%s/android/snippets/cm.xml' % base_path)
372 if xml_snippet is None:
373 xml_snippet = load_xml(x='%s/android/snippets/hal_cm_all.xml' % base_path)
374 if xml_snippet is not None:
375 xml_files = (xml_android, xml_snippet, xml_extra)
Michael Bestas687679f2016-12-07 23:20:12 +0200376 else:
377 xml_files = (xml_android, xml_extra)
378
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300379 if args.config:
Michael Bestas03bc7052016-03-12 03:19:10 +0200380 files = ['%s/config/%s' % (_DIR, args.config)]
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300381 else:
Michael Bestas03bc7052016-03-12 03:19:10 +0200382 files = ['%s/config/%s.yaml' % (_DIR, default_branch),
383 '%s/config/%s_aosp.yaml' % (_DIR, default_branch)]
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300384 if not check_files(files):
Anthony Kingb8607632015-05-01 22:06:37 +0300385 sys.exit(1)
386
Michael Bestasfd5d1362015-12-18 20:34:32 +0200387 if args.download and args.username is None:
388 print('Argument -u/--username is required for translations download')
389 sys.exit(1)
390
391 if args.upload_sources:
392 upload_sources_crowdin(default_branch, args.config)
393 if args.upload_translations:
394 upload_translations_crowdin(default_branch, args.config)
395 if args.download:
Michael Bestas687679f2016-12-07 23:20:12 +0200396 download_crowdin(base_path, default_branch, xml_files,
Michael Bestas80b22ef2018-11-14 23:12:33 +0200397 args.username, args.config)
Tom Powell44256852016-07-06 15:23:25 -0700398
399 if _COMMITS_CREATED:
400 print('\nDone!')
401 sys.exit(0)
Tom Powellf42586f2016-07-11 11:02:54 -0700402 else:
Tom Powell44256852016-07-06 15:23:25 -0700403 print('\nNothing to commit')
404 sys.exit(-1)
Anthony Kingb8607632015-05-01 22:06:37 +0300405
406if __name__ == '__main__':
407 main()