blob: 8a6ad84334e424fb5bbfd62d64af6e97502a29ed [file] [log] [blame]
Marco Brohet4683bee2014-02-28 01:06:03 +01001#!/usr/bin/python2
2#
3# cm_crowdin_sync.py
4#
5# Updates Crowdin source translations and pulls translations
Marco Brohet8b78a1b2014-02-28 21:01:26 +01006# directly to CyanogenMod's Git.
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.
Marco Brohet4683bee2014-02-28 01:06:03 +010021
Marco Brohet4683bee2014-02-28 01:06:03 +010022import git
23import mmap
Marco Brohetcf4069b2014-02-28 18:48:17 +010024import os
Marco Brohet4683bee2014-02-28 01:06:03 +010025import os.path
26import re
27import shutil
28import subprocess
29import sys
30from urllib import urlretrieve
31from xml.dom import minidom
32
Marco Brohet8b78a1b2014-02-28 21:01:26 +010033def get_caf_additions(strings_base, strings_cm):
34 # Load AOSP file and resources
35 xml_base = minidom.parse(strings_base)
36 list_base_string = xml_base.getElementsByTagName('string')
37 list_base_string_array = xml_base.getElementsByTagName('string-array')
38 list_base_plurals = xml_base.getElementsByTagName('plurals')
39 # Load CM file and resources
40 xml_cm = minidom.parse(strings_cm)
41 list_cm_string = xml_cm.getElementsByTagName('string')
42 list_cm_string_array = xml_cm.getElementsByTagName('string-array')
43 list_cm_plurals = xml_cm.getElementsByTagName('plurals')
Marco Brohet4683bee2014-02-28 01:06:03 +010044
Marco Brohet8b78a1b2014-02-28 21:01:26 +010045 # All names from CM
46 names_cm_string = []
47 names_cm_string_array = []
48 names_cm_plurals = []
49 # All names from AOSP
50 names_base_string = []
51 names_base_string_array = []
52 names_base_plurals = []
53
54 # Get all names from CM
55 for s in list_cm_string :
56 if not s.hasAttribute('translatable') and not s.hasAttribute('translate'):
57 names_cm_string.append(s.attributes['name'].value)
58 for s in list_cm_string_array :
59 if not s.hasAttribute('translatable') and not s.hasAttribute('translate'):
60 names_cm_string_array.append(s.attributes['name'].value)
61 for s in list_cm_plurals :
62 if not s.hasAttribute('translatable') and not s.hasAttribute('translate'):
63 names_cm_plurals.append(s.attributes['name'].value)
64 # Get all names from AOSP
65 for s in list_base_string :
66 if not s.hasAttribute('translatable') and not s.hasAttribute('translate'):
67 names_base_string.append(s.attributes['name'].value)
68 for s in list_base_string_array :
69 if not s.hasAttribute('translatable') and not s.hasAttribute('translate'):
70 names_base_string_array.append(s.attributes['name'].value)
71 for s in list_base_plurals :
72 if not s.hasAttribute('translatable') and not s.hasAttribute('translate'):
73 names_base_plurals.append(s.attributes['name'].value)
74
75 # Store all differences in this list
76 caf_additions = []
77
78 # Add all CAF additions to the list 'caf_additions'
79 for z in names_cm_string:
80 if not z in names_base_string:
81 caf_additions.append(' ' + list_cm_string[names_cm_string.index(z)].toxml())
82 for z in names_cm_string_array:
83 if not z in names_base_string_array:
84 caf_additions.append(' ' + list_cm_string_array[names_cm_string_array.index(z)].toxml())
85 for z in names_cm_plurals:
86 if not z in names_base_plurals:
87 caf_additions.append(' ' + list_cm_plurals[names_cm_plurals.index(z)].toxml())
88
89 # Done :-)
90 return caf_additions
91
Marco Brohet7165b4e2014-03-02 17:31:17 +010092def sync_js_translations(sync_type, path, lang=''):
93 # lang is necessary in download mode
94 if sync_type == 'download' and lang == '':
95 sys.exit('Invalid syntax. Language code is required in download mode.')
96
97 # Read source en.js file. This is necessary for both upload and download modes
98 with open(path + 'en.js') as f:
99 content = f.readlines()
100
101 if sync_type == 'upload':
102 # Prepare XML file structure
103 doc = xml.dom.minidom.Document()
104 header = doc.createElement('resources')
105 file_write = open(path + 'en.xml', 'w')
106 else:
107 # Open translation files
108 file_write = open(path + lang + '.js', 'w')
109 xml_base = xml.dom.minidom.parse(path + lang + '.xml')
110 tags = xml_base.getElementsByTagName('string')
111
112 # Read each line of en.js
113 for a_line in content:
114 # Regex to determine string id
115 m = re.search(' (.*): [\'|\"]', a_line)
116 if m is not None:
117 for string_id in m.groups():
118 if string_id is not None:
119 # Find string id
120 string_id = string_id.replace(' ', '')
121 m2 = re.search('\'(.*)\'|"(.*)"', a_line)
122 # Find string contents
123 for string_content in m2.groups():
124 if string_content is not None:
125 break
126 if sync_type == 'upload':
127 # In upload mode, create the appropriate string element.
128 contents = doc.createElement('string')
129 contents.attributes['name'] = string_id
130 contents.appendChild(doc.createTextNode(string_content))
131 header.appendChild(contents)
132 else:
133 # In download mode, check if string_id matches a name attribute in the translation XML file.
134 # If it does, replace English text with the translation.
135 # If it does not, English text will remain and will be added to the file to retain the file structure.
136 for string in tags:
137 if string.attributes['name'].value == string_id:
138 a_line = a_line.replace(string_content.rstrip(), string.firstChild.nodeValue)
139 break
140 break
141 # In download mode do not write comments
142 if sync_type == 'download' and not '//' in a_line:
143 # Add language identifier (1)
144 if 'cmaccount.l10n.en' in a_line:
145 a_line = a_line.replace('l10n.en', 'l10n.' + lang)
146 # Add language identifier (2)
147 if 'l10n.add(\'en\'' in a_line:
148 a_line = a_line.replace('l10n.add(\'en\'', 'l10n.add(\'' + lang + '\'')
149 # Now write the line
150 file_write.write(a_line)
151
152
153 # Create XML file structure
154 if sync_type == 'upload':
155 header.appendChild(contents)
156 contents = header.toxml().replace('<string', '\n <string').replace('</resources>', '\n</resources>')
157 file_write.write('<?xml version="1.0" encoding="utf-8"?>\n')
158 file_write.write('<!-- .JS CONVERTED TO .XML - DO NOT MERGE THIS FILE -->\n')
159 file_write.write(contents)
160
161 # Close file
162 file_write.close()
163
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100164def push_as_commit(path, name):
165 # Get path
166 path = os.getcwd() + '/' + path
167
168 # Create git commit
169 repo = git.Repo(path)
170 repo.git.add(path)
171 try:
172 repo.git.commit(m='DO NOT MERGE: Automatic translation import test commit')
173# repo.git.push('ssh://cobjeM@review.cyanogenmod.org:29418/' + name, 'HEAD:refs/for/cm-11.0')
174 print 'Succesfully pushed commit for ' + name
175 except:
176 # If git commit fails, it's probably because of no changes.
177 # Just continue.
178 print 'No commit pushed (probably empty?) for ' + name
179 print 'WARNING: If the repository name was not obtained from default.xml, the name might be wrong!'
180
181print('Welcome to the CM Crowdin sync script!')
182
183print('\nSTEP 0: Checking dependencies')
Marco Brohet7165b4e2014-03-02 17:31:17 +0100184# Check for Ruby version of crowdin-cli
Marco Brohet4683bee2014-02-28 01:06:03 +0100185if subprocess.check_output(['rvm', 'all', 'do', 'gem', 'list', 'crowdin-cli', '-i']) == 'true':
186 sys.exit('You have not installed crowdin-cli. Terminating.')
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100187else:
188 print('Found: crowdin-cli')
Marco Brohet7165b4e2014-03-02 17:31:17 +0100189# Check for caf.xml
Marco Brohet4683bee2014-02-28 01:06:03 +0100190if not os.path.isfile('caf.xml'):
191 sys.exit('You have no caf.xml. Terminating.')
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100192else:
193 print('Found: caf.xml')
Marco Brohet7165b4e2014-03-02 17:31:17 +0100194# Check for default.xml
Marco Brohet4683bee2014-02-28 01:06:03 +0100195if not os.path.isfile('default.xml'):
196 sys.exit('You have no default.xml. Terminating.')
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100197else:
198 print('Found: default.xml')
Marco Brohet7165b4e2014-03-02 17:31:17 +0100199# Check for repo
200try:
201 subprocess.check_output(['which', 'repo'])
202except:
203 sys.exit('You have not installed repo. Terminating.')
Marco Brohet4683bee2014-02-28 01:06:03 +0100204
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100205print('\nSTEP 1: Create cm_caf.xml')
206# Load caf.xml
Marco Brohet7165b4e2014-03-02 17:31:17 +0100207print('Loading caf.xml')
Marco Brohet4683bee2014-02-28 01:06:03 +0100208xml = minidom.parse('caf.xml')
209items = xml.getElementsByTagName('item')
210
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100211# Store all created cm_caf.xml files in here.
212# Easier to remove them afterwards, as they cannot be committed
Marco Brohet4683bee2014-02-28 01:06:03 +0100213cm_caf = []
214
215for item in items:
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100216 # Create tmp dir for download of AOSP base file
217 path_to_values = item.attributes["path"].value
218 subprocess.call(['mkdir', '-p', 'tmp/' + path_to_values])
219 # Create cm_caf.xml - header
220 f = open(path_to_values + '/cm_caf.xml','w')
221 f.write('<?xml version="1.0" encoding="utf-8"?>\n')
222 f.write('<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">\n')
223 # Create cm_caf.xml - contents
224 # This means we also support multiple base files (e.g. checking if strings.xml and arrays.xml are changed)
225 contents = []
Marco Brohet4683bee2014-02-28 01:06:03 +0100226 item_aosp = item.getElementsByTagName('aosp')
227 for aosp_item in item_aosp:
228 url = aosp_item.firstChild.nodeValue
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100229 xml_file = aosp_item.attributes["file"].value
230 path_to_base = 'tmp/' + path_to_values + '/' + xml_file
231 path_to_cm = path_to_values + '/' + xml_file
Marco Brohet4683bee2014-02-28 01:06:03 +0100232 urlretrieve(url, path_to_base)
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100233 contents = contents + get_caf_additions(path_to_base, path_to_cm)
234 for addition in contents:
235 f.write(addition + '\n')
236 # Create cm_caf.xml - the end
237 f.write('</resources>')
238 f.close()
239 cm_caf.append(path_to_values + '/cm_caf.xml')
240 print('Created ' + path_to_values + '/cm_caf.xml')
Marco Brohet4683bee2014-02-28 01:06:03 +0100241
242print('\nSTEP 2: Upload Crowdin source translations')
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100243# Execute 'crowdin-cli upload sources' and show output
Marco Brohet4683bee2014-02-28 01:06:03 +0100244print(subprocess.check_output(['crowdin-cli', 'upload', 'sources']))
245
Marco Brohetcf4069b2014-02-28 18:48:17 +0100246print('STEP 3: Download Crowdin translations')
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100247# Execute 'crowdin-cli download' and show output
Marco Brohetcf4069b2014-02-28 18:48:17 +0100248print(subprocess.check_output(['crowdin-cli', "download"]))
Marco Brohet4683bee2014-02-28 01:06:03 +0100249
Marco Brohetcf4069b2014-02-28 18:48:17 +0100250print('STEP 4A: Clean up of source cm_caf.xmls')
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100251# Remove all cm_caf.xml files, which you can find in the list 'cm_caf'
Marco Brohetcf4069b2014-02-28 18:48:17 +0100252for cm_caf_file in cm_caf:
253 print ('Removing ' + cm_caf_file)
254 os.remove(cm_caf_file)
255
256print('\nSTEP 4B: Clean up of temp dir')
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100257# We are done with cm_caf.xml files, so remove tmp/
Marco Brohetcf4069b2014-02-28 18:48:17 +0100258shutil.rmtree(os.getcwd() + '/tmp')
259
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100260print('\nSTEP 4C: Clean up of empty translations')
261# Some line of code that I found to find all XML files
Marco Brohet4683bee2014-02-28 01:06:03 +0100262result = [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']
263for xml_file in result:
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100264 # We hate empty, useless files. Crowdin exports them with <resources/> (sometimes with xliff).
265 # That means: easy to find
Marco Brohet4683bee2014-02-28 01:06:03 +0100266 if '<resources/>' in open(xml_file).read():
267 print ('Removing ' + xml_file)
268 os.remove(xml_file)
Marco Brohetcf4069b2014-02-28 18:48:17 +0100269 elif '<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>' in open(xml_file).read():
Marco Brohet4683bee2014-02-28 01:06:03 +0100270 print ('Removing ' + xml_file)
271 os.remove(xml_file)
272
Marco Brohet4683bee2014-02-28 01:06:03 +0100273print('\nSTEP 5: Push translations to Git')
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100274# Get all files that Crowdin pushed
Marco Brohet4683bee2014-02-28 01:06:03 +0100275proc = subprocess.Popen(['crowdin-cli', 'list', 'sources'],stdout=subprocess.PIPE)
Marco Brohet4683bee2014-02-28 01:06:03 +0100276xml = minidom.parse('default.xml')
277items = xml.getElementsByTagName('project')
Marco Brohetcf4069b2014-02-28 18:48:17 +0100278all_projects = []
Marco Brohet4683bee2014-02-28 01:06:03 +0100279
Marco Brohetcf4069b2014-02-28 18:48:17 +0100280for path in iter(proc.stdout.readline,''):
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100281 # Remove the \n at the end of each line
Marco Brohetcf4069b2014-02-28 18:48:17 +0100282 path = path.rstrip()
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100283 # Get project root dir from Crowdin's output
Marco Brohetcf4069b2014-02-28 18:48:17 +0100284 m = re.search('/(.*Superuser)/Superuser.*|/(.*LatinIME).*|/(frameworks/base).*|/(.*CMFileManager).*|/(device/.*/.*)/.*/res/values.*|/(hardware/.*/.*)/.*/res/values.*|/(.*)/res/values.*', path)
285 for good_path in m.groups():
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100286 # When a project has multiple translatable files, Crowdin will give duplicates.
287 # We don't want that (useless empty commits), so we save each project in all_projects
288 # and check if it's already in there.
Marco Brohetcf4069b2014-02-28 18:48:17 +0100289 if good_path is not None and not good_path in all_projects:
290 all_projects.append(good_path)
291 working = 'false'
292 for project_item in items:
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100293 # We need to have the Github repository for the git push url. Obtain them from
294 # default.xml based on the project root dir.
Marco Brohetcf4069b2014-02-28 18:48:17 +0100295 if project_item.attributes["path"].value == good_path:
296 working = 'true'
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100297 push_as_commit(good_path, project_item.attributes['name'].value)
298 print 'Committing ' + project_item.attributes['name'].value + ' (based on default.xml)'
299 # We also translate repositories that are not downloaded by default (e.g. device parts).
300 # This is just a fallback.
301 # WARNING: If the name is wrong, this will not stop the script.
Marco Brohetcf4069b2014-02-28 18:48:17 +0100302 if working == 'false':
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100303 push_as_commit(good_path, 'CyanogenMod/android_' + good_path.replace('/', '_'))
304 print 'Committing ' + project_item.attributes['name'].value + ' (workaround)'
Marco Brohet4683bee2014-02-28 01:06:03 +0100305
306print('STEP 6: Done!')