blob: 1d64bc2e84f482bed1c7df578222683ff9eeb4c1 [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
6# directly to CyanogenMod's Gerrit.
7#
Michael Bestas97677e12015-02-08 13:11:59 +02008# Copyright (C) 2014-2015 The CyanogenMod Project
Marco Brohetcb5cdb42014-07-11 22:41:53 +02009#
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 Kingb8607632015-05-01 22:06:37 +030022# ################################# IMPORTS ################################## #
23
24from __future__ import print_function
Marco Brohet6b6b4e52014-07-20 00:05:16 +020025
26import argparse
Marco Brohetcb5cdb42014-07-11 22:41:53 +020027import git
28import os
Marco Brohetcb5cdb42014-07-11 22:41:53 +020029import subprocess
30import sys
Anthony Kingb8607632015-05-01 22:06:37 +030031
Marco Brohetcb5cdb42014-07-11 22:41:53 +020032from xml.dom import minidom
33
Anthony Kingd0d56cf2015-06-05 10:48:38 +010034# ################################# GLOBALS ################################## #
35
36_DIR = os.path.dirname(os.path.realpath(__file__))
37
Anthony Kingb8607632015-05-01 22:06:37 +030038# ################################ FUNCTIONS ################################# #
39
40
41def 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 Brohet6b6b4e52014-07-20 00:05:16 +020055
Michael Bestas118fcaf2015-06-04 23:02:20 +030056def push_as_commit(base_path, path, name, branch, username):
Anthony Kingb8607632015-05-01 22:06:37 +030057 print('Committing %s on branch %s' % (name, branch))
Marco Brohetcb5cdb42014-07-11 22:41:53 +020058
59 # Get path
Michael Bestas118fcaf2015-06-04 23:02:20 +030060 path = os.path.join(base_path, path)
Anthony Kingb8607632015-05-01 22:06:37 +030061 if not path.endswith('.git'):
62 path = os.path.join(path, '.git')
Marco Brohetcb5cdb42014-07-11 22:41:53 +020063
Marco Brohet6b6b4e52014-07-20 00:05:16 +020064 # Create repo object
Marco Brohetcb5cdb42014-07-11 22:41:53 +020065 repo = git.Repo(path)
Marco Brohet6b6b4e52014-07-20 00:05:16 +020066
67 # Remove previously deleted files from Git
Anthony Kingb8607632015-05-01 22:06:37 +030068 files = repo.git.ls_files(d=True).split('\n')
69 if files and files[0]:
70 repo.git.rm(files)
Marco Brohet6b6b4e52014-07-20 00:05:16 +020071
72 # Add all files to commit
Marco Brohetcb5cdb42014-07-11 22:41:53 +020073 repo.git.add('-A')
Marco Brohet6b6b4e52014-07-20 00:05:16 +020074
75 # Create commit; if it fails, probably empty so skipping
Marco Brohetcb5cdb42014-07-11 22:41:53 +020076 try:
77 repo.git.commit(m='Automatic translation import')
78 except:
Anthony Kingb8607632015-05-01 22:06:37 +030079 print('Failed to create commit for %s, probably empty: skipping'
80 % name, file=sys.stderr)
Marco Brohetcb5cdb42014-07-11 22:41:53 +020081 return
Marco Brohet6b6b4e52014-07-20 00:05:16 +020082
83 # Push commit
Michael Bestasf96f67b2014-10-21 00:43:37 +030084 try:
Anthony Kingb8607632015-05-01 22:06:37 +030085 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 Bestasf96f67b2014-10-21 00:43:37 +030088 except:
Anthony Kingb8607632015-05-01 22:06:37 +030089 print('Failed to push commit for %s' % name, file=sys.stderr)
Marco Brohetcb5cdb42014-07-11 22:41:53 +020090
Anthony Kingb8607632015-05-01 22:06:37 +030091
92def check_run(cmd):
Michael Bestas97677e12015-02-08 13:11:59 +020093 p = subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr)
94 ret = p.wait()
95 if ret != 0:
Anthony Kingb8607632015-05-01 22:06:37 +030096 print('Failed to run cmd: %s' % ' '.join(cmd), file=sys.stderr)
Michael Bestas97677e12015-02-08 13:11:59 +020097 sys.exit(ret)
98
Marco Brohet6b6b4e52014-07-20 00:05:16 +020099
Michael Bestas118fcaf2015-06-04 23:02:20 +0300100def find_xml(base_path):
101 for dp, dn, file_names in os.walk(base_path):
Anthony Kingb8607632015-05-01 22:06:37 +0300102 for f in file_names:
103 if os.path.splitext(f)[1] == '.xml':
104 yield os.path.join(dp, f)
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200105
Anthony Kingb8607632015-05-01 22:06:37 +0300106# ############################################################################ #
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200107
Michael Bestas6b6db122015-02-08 13:22:22 +0200108
Anthony Kingb8607632015-05-01 22:06:37 +0300109def parse_args():
110 parser = argparse.ArgumentParser(
111 description="Synchronising CyanogenMod's translations with Crowdin")
Michael Bestasfd5d1362015-12-18 20:34:32 +0200112 parser.add_argument('-u', '--username', help='Gerrit username')
Anthony Kingb8607632015-05-01 22:06:37 +0300113 parser.add_argument('-b', '--branch', help='CyanogenMod branch',
114 required=True)
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300115 parser.add_argument('-c', '--config', help='Custom yaml config')
Michael Bestasfd5d1362015-12-18 20:34:32 +0200116 parser.add_argument('--upload-sources', action='store_true',
117 help='Upload sources to Crowdin')
118 parser.add_argument('--upload-translations', action='store_true',
119 help='Upload translations to Crowdin')
120 parser.add_argument('--download', action='store_true',
121 help='Download translations from Crowdin')
Anthony Kingb8607632015-05-01 22:06:37 +0300122 return parser.parse_args()
Michael Bestas6b6db122015-02-08 13:22:22 +0200123
Anthony Kingb8607632015-05-01 22:06:37 +0300124# ################################# PREPARE ################################## #
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200125
Anthony Kingb8607632015-05-01 22:06:37 +0300126
127def check_dependencies():
Anthony Kingb8607632015-05-01 22:06:37 +0300128 # 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 Kingb8607632015-05-01 22:06:37 +0300133 return True
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200134
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200135
Michael Bestas118fcaf2015-06-04 23:02:20 +0300136def load_xml(x):
Anthony Kingb8607632015-05-01 22:06:37 +0300137 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 Brohet6b6b4e52014-07-20 00:05:16 +0200146
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300147
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300148def check_files(files):
Anthony Kingb8607632015-05-01 22:06:37 +0300149 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 Kingb8607632015-05-01 22:06:37 +0300153 return True
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300154
Anthony Kingb8607632015-05-01 22:06:37 +0300155# ################################### MAIN ################################### #
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300156
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300157
Michael Bestasfd5d1362015-12-18 20:34:32 +0200158def upload_sources_crowdin(branch, config):
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300159 if config:
Michael Bestasfd5d1362015-12-18 20:34:32 +0200160 print('\nUploading sources to Crowdin (custom config)')
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300161 check_run(['crowdin-cli',
162 '--config=%s/crowdin/%s' % (_DIR, config),
Michael Bestas44fbb352015-12-17 02:01:42 +0200163 'upload', 'sources', '--branch=%s' % branch])
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300164 else:
Michael Bestasfd5d1362015-12-18 20:34:32 +0200165 print('\nUploading sources to Crowdin (AOSP supported languages)')
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300166 check_run(['crowdin-cli',
167 '--config=%s/crowdin/crowdin_%s.yaml' % (_DIR, branch),
Michael Bestas44fbb352015-12-17 02:01:42 +0200168 'upload', 'sources', '--branch=%s' % branch])
Anthony King69a95382015-02-08 18:44:10 +0000169
Michael Bestasfd5d1362015-12-18 20:34:32 +0200170 print('\nUploading sources to Crowdin (non-AOSP supported languages)')
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300171 check_run(['crowdin-cli',
172 '--config=%s/crowdin/crowdin_%s_aosp.yaml' % (_DIR, branch),
Michael Bestas44fbb352015-12-17 02:01:42 +0200173 'upload', 'sources', '--branch=%s' % branch])
Anthony Kingb8607632015-05-01 22:06:37 +0300174
175
Michael Bestasfd5d1362015-12-18 20:34:32 +0200176def upload_translations_crowdin(branch, config):
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300177 if config:
Michael Bestasfd5d1362015-12-18 20:34:32 +0200178 print('\nUploading translations to Crowdin (custom config)')
179 check_run(['crowdin-cli',
180 '--config=%s/crowdin/%s' % (_DIR, config),
181 'upload', 'translations', '--branch=%s' % branch,
182 '--no-import-duplicates', '--import-eq-suggestions',
183 '--auto-approve-imported'])
184 else:
185 print('\nUploading translations to Crowdin '
186 '(AOSP supported languages)')
187 check_run(['crowdin-cli',
188 '--config=%s/crowdin/crowdin_%s.yaml' % (_DIR, branch),
189 'upload', 'translations', '--branch=%s' % branch,
190 '--no-import-duplicates', '--import-eq-suggestions',
191 '--auto-approve-imported'])
192
193 print('\nUploading translations to Crowdin '
194 '(non-AOSP supported languages)')
195 check_run(['crowdin-cli',
196 '--config=%s/crowdin/crowdin_%s_aosp.yaml' % (_DIR, branch),
197 'upload', 'translations', '--branch=%s' % branch,
198 '--no-import-duplicates', '--import-eq-suggestions',
199 '--auto-approve-imported'])
200
201
202def download_crowdin(base_path, branch, xml, username, config):
203 if config:
204 print('\nDownloading translations from Crowdin (custom config)')
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300205 check_run(['crowdin-cli',
206 '--config=%s/crowdin/%s' % (_DIR, config),
Michael Bestas44fbb352015-12-17 02:01:42 +0200207 'download', '--branch=%s' % branch])
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300208 else:
Michael Bestasfd5d1362015-12-18 20:34:32 +0200209 print('\nDownloading translations from Crowdin '
210 '(AOSP supported languages)')
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300211 check_run(['crowdin-cli',
212 '--config=%s/crowdin/crowdin_%s.yaml' % (_DIR, branch),
Michael Bestas44fbb352015-12-17 02:01:42 +0200213 'download', '--branch=%s' % branch])
Michael Bestas50579d22014-08-09 17:49:14 +0300214
Michael Bestasfd5d1362015-12-18 20:34:32 +0200215 print('\nDownloading translations from Crowdin '
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300216 '(non-AOSP supported languages)')
217 check_run(['crowdin-cli',
218 '--config=%s/crowdin/crowdin_%s_aosp.yaml' % (_DIR, branch),
Michael Bestas44fbb352015-12-17 02:01:42 +0200219 'download', '--branch=%s' % branch])
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200220
Michael Bestas99f5fce2015-06-04 22:07:51 +0300221 print('\nRemoving useless empty translation files')
Anthony Kingb8607632015-05-01 22:06:37 +0300222 empty_contents = {
223 '<resources/>',
224 '<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>',
225 ('<resources xmlns:android='
226 '"http://schemas.android.com/apk/res/android"/>'),
227 ('<resources xmlns:android="http://schemas.android.com/apk/res/android"'
228 ' xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>'),
229 ('<resources xmlns:tools="http://schemas.android.com/tools"'
230 ' xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>')
231 }
232 xf = None
Michael Bestas118fcaf2015-06-04 23:02:20 +0300233 for xml_file in find_xml(base_path):
Anthony Kingb8607632015-05-01 22:06:37 +0300234 xf = open(xml_file).read()
Michael Bestas919053f2014-10-20 23:30:54 +0300235 for line in empty_contents:
Anthony Kingb8607632015-05-01 22:06:37 +0300236 if line in xf:
Michael Bestas919053f2014-10-20 23:30:54 +0300237 print('Removing ' + xml_file)
238 os.remove(xml_file)
239 break
Anthony Kingb8607632015-05-01 22:06:37 +0300240 del xf
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200241
Michael Bestas99f5fce2015-06-04 22:07:51 +0300242 print('\nCreating a list of pushable translations')
Michael Bestas919053f2014-10-20 23:30:54 +0300243 # Get all files that Crowdin pushed
Anthony Kingb8607632015-05-01 22:06:37 +0300244 paths = []
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300245 if config:
246 files = ['%s/crowdin/%s' % (_DIR, config)]
247 else:
248 files = ['%s/crowdin/crowdin_%s.yaml' % (_DIR, branch),
Michael Bestas44fbb352015-12-17 02:01:42 +0200249 '%s/crowdin/crowdin_%s_aosp.yaml' % (_DIR, branch)]
Michael Bestas6c327e62015-05-02 01:58:01 +0300250 for c in files:
Michael Bestas44fbb352015-12-17 02:01:42 +0200251 cmd = ['crowdin-cli', '--config=%s' % c, 'list', 'project',
252 '--branch=%s' % branch]
Anthony Kingb8607632015-05-01 22:06:37 +0300253 comm, ret = run_subprocess(cmd)
254 if ret != 0:
255 sys.exit(ret)
256 for p in str(comm[0]).split("\n"):
257 paths.append(p.replace('/%s' % branch, ''))
Michael Bestas50579d22014-08-09 17:49:14 +0300258
Michael Bestas99f5fce2015-06-04 22:07:51 +0300259 print('\nUploading translations to Gerrit')
Anthony Kingb8607632015-05-01 22:06:37 +0300260 items = [x for sub in xml for x in sub.getElementsByTagName('project')]
Michael Bestas919053f2014-10-20 23:30:54 +0300261 all_projects = []
262
Anthony Kingb8607632015-05-01 22:06:37 +0300263 for path in paths:
264 path = path.strip()
Michael Bestas919053f2014-10-20 23:30:54 +0300265 if not path:
266 continue
267
Anthony Kingb8607632015-05-01 22:06:37 +0300268 if "/res" not in path:
269 print('WARNING: Cannot determine project root dir of '
270 '[%s], skipping.' % path)
Anthony King69a95382015-02-08 18:44:10 +0000271 continue
Anthony Kingb8607632015-05-01 22:06:37 +0300272 result = path.split('/res')[0].strip('/')
273 if result == path.strip('/'):
274 print('WARNING: Cannot determine project root dir of '
275 '[%s], skipping.' % path)
276 continue
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200277
Michael Bestasc899b8c2015-03-03 00:53:19 +0200278 if result in all_projects:
Michael Bestasc899b8c2015-03-03 00:53:19 +0200279 continue
Michael Bestas50579d22014-08-09 17:49:14 +0300280
Anthony Kingb8607632015-05-01 22:06:37 +0300281 # When a project has multiple translatable files, Crowdin will
282 # give duplicates.
283 # We don't want that (useless empty commits), so we save each
284 # project in all_projects and check if it's already in there.
Michael Bestasc899b8c2015-03-03 00:53:19 +0200285 all_projects.append(result)
Anthony King69a95382015-02-08 18:44:10 +0000286
Anthony Kingb8607632015-05-01 22:06:37 +0300287 # Search android/default.xml or crowdin/extra_packages_%(branch).xml
288 # for the project's name
289 for project in items:
290 path = project.attributes['path'].value
291 if not (result + '/').startswith(path +'/'):
Michael Bestasc899b8c2015-03-03 00:53:19 +0200292 continue
Anthony Kingb8607632015-05-01 22:06:37 +0300293 if result != path:
294 if path in all_projects:
295 break
296 result = path
297 all_projects.append(result)
Anthony King69a95382015-02-08 18:44:10 +0000298
Anthony Kingb8607632015-05-01 22:06:37 +0300299 br = project.getAttribute('revision') or branch
Anthony King69a95382015-02-08 18:44:10 +0000300
Michael Bestas118fcaf2015-06-04 23:02:20 +0300301 push_as_commit(base_path, result,
302 project.getAttribute('name'), br, username)
Anthony Kingb8607632015-05-01 22:06:37 +0300303 break
Anthony King69a95382015-02-08 18:44:10 +0000304
Anthony King69a95382015-02-08 18:44:10 +0000305
Anthony Kingb8607632015-05-01 22:06:37 +0300306def main():
Anthony Kingb8607632015-05-01 22:06:37 +0300307 args = parse_args()
308 default_branch = args.branch
Michael Bestas118fcaf2015-06-04 23:02:20 +0300309
310 base_path = os.getenv('CM_CROWDIN_BASE_PATH')
311 if base_path is None:
Anthony Kingd0d56cf2015-06-05 10:48:38 +0100312 cwd = os.getcwd()
Michael Bestas118fcaf2015-06-04 23:02:20 +0300313 print('You have not set CM_CROWDIN_BASE_PATH. Defaulting to %s' % cwd)
314 base_path = cwd
315 else:
316 base_path = os.path.join(os.path.realpath(base_path), default_branch)
317 if not os.path.isdir(base_path):
318 print('CM_CROWDIN_BASE_PATH + branch is not a real directory: %s'
319 % base_path)
320 sys.exit(1)
Anthony Kingb8607632015-05-01 22:06:37 +0300321
Michael Bestas99f5fce2015-06-04 22:07:51 +0300322 if not check_dependencies():
323 sys.exit(1)
Anthony Kingb8607632015-05-01 22:06:37 +0300324
Michael Bestas118fcaf2015-06-04 23:02:20 +0300325 xml_android = load_xml(x='%s/android/default.xml' % base_path)
Anthony Kingb8607632015-05-01 22:06:37 +0300326 if xml_android is None:
327 sys.exit(1)
328
Michael Bestas118fcaf2015-06-04 23:02:20 +0300329 xml_extra = load_xml(x='%s/crowdin/extra_packages_%s.xml'
Anthony Kingd0d56cf2015-06-05 10:48:38 +0100330 % (_DIR, default_branch))
Anthony Kingb8607632015-05-01 22:06:37 +0300331 if xml_extra is None:
332 sys.exit(1)
333
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300334 if args.config:
335 files = ['%s/crowdin/%s' % (_DIR, args.config)]
336 else:
337 files = ['%s/crowdin/crowdin_%s.yaml' % (_DIR, default_branch),
Michael Bestas44fbb352015-12-17 02:01:42 +0200338 '%s/crowdin/crowdin_%s_aosp.yaml' % (_DIR, default_branch)]
Michael Bestas2f8c4a52015-08-05 21:33:50 +0300339 if not check_files(files):
Anthony Kingb8607632015-05-01 22:06:37 +0300340 sys.exit(1)
341
Michael Bestasfd5d1362015-12-18 20:34:32 +0200342 if args.download and args.username is None:
343 print('Argument -u/--username is required for translations download')
344 sys.exit(1)
345
346 if args.upload_sources:
347 upload_sources_crowdin(default_branch, args.config)
348 if args.upload_translations:
349 upload_translations_crowdin(default_branch, args.config)
350 if args.download:
351 download_crowdin(base_path, default_branch, (xml_android, xml_extra),
352 args.username, args.config)
Anthony Kingb8607632015-05-01 22:06:37 +0300353 print('\nDone!')
354
355if __name__ == '__main__':
356 main()