blob: 526b7de4c12e144ab521a1c230aa8251fa6221c5 [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")
112 sync = parser.add_mutually_exclusive_group()
113 parser.add_argument('-u', '--username', help='Gerrit username',
114 required=True)
115 parser.add_argument('-b', '--branch', help='CyanogenMod branch',
116 required=True)
117 sync.add_argument('--no-upload', action='store_true',
118 help='Only download CM translations from Crowdin')
119 sync.add_argument('--no-download', action='store_true',
120 help='Only upload CM source translations to Crowdin')
121 return parser.parse_args()
Michael Bestas6b6db122015-02-08 13:22:22 +0200122
Anthony Kingb8607632015-05-01 22:06:37 +0300123# ################################# PREPARE ################################## #
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200124
Anthony Kingb8607632015-05-01 22:06:37 +0300125
126def check_dependencies():
Anthony Kingb8607632015-05-01 22:06:37 +0300127 # Check for Ruby version of crowdin-cli
128 cmd = ['gem', 'list', 'crowdin-cli', '-i']
129 if run_subprocess(cmd, silent=True)[1] != 0:
130 print('You have not installed crowdin-cli.', file=sys.stderr)
131 return False
Anthony Kingb8607632015-05-01 22:06:37 +0300132 return True
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200133
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200134
Michael Bestas118fcaf2015-06-04 23:02:20 +0300135def load_xml(x):
Anthony Kingb8607632015-05-01 22:06:37 +0300136 try:
137 return minidom.parse(x)
138 except IOError:
139 print('You have no %s.' % x, file=sys.stderr)
140 return None
141 except Exception:
142 # TODO: minidom should not be used.
143 print('Malformed %s.' % x, file=sys.stderr)
144 return None
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200145
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300146
Anthony Kingd0d56cf2015-06-05 10:48:38 +0100147def check_files(branch):
148 files = ['%s/crowdin/extra_packages_%s.xml' % (_DIR, branch),
149 '%s/crowdin/crowdin_%s.yaml' % (_DIR, branch),
150 '%s/crowdin/crowdin_%s_aosp.yaml' % (_DIR, branch)
Anthony Kingb8607632015-05-01 22:06:37 +0300151 ]
152 for f in files:
153 if not os.path.isfile(f):
154 print('You have no %s.' % f, file=sys.stderr)
155 return False
Anthony Kingb8607632015-05-01 22:06:37 +0300156 return True
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300157
Anthony Kingb8607632015-05-01 22:06:37 +0300158# ################################### MAIN ################################### #
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300159
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300160
Anthony Kingd0d56cf2015-06-05 10:48:38 +0100161def upload_crowdin(branch, no_upload=False):
Anthony Kingb8607632015-05-01 22:06:37 +0300162 if no_upload:
163 print('Skipping source translations upload')
164 return
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200165
Michael Bestas99f5fce2015-06-04 22:07:51 +0300166 print('\nUploading Crowdin source translations (AOSP supported languages)')
Michael Bestas118fcaf2015-06-04 23:02:20 +0300167 check_run(['crowdin-cli',
Anthony Kingd0d56cf2015-06-05 10:48:38 +0100168 '--config=%s/crowdin/crowdin_%s.yaml' % (_DIR, branch),
Anthony Kingb8607632015-05-01 22:06:37 +0300169 'upload', 'sources'])
Anthony King69a95382015-02-08 18:44:10 +0000170
Anthony Kingb8607632015-05-01 22:06:37 +0300171 print('\nUploading Crowdin source translations '
172 '(non-AOSP supported languages)')
Michael Bestas118fcaf2015-06-04 23:02:20 +0300173 check_run(['crowdin-cli',
Anthony Kingd0d56cf2015-06-05 10:48:38 +0100174 '--config=%s/crowdin/crowdin_%s_aosp.yaml' % (_DIR, branch),
Anthony Kingb8607632015-05-01 22:06:37 +0300175 'upload', 'sources'])
176
177
Anthony Kingd0d56cf2015-06-05 10:48:38 +0100178def download_crowdin(base_path, branch, xml, username, no_download=False):
Anthony Kingb8607632015-05-01 22:06:37 +0300179 if no_download:
180 print('Skipping translations download')
181 return
182
183 print('\nDownloading Crowdin translations (AOSP supported languages)')
Michael Bestas118fcaf2015-06-04 23:02:20 +0300184 check_run(['crowdin-cli',
Anthony Kingd0d56cf2015-06-05 10:48:38 +0100185 '--config=%s/crowdin/crowdin_%s.yaml' % (_DIR, branch),
Anthony Kingb8607632015-05-01 22:06:37 +0300186 'download', '--ignore-match'])
Michael Bestas50579d22014-08-09 17:49:14 +0300187
Michael Bestasa02eb4b2015-02-08 15:47:01 +0200188 print('\nDownloading Crowdin translations (non-AOSP supported languages)')
Michael Bestas118fcaf2015-06-04 23:02:20 +0300189 check_run(['crowdin-cli',
Anthony Kingd0d56cf2015-06-05 10:48:38 +0100190 '--config=%s/crowdin/crowdin_%s_aosp.yaml' % (_DIR, branch),
Anthony Kingb8607632015-05-01 22:06:37 +0300191 'download', '--ignore-match'])
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200192
Michael Bestas99f5fce2015-06-04 22:07:51 +0300193 print('\nRemoving useless empty translation files')
Anthony Kingb8607632015-05-01 22:06:37 +0300194 empty_contents = {
195 '<resources/>',
196 '<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>',
197 ('<resources xmlns:android='
198 '"http://schemas.android.com/apk/res/android"/>'),
199 ('<resources xmlns:android="http://schemas.android.com/apk/res/android"'
200 ' xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>'),
201 ('<resources xmlns:tools="http://schemas.android.com/tools"'
202 ' xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>')
203 }
204 xf = None
Michael Bestas118fcaf2015-06-04 23:02:20 +0300205 for xml_file in find_xml(base_path):
Anthony Kingb8607632015-05-01 22:06:37 +0300206 xf = open(xml_file).read()
Michael Bestas919053f2014-10-20 23:30:54 +0300207 for line in empty_contents:
Anthony Kingb8607632015-05-01 22:06:37 +0300208 if line in xf:
Michael Bestas919053f2014-10-20 23:30:54 +0300209 print('Removing ' + xml_file)
210 os.remove(xml_file)
211 break
Anthony Kingb8607632015-05-01 22:06:37 +0300212 del xf
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200213
Michael Bestas99f5fce2015-06-04 22:07:51 +0300214 print('\nCreating a list of pushable translations')
Michael Bestas919053f2014-10-20 23:30:54 +0300215 # Get all files that Crowdin pushed
Anthony Kingb8607632015-05-01 22:06:37 +0300216 paths = []
217 files = [
Anthony Kingd0d56cf2015-06-05 10:48:38 +0100218 ('%s/crowdin/crowdin_%s.yaml' % (_DIR, branch)),
219 ('%s/crowdin/crowdin_%s_aosp.yaml' % (_DIR, branch))
Anthony Kingb8607632015-05-01 22:06:37 +0300220 ]
Michael Bestas6c327e62015-05-02 01:58:01 +0300221 for c in files:
222 cmd = ['crowdin-cli', '--config=%s' % c, 'list', 'sources']
Anthony Kingb8607632015-05-01 22:06:37 +0300223 comm, ret = run_subprocess(cmd)
224 if ret != 0:
225 sys.exit(ret)
226 for p in str(comm[0]).split("\n"):
227 paths.append(p.replace('/%s' % branch, ''))
Michael Bestas50579d22014-08-09 17:49:14 +0300228
Michael Bestas99f5fce2015-06-04 22:07:51 +0300229 print('\nUploading translations to Gerrit')
Anthony Kingb8607632015-05-01 22:06:37 +0300230 items = [x for sub in xml for x in sub.getElementsByTagName('project')]
Michael Bestas919053f2014-10-20 23:30:54 +0300231 all_projects = []
232
Anthony Kingb8607632015-05-01 22:06:37 +0300233 for path in paths:
234 path = path.strip()
Michael Bestas919053f2014-10-20 23:30:54 +0300235 if not path:
236 continue
237
Anthony Kingb8607632015-05-01 22:06:37 +0300238 if "/res" not in path:
239 print('WARNING: Cannot determine project root dir of '
240 '[%s], skipping.' % path)
Anthony King69a95382015-02-08 18:44:10 +0000241 continue
Anthony Kingb8607632015-05-01 22:06:37 +0300242 result = path.split('/res')[0].strip('/')
243 if result == path.strip('/'):
244 print('WARNING: Cannot determine project root dir of '
245 '[%s], skipping.' % path)
246 continue
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200247
Michael Bestasc899b8c2015-03-03 00:53:19 +0200248 if result in all_projects:
Michael Bestasc899b8c2015-03-03 00:53:19 +0200249 continue
Michael Bestas50579d22014-08-09 17:49:14 +0300250
Anthony Kingb8607632015-05-01 22:06:37 +0300251 # When a project has multiple translatable files, Crowdin will
252 # give duplicates.
253 # We don't want that (useless empty commits), so we save each
254 # project in all_projects and check if it's already in there.
Michael Bestasc899b8c2015-03-03 00:53:19 +0200255 all_projects.append(result)
Anthony King69a95382015-02-08 18:44:10 +0000256
Anthony Kingb8607632015-05-01 22:06:37 +0300257 # Search android/default.xml or crowdin/extra_packages_%(branch).xml
258 # for the project's name
259 for project in items:
260 path = project.attributes['path'].value
261 if not (result + '/').startswith(path +'/'):
Michael Bestasc899b8c2015-03-03 00:53:19 +0200262 continue
Anthony Kingb8607632015-05-01 22:06:37 +0300263 if result != path:
264 if path in all_projects:
265 break
266 result = path
267 all_projects.append(result)
Anthony King69a95382015-02-08 18:44:10 +0000268
Anthony Kingb8607632015-05-01 22:06:37 +0300269 br = project.getAttribute('revision') or branch
Anthony King69a95382015-02-08 18:44:10 +0000270
Michael Bestas118fcaf2015-06-04 23:02:20 +0300271 push_as_commit(base_path, result,
272 project.getAttribute('name'), br, username)
Anthony Kingb8607632015-05-01 22:06:37 +0300273 break
Anthony King69a95382015-02-08 18:44:10 +0000274
Anthony King69a95382015-02-08 18:44:10 +0000275
Anthony Kingb8607632015-05-01 22:06:37 +0300276def main():
Anthony Kingb8607632015-05-01 22:06:37 +0300277 args = parse_args()
278 default_branch = args.branch
Michael Bestas118fcaf2015-06-04 23:02:20 +0300279
280 base_path = os.getenv('CM_CROWDIN_BASE_PATH')
281 if base_path is None:
Anthony Kingd0d56cf2015-06-05 10:48:38 +0100282 cwd = os.getcwd()
Michael Bestas118fcaf2015-06-04 23:02:20 +0300283 print('You have not set CM_CROWDIN_BASE_PATH. Defaulting to %s' % cwd)
284 base_path = cwd
285 else:
286 base_path = os.path.join(os.path.realpath(base_path), default_branch)
287 if not os.path.isdir(base_path):
288 print('CM_CROWDIN_BASE_PATH + branch is not a real directory: %s'
289 % base_path)
290 sys.exit(1)
Anthony Kingb8607632015-05-01 22:06:37 +0300291
Michael Bestas99f5fce2015-06-04 22:07:51 +0300292 if not check_dependencies():
293 sys.exit(1)
Anthony Kingb8607632015-05-01 22:06:37 +0300294
Michael Bestas118fcaf2015-06-04 23:02:20 +0300295 xml_android = load_xml(x='%s/android/default.xml' % base_path)
Anthony Kingb8607632015-05-01 22:06:37 +0300296 if xml_android is None:
297 sys.exit(1)
298
Michael Bestas118fcaf2015-06-04 23:02:20 +0300299 xml_extra = load_xml(x='%s/crowdin/extra_packages_%s.xml'
Anthony Kingd0d56cf2015-06-05 10:48:38 +0100300 % (_DIR, default_branch))
Anthony Kingb8607632015-05-01 22:06:37 +0300301 if xml_extra is None:
302 sys.exit(1)
303
Anthony Kingd0d56cf2015-06-05 10:48:38 +0100304 if not check_files(default_branch):
Anthony Kingb8607632015-05-01 22:06:37 +0300305 sys.exit(1)
306
Anthony Kingd0d56cf2015-06-05 10:48:38 +0100307 upload_crowdin(default_branch, args.no_upload)
308 download_crowdin(base_path, default_branch, (xml_android, xml_extra),
Anthony Kingb8607632015-05-01 22:06:37 +0300309 args.username, args.no_download)
310 print('\nDone!')
311
312if __name__ == '__main__':
313 main()