blob: a36d473a10282ec5ff58a288529b7b45c6414c03 [file] [log] [blame]
Anthony King69a95382015-02-08 18:44:10 +00001#!/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 King69a95382015-02-08 18:44:10 +000022# ################################# 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 King69a95382015-02-08 18:44:10 +000031
Marco Brohetcb5cdb42014-07-11 22:41:53 +020032from xml.dom import minidom
33
Anthony King69a95382015-02-08 18:44:10 +000034# ################################ 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 King69a95382015-02-08 18:44:10 +000053 print('Committing %s on branch %s' % (name, branch))
Marco Brohetcb5cdb42014-07-11 22:41:53 +020054
55 # Get path
Anthony King69a95382015-02-08 18:44:10 +000056 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 King69a95382015-02-08 18:44:10 +000064 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 King69a95382015-02-08 18:44:10 +000075 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 King69a95382015-02-08 18:44:10 +000081 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 King69a95382015-02-08 18:44:10 +000085 print('Failed to push commit for %s' % name, file=sys.stderr)
Marco Brohetcb5cdb42014-07-11 22:41:53 +020086
Anthony King69a95382015-02-08 18:44:10 +000087
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 King69a95382015-02-08 18:44:10 +000092 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 King69a95382015-02-08 18:44:10 +000096def 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 King69a95382015-02-08 18:44:10 +0000102# ############################################################################ #
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200103
Michael Bestas6b6db122015-02-08 13:22:22 +0200104
Anthony King69a95382015-02-08 18:44:10 +0000105def 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 King69a95382015-02-08 18:44:10 +0000119# ################################# PREPARE ################################## #
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200120
Anthony King69a95382015-02-08 18:44:10 +0000121
122def check_dependencies():
123 print('\nSTEP 0: Checking dependencies & define shared variables')
124
125 # Check for Ruby version of crowdin-cli
126 cmd = ['gem', 'list', 'crowdin-cli', '-i']
127 if run_subprocess(cmd, silent=True)[1] != 0:
128 print('You have not installed crowdin-cli.', file=sys.stderr)
129 return False
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200130 print('Found: crowdin-cli')
131
Anthony King69a95382015-02-08 18:44:10 +0000132 return True
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200133
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200134
Anthony King69a95382015-02-08 18:44:10 +0000135def load_xml(x='android/default.xml'):
136 # Variables regarding android/default.xml
137 print('Loading: %s' % x)
138 try:
139 return minidom.parse(x)
140 except IOError:
141 print('You have no %s.' % x, file=sys.stderr)
142 return None
143 except Exception:
144 # TODO: minidom should not be used.
145 print('Malformed %s.' % x, file=sys.stderr)
146 return None
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200147
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300148
Anthony King69a95382015-02-08 18:44:10 +0000149def check_files(branch):
150 files = ['crowdin/config.yaml',
151 'crowdin/extra_packages_%s.xml' % branch,
152 'crowdin/config_aosp.yaml',
153 'crowdin/crowdin_%s.yaml' % branch,
154 'crowdin/crowdin_%s_aosp.yaml' % branch
155 ]
156 for f in files:
157 if not os.path.isfile(f):
158 print('You have no %s.' % f, file=sys.stderr)
159 return False
160 print('Found: %s' % f)
161 return True
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300162
Anthony King69a95382015-02-08 18:44:10 +0000163# ################################### MAIN ################################### #
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300164
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300165
Anthony King69a95382015-02-08 18:44:10 +0000166def upload_crowdin(branch, no_upload=False):
Michael Bestas919053f2014-10-20 23:30:54 +0300167 print('\nSTEP 1: Upload Crowdin source translations')
Anthony King69a95382015-02-08 18:44:10 +0000168 if no_upload:
169 print('Skipping source translations upload')
170 return
171 print('\nUploading Crowdin source translations (AOSP supported languages)')
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300172
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300173 # Execute 'crowdin-cli upload sources' and show output
Anthony King69a95382015-02-08 18:44:10 +0000174 check_run(['crowdin-cli', '--config=crowdin/crowdin_%s.yaml' % branch,
175 '--identity=crowdin/config.yaml', 'upload', 'sources'])
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200176
Anthony King69a95382015-02-08 18:44:10 +0000177 print('\nUploading Crowdin source translations '
178 '(non-AOSP supported languages)')
179 # Execute 'crowdin-cli upload sources' and show output
180 check_run(['crowdin-cli', '--identity=crowdin/config_aosp.yaml',
181 '--config=crowdin/crowdin_%s_aosp.yaml' % branch,
182 'upload', 'sources'])
183
184
185def download_crowdin(branch, xml, username, no_download=False):
Michael Bestas919053f2014-10-20 23:30:54 +0300186 print('\nSTEP 2: Download Crowdin translations')
Anthony King69a95382015-02-08 18:44:10 +0000187 if no_download:
188 print('Skipping translations download')
189 return
190
191 print('\nDownloading Crowdin translations (AOSP supported languages)')
Michael Bestas919053f2014-10-20 23:30:54 +0300192 # Execute 'crowdin-cli download' and show output
Anthony King69a95382015-02-08 18:44:10 +0000193 check_run(['crowdin-cli', '--config=crowdin/crowdin_%s.yaml' % branch,
194 '--identity=crowdin/config.yaml', 'download'])
Michael Bestas50579d22014-08-09 17:49:14 +0300195
Michael Bestasa02eb4b2015-02-08 15:47:01 +0200196 print('\nDownloading Crowdin translations (non-AOSP supported languages)')
Michael Bestas919053f2014-10-20 23:30:54 +0300197 # Execute 'crowdin-cli download' and show output
Anthony King69a95382015-02-08 18:44:10 +0000198 check_run(['crowdin-cli', '--identity=crowdin/config_aosp.yaml',
199 '--config=crowdin/crowdin_%s_aosp.yaml' % branch, 'download'])
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200200
Michael Bestas919053f2014-10-20 23:30:54 +0300201 print('\nSTEP 3: Remove useless empty translations')
Anthony King69a95382015-02-08 18:44:10 +0000202 empty_contents = {
203 '<resources/>',
204 '<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>',
205 ('<resources xmlns:android='
206 '"http://schemas.android.com/apk/res/android"/>'),
207 ('<resources xmlns:android="http://schemas.android.com/apk/res/android"'
208 ' xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>')
209 }
210 xf = None
211 for xml_file in find_xml():
212 xf = open(xml_file).read()
Michael Bestas919053f2014-10-20 23:30:54 +0300213 for line in empty_contents:
Anthony King69a95382015-02-08 18:44:10 +0000214 if line in xf:
Michael Bestas919053f2014-10-20 23:30:54 +0300215 print('Removing ' + xml_file)
216 os.remove(xml_file)
217 break
Anthony King69a95382015-02-08 18:44:10 +0000218 del xf
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200219
Michael Bestas919053f2014-10-20 23:30:54 +0300220 print('\nSTEP 4: Create a list of pushable translations')
221 # Get all files that Crowdin pushed
Anthony King69a95382015-02-08 18:44:10 +0000222 paths = []
223 files = [
224 ('crowdin/crowdin_%s.yaml' % branch, 'crowdin/config.yaml'),
225 ('crowdin/crowdin_%s_aosp.yaml' % branch, 'crowdin/config_aosp.yaml')
226 ]
227 for c, i in files:
228 cmd = ['crowdin-cli', '--config=%s' % c, '--identity=%s' % i,
229 'list', 'sources']
230 comm, ret = run_subprocess(cmd)
231 if ret != 0:
232 sys.exit(ret)
233 for p in str(comm[0]).split("\n"):
234 paths.append(p.replace('/%s' % branch, ''))
Michael Bestas50579d22014-08-09 17:49:14 +0300235
Michael Bestas919053f2014-10-20 23:30:54 +0300236 print('\nSTEP 5: Upload to Gerrit')
Anthony King69a95382015-02-08 18:44:10 +0000237 items = [x for sub in xml for x in sub.getElementsByTagName('project')]
Michael Bestas919053f2014-10-20 23:30:54 +0300238 all_projects = []
239
Anthony King69a95382015-02-08 18:44:10 +0000240 for path in paths:
241 path = path.strip()
Michael Bestas919053f2014-10-20 23:30:54 +0300242 if not path:
243 continue
244
Anthony King69a95382015-02-08 18:44:10 +0000245 if "/res" not in path:
246 print('WARNING: Cannot determine project root dir of '
247 '[%s], skipping.' % path)
Michael Bestas919053f2014-10-20 23:30:54 +0300248 continue
Anthony King69a95382015-02-08 18:44:10 +0000249 result = path.split('/res')[0].strip('/')
250 if result == path.strip('/'):
251 print('WARNING: Cannot determine project root dir of '
252 '[%s], skipping.' % path)
253 continue
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200254
Michael Bestas919053f2014-10-20 23:30:54 +0300255 if result in all_projects:
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200256 continue
257
Anthony King69a95382015-02-08 18:44:10 +0000258 # When a project has multiple translatable files, Crowdin will
259 # give duplicates.
260 # We don't want that (useless empty commits), so we save each
261 # project in all_projects and check if it's already in there.
Michael Bestas919053f2014-10-20 23:30:54 +0300262 all_projects.append(result)
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200263
Anthony King69a95382015-02-08 18:44:10 +0000264 # Search android/default.xml or crowdin/extra_packages_%(branch).xml
265 # for the project's name
266 for project in items:
267 if project.attributes['path'].value != result:
Michael Bestas919053f2014-10-20 23:30:54 +0300268 continue
269
Anthony King69a95382015-02-08 18:44:10 +0000270 br = project.getAttribute('revision') or branch
Michael Bestas919053f2014-10-20 23:30:54 +0300271
Anthony King69a95382015-02-08 18:44:10 +0000272 push_as_commit(result, project.getAttribute('name'), br, username)
273 break
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200274
Michael Bestas50579d22014-08-09 17:49:14 +0300275
Anthony King69a95382015-02-08 18:44:10 +0000276def main():
277 if not check_dependencies():
278 sys.exit(1)
279
280 args = parse_args()
281 default_branch = args.branch
282
283 print('Welcome to the CM Crowdin sync script!')
284
285 xml_android = load_xml()
286 if xml_android is None:
287 sys.exit(1)
288
289 xml_extra = load_xml(x='crowdin/extra_packages_%s.xml' % default_branch)
290 if xml_extra is None:
291 sys.exit(1)
292
293 if not check_files(default_branch):
294 sys.exit(1)
295
296 upload_crowdin(default_branch, args.no_upload)
297 download_crowdin(default_branch, (xml_android, xml_extra),
298 args.username, args.no_download)
299 print('\nDone!')
300
301if __name__ == '__main__':
302 main()