blob: b5d52944a98d3ea57e5d630538d9548c64ddae14 [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 Kingb8607632015-05-01 22:06:37 +030034# ################################ FUNCTIONS ################################# #
35
36
37def run_subprocess(cmd, silent=False):
38 p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
39 universal_newlines=True)
40 comm = p.communicate()
41 exit_code = p.returncode
42 if exit_code != 0 and not silent:
43 print("There was an error running the subprocess.\n"
44 "cmd: %s\n"
45 "exit code: %d\n"
46 "stdout: %s\n"
47 "stderr: %s" % (cmd, exit_code, comm[0], comm[1]),
48 file=sys.stderr)
49 return comm, exit_code
50
Marco Brohet6b6b4e52014-07-20 00:05:16 +020051
Marco Brohet6b6b4e52014-07-20 00:05:16 +020052def push_as_commit(path, name, branch, username):
Anthony Kingb8607632015-05-01 22:06:37 +030053 print('Committing %s on branch %s' % (name, branch))
Marco Brohetcb5cdb42014-07-11 22:41:53 +020054
55 # Get path
Anthony Kingb8607632015-05-01 22:06:37 +030056 path = os.path.join(os.getcwd(), path)
57 if not path.endswith('.git'):
58 path = os.path.join(path, '.git')
Marco Brohetcb5cdb42014-07-11 22:41:53 +020059
Marco Brohet6b6b4e52014-07-20 00:05:16 +020060 # Create repo object
Marco Brohetcb5cdb42014-07-11 22:41:53 +020061 repo = git.Repo(path)
Marco Brohet6b6b4e52014-07-20 00:05:16 +020062
63 # Remove previously deleted files from Git
Anthony Kingb8607632015-05-01 22:06:37 +030064 files = repo.git.ls_files(d=True).split('\n')
65 if files and files[0]:
66 repo.git.rm(files)
Marco Brohet6b6b4e52014-07-20 00:05:16 +020067
68 # Add all files to commit
Marco Brohetcb5cdb42014-07-11 22:41:53 +020069 repo.git.add('-A')
Marco Brohet6b6b4e52014-07-20 00:05:16 +020070
71 # Create commit; if it fails, probably empty so skipping
Marco Brohetcb5cdb42014-07-11 22:41:53 +020072 try:
73 repo.git.commit(m='Automatic translation import')
74 except:
Anthony Kingb8607632015-05-01 22:06:37 +030075 print('Failed to create commit for %s, probably empty: skipping'
76 % name, file=sys.stderr)
Marco Brohetcb5cdb42014-07-11 22:41:53 +020077 return
Marco Brohet6b6b4e52014-07-20 00:05:16 +020078
79 # Push commit
Michael Bestasf96f67b2014-10-21 00:43:37 +030080 try:
Anthony Kingb8607632015-05-01 22:06:37 +030081 repo.git.push('ssh://%s@review.cyanogenmod.org:29418/%s' % (username, name),
82 'HEAD:refs/for/%s%%topic=translation' % branch)
83 print('Successfully pushed commit for %s' % name)
Michael Bestasf96f67b2014-10-21 00:43:37 +030084 except:
Anthony Kingb8607632015-05-01 22:06:37 +030085 print('Failed to push commit for %s' % name, file=sys.stderr)
Marco Brohetcb5cdb42014-07-11 22:41:53 +020086
Anthony Kingb8607632015-05-01 22:06:37 +030087
88def check_run(cmd):
Michael Bestas97677e12015-02-08 13:11:59 +020089 p = subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr)
90 ret = p.wait()
91 if ret != 0:
Anthony Kingb8607632015-05-01 22:06:37 +030092 print('Failed to run cmd: %s' % ' '.join(cmd), file=sys.stderr)
Michael Bestas97677e12015-02-08 13:11:59 +020093 sys.exit(ret)
94
Marco Brohet6b6b4e52014-07-20 00:05:16 +020095
Anthony Kingb8607632015-05-01 22:06:37 +030096def find_xml():
97 for dp, dn, file_names in os.walk(os.getcwd()):
98 for f in file_names:
99 if os.path.splitext(f)[1] == '.xml':
100 yield os.path.join(dp, f)
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200101
Anthony Kingb8607632015-05-01 22:06:37 +0300102# ############################################################################ #
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200103
Michael Bestas6b6db122015-02-08 13:22:22 +0200104
Anthony Kingb8607632015-05-01 22:06:37 +0300105def parse_args():
106 parser = argparse.ArgumentParser(
107 description="Synchronising CyanogenMod's translations with Crowdin")
108 sync = parser.add_mutually_exclusive_group()
109 parser.add_argument('-u', '--username', help='Gerrit username',
110 required=True)
111 parser.add_argument('-b', '--branch', help='CyanogenMod branch',
112 required=True)
113 sync.add_argument('--no-upload', action='store_true',
114 help='Only download CM translations from Crowdin')
115 sync.add_argument('--no-download', action='store_true',
116 help='Only upload CM source translations to Crowdin')
117 return parser.parse_args()
Michael Bestas6b6db122015-02-08 13:22:22 +0200118
Anthony Kingb8607632015-05-01 22:06:37 +0300119# ################################# PREPARE ################################## #
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200120
Anthony Kingb8607632015-05-01 22:06:37 +0300121
122def check_dependencies():
Anthony Kingb8607632015-05-01 22:06:37 +0300123 # Check for Ruby version of crowdin-cli
124 cmd = ['gem', 'list', 'crowdin-cli', '-i']
125 if run_subprocess(cmd, silent=True)[1] != 0:
126 print('You have not installed crowdin-cli.', file=sys.stderr)
127 return False
Anthony Kingb8607632015-05-01 22:06:37 +0300128 return True
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200129
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200130
Anthony Kingb8607632015-05-01 22:06:37 +0300131def load_xml(x='android/default.xml'):
132 # Variables regarding android/default.xml
133 print('Loading: %s' % x)
134 try:
135 return minidom.parse(x)
136 except IOError:
137 print('You have no %s.' % x, file=sys.stderr)
138 return None
139 except Exception:
140 # TODO: minidom should not be used.
141 print('Malformed %s.' % x, file=sys.stderr)
142 return None
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200143
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300144
Anthony Kingb8607632015-05-01 22:06:37 +0300145def check_files(branch):
Michael Bestas6c327e62015-05-02 01:58:01 +0300146 files = ['crowdin/extra_packages_%s.xml' % branch,
Anthony Kingb8607632015-05-01 22:06:37 +0300147 'crowdin/crowdin_%s.yaml' % branch,
148 'crowdin/crowdin_%s_aosp.yaml' % branch
149 ]
150 for f in files:
151 if not os.path.isfile(f):
152 print('You have no %s.' % f, file=sys.stderr)
153 return False
Anthony Kingb8607632015-05-01 22:06:37 +0300154 return True
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300155
Anthony Kingb8607632015-05-01 22:06:37 +0300156# ################################### MAIN ################################### #
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300157
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300158
Anthony Kingb8607632015-05-01 22:06:37 +0300159def upload_crowdin(branch, no_upload=False):
Anthony Kingb8607632015-05-01 22:06:37 +0300160 if no_upload:
161 print('Skipping source translations upload')
162 return
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200163
Michael Bestas99f5fce2015-06-04 22:07:51 +0300164 print('\nUploading Crowdin source translations (AOSP supported languages)')
Anthony King69a95382015-02-08 18:44:10 +0000165 # Execute 'crowdin-cli upload sources' and show output
Michael Bestas6c327e62015-05-02 01:58:01 +0300166 check_run(['crowdin-cli', '--config=crowdin/crowdin_%s.yaml' % branch,
Anthony Kingb8607632015-05-01 22:06:37 +0300167 'upload', 'sources'])
Anthony King69a95382015-02-08 18:44:10 +0000168
Anthony Kingb8607632015-05-01 22:06:37 +0300169 print('\nUploading Crowdin source translations '
170 '(non-AOSP supported languages)')
171 # Execute 'crowdin-cli upload sources' and show output
Michael Bestas6c327e62015-05-02 01:58:01 +0300172 check_run(['crowdin-cli', '--config=crowdin/crowdin_%s_aosp.yaml' % branch,
Anthony Kingb8607632015-05-01 22:06:37 +0300173 'upload', 'sources'])
174
175
176def download_crowdin(branch, xml, username, no_download=False):
Anthony Kingb8607632015-05-01 22:06:37 +0300177 if no_download:
178 print('Skipping translations download')
179 return
180
181 print('\nDownloading Crowdin translations (AOSP supported languages)')
Michael Bestas919053f2014-10-20 23:30:54 +0300182 # Execute 'crowdin-cli download' and show output
Michael Bestas6c327e62015-05-02 01:58:01 +0300183 check_run(['crowdin-cli', '--config=crowdin/crowdin_%s.yaml' % branch,
Anthony Kingb8607632015-05-01 22:06:37 +0300184 'download', '--ignore-match'])
Michael Bestas50579d22014-08-09 17:49:14 +0300185
Michael Bestasa02eb4b2015-02-08 15:47:01 +0200186 print('\nDownloading Crowdin translations (non-AOSP supported languages)')
Michael Bestas919053f2014-10-20 23:30:54 +0300187 # Execute 'crowdin-cli download' and show output
Michael Bestas6c327e62015-05-02 01:58:01 +0300188 check_run(['crowdin-cli', '--config=crowdin/crowdin_%s_aosp.yaml' % branch,
Anthony Kingb8607632015-05-01 22:06:37 +0300189 'download', '--ignore-match'])
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200190
Michael Bestas99f5fce2015-06-04 22:07:51 +0300191 print('\nRemoving useless empty translation files')
Anthony Kingb8607632015-05-01 22:06:37 +0300192 empty_contents = {
193 '<resources/>',
194 '<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>',
195 ('<resources xmlns:android='
196 '"http://schemas.android.com/apk/res/android"/>'),
197 ('<resources xmlns:android="http://schemas.android.com/apk/res/android"'
198 ' xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>'),
199 ('<resources xmlns:tools="http://schemas.android.com/tools"'
200 ' xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>')
201 }
202 xf = None
203 for xml_file in find_xml():
204 xf = open(xml_file).read()
Michael Bestas919053f2014-10-20 23:30:54 +0300205 for line in empty_contents:
Anthony Kingb8607632015-05-01 22:06:37 +0300206 if line in xf:
Michael Bestas919053f2014-10-20 23:30:54 +0300207 print('Removing ' + xml_file)
208 os.remove(xml_file)
209 break
Anthony Kingb8607632015-05-01 22:06:37 +0300210 del xf
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200211
Michael Bestas99f5fce2015-06-04 22:07:51 +0300212 print('\nCreating a list of pushable translations')
Michael Bestas919053f2014-10-20 23:30:54 +0300213 # Get all files that Crowdin pushed
Anthony Kingb8607632015-05-01 22:06:37 +0300214 paths = []
215 files = [
Michael Bestas6c327e62015-05-02 01:58:01 +0300216 ('crowdin/crowdin_%s.yaml' % branch),
217 ('crowdin/crowdin_%s_aosp.yaml' % branch)
Anthony Kingb8607632015-05-01 22:06:37 +0300218 ]
Michael Bestas6c327e62015-05-02 01:58:01 +0300219 for c in files:
220 cmd = ['crowdin-cli', '--config=%s' % c, 'list', 'sources']
Anthony Kingb8607632015-05-01 22:06:37 +0300221 comm, ret = run_subprocess(cmd)
222 if ret != 0:
223 sys.exit(ret)
224 for p in str(comm[0]).split("\n"):
225 paths.append(p.replace('/%s' % branch, ''))
Michael Bestas50579d22014-08-09 17:49:14 +0300226
Michael Bestas99f5fce2015-06-04 22:07:51 +0300227 print('\nUploading translations to Gerrit')
Anthony Kingb8607632015-05-01 22:06:37 +0300228 items = [x for sub in xml for x in sub.getElementsByTagName('project')]
Michael Bestas919053f2014-10-20 23:30:54 +0300229 all_projects = []
230
Anthony Kingb8607632015-05-01 22:06:37 +0300231 for path in paths:
232 path = path.strip()
Michael Bestas919053f2014-10-20 23:30:54 +0300233 if not path:
234 continue
235
Anthony Kingb8607632015-05-01 22:06:37 +0300236 if "/res" not in path:
237 print('WARNING: Cannot determine project root dir of '
238 '[%s], skipping.' % path)
Anthony King69a95382015-02-08 18:44:10 +0000239 continue
Anthony Kingb8607632015-05-01 22:06:37 +0300240 result = path.split('/res')[0].strip('/')
241 if result == path.strip('/'):
242 print('WARNING: Cannot determine project root dir of '
243 '[%s], skipping.' % path)
244 continue
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200245
Michael Bestasc899b8c2015-03-03 00:53:19 +0200246 if result in all_projects:
Michael Bestasc899b8c2015-03-03 00:53:19 +0200247 continue
Michael Bestas50579d22014-08-09 17:49:14 +0300248
Anthony Kingb8607632015-05-01 22:06:37 +0300249 # When a project has multiple translatable files, Crowdin will
250 # give duplicates.
251 # We don't want that (useless empty commits), so we save each
252 # project in all_projects and check if it's already in there.
Michael Bestasc899b8c2015-03-03 00:53:19 +0200253 all_projects.append(result)
Anthony King69a95382015-02-08 18:44:10 +0000254
Anthony Kingb8607632015-05-01 22:06:37 +0300255 # Search android/default.xml or crowdin/extra_packages_%(branch).xml
256 # for the project's name
257 for project in items:
258 path = project.attributes['path'].value
259 if not (result + '/').startswith(path +'/'):
Michael Bestasc899b8c2015-03-03 00:53:19 +0200260 continue
Anthony Kingb8607632015-05-01 22:06:37 +0300261 if result != path:
262 if path in all_projects:
263 break
264 result = path
265 all_projects.append(result)
Anthony King69a95382015-02-08 18:44:10 +0000266
Anthony Kingb8607632015-05-01 22:06:37 +0300267 br = project.getAttribute('revision') or branch
Anthony King69a95382015-02-08 18:44:10 +0000268
Anthony Kingb8607632015-05-01 22:06:37 +0300269 push_as_commit(result, project.getAttribute('name'), br, username)
270 break
Anthony King69a95382015-02-08 18:44:10 +0000271
Anthony King69a95382015-02-08 18:44:10 +0000272
Anthony Kingb8607632015-05-01 22:06:37 +0300273def main():
Anthony Kingb8607632015-05-01 22:06:37 +0300274 args = parse_args()
275 default_branch = args.branch
276
Michael Bestas99f5fce2015-06-04 22:07:51 +0300277 if not check_dependencies():
278 sys.exit(1)
Anthony Kingb8607632015-05-01 22:06:37 +0300279
280 xml_android = load_xml()
281 if xml_android is None:
282 sys.exit(1)
283
284 xml_extra = load_xml(x='crowdin/extra_packages_%s.xml' % default_branch)
285 if xml_extra is None:
286 sys.exit(1)
287
288 if not check_files(default_branch):
289 sys.exit(1)
290
291 upload_crowdin(default_branch, args.no_upload)
292 download_crowdin(default_branch, (xml_android, xml_extra),
293 args.username, args.no_download)
294 print('\nDone!')
295
296if __name__ == '__main__':
297 main()