blob: 6dae300b20a9d5a784df9cdf202bc07e16ef67b8 [file] [log] [blame]
Marco Brohet4683bee2014-02-28 01:06:03 +01001#!/usr/bin/python2
Marco Brohetf1742722014-03-04 22:41:18 +01002# -*- coding: utf-8 -*-
Marco Brohet4683bee2014-02-28 01:06:03 +01003# 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 Brohetf1742722014-03-04 22:41:18 +010022import codecs
Marco Brohet4683bee2014-02-28 01:06:03 +010023import git
24import mmap
Marco Brohetcf4069b2014-02-28 18:48:17 +010025import os
Marco Brohet4683bee2014-02-28 01:06:03 +010026import os.path
27import re
28import shutil
29import subprocess
30import sys
31from urllib import urlretrieve
32from xml.dom import minidom
33
Marco Brohet8b78a1b2014-02-28 21:01:26 +010034def get_caf_additions(strings_base, strings_cm):
35 # Load AOSP file and resources
36 xml_base = minidom.parse(strings_base)
37 list_base_string = xml_base.getElementsByTagName('string')
38 list_base_string_array = xml_base.getElementsByTagName('string-array')
39 list_base_plurals = xml_base.getElementsByTagName('plurals')
40 # Load CM file and resources
41 xml_cm = minidom.parse(strings_cm)
42 list_cm_string = xml_cm.getElementsByTagName('string')
43 list_cm_string_array = xml_cm.getElementsByTagName('string-array')
44 list_cm_plurals = xml_cm.getElementsByTagName('plurals')
Marco Brohet4683bee2014-02-28 01:06:03 +010045
Marco Brohet8b78a1b2014-02-28 21:01:26 +010046 # All names from CM
47 names_cm_string = []
48 names_cm_string_array = []
49 names_cm_plurals = []
50 # All names from AOSP
51 names_base_string = []
52 names_base_string_array = []
53 names_base_plurals = []
54
55 # Get all names from CM
56 for s in list_cm_string :
57 if not s.hasAttribute('translatable') and not s.hasAttribute('translate'):
58 names_cm_string.append(s.attributes['name'].value)
59 for s in list_cm_string_array :
60 if not s.hasAttribute('translatable') and not s.hasAttribute('translate'):
61 names_cm_string_array.append(s.attributes['name'].value)
62 for s in list_cm_plurals :
63 if not s.hasAttribute('translatable') and not s.hasAttribute('translate'):
64 names_cm_plurals.append(s.attributes['name'].value)
65 # Get all names from AOSP
66 for s in list_base_string :
67 if not s.hasAttribute('translatable') and not s.hasAttribute('translate'):
68 names_base_string.append(s.attributes['name'].value)
69 for s in list_base_string_array :
70 if not s.hasAttribute('translatable') and not s.hasAttribute('translate'):
71 names_base_string_array.append(s.attributes['name'].value)
72 for s in list_base_plurals :
73 if not s.hasAttribute('translatable') and not s.hasAttribute('translate'):
74 names_base_plurals.append(s.attributes['name'].value)
75
76 # Store all differences in this list
77 caf_additions = []
78
79 # Add all CAF additions to the list 'caf_additions'
80 for z in names_cm_string:
81 if not z in names_base_string:
82 caf_additions.append(' ' + list_cm_string[names_cm_string.index(z)].toxml())
83 for z in names_cm_string_array:
84 if not z in names_base_string_array:
85 caf_additions.append(' ' + list_cm_string_array[names_cm_string_array.index(z)].toxml())
86 for z in names_cm_plurals:
87 if not z in names_base_plurals:
88 caf_additions.append(' ' + list_cm_plurals[names_cm_plurals.index(z)].toxml())
89
90 # Done :-)
91 return caf_additions
92
Marco Brohet7165b4e2014-03-02 17:31:17 +010093def sync_js_translations(sync_type, path, lang=''):
94 # lang is necessary in download mode
95 if sync_type == 'download' and lang == '':
96 sys.exit('Invalid syntax. Language code is required in download mode.')
97
98 # Read source en.js file. This is necessary for both upload and download modes
Marco Brohet44657ed2014-03-04 22:49:23 +010099 with codecs.open(path + 'en.js', 'r', 'utf-8') as f:
Marco Brohet7165b4e2014-03-02 17:31:17 +0100100 content = f.readlines()
101
102 if sync_type == 'upload':
103 # Prepare XML file structure
104 doc = xml.dom.minidom.Document()
105 header = doc.createElement('resources')
Marco Brohet44657ed2014-03-04 22:49:23 +0100106 file_write = codecs.open(path + 'en.xml', 'w', 'utf-8')
Marco Brohet7165b4e2014-03-02 17:31:17 +0100107 else:
108 # Open translation files
Marco Brohet44657ed2014-03-04 22:49:23 +0100109 file_write = codecs.open(path + lang + '.js', 'w', 'utf-8')
Marco Brohet7165b4e2014-03-02 17:31:17 +0100110 xml_base = xml.dom.minidom.parse(path + lang + '.xml')
111 tags = xml_base.getElementsByTagName('string')
112
113 # Read each line of en.js
114 for a_line in content:
115 # Regex to determine string id
116 m = re.search(' (.*): [\'|\"]', a_line)
117 if m is not None:
118 for string_id in m.groups():
119 if string_id is not None:
120 # Find string id
121 string_id = string_id.replace(' ', '')
122 m2 = re.search('\'(.*)\'|"(.*)"', a_line)
123 # Find string contents
124 for string_content in m2.groups():
125 if string_content is not None:
126 break
127 if sync_type == 'upload':
128 # In upload mode, create the appropriate string element.
129 contents = doc.createElement('string')
130 contents.attributes['name'] = string_id
131 contents.appendChild(doc.createTextNode(string_content))
132 header.appendChild(contents)
133 else:
134 # In download mode, check if string_id matches a name attribute in the translation XML file.
135 # If it does, replace English text with the translation.
136 # If it does not, English text will remain and will be added to the file to retain the file structure.
137 for string in tags:
138 if string.attributes['name'].value == string_id:
139 a_line = a_line.replace(string_content.rstrip(), string.firstChild.nodeValue)
140 break
141 break
142 # In download mode do not write comments
143 if sync_type == 'download' and not '//' in a_line:
144 # Add language identifier (1)
145 if 'cmaccount.l10n.en' in a_line:
146 a_line = a_line.replace('l10n.en', 'l10n.' + lang)
147 # Add language identifier (2)
148 if 'l10n.add(\'en\'' in a_line:
149 a_line = a_line.replace('l10n.add(\'en\'', 'l10n.add(\'' + lang + '\'')
150 # Now write the line
151 file_write.write(a_line)
152
153
154 # Create XML file structure
155 if sync_type == 'upload':
156 header.appendChild(contents)
157 contents = header.toxml().replace('<string', '\n <string').replace('</resources>', '\n</resources>')
158 file_write.write('<?xml version="1.0" encoding="utf-8"?>\n')
159 file_write.write('<!-- .JS CONVERTED TO .XML - DO NOT MERGE THIS FILE -->\n')
160 file_write.write(contents)
161
162 # Close file
163 file_write.close()
164
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100165def push_as_commit(path, name):
166 # Get path
167 path = os.getcwd() + '/' + path
168
169 # Create git commit
170 repo = git.Repo(path)
171 repo.git.add(path)
172 try:
173 repo.git.commit(m='DO NOT MERGE: Automatic translation import test commit')
174# repo.git.push('ssh://cobjeM@review.cyanogenmod.org:29418/' + name, 'HEAD:refs/for/cm-11.0')
175 print 'Succesfully pushed commit for ' + name
176 except:
177 # If git commit fails, it's probably because of no changes.
178 # Just continue.
179 print 'No commit pushed (probably empty?) for ' + name
180 print 'WARNING: If the repository name was not obtained from default.xml, the name might be wrong!'
181
182print('Welcome to the CM Crowdin sync script!')
183
184print('\nSTEP 0: Checking dependencies')
Marco Brohet7165b4e2014-03-02 17:31:17 +0100185# Check for Ruby version of crowdin-cli
Marco Brohet4683bee2014-02-28 01:06:03 +0100186if subprocess.check_output(['rvm', 'all', 'do', 'gem', 'list', 'crowdin-cli', '-i']) == 'true':
187 sys.exit('You have not installed crowdin-cli. Terminating.')
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100188else:
189 print('Found: crowdin-cli')
Marco Brohet7165b4e2014-03-02 17:31:17 +0100190# Check for caf.xml
Marco Brohet4683bee2014-02-28 01:06:03 +0100191if not os.path.isfile('caf.xml'):
192 sys.exit('You have no caf.xml. Terminating.')
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100193else:
194 print('Found: caf.xml')
Marco Brohet7165b4e2014-03-02 17:31:17 +0100195# Check for default.xml
Marco Brohet4683bee2014-02-28 01:06:03 +0100196if not os.path.isfile('default.xml'):
197 sys.exit('You have no default.xml. Terminating.')
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100198else:
199 print('Found: default.xml')
Marco Brohet7165b4e2014-03-02 17:31:17 +0100200# Check for repo
201try:
202 subprocess.check_output(['which', 'repo'])
203except:
204 sys.exit('You have not installed repo. Terminating.')
Marco Brohet4683bee2014-02-28 01:06:03 +0100205
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100206print('\nSTEP 1: Create cm_caf.xml')
207# Load caf.xml
Marco Brohet7165b4e2014-03-02 17:31:17 +0100208print('Loading caf.xml')
Marco Brohet4683bee2014-02-28 01:06:03 +0100209xml = minidom.parse('caf.xml')
210items = xml.getElementsByTagName('item')
211
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100212# Store all created cm_caf.xml files in here.
213# Easier to remove them afterwards, as they cannot be committed
Marco Brohet4683bee2014-02-28 01:06:03 +0100214cm_caf = []
215
216for item in items:
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100217 # Create tmp dir for download of AOSP base file
218 path_to_values = item.attributes["path"].value
219 subprocess.call(['mkdir', '-p', 'tmp/' + path_to_values])
220 # Create cm_caf.xml - header
Marco Brohetf1742722014-03-04 22:41:18 +0100221 f = codecs.open(path_to_values + '/cm_caf.xml', 'w', 'utf-8')
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100222 f.write('<?xml version="1.0" encoding="utf-8"?>\n')
223 f.write('<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">\n')
224 # Create cm_caf.xml - contents
225 # This means we also support multiple base files (e.g. checking if strings.xml and arrays.xml are changed)
226 contents = []
Marco Brohet4683bee2014-02-28 01:06:03 +0100227 item_aosp = item.getElementsByTagName('aosp')
228 for aosp_item in item_aosp:
229 url = aosp_item.firstChild.nodeValue
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100230 xml_file = aosp_item.attributes["file"].value
231 path_to_base = 'tmp/' + path_to_values + '/' + xml_file
232 path_to_cm = path_to_values + '/' + xml_file
Marco Brohet4683bee2014-02-28 01:06:03 +0100233 urlretrieve(url, path_to_base)
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100234 contents = contents + get_caf_additions(path_to_base, path_to_cm)
235 for addition in contents:
236 f.write(addition + '\n')
237 # Create cm_caf.xml - the end
238 f.write('</resources>')
239 f.close()
240 cm_caf.append(path_to_values + '/cm_caf.xml')
241 print('Created ' + path_to_values + '/cm_caf.xml')
Marco Brohet4683bee2014-02-28 01:06:03 +0100242
243print('\nSTEP 2: Upload Crowdin source translations')
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100244# Execute 'crowdin-cli upload sources' and show output
Marco Brohet4683bee2014-02-28 01:06:03 +0100245print(subprocess.check_output(['crowdin-cli', 'upload', 'sources']))
246
Marco Brohetcf4069b2014-02-28 18:48:17 +0100247print('STEP 3: Download Crowdin translations')
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100248# Execute 'crowdin-cli download' and show output
Marco Brohetcf4069b2014-02-28 18:48:17 +0100249print(subprocess.check_output(['crowdin-cli', "download"]))
Marco Brohet4683bee2014-02-28 01:06:03 +0100250
Marco Brohetcf4069b2014-02-28 18:48:17 +0100251print('STEP 4A: Clean up of source cm_caf.xmls')
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100252# Remove all cm_caf.xml files, which you can find in the list 'cm_caf'
Marco Brohetcf4069b2014-02-28 18:48:17 +0100253for cm_caf_file in cm_caf:
254 print ('Removing ' + cm_caf_file)
255 os.remove(cm_caf_file)
256
257print('\nSTEP 4B: Clean up of temp dir')
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100258# We are done with cm_caf.xml files, so remove tmp/
Marco Brohetcf4069b2014-02-28 18:48:17 +0100259shutil.rmtree(os.getcwd() + '/tmp')
260
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100261print('\nSTEP 4C: Clean up of empty translations')
262# Some line of code that I found to find all XML files
Marco Brohet4683bee2014-02-28 01:06:03 +0100263result = [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']
264for xml_file in result:
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100265 # We hate empty, useless files. Crowdin exports them with <resources/> (sometimes with xliff).
266 # That means: easy to find
Marco Brohet4683bee2014-02-28 01:06:03 +0100267 if '<resources/>' in open(xml_file).read():
268 print ('Removing ' + xml_file)
269 os.remove(xml_file)
Marco Brohetcf4069b2014-02-28 18:48:17 +0100270 elif '<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>' in open(xml_file).read():
Marco Brohet4683bee2014-02-28 01:06:03 +0100271 print ('Removing ' + xml_file)
272 os.remove(xml_file)
273
Marco Brohet4683bee2014-02-28 01:06:03 +0100274print('\nSTEP 5: Push translations to Git')
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100275# Get all files that Crowdin pushed
Marco Brohet4683bee2014-02-28 01:06:03 +0100276proc = subprocess.Popen(['crowdin-cli', 'list', 'sources'],stdout=subprocess.PIPE)
Marco Brohet4683bee2014-02-28 01:06:03 +0100277xml = minidom.parse('default.xml')
278items = xml.getElementsByTagName('project')
Marco Brohetcf4069b2014-02-28 18:48:17 +0100279all_projects = []
Marco Brohet4683bee2014-02-28 01:06:03 +0100280
Marco Brohetcf4069b2014-02-28 18:48:17 +0100281for path in iter(proc.stdout.readline,''):
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100282 # Remove the \n at the end of each line
Marco Brohetcf4069b2014-02-28 18:48:17 +0100283 path = path.rstrip()
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100284 # Get project root dir from Crowdin's output
Marco Brohetcf4069b2014-02-28 18:48:17 +0100285 m = re.search('/(.*Superuser)/Superuser.*|/(.*LatinIME).*|/(frameworks/base).*|/(.*CMFileManager).*|/(device/.*/.*)/.*/res/values.*|/(hardware/.*/.*)/.*/res/values.*|/(.*)/res/values.*', path)
286 for good_path in m.groups():
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100287 # When a project has multiple translatable files, Crowdin will give duplicates.
288 # We don't want that (useless empty commits), so we save each project in all_projects
289 # and check if it's already in there.
Marco Brohetcf4069b2014-02-28 18:48:17 +0100290 if good_path is not None and not good_path in all_projects:
291 all_projects.append(good_path)
292 working = 'false'
293 for project_item in items:
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100294 # We need to have the Github repository for the git push url. Obtain them from
295 # default.xml based on the project root dir.
Marco Brohetcf4069b2014-02-28 18:48:17 +0100296 if project_item.attributes["path"].value == good_path:
297 working = 'true'
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100298 push_as_commit(good_path, project_item.attributes['name'].value)
299 print 'Committing ' + project_item.attributes['name'].value + ' (based on default.xml)'
300 # We also translate repositories that are not downloaded by default (e.g. device parts).
301 # This is just a fallback.
302 # WARNING: If the name is wrong, this will not stop the script.
Marco Brohetcf4069b2014-02-28 18:48:17 +0100303 if working == 'false':
Marco Brohet8b78a1b2014-02-28 21:01:26 +0100304 push_as_commit(good_path, 'CyanogenMod/android_' + good_path.replace('/', '_'))
305 print 'Committing ' + project_item.attributes['name'].value + ' (workaround)'
Marco Brohet4683bee2014-02-28 01:06:03 +0100306
307print('STEP 6: Done!')