blob: 19893d5f4c0a8e9a5585cdc271806c7ec04722da [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
Michael W6e0a7032019-02-27 17:04:16 +010028import json
Marco Brohetcb5cdb42014-07-11 22:41:53 +020029import git
30import os
Marco Brohetcb5cdb42014-07-11 22:41:53 +020031import subprocess
32import sys
Anthony Kingb8607632015-05-01 22:06:37 +030033
Marco Brohetcb5cdb42014-07-11 22:41:53 +020034from xml.dom import minidom
35
Anthony Kingd0d56cf2015-06-05 10:48:38 +010036# ################################# GLOBALS ################################## #
37
38_DIR = os.path.dirname(os.path.realpath(__file__))
Tom Powell44256852016-07-06 15:23:25 -070039_COMMITS_CREATED = False
Anthony Kingd0d56cf2015-06-05 10:48:38 +010040
Anthony Kingb8607632015-05-01 22:06:37 +030041# ################################ FUNCTIONS ################################# #
42
43
44def run_subprocess(cmd, silent=False):
45 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
46 universal_newlines=True)
47 comm = p.communicate()
48 exit_code = p.returncode
49 if exit_code != 0 and not silent:
50 print("There was an error running the subprocess.\n"
51 "cmd: %s\n"
52 "exit code: %d\n"
53 "stdout: %s\n"
54 "stderr: %s" % (cmd, exit_code, comm[0], comm[1]),
55 file=sys.stderr)
56 return comm, exit_code
57
Marco Brohet6b6b4e52014-07-20 00:05:16 +020058
Michael Bestas80b22ef2018-11-14 23:12:33 +020059def push_as_commit(base_path, path, name, branch, username):
Anthony Kingb8607632015-05-01 22:06:37 +030060 print('Committing %s on branch %s' % (name, branch))
Marco Brohetcb5cdb42014-07-11 22:41:53 +020061
62 # Get path
Michael Bestas118fcaf2015-06-04 23:02:20 +030063 path = os.path.join(base_path, path)
Anthony Kingb8607632015-05-01 22:06:37 +030064 if not path.endswith('.git'):
65 path = os.path.join(path, '.git')
Marco Brohetcb5cdb42014-07-11 22:41:53 +020066
Marco Brohet6b6b4e52014-07-20 00:05:16 +020067 # Create repo object
Marco Brohetcb5cdb42014-07-11 22:41:53 +020068 repo = git.Repo(path)
Marco Brohet6b6b4e52014-07-20 00:05:16 +020069
70 # Remove previously deleted files from Git
Anthony Kingb8607632015-05-01 22:06:37 +030071 files = repo.git.ls_files(d=True).split('\n')
72 if files and files[0]:
73 repo.git.rm(files)
Marco Brohet6b6b4e52014-07-20 00:05:16 +020074
75 # Add all files to commit
Marco Brohetcb5cdb42014-07-11 22:41:53 +020076 repo.git.add('-A')
Marco Brohet6b6b4e52014-07-20 00:05:16 +020077
78 # Create commit; if it fails, probably empty so skipping
Marco Brohetcb5cdb42014-07-11 22:41:53 +020079 try:
Michael Bestas80b22ef2018-11-14 23:12:33 +020080 repo.git.commit(m='Automatic translation import')
Marco Brohetcb5cdb42014-07-11 22:41:53 +020081 except:
Anthony Kingb8607632015-05-01 22:06:37 +030082 print('Failed to create commit for %s, probably empty: skipping'
83 % name, file=sys.stderr)
Marco Brohetcb5cdb42014-07-11 22:41:53 +020084 return
Marco Brohet6b6b4e52014-07-20 00:05:16 +020085
86 # Push commit
Michael Bestasf96f67b2014-10-21 00:43:37 +030087 try:
Abhisek Devkotab78def42016-12-27 13:06:52 -080088 repo.git.push('ssh://%s@review.lineageos.org:29418/%s' % (username, name),
Anthony Kingb8607632015-05-01 22:06:37 +030089 'HEAD:refs/for/%s%%topic=translation' % branch)
90 print('Successfully pushed commit for %s' % name)
Michael Bestasf96f67b2014-10-21 00:43:37 +030091 except:
Anthony Kingb8607632015-05-01 22:06:37 +030092 print('Failed to push commit for %s' % name, file=sys.stderr)
Marco Brohetcb5cdb42014-07-11 22:41:53 +020093
Tom Powell44256852016-07-06 15:23:25 -070094 _COMMITS_CREATED = True
95
Anthony Kingb8607632015-05-01 22:06:37 +030096
Michael Wd13658a2019-01-13 14:05:37 +010097def submit_gerrit(branch, username):
98 # Find all open translation changes
99 cmd = ['ssh', '-p', '29418',
100 '{}@review.lineageos.org'.format(username),
101 'gerrit', 'query',
102 'status:open',
103 'branch:{}'.format(branch),
104 'message:"Automatic translation import"',
105 'topic:translation',
Michael W6e0a7032019-02-27 17:04:16 +0100106 '--current-patch-set',
107 '--format=JSON']
108 commits = 0
Michael Wd13658a2019-01-13 14:05:37 +0100109 msg, code = run_subprocess(cmd)
110 if code != 0:
111 print('Failed: {0}'.format(msg[1]))
112 return
113
Michael W6e0a7032019-02-27 17:04:16 +0100114 # Each line is one valid JSON object, except the last one, which is empty
115 for line in msg[0].strip('\n').split('\n'):
116 js = json.loads(line)
117 # We get valid JSON, but not every result line is one we want
118 if not 'currentPatchSet' in js or not 'revision' in js['currentPatchSet']:
119 continue
Michael Wd13658a2019-01-13 14:05:37 +0100120 # Add Code-Review +2 and Verified+1 labels and submit
121 cmd = ['ssh', '-p', '29418',
122 '{}@review.lineageos.org'.format(username),
123 'gerrit', 'review',
124 '--verified +1',
125 '--code-review +2',
Michael W6e0a7032019-02-27 17:04:16 +0100126 '--submit', js['currentPatchSet']['revision']]
Michael Wd13658a2019-01-13 14:05:37 +0100127 msg, code = run_subprocess(cmd, True)
128 if code != 0:
129 errorText = msg[1].replace('\n\n', '; ').replace('\n', '')
Michael W6e0a7032019-02-27 17:04:16 +0100130 print('Submitting commit {0} failed: {1}'.format(js['url'], errorText))
Michael Wd13658a2019-01-13 14:05:37 +0100131 else:
Michael W6e0a7032019-02-27 17:04:16 +0100132 print('Success when submitting commit {0}'.format(js['url']))
133
134 commits += 1
135
136 if commits == 0:
137 print("Nothing to submit!")
138 return
Michael Wd13658a2019-01-13 14:05:37 +0100139
140
Anthony Kingb8607632015-05-01 22:06:37 +0300141def check_run(cmd):
Michael Bestas97677e12015-02-08 13:11:59 +0200142 p = subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr)
143 ret = p.wait()
144 if ret != 0:
Anthony Kingb8607632015-05-01 22:06:37 +0300145 print('Failed to run cmd: %s' % ' '.join(cmd), file=sys.stderr)
Michael Bestas97677e12015-02-08 13:11:59 +0200146 sys.exit(ret)
147
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200148
Michael Bestas118fcaf2015-06-04 23:02:20 +0300149def find_xml(base_path):
150 for dp, dn, file_names in os.walk(base_path):
Anthony Kingb8607632015-05-01 22:06:37 +0300151 for f in file_names:
152 if os.path.splitext(f)[1] == '.xml':
153 yield os.path.join(dp, f)
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200154
Anthony Kingb8607632015-05-01 22:06:37 +0300155# ############################################################################ #
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200156
Michael Bestas6b6db122015-02-08 13:22:22 +0200157
Anthony Kingb8607632015-05-01 22:06:37 +0300158def parse_args():
159 parser = argparse.ArgumentParser(
Abhisek Devkotab78def42016-12-27 13:06:52 -0800160 description="Synchronising LineageOS' translations with Crowdin")
Michael Bestasfd5d1362015-12-18 20:34:32 +0200161 parser.add_argument('-u', '--username', help='Gerrit username')
Abhisek Devkotab78def42016-12-27 13:06:52 -0800162 parser.add_argument('-b', '--branch', help='LineageOS branch',
Anthony Kingb8607632015-05-01 22:06:37 +0300163 required=True)
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300164 parser.add_argument('-c', '--config', help='Custom yaml config')
Michael Bestasfd5d1362015-12-18 20:34:32 +0200165 parser.add_argument('--upload-sources', action='store_true',
166 help='Upload sources to Crowdin')
167 parser.add_argument('--upload-translations', action='store_true',
168 help='Upload translations to Crowdin')
169 parser.add_argument('--download', action='store_true',
170 help='Download translations from Crowdin')
Michael Wd13658a2019-01-13 14:05:37 +0100171 parser.add_argument('-s', '--submit', action='store_true',
172 help='Merge open translation commits')
Anthony Kingb8607632015-05-01 22:06:37 +0300173 return parser.parse_args()
Michael Bestas6b6db122015-02-08 13:22:22 +0200174
Anthony Kingb8607632015-05-01 22:06:37 +0300175# ################################# PREPARE ################################## #
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200176
Anthony Kingb8607632015-05-01 22:06:37 +0300177
178def check_dependencies():
Michael Bestaseb4629a2018-11-14 23:03:18 +0200179 # Check for Java version of crowdin
180 cmd = ['dpkg-query', '-W', 'crowdin']
Anthony Kingb8607632015-05-01 22:06:37 +0300181 if run_subprocess(cmd, silent=True)[1] != 0:
Michael Bestaseb4629a2018-11-14 23:03:18 +0200182 print('You have not installed crowdin.', file=sys.stderr)
Anthony Kingb8607632015-05-01 22:06:37 +0300183 return False
Anthony Kingb8607632015-05-01 22:06:37 +0300184 return True
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200185
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200186
Michael Bestas118fcaf2015-06-04 23:02:20 +0300187def load_xml(x):
Anthony Kingb8607632015-05-01 22:06:37 +0300188 try:
189 return minidom.parse(x)
190 except IOError:
191 print('You have no %s.' % x, file=sys.stderr)
192 return None
193 except Exception:
194 # TODO: minidom should not be used.
195 print('Malformed %s.' % x, file=sys.stderr)
196 return None
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200197
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300198
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300199def check_files(files):
Anthony Kingb8607632015-05-01 22:06:37 +0300200 for f in files:
201 if not os.path.isfile(f):
202 print('You have no %s.' % f, file=sys.stderr)
203 return False
Anthony Kingb8607632015-05-01 22:06:37 +0300204 return True
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300205
Anthony Kingb8607632015-05-01 22:06:37 +0300206# ################################### MAIN ################################### #
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300207
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300208
Michael Bestasfd5d1362015-12-18 20:34:32 +0200209def upload_sources_crowdin(branch, config):
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300210 if config:
Michael Bestasfd5d1362015-12-18 20:34:32 +0200211 print('\nUploading sources to Crowdin (custom config)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200212 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200213 '--config=%s/config/%s' % (_DIR, config),
Michael Bestas44fbb352015-12-17 02:01:42 +0200214 'upload', 'sources', '--branch=%s' % branch])
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300215 else:
Michael Bestasfd5d1362015-12-18 20:34:32 +0200216 print('\nUploading sources to Crowdin (AOSP supported languages)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200217 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200218 '--config=%s/config/%s.yaml' % (_DIR, branch),
Michael Bestas44fbb352015-12-17 02:01:42 +0200219 'upload', 'sources', '--branch=%s' % branch])
Anthony King69a95382015-02-08 18:44:10 +0000220
Michael Bestasfd5d1362015-12-18 20:34:32 +0200221 print('\nUploading sources to Crowdin (non-AOSP supported languages)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200222 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200223 '--config=%s/config/%s_aosp.yaml' % (_DIR, branch),
Michael Bestas44fbb352015-12-17 02:01:42 +0200224 'upload', 'sources', '--branch=%s' % branch])
Anthony Kingb8607632015-05-01 22:06:37 +0300225
226
Michael Bestasfd5d1362015-12-18 20:34:32 +0200227def upload_translations_crowdin(branch, config):
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300228 if config:
Michael Bestasfd5d1362015-12-18 20:34:32 +0200229 print('\nUploading translations to Crowdin (custom config)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200230 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200231 '--config=%s/config/%s' % (_DIR, config),
Michael Bestasfd5d1362015-12-18 20:34:32 +0200232 'upload', 'translations', '--branch=%s' % branch,
233 '--no-import-duplicates', '--import-eq-suggestions',
234 '--auto-approve-imported'])
235 else:
236 print('\nUploading translations to Crowdin '
237 '(AOSP supported languages)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200238 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200239 '--config=%s/config/%s.yaml' % (_DIR, branch),
Michael Bestasfd5d1362015-12-18 20:34:32 +0200240 'upload', 'translations', '--branch=%s' % branch,
241 '--no-import-duplicates', '--import-eq-suggestions',
242 '--auto-approve-imported'])
243
244 print('\nUploading translations to Crowdin '
245 '(non-AOSP supported languages)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200246 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200247 '--config=%s/config/%s_aosp.yaml' % (_DIR, branch),
Michael Bestasfd5d1362015-12-18 20:34:32 +0200248 'upload', 'translations', '--branch=%s' % branch,
249 '--no-import-duplicates', '--import-eq-suggestions',
250 '--auto-approve-imported'])
251
252
Michael Bestas80b22ef2018-11-14 23:12:33 +0200253def download_crowdin(base_path, branch, xml, username, config):
Michael Bestasfd5d1362015-12-18 20:34:32 +0200254 if config:
255 print('\nDownloading translations from Crowdin (custom config)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200256 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200257 '--config=%s/config/%s' % (_DIR, config),
Michael Bestas44fbb352015-12-17 02:01:42 +0200258 'download', '--branch=%s' % branch])
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300259 else:
Michael Bestasfd5d1362015-12-18 20:34:32 +0200260 print('\nDownloading translations from Crowdin '
261 '(AOSP supported languages)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200262 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200263 '--config=%s/config/%s.yaml' % (_DIR, branch),
Michael Bestas44fbb352015-12-17 02:01:42 +0200264 'download', '--branch=%s' % branch])
Michael Bestas50579d22014-08-09 17:49:14 +0300265
Michael Bestasfd5d1362015-12-18 20:34:32 +0200266 print('\nDownloading translations from Crowdin '
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300267 '(non-AOSP supported languages)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200268 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200269 '--config=%s/config/%s_aosp.yaml' % (_DIR, branch),
Michael Bestas44fbb352015-12-17 02:01:42 +0200270 'download', '--branch=%s' % branch])
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200271
Michael Bestas99f5fce2015-06-04 22:07:51 +0300272 print('\nCreating a list of pushable translations')
Michael Bestas919053f2014-10-20 23:30:54 +0300273 # Get all files that Crowdin pushed
Anthony Kingb8607632015-05-01 22:06:37 +0300274 paths = []
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300275 if config:
Michael Bestas03bc7052016-03-12 03:19:10 +0200276 files = ['%s/config/%s' % (_DIR, config)]
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300277 else:
Michael Bestas03bc7052016-03-12 03:19:10 +0200278 files = ['%s/config/%s.yaml' % (_DIR, branch),
279 '%s/config/%s_aosp.yaml' % (_DIR, branch)]
Michael Bestas6c327e62015-05-02 01:58:01 +0300280 for c in files:
Michael Bestaseb4629a2018-11-14 23:03:18 +0200281 cmd = ['crowdin', '--config=%s' % c, 'list', 'project',
Michael Bestas44fbb352015-12-17 02:01:42 +0200282 '--branch=%s' % branch]
Anthony Kingb8607632015-05-01 22:06:37 +0300283 comm, ret = run_subprocess(cmd)
284 if ret != 0:
285 sys.exit(ret)
286 for p in str(comm[0]).split("\n"):
287 paths.append(p.replace('/%s' % branch, ''))
Michael Bestas50579d22014-08-09 17:49:14 +0300288
Michael Bestas99f5fce2015-06-04 22:07:51 +0300289 print('\nUploading translations to Gerrit')
Anthony Kingb8607632015-05-01 22:06:37 +0300290 items = [x for sub in xml for x in sub.getElementsByTagName('project')]
Michael Bestas919053f2014-10-20 23:30:54 +0300291 all_projects = []
292
Anthony Kingb8607632015-05-01 22:06:37 +0300293 for path in paths:
294 path = path.strip()
Michael Bestas919053f2014-10-20 23:30:54 +0300295 if not path:
296 continue
297
Anthony Kingb8607632015-05-01 22:06:37 +0300298 if "/res" not in path:
299 print('WARNING: Cannot determine project root dir of '
300 '[%s], skipping.' % path)
Anthony King69a95382015-02-08 18:44:10 +0000301 continue
Anthony Kingb8607632015-05-01 22:06:37 +0300302 result = path.split('/res')[0].strip('/')
303 if result == path.strip('/'):
304 print('WARNING: Cannot determine project root dir of '
305 '[%s], skipping.' % path)
306 continue
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200307
Michael Bestasc899b8c2015-03-03 00:53:19 +0200308 if result in all_projects:
Michael Bestasc899b8c2015-03-03 00:53:19 +0200309 continue
Michael Bestas50579d22014-08-09 17:49:14 +0300310
Anthony Kingb8607632015-05-01 22:06:37 +0300311 # When a project has multiple translatable files, Crowdin will
312 # give duplicates.
313 # We don't want that (useless empty commits), so we save each
314 # project in all_projects and check if it's already in there.
Michael Bestasc899b8c2015-03-03 00:53:19 +0200315 all_projects.append(result)
Anthony King69a95382015-02-08 18:44:10 +0000316
Michael Bestas42e25e32016-03-12 20:18:39 +0200317 # Search android/default.xml or config/%(branch)_extra_packages.xml
Anthony Kingb8607632015-05-01 22:06:37 +0300318 # for the project's name
319 for project in items:
320 path = project.attributes['path'].value
321 if not (result + '/').startswith(path +'/'):
Michael Bestasc899b8c2015-03-03 00:53:19 +0200322 continue
Anthony Kingb8607632015-05-01 22:06:37 +0300323 if result != path:
324 if path in all_projects:
325 break
326 result = path
327 all_projects.append(result)
Anthony King69a95382015-02-08 18:44:10 +0000328
Anthony Kingb8607632015-05-01 22:06:37 +0300329 br = project.getAttribute('revision') or branch
Anthony King69a95382015-02-08 18:44:10 +0000330
Michael Bestas118fcaf2015-06-04 23:02:20 +0300331 push_as_commit(base_path, result,
Michael Bestas80b22ef2018-11-14 23:12:33 +0200332 project.getAttribute('name'), br, username)
Anthony Kingb8607632015-05-01 22:06:37 +0300333 break
Anthony King69a95382015-02-08 18:44:10 +0000334
Anthony King69a95382015-02-08 18:44:10 +0000335
Anthony Kingb8607632015-05-01 22:06:37 +0300336def main():
Anthony Kingb8607632015-05-01 22:06:37 +0300337 args = parse_args()
338 default_branch = args.branch
Michael Bestas118fcaf2015-06-04 23:02:20 +0300339
Michael Wd13658a2019-01-13 14:05:37 +0100340 if args.submit:
341 if args.username is None:
342 print('Argument -u/--username is required for submitting!')
343 sys.exit(1)
344 submit_gerrit(default_branch, args.username)
345 sys.exit(0)
346
Michael Bestaseb4629a2018-11-14 23:03:18 +0200347 base_path_branch_suffix = default_branch.replace('-', '_').replace('.', '_').upper()
348 base_path_env = 'LINEAGE_CROWDIN_BASE_PATH_%s' % base_path_branch_suffix
349 base_path = os.getenv(base_path_env)
Michael Bestas118fcaf2015-06-04 23:02:20 +0300350 if base_path is None:
Anthony Kingd0d56cf2015-06-05 10:48:38 +0100351 cwd = os.getcwd()
Michael Bestaseb4629a2018-11-14 23:03:18 +0200352 print('You have not set %s. Defaulting to %s' % (base_path_env, cwd))
Michael Bestas118fcaf2015-06-04 23:02:20 +0300353 base_path = cwd
Michael Bestas118fcaf2015-06-04 23:02:20 +0300354 if not os.path.isdir(base_path):
Michael Bestaseb4629a2018-11-14 23:03:18 +0200355 print('%s is not a real directory: %s' % (base_path_env, base_path))
Michael Bestas118fcaf2015-06-04 23:02:20 +0300356 sys.exit(1)
Anthony Kingb8607632015-05-01 22:06:37 +0300357
Michael Bestas99f5fce2015-06-04 22:07:51 +0300358 if not check_dependencies():
359 sys.exit(1)
Anthony Kingb8607632015-05-01 22:06:37 +0300360
Michael Bestas118fcaf2015-06-04 23:02:20 +0300361 xml_android = load_xml(x='%s/android/default.xml' % base_path)
Anthony Kingb8607632015-05-01 22:06:37 +0300362 if xml_android is None:
363 sys.exit(1)
364
Michael Bestas42e25e32016-03-12 20:18:39 +0200365 xml_extra = load_xml(x='%s/config/%s_extra_packages.xml'
Anthony Kingd0d56cf2015-06-05 10:48:38 +0100366 % (_DIR, default_branch))
Anthony Kingb8607632015-05-01 22:06:37 +0300367 if xml_extra is None:
368 sys.exit(1)
369
Michael Bestas19dc3352018-02-03 20:24:00 +0200370 xml_snippet = load_xml(x='%s/android/snippets/lineage.xml' % base_path)
371 if xml_snippet is None:
372 xml_snippet = load_xml(x='%s/android/snippets/cm.xml' % base_path)
373 if xml_snippet is None:
374 xml_snippet = load_xml(x='%s/android/snippets/hal_cm_all.xml' % base_path)
375 if xml_snippet is not None:
376 xml_files = (xml_android, xml_snippet, xml_extra)
Michael Bestas687679f2016-12-07 23:20:12 +0200377 else:
378 xml_files = (xml_android, xml_extra)
379
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300380 if args.config:
Michael Bestas03bc7052016-03-12 03:19:10 +0200381 files = ['%s/config/%s' % (_DIR, args.config)]
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300382 else:
Michael Bestas03bc7052016-03-12 03:19:10 +0200383 files = ['%s/config/%s.yaml' % (_DIR, default_branch),
384 '%s/config/%s_aosp.yaml' % (_DIR, default_branch)]
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300385 if not check_files(files):
Anthony Kingb8607632015-05-01 22:06:37 +0300386 sys.exit(1)
387
Michael Bestasfd5d1362015-12-18 20:34:32 +0200388 if args.download and args.username is None:
389 print('Argument -u/--username is required for translations download')
390 sys.exit(1)
391
392 if args.upload_sources:
393 upload_sources_crowdin(default_branch, args.config)
394 if args.upload_translations:
395 upload_translations_crowdin(default_branch, args.config)
396 if args.download:
Michael Bestas687679f2016-12-07 23:20:12 +0200397 download_crowdin(base_path, default_branch, xml_files,
Michael Bestas80b22ef2018-11-14 23:12:33 +0200398 args.username, args.config)
Tom Powell44256852016-07-06 15:23:25 -0700399
400 if _COMMITS_CREATED:
401 print('\nDone!')
402 sys.exit(0)
Tom Powellf42586f2016-07-11 11:02:54 -0700403 else:
Tom Powell44256852016-07-06 15:23:25 -0700404 print('\nNothing to commit')
405 sys.exit(-1)
Anthony Kingb8607632015-05-01 22:06:37 +0300406
407if __name__ == '__main__':
408 main()