blob: 2bce4f4a4fb7370ac8c81a275b0679641272dea6 [file] [log] [blame]
Marco Brohetcb5cdb42014-07-11 22:41:53 +02001#!/usr/bin/python2
2# -*- 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#
8# Copyright (C) 2014 The CyanogenMod Project
9#
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
Marco Brohet6b6b4e52014-07-20 00:05:16 +020022############################################# IMPORTS ##############################################
23
24import argparse
Marco Brohetcb5cdb42014-07-11 22:41:53 +020025import codecs
26import git
27import os
28import os.path
29import re
30import shutil
31import subprocess
32import sys
33from urllib import urlretrieve
34from xml.dom import minidom
35
Marco Brohet6b6b4e52014-07-20 00:05:16 +020036############################################ FUNCTIONS #############################################
37
Marco Brohet6b6b4e52014-07-20 00:05:16 +020038def push_as_commit(path, name, branch, username):
39 print('Committing ' + name + ' on branch ' + branch)
Marco Brohetcb5cdb42014-07-11 22:41:53 +020040
41 # Get path
42 path = os.getcwd() + '/' + path
43
Marco Brohet6b6b4e52014-07-20 00:05:16 +020044 # Create repo object
Marco Brohetcb5cdb42014-07-11 22:41:53 +020045 repo = git.Repo(path)
Marco Brohet6b6b4e52014-07-20 00:05:16 +020046
47 # Remove previously deleted files from Git
Marco Brohetcb5cdb42014-07-11 22:41:53 +020048 removed_files = repo.git.ls_files(d=True).split('\n')
49 try:
50 repo.git.rm(removed_files)
51 except:
52 pass
Marco Brohet6b6b4e52014-07-20 00:05:16 +020053
54 # Add all files to commit
Marco Brohetcb5cdb42014-07-11 22:41:53 +020055 repo.git.add('-A')
Marco Brohet6b6b4e52014-07-20 00:05:16 +020056
57 # Create commit; if it fails, probably empty so skipping
Marco Brohetcb5cdb42014-07-11 22:41:53 +020058 try:
59 repo.git.commit(m='Automatic translation import')
60 except:
61 print('Failed to create commit for ' + name + ', probably empty: skipping')
62 return
Marco Brohet6b6b4e52014-07-20 00:05:16 +020063
64 # Push commit
Michael Bestasf96f67b2014-10-21 00:43:37 +030065 try:
66 repo.git.push('ssh://' + username + '@review.cyanogenmod.org:29418/' + name, 'HEAD:refs/for/' + branch + '%topic=translation')
67 print('Succesfully pushed commit for ' + name)
68 except:
69 print('Failed to push commit for ' + name)
Marco Brohetcb5cdb42014-07-11 22:41:53 +020070
Michael Bestas919053f2014-10-20 23:30:54 +030071####################################################################################################
Marco Brohet6b6b4e52014-07-20 00:05:16 +020072
Marco Brohet6b6b4e52014-07-20 00:05:16 +020073parser = argparse.ArgumentParser(description='Synchronising CyanogenMod\'s translations with Crowdin')
Michael Bestas919053f2014-10-20 23:30:54 +030074sync = parser.add_mutually_exclusive_group()
75parser.add_argument('-u', '--username', help='Gerrit username', required=True)
Michael Bestas5bd992c2015-02-07 00:28:41 +020076parser.add_argument('-b', '--branch', help='CyanogenMod branch', required=True)
Michael Bestas919053f2014-10-20 23:30:54 +030077sync.add_argument('--no-upload', action='store_true', help='Only download CM translations from Crowdin')
78sync.add_argument('--no-download', action='store_true', help='Only upload CM source translations to Crowdin')
79args = parser.parse_args()
80argsdict = vars(args)
Marco Brohet6b6b4e52014-07-20 00:05:16 +020081
Michael Bestas919053f2014-10-20 23:30:54 +030082username = argsdict['username']
Michael Bestas5bd992c2015-02-07 00:28:41 +020083default_branch = argsdict['branch']
Marco Brohet6b6b4e52014-07-20 00:05:16 +020084
Michael Bestas6b6db122015-02-08 13:22:22 +020085####################################################################################################
86
87print('Welcome to the CM Crowdin sync script!')
88
Michael Bestas919053f2014-10-20 23:30:54 +030089############################################# PREPARE ##############################################
Marco Brohet6b6b4e52014-07-20 00:05:16 +020090
Michael Bestas4b26c4e2014-10-23 23:21:59 +030091print('\nSTEP 0: Checking dependencies & define shared variables')
Marco Brohetcb5cdb42014-07-11 22:41:53 +020092# Check for Ruby version of crowdin-cli
93if subprocess.check_output(['rvm', 'all', 'do', 'gem', 'list', 'crowdin-cli', '-i']) == 'true':
94 sys.exit('You have not installed crowdin-cli. Terminating.')
95else:
96 print('Found: crowdin-cli')
97
98# Check for repo
99try:
100 subprocess.check_output(['which', 'repo'])
101except:
102 sys.exit('You have not installed repo. Terminating.')
103
104# Check for android/default.xml
105if not os.path.isfile('android/default.xml'):
106 sys.exit('You have no android/default.xml. Terminating.')
107else:
108 print('Found: android/default.xml')
109
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200110# Variables regarding android/default.xml
111print('Loading: android/default.xml')
112xml_android = minidom.parse('android/default.xml')
113
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300114# Check for crowdin/extra_packages_' + default_branch + '.xml
115if not os.path.isfile('crowdin/extra_packages_' + default_branch + '.xml'):
116 sys.exit('You have no crowdin/extra_packages_' + default_branch + '.xml. Terminating.')
117else:
118 print('Found: crowdin/extra_packages_' + default_branch + '.xml')
119
120# Check for crowdin/config.yaml
121if not os.path.isfile('crowdin/config.yaml'):
122 sys.exit('You have no crowdin/config.yaml. Terminating.')
123else:
124 print('Found: crowdin/config.yaml')
125
126# Check for crowdin/config_aosp.yaml
127if not os.path.isfile('crowdin/config_aosp.yaml'):
128 sys.exit('You have no crowdin/config_aosp.yaml. Terminating.')
129else:
130 print('Found: crowdin/config_aosp.yaml')
131
132# Check for crowdin/crowdin_' + default_branch + '.yaml
133if not os.path.isfile('crowdin/crowdin_' + default_branch + '.yaml'):
134 sys.exit('You have no crowdin/crowdin_' + default_branch + '.yaml. Terminating.')
135else:
136 print('Found: crowdin/crowdin_' + default_branch + '.yaml')
137
138# Check for crowdin/crowdin_' + default_branch + '_aosp.yaml
139if not os.path.isfile('crowdin/crowdin_' + default_branch + '_aosp.yaml'):
140 sys.exit('You have no crowdin/crowdin_' + default_branch + '_aosp.yaml. Terminating.')
141else:
142 print('Found: crowdin/crowdin_' + default_branch + '_aosp.yaml')
143
Michael Bestas919053f2014-10-20 23:30:54 +0300144############################################### MAIN ###############################################
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200145
Michael Bestas919053f2014-10-20 23:30:54 +0300146if not args.no_upload:
147 print('\nSTEP 1: Upload Crowdin source translations')
Michael Bestas919053f2014-10-20 23:30:54 +0300148 print('Uploading Crowdin source translations (AOSP supported languages)')
149 # Execute 'crowdin-cli upload sources' and show output
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300150 print(subprocess.check_output(['crowdin-cli', '--config=crowdin/crowdin_' + default_branch + '.yaml', '--identity=crowdin/config.yaml', 'upload', 'sources']))
151
152 print('Uploading Crowdin source translations (non-AOSP supported languages)')
153 # Execute 'crowdin-cli upload sources' and show output
154 print(subprocess.check_output(['crowdin-cli', '--config=crowdin/crowdin_' + default_branch + '_aosp.yaml', '--identity=crowdin/config_aosp.yaml', 'upload', 'sources']))
Michael Bestas919053f2014-10-20 23:30:54 +0300155else:
156 print('\nSkipping source translations upload')
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200157
Michael Bestas919053f2014-10-20 23:30:54 +0300158if not args.no_download:
159 print('\nSTEP 2: Download Crowdin translations')
160 print('Downloading Crowdin translations (AOSP supported languages)')
161 # Execute 'crowdin-cli download' and show output
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300162 print(subprocess.check_output(['crowdin-cli', '--config=crowdin/crowdin_' + default_branch + '.yaml', '--identity=crowdin/config.yaml', 'download']))
Michael Bestas50579d22014-08-09 17:49:14 +0300163
Michael Bestas919053f2014-10-20 23:30:54 +0300164 print('Downloading Crowdin translations (non-AOSP supported languages)')
165 # Execute 'crowdin-cli download' and show output
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300166 print(subprocess.check_output(['crowdin-cli', '--config=crowdin/crowdin_' + default_branch + '_aosp.yaml', '--identity=crowdin/config_aosp.yaml', 'download']))
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200167
Michael Bestas919053f2014-10-20 23:30:54 +0300168 print('\nSTEP 3: Remove useless empty translations')
169 # Some line of code that I found to find all XML files
170 result = [os.path.join(dp, f) for dp, dn, filenames in os.walk(os.getcwd()) for f in filenames if os.path.splitext(f)[1] == '.xml']
171 empty_contents = {'<resources/>', '<resources xmlns:android="http://schemas.android.com/apk/res/android"/>', '<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>', '<resources xmlns:android="http://schemas.android.com/apk/res/android" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>'}
172 for xml_file in result:
173 for line in empty_contents:
174 if line in open(xml_file).read():
175 print('Removing ' + xml_file)
176 os.remove(xml_file)
177 break
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200178
Michael Bestas919053f2014-10-20 23:30:54 +0300179 print('\nSTEP 4: Create a list of pushable translations')
180 # Get all files that Crowdin pushed
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300181 proc = subprocess.Popen(['crowdin-cli --config=crowdin/crowdin_' + default_branch + '.yaml --identity=crowdin/config.yaml list sources | grep "' + default_branch + '" | sed "s#/' + default_branch + '##g" && crowdin-cli --config=crowdin/crowdin_' + default_branch + '_aosp.yaml --identity=crowdin/config_aosp.yaml list sources | grep "' + default_branch + '" | sed "s#/' + default_branch + '##g"'], stdout=subprocess.PIPE, shell=True)
Michael Bestas919053f2014-10-20 23:30:54 +0300182 proc.wait() # Wait for the above to finish
Michael Bestas50579d22014-08-09 17:49:14 +0300183
Michael Bestas919053f2014-10-20 23:30:54 +0300184 print('\nSTEP 5: Upload to Gerrit')
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300185 xml_extra = minidom.parse('crowdin/extra_packages_' + default_branch + '.xml')
Michael Bestas919053f2014-10-20 23:30:54 +0300186 items = xml_android.getElementsByTagName('project')
187 items += xml_extra.getElementsByTagName('project')
188 all_projects = []
189
190 for path in iter(proc.stdout.readline,''):
191 # Remove the \n at the end of each line
192 path = path.rstrip()
193
194 if not path:
195 continue
196
197 # Get project root dir from Crowdin's output by regex
Michael Bestas44261422015-02-08 05:26:11 +0200198 m = re.search('/(.*LatinIME).*|/(frameworks/base).*|/(.*CMFileManager).*|/(device/.*/.*)/.*/res/values.*|/(hardware/.*/.*)/.*/res/values.*|/(.*)/res/values.*', path)
Michael Bestas919053f2014-10-20 23:30:54 +0300199
200 if not m.groups():
201 # Regex result is empty, warn the user
202 print('WARNING: Cannot determine project root dir of [' + path + '], skipping')
203 continue
204
205 for i in m.groups():
206 if not i:
207 continue
208 result = i
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200209 break
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200210
Michael Bestas919053f2014-10-20 23:30:54 +0300211 if result in all_projects:
212 # Already committed for this project, go to next project
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200213 continue
214
Michael Bestas919053f2014-10-20 23:30:54 +0300215 # When a project has multiple translatable files, Crowdin will give duplicates.
216 # We don't want that (useless empty commits), so we save each project in all_projects
217 # and check if it's already in there.
218 all_projects.append(result)
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200219
Michael Bestas4b26c4e2014-10-23 23:21:59 +0300220 # Search in android/default.xml or crowdin/extra_packages_' + default_branch + '.xml for the project's name
Michael Bestas919053f2014-10-20 23:30:54 +0300221 for project_item in items:
222 if project_item.attributes['path'].value != result:
223 # No match found, go to next item
224 continue
225
226 # Define branch (custom branch if defined in xml file, otherwise the default one)
227 if project_item.hasAttribute('revision'):
228 branch = project_item.attributes['revision'].value
229 else:
230 branch = default_branch
231
232 push_as_commit(result, project_item.attributes['name'].value, branch, username)
233else:
234 print('\nSkipping translations download')
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200235
236############################################### DONE ###############################################
Michael Bestas50579d22014-08-09 17:49:14 +0300237
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200238print('\nDone!')