blob: 7b5632f40801cb981a916c9e89e657ca17ba902b [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
Michael W1bb2f922019-02-27 17:46:28 +01009# Copyright (C) 2017-2019 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
Michael W1bb2f922019-02-27 17:46:28 +010033import yaml
Anthony Kingb8607632015-05-01 22:06:37 +030034
Marco Brohetcb5cdb42014-07-11 22:41:53 +020035from xml.dom import minidom
36
Anthony Kingd0d56cf2015-06-05 10:48:38 +010037# ################################# GLOBALS ################################## #
38
39_DIR = os.path.dirname(os.path.realpath(__file__))
Tom Powell44256852016-07-06 15:23:25 -070040_COMMITS_CREATED = False
Anthony Kingd0d56cf2015-06-05 10:48:38 +010041
Anthony Kingb8607632015-05-01 22:06:37 +030042# ################################ FUNCTIONS ################################# #
43
44
45def run_subprocess(cmd, silent=False):
46 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
47 universal_newlines=True)
48 comm = p.communicate()
49 exit_code = p.returncode
50 if exit_code != 0 and not silent:
51 print("There was an error running the subprocess.\n"
52 "cmd: %s\n"
53 "exit code: %d\n"
54 "stdout: %s\n"
55 "stderr: %s" % (cmd, exit_code, comm[0], comm[1]),
56 file=sys.stderr)
57 return comm, exit_code
58
Marco Brohet6b6b4e52014-07-20 00:05:16 +020059
Michael W1bb2f922019-02-27 17:46:28 +010060def add_target_paths(config_files, repo, project_path):
61 # Add or remove the files given in the config files to the commit
62 count = 0
63 file_paths = []
64 for f in config_files:
65 fh = open(f, "r")
66 try:
67 config = yaml.load(fh)
68 for tf in config['files']:
69 if project_path in tf['source']:
70 target_path = tf['translation']
71 lang_codes = tf['languages_mapping']['android_code']
72 for l in lang_codes:
73 lpath = get_target_path(tf['translation'], tf['source'],
74 lang_codes[l], project_path)
75 file_paths.append(lpath)
76 except yaml.YAMLError as e:
77 print(e, '\n Could not parse YAML.')
78 exit()
79 fh.close()
80
81 modified = repo.git.ls_files(m=True)
82 for m in modified.split('\n'):
83 if m in file_paths:
84 repo.git.add(m)
85 count += 1
86
87 deleted = repo.git.ls_files(d=True)
88 for d in deleted.split('\n'):
89 if d in file_paths:
90 repo.git.rm(d)
91 count += 1
92
93 return count
94
95
96def split_path(path):
97 # Split the given string to path and filename
98 if '/' in path:
99 original_file_name = path[1:][path.rfind("/"):]
100 original_path = path[:path.rfind("/")]
101 else:
102 original_file_name = path
103 original_path = ''
104
105 return original_path, original_file_name
106
107
108def get_target_path(pattern, source, lang, project_path):
109 # Make strings like '/%original_path%-%android_code%/%original_file_name%' valid file paths
110 # based on the source string's path
111 original_path, original_file_name = split_path(source)
112
113 target_path = pattern #.lstrip('/')
114 target_path = target_path.replace('%original_path%', original_path)
115 target_path = target_path.replace('%android_code%', lang)
116 target_path = target_path.replace('%original_file_name%', original_file_name)
117 target_path = target_path.replace(project_path, '')
118 target_path = target_path.lstrip('/')
119 return target_path
120
121
122def push_as_commit(config_files, base_path, path, name, branch, username):
Anthony Kingb8607632015-05-01 22:06:37 +0300123 print('Committing %s on branch %s' % (name, branch))
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200124
125 # Get path
Michael W1bb2f922019-02-27 17:46:28 +0100126 project_path = path
Michael Bestas118fcaf2015-06-04 23:02:20 +0300127 path = os.path.join(base_path, path)
Anthony Kingb8607632015-05-01 22:06:37 +0300128 if not path.endswith('.git'):
129 path = os.path.join(path, '.git')
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200130
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200131 # Create repo object
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200132 repo = git.Repo(path)
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200133
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200134 # Add all files to commit
Michael W1bb2f922019-02-27 17:46:28 +0100135 count = add_target_paths(config_files, repo, project_path)
136
137 if count == 0:
138 print('Nothing to commit')
139 return
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200140
141 # Create commit; if it fails, probably empty so skipping
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200142 try:
Michael Bestas80b22ef2018-11-14 23:12:33 +0200143 repo.git.commit(m='Automatic translation import')
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200144 except:
Anthony Kingb8607632015-05-01 22:06:37 +0300145 print('Failed to create commit for %s, probably empty: skipping'
146 % name, file=sys.stderr)
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200147 return
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200148
149 # Push commit
Michael Bestasf96f67b2014-10-21 00:43:37 +0300150 try:
Abhisek Devkotab78def42016-12-27 13:06:52 -0800151 repo.git.push('ssh://%s@review.lineageos.org:29418/%s' % (username, name),
Anthony Kingb8607632015-05-01 22:06:37 +0300152 'HEAD:refs/for/%s%%topic=translation' % branch)
153 print('Successfully pushed commit for %s' % name)
Michael Bestasf96f67b2014-10-21 00:43:37 +0300154 except:
Anthony Kingb8607632015-05-01 22:06:37 +0300155 print('Failed to push commit for %s' % name, file=sys.stderr)
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200156
Tom Powell44256852016-07-06 15:23:25 -0700157 _COMMITS_CREATED = True
158
Anthony Kingb8607632015-05-01 22:06:37 +0300159
Michael Wd13658a2019-01-13 14:05:37 +0100160def submit_gerrit(branch, username):
161 # Find all open translation changes
162 cmd = ['ssh', '-p', '29418',
163 '{}@review.lineageos.org'.format(username),
164 'gerrit', 'query',
165 'status:open',
166 'branch:{}'.format(branch),
167 'message:"Automatic translation import"',
168 'topic:translation',
Michael W6e0a7032019-02-27 17:04:16 +0100169 '--current-patch-set',
170 '--format=JSON']
171 commits = 0
Michael Wd13658a2019-01-13 14:05:37 +0100172 msg, code = run_subprocess(cmd)
173 if code != 0:
174 print('Failed: {0}'.format(msg[1]))
175 return
176
Michael W6e0a7032019-02-27 17:04:16 +0100177 # Each line is one valid JSON object, except the last one, which is empty
178 for line in msg[0].strip('\n').split('\n'):
179 js = json.loads(line)
180 # We get valid JSON, but not every result line is one we want
181 if not 'currentPatchSet' in js or not 'revision' in js['currentPatchSet']:
182 continue
Michael Wd13658a2019-01-13 14:05:37 +0100183 # Add Code-Review +2 and Verified+1 labels and submit
184 cmd = ['ssh', '-p', '29418',
185 '{}@review.lineageos.org'.format(username),
186 'gerrit', 'review',
187 '--verified +1',
188 '--code-review +2',
Michael W6e0a7032019-02-27 17:04:16 +0100189 '--submit', js['currentPatchSet']['revision']]
Michael Wd13658a2019-01-13 14:05:37 +0100190 msg, code = run_subprocess(cmd, True)
191 if code != 0:
192 errorText = msg[1].replace('\n\n', '; ').replace('\n', '')
Michael W6e0a7032019-02-27 17:04:16 +0100193 print('Submitting commit {0} failed: {1}'.format(js['url'], errorText))
Michael Wd13658a2019-01-13 14:05:37 +0100194 else:
Michael W6e0a7032019-02-27 17:04:16 +0100195 print('Success when submitting commit {0}'.format(js['url']))
196
197 commits += 1
198
199 if commits == 0:
200 print("Nothing to submit!")
201 return
Michael Wd13658a2019-01-13 14:05:37 +0100202
203
Anthony Kingb8607632015-05-01 22:06:37 +0300204def check_run(cmd):
Michael Bestas97677e12015-02-08 13:11:59 +0200205 p = subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr)
206 ret = p.wait()
207 if ret != 0:
Anthony Kingb8607632015-05-01 22:06:37 +0300208 print('Failed to run cmd: %s' % ' '.join(cmd), file=sys.stderr)
Michael Bestas97677e12015-02-08 13:11:59 +0200209 sys.exit(ret)
210
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200211
Michael Bestas118fcaf2015-06-04 23:02:20 +0300212def find_xml(base_path):
213 for dp, dn, file_names in os.walk(base_path):
Anthony Kingb8607632015-05-01 22:06:37 +0300214 for f in file_names:
215 if os.path.splitext(f)[1] == '.xml':
216 yield os.path.join(dp, f)
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200217
Anthony Kingb8607632015-05-01 22:06:37 +0300218# ############################################################################ #
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200219
Michael Bestas6b6db122015-02-08 13:22:22 +0200220
Anthony Kingb8607632015-05-01 22:06:37 +0300221def parse_args():
222 parser = argparse.ArgumentParser(
Abhisek Devkotab78def42016-12-27 13:06:52 -0800223 description="Synchronising LineageOS' translations with Crowdin")
Michael Bestasfd5d1362015-12-18 20:34:32 +0200224 parser.add_argument('-u', '--username', help='Gerrit username')
Abhisek Devkotab78def42016-12-27 13:06:52 -0800225 parser.add_argument('-b', '--branch', help='LineageOS branch',
Anthony Kingb8607632015-05-01 22:06:37 +0300226 required=True)
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300227 parser.add_argument('-c', '--config', help='Custom yaml config')
Michael Bestasfd5d1362015-12-18 20:34:32 +0200228 parser.add_argument('--upload-sources', action='store_true',
229 help='Upload sources to Crowdin')
230 parser.add_argument('--upload-translations', action='store_true',
231 help='Upload translations to Crowdin')
232 parser.add_argument('--download', action='store_true',
233 help='Download translations from Crowdin')
Michael Wd13658a2019-01-13 14:05:37 +0100234 parser.add_argument('-s', '--submit', action='store_true',
235 help='Merge open translation commits')
Anthony Kingb8607632015-05-01 22:06:37 +0300236 return parser.parse_args()
Michael Bestas6b6db122015-02-08 13:22:22 +0200237
Anthony Kingb8607632015-05-01 22:06:37 +0300238# ################################# PREPARE ################################## #
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200239
Anthony Kingb8607632015-05-01 22:06:37 +0300240
241def check_dependencies():
Michael Bestaseb4629a2018-11-14 23:03:18 +0200242 # Check for Java version of crowdin
243 cmd = ['dpkg-query', '-W', 'crowdin']
Anthony Kingb8607632015-05-01 22:06:37 +0300244 if run_subprocess(cmd, silent=True)[1] != 0:
Michael Bestaseb4629a2018-11-14 23:03:18 +0200245 print('You have not installed crowdin.', file=sys.stderr)
Anthony Kingb8607632015-05-01 22:06:37 +0300246 return False
Anthony Kingb8607632015-05-01 22:06:37 +0300247 return True
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200248
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200249
Michael Bestas118fcaf2015-06-04 23:02:20 +0300250def load_xml(x):
Anthony Kingb8607632015-05-01 22:06:37 +0300251 try:
252 return minidom.parse(x)
253 except IOError:
254 print('You have no %s.' % x, file=sys.stderr)
255 return None
256 except Exception:
257 # TODO: minidom should not be used.
258 print('Malformed %s.' % x, file=sys.stderr)
259 return None
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200260
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300261
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300262def check_files(files):
Anthony Kingb8607632015-05-01 22:06:37 +0300263 for f in files:
264 if not os.path.isfile(f):
265 print('You have no %s.' % f, file=sys.stderr)
266 return False
Anthony Kingb8607632015-05-01 22:06:37 +0300267 return True
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300268
Anthony Kingb8607632015-05-01 22:06:37 +0300269# ################################### MAIN ################################### #
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300270
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300271
Michael Bestasfd5d1362015-12-18 20:34:32 +0200272def upload_sources_crowdin(branch, config):
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300273 if config:
Michael Bestasfd5d1362015-12-18 20:34:32 +0200274 print('\nUploading sources to Crowdin (custom config)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200275 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200276 '--config=%s/config/%s' % (_DIR, config),
Michael Bestas44fbb352015-12-17 02:01:42 +0200277 'upload', 'sources', '--branch=%s' % branch])
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300278 else:
Michael Bestasfd5d1362015-12-18 20:34:32 +0200279 print('\nUploading sources to Crowdin (AOSP supported languages)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200280 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200281 '--config=%s/config/%s.yaml' % (_DIR, branch),
Michael Bestas44fbb352015-12-17 02:01:42 +0200282 'upload', 'sources', '--branch=%s' % branch])
Anthony King69a95382015-02-08 18:44:10 +0000283
Michael Bestasfd5d1362015-12-18 20:34:32 +0200284 print('\nUploading sources to Crowdin (non-AOSP supported languages)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200285 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200286 '--config=%s/config/%s_aosp.yaml' % (_DIR, branch),
Michael Bestas44fbb352015-12-17 02:01:42 +0200287 'upload', 'sources', '--branch=%s' % branch])
Anthony Kingb8607632015-05-01 22:06:37 +0300288
289
Michael Bestasfd5d1362015-12-18 20:34:32 +0200290def upload_translations_crowdin(branch, config):
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300291 if config:
Michael Bestasfd5d1362015-12-18 20:34:32 +0200292 print('\nUploading translations to Crowdin (custom config)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200293 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200294 '--config=%s/config/%s' % (_DIR, config),
Michael Bestasfd5d1362015-12-18 20:34:32 +0200295 'upload', 'translations', '--branch=%s' % branch,
296 '--no-import-duplicates', '--import-eq-suggestions',
297 '--auto-approve-imported'])
298 else:
299 print('\nUploading translations to Crowdin '
300 '(AOSP supported languages)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200301 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200302 '--config=%s/config/%s.yaml' % (_DIR, branch),
Michael Bestasfd5d1362015-12-18 20:34:32 +0200303 'upload', 'translations', '--branch=%s' % branch,
304 '--no-import-duplicates', '--import-eq-suggestions',
305 '--auto-approve-imported'])
306
307 print('\nUploading translations to Crowdin '
308 '(non-AOSP supported languages)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200309 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200310 '--config=%s/config/%s_aosp.yaml' % (_DIR, branch),
Michael Bestasfd5d1362015-12-18 20:34:32 +0200311 'upload', 'translations', '--branch=%s' % branch,
312 '--no-import-duplicates', '--import-eq-suggestions',
313 '--auto-approve-imported'])
314
315
Michael Bestas80b22ef2018-11-14 23:12:33 +0200316def download_crowdin(base_path, branch, xml, username, config):
Michael Bestasfd5d1362015-12-18 20:34:32 +0200317 if config:
318 print('\nDownloading translations from Crowdin (custom config)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200319 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200320 '--config=%s/config/%s' % (_DIR, config),
Michael Bestas44fbb352015-12-17 02:01:42 +0200321 'download', '--branch=%s' % branch])
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300322 else:
Michael Bestasfd5d1362015-12-18 20:34:32 +0200323 print('\nDownloading translations from Crowdin '
324 '(AOSP supported languages)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200325 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200326 '--config=%s/config/%s.yaml' % (_DIR, branch),
Michael Bestas44fbb352015-12-17 02:01:42 +0200327 'download', '--branch=%s' % branch])
Michael Bestas50579d22014-08-09 17:49:14 +0300328
Michael Bestasfd5d1362015-12-18 20:34:32 +0200329 print('\nDownloading translations from Crowdin '
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300330 '(non-AOSP supported languages)')
Michael Bestaseb4629a2018-11-14 23:03:18 +0200331 check_run(['crowdin',
Michael Bestas03bc7052016-03-12 03:19:10 +0200332 '--config=%s/config/%s_aosp.yaml' % (_DIR, branch),
Michael Bestas44fbb352015-12-17 02:01:42 +0200333 'download', '--branch=%s' % branch])
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200334
Michael Bestas99f5fce2015-06-04 22:07:51 +0300335 print('\nCreating a list of pushable translations')
Michael Bestas919053f2014-10-20 23:30:54 +0300336 # Get all files that Crowdin pushed
Anthony Kingb8607632015-05-01 22:06:37 +0300337 paths = []
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300338 if config:
Michael Bestas03bc7052016-03-12 03:19:10 +0200339 files = ['%s/config/%s' % (_DIR, config)]
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300340 else:
Michael Bestas03bc7052016-03-12 03:19:10 +0200341 files = ['%s/config/%s.yaml' % (_DIR, branch),
342 '%s/config/%s_aosp.yaml' % (_DIR, branch)]
Michael Bestas6c327e62015-05-02 01:58:01 +0300343 for c in files:
Michael Bestaseb4629a2018-11-14 23:03:18 +0200344 cmd = ['crowdin', '--config=%s' % c, 'list', 'project',
Michael Bestas44fbb352015-12-17 02:01:42 +0200345 '--branch=%s' % branch]
Anthony Kingb8607632015-05-01 22:06:37 +0300346 comm, ret = run_subprocess(cmd)
347 if ret != 0:
348 sys.exit(ret)
349 for p in str(comm[0]).split("\n"):
350 paths.append(p.replace('/%s' % branch, ''))
Michael Bestas50579d22014-08-09 17:49:14 +0300351
Michael Bestas99f5fce2015-06-04 22:07:51 +0300352 print('\nUploading translations to Gerrit')
Anthony Kingb8607632015-05-01 22:06:37 +0300353 items = [x for sub in xml for x in sub.getElementsByTagName('project')]
Michael Bestas919053f2014-10-20 23:30:54 +0300354 all_projects = []
355
Anthony Kingb8607632015-05-01 22:06:37 +0300356 for path in paths:
357 path = path.strip()
Michael Bestas919053f2014-10-20 23:30:54 +0300358 if not path:
359 continue
360
Anthony Kingb8607632015-05-01 22:06:37 +0300361 if "/res" not in path:
362 print('WARNING: Cannot determine project root dir of '
363 '[%s], skipping.' % path)
Anthony King69a95382015-02-08 18:44:10 +0000364 continue
Anthony Kingb8607632015-05-01 22:06:37 +0300365 result = path.split('/res')[0].strip('/')
366 if result == path.strip('/'):
367 print('WARNING: Cannot determine project root dir of '
368 '[%s], skipping.' % path)
369 continue
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200370
Michael Bestasc899b8c2015-03-03 00:53:19 +0200371 if result in all_projects:
Michael Bestasc899b8c2015-03-03 00:53:19 +0200372 continue
Michael Bestas50579d22014-08-09 17:49:14 +0300373
Anthony Kingb8607632015-05-01 22:06:37 +0300374 # When a project has multiple translatable files, Crowdin will
375 # give duplicates.
376 # We don't want that (useless empty commits), so we save each
377 # project in all_projects and check if it's already in there.
Michael Bestasc899b8c2015-03-03 00:53:19 +0200378 all_projects.append(result)
Anthony King69a95382015-02-08 18:44:10 +0000379
Michael Bestas42e25e32016-03-12 20:18:39 +0200380 # Search android/default.xml or config/%(branch)_extra_packages.xml
Anthony Kingb8607632015-05-01 22:06:37 +0300381 # for the project's name
382 for project in items:
383 path = project.attributes['path'].value
384 if not (result + '/').startswith(path +'/'):
Michael Bestasc899b8c2015-03-03 00:53:19 +0200385 continue
Anthony Kingb8607632015-05-01 22:06:37 +0300386 if result != path:
387 if path in all_projects:
388 break
389 result = path
390 all_projects.append(result)
Anthony King69a95382015-02-08 18:44:10 +0000391
Anthony Kingb8607632015-05-01 22:06:37 +0300392 br = project.getAttribute('revision') or branch
Anthony King69a95382015-02-08 18:44:10 +0000393
Michael W1bb2f922019-02-27 17:46:28 +0100394 push_as_commit(files, base_path, result,
Michael Bestas80b22ef2018-11-14 23:12:33 +0200395 project.getAttribute('name'), br, username)
Anthony Kingb8607632015-05-01 22:06:37 +0300396 break
Anthony King69a95382015-02-08 18:44:10 +0000397
Anthony King69a95382015-02-08 18:44:10 +0000398
Anthony Kingb8607632015-05-01 22:06:37 +0300399def main():
Anthony Kingb8607632015-05-01 22:06:37 +0300400 args = parse_args()
401 default_branch = args.branch
Michael Bestas118fcaf2015-06-04 23:02:20 +0300402
Michael Wd13658a2019-01-13 14:05:37 +0100403 if args.submit:
404 if args.username is None:
405 print('Argument -u/--username is required for submitting!')
406 sys.exit(1)
407 submit_gerrit(default_branch, args.username)
408 sys.exit(0)
409
Michael Bestaseb4629a2018-11-14 23:03:18 +0200410 base_path_branch_suffix = default_branch.replace('-', '_').replace('.', '_').upper()
411 base_path_env = 'LINEAGE_CROWDIN_BASE_PATH_%s' % base_path_branch_suffix
412 base_path = os.getenv(base_path_env)
Michael Bestas118fcaf2015-06-04 23:02:20 +0300413 if base_path is None:
Anthony Kingd0d56cf2015-06-05 10:48:38 +0100414 cwd = os.getcwd()
Michael Bestaseb4629a2018-11-14 23:03:18 +0200415 print('You have not set %s. Defaulting to %s' % (base_path_env, cwd))
Michael Bestas118fcaf2015-06-04 23:02:20 +0300416 base_path = cwd
Michael Bestas118fcaf2015-06-04 23:02:20 +0300417 if not os.path.isdir(base_path):
Michael Bestaseb4629a2018-11-14 23:03:18 +0200418 print('%s is not a real directory: %s' % (base_path_env, base_path))
Michael Bestas118fcaf2015-06-04 23:02:20 +0300419 sys.exit(1)
Anthony Kingb8607632015-05-01 22:06:37 +0300420
Michael Bestas99f5fce2015-06-04 22:07:51 +0300421 if not check_dependencies():
422 sys.exit(1)
Anthony Kingb8607632015-05-01 22:06:37 +0300423
Michael Bestas118fcaf2015-06-04 23:02:20 +0300424 xml_android = load_xml(x='%s/android/default.xml' % base_path)
Anthony Kingb8607632015-05-01 22:06:37 +0300425 if xml_android is None:
426 sys.exit(1)
427
Michael Bestas42e25e32016-03-12 20:18:39 +0200428 xml_extra = load_xml(x='%s/config/%s_extra_packages.xml'
Anthony Kingd0d56cf2015-06-05 10:48:38 +0100429 % (_DIR, default_branch))
Anthony Kingb8607632015-05-01 22:06:37 +0300430 if xml_extra is None:
431 sys.exit(1)
432
Michael Bestas19dc3352018-02-03 20:24:00 +0200433 xml_snippet = load_xml(x='%s/android/snippets/lineage.xml' % base_path)
434 if xml_snippet is None:
435 xml_snippet = load_xml(x='%s/android/snippets/cm.xml' % base_path)
436 if xml_snippet is None:
437 xml_snippet = load_xml(x='%s/android/snippets/hal_cm_all.xml' % base_path)
438 if xml_snippet is not None:
439 xml_files = (xml_android, xml_snippet, xml_extra)
Michael Bestas687679f2016-12-07 23:20:12 +0200440 else:
441 xml_files = (xml_android, xml_extra)
442
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300443 if args.config:
Michael Bestas03bc7052016-03-12 03:19:10 +0200444 files = ['%s/config/%s' % (_DIR, args.config)]
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300445 else:
Michael Bestas03bc7052016-03-12 03:19:10 +0200446 files = ['%s/config/%s.yaml' % (_DIR, default_branch),
447 '%s/config/%s_aosp.yaml' % (_DIR, default_branch)]
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300448 if not check_files(files):
Anthony Kingb8607632015-05-01 22:06:37 +0300449 sys.exit(1)
450
Michael Bestasfd5d1362015-12-18 20:34:32 +0200451 if args.download and args.username is None:
452 print('Argument -u/--username is required for translations download')
453 sys.exit(1)
454
455 if args.upload_sources:
456 upload_sources_crowdin(default_branch, args.config)
457 if args.upload_translations:
458 upload_translations_crowdin(default_branch, args.config)
459 if args.download:
Michael Bestas687679f2016-12-07 23:20:12 +0200460 download_crowdin(base_path, default_branch, xml_files,
Michael Bestas80b22ef2018-11-14 23:12:33 +0200461 args.username, args.config)
Tom Powell44256852016-07-06 15:23:25 -0700462
463 if _COMMITS_CREATED:
464 print('\nDone!')
465 sys.exit(0)
Tom Powellf42586f2016-07-11 11:02:54 -0700466 else:
Tom Powell44256852016-07-06 15:23:25 -0700467 print('\nNothing to commit')
468 sys.exit(-1)
Anthony Kingb8607632015-05-01 22:06:37 +0300469
470if __name__ == '__main__':
471 main()