Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 1 | #!/usr/bin/python2 |
Marco Brohet | f174272 | 2014-03-04 22:41:18 +0100 | [diff] [blame] | 2 | # -*- coding: utf-8 -*- |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 3 | # cm_crowdin_sync.py |
| 4 | # |
| 5 | # Updates Crowdin source translations and pulls translations |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 6 | # 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 Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 21 | |
Marco Brohet | f174272 | 2014-03-04 22:41:18 +0100 | [diff] [blame] | 22 | import codecs |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 23 | import git |
| 24 | import mmap |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame] | 25 | import os |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 26 | import os.path |
| 27 | import re |
| 28 | import shutil |
| 29 | import subprocess |
| 30 | import sys |
| 31 | from urllib import urlretrieve |
| 32 | from xml.dom import minidom |
| 33 | |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 34 | def 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 Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 45 | |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 46 | # 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: |
Marco Brohet | 25623ce | 2014-03-08 19:13:07 +0100 | [diff] [blame^] | 81 | if z not in names_base_string: |
| 82 | for string_item in list_cm_string: |
| 83 | if string_item.attributes['name'].value == z: |
| 84 | caf_additions.append(' ' + string_item.toxml()) |
| 85 | break |
| 86 | for y in names_cm_string_array: |
| 87 | if y not in names_base_string_array: |
| 88 | for string_array_item in list_cm_string_array: |
| 89 | if string_array_item.attributes['name'].value == y: |
| 90 | caf_additions.append(' ' + string_array_item.toxml()) |
| 91 | break |
| 92 | for x in names_cm_plurals: |
| 93 | if x not in names_base_plurals: |
| 94 | for plurals_item in list_cm_plurals: |
| 95 | if plurals_item.attributes['name'].value == x: |
| 96 | caf_additions.append(' ' + plurals_item.toxml()) |
| 97 | break |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 98 | |
| 99 | # Done :-) |
| 100 | return caf_additions |
| 101 | |
Marco Brohet | 7165b4e | 2014-03-02 17:31:17 +0100 | [diff] [blame] | 102 | def sync_js_translations(sync_type, path, lang=''): |
| 103 | # lang is necessary in download mode |
| 104 | if sync_type == 'download' and lang == '': |
| 105 | sys.exit('Invalid syntax. Language code is required in download mode.') |
| 106 | |
| 107 | # Read source en.js file. This is necessary for both upload and download modes |
Marco Brohet | 44657ed | 2014-03-04 22:49:23 +0100 | [diff] [blame] | 108 | with codecs.open(path + 'en.js', 'r', 'utf-8') as f: |
Marco Brohet | 7165b4e | 2014-03-02 17:31:17 +0100 | [diff] [blame] | 109 | content = f.readlines() |
| 110 | |
| 111 | if sync_type == 'upload': |
| 112 | # Prepare XML file structure |
| 113 | doc = xml.dom.minidom.Document() |
| 114 | header = doc.createElement('resources') |
Marco Brohet | 44657ed | 2014-03-04 22:49:23 +0100 | [diff] [blame] | 115 | file_write = codecs.open(path + 'en.xml', 'w', 'utf-8') |
Marco Brohet | 7165b4e | 2014-03-02 17:31:17 +0100 | [diff] [blame] | 116 | else: |
| 117 | # Open translation files |
Marco Brohet | 44657ed | 2014-03-04 22:49:23 +0100 | [diff] [blame] | 118 | file_write = codecs.open(path + lang + '.js', 'w', 'utf-8') |
Marco Brohet | 7165b4e | 2014-03-02 17:31:17 +0100 | [diff] [blame] | 119 | xml_base = xml.dom.minidom.parse(path + lang + '.xml') |
| 120 | tags = xml_base.getElementsByTagName('string') |
| 121 | |
| 122 | # Read each line of en.js |
| 123 | for a_line in content: |
| 124 | # Regex to determine string id |
| 125 | m = re.search(' (.*): [\'|\"]', a_line) |
| 126 | if m is not None: |
| 127 | for string_id in m.groups(): |
| 128 | if string_id is not None: |
| 129 | # Find string id |
| 130 | string_id = string_id.replace(' ', '') |
| 131 | m2 = re.search('\'(.*)\'|"(.*)"', a_line) |
| 132 | # Find string contents |
| 133 | for string_content in m2.groups(): |
| 134 | if string_content is not None: |
| 135 | break |
| 136 | if sync_type == 'upload': |
| 137 | # In upload mode, create the appropriate string element. |
| 138 | contents = doc.createElement('string') |
| 139 | contents.attributes['name'] = string_id |
| 140 | contents.appendChild(doc.createTextNode(string_content)) |
| 141 | header.appendChild(contents) |
| 142 | else: |
| 143 | # In download mode, check if string_id matches a name attribute in the translation XML file. |
| 144 | # If it does, replace English text with the translation. |
| 145 | # If it does not, English text will remain and will be added to the file to retain the file structure. |
| 146 | for string in tags: |
| 147 | if string.attributes['name'].value == string_id: |
| 148 | a_line = a_line.replace(string_content.rstrip(), string.firstChild.nodeValue) |
| 149 | break |
| 150 | break |
| 151 | # In download mode do not write comments |
| 152 | if sync_type == 'download' and not '//' in a_line: |
| 153 | # Add language identifier (1) |
| 154 | if 'cmaccount.l10n.en' in a_line: |
| 155 | a_line = a_line.replace('l10n.en', 'l10n.' + lang) |
| 156 | # Add language identifier (2) |
| 157 | if 'l10n.add(\'en\'' in a_line: |
| 158 | a_line = a_line.replace('l10n.add(\'en\'', 'l10n.add(\'' + lang + '\'') |
| 159 | # Now write the line |
| 160 | file_write.write(a_line) |
| 161 | |
| 162 | |
| 163 | # Create XML file structure |
| 164 | if sync_type == 'upload': |
| 165 | header.appendChild(contents) |
| 166 | contents = header.toxml().replace('<string', '\n <string').replace('</resources>', '\n</resources>') |
| 167 | file_write.write('<?xml version="1.0" encoding="utf-8"?>\n') |
| 168 | file_write.write('<!-- .JS CONVERTED TO .XML - DO NOT MERGE THIS FILE -->\n') |
| 169 | file_write.write(contents) |
| 170 | |
| 171 | # Close file |
| 172 | file_write.close() |
| 173 | |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 174 | def push_as_commit(path, name): |
| 175 | # Get path |
| 176 | path = os.getcwd() + '/' + path |
| 177 | |
| 178 | # Create git commit |
| 179 | repo = git.Repo(path) |
| 180 | repo.git.add(path) |
| 181 | try: |
| 182 | repo.git.commit(m='DO NOT MERGE: Automatic translation import test commit') |
| 183 | # repo.git.push('ssh://cobjeM@review.cyanogenmod.org:29418/' + name, 'HEAD:refs/for/cm-11.0') |
| 184 | print 'Succesfully pushed commit for ' + name |
| 185 | except: |
| 186 | # If git commit fails, it's probably because of no changes. |
| 187 | # Just continue. |
| 188 | print 'No commit pushed (probably empty?) for ' + name |
| 189 | print 'WARNING: If the repository name was not obtained from default.xml, the name might be wrong!' |
| 190 | |
| 191 | print('Welcome to the CM Crowdin sync script!') |
| 192 | |
| 193 | print('\nSTEP 0: Checking dependencies') |
Marco Brohet | 7165b4e | 2014-03-02 17:31:17 +0100 | [diff] [blame] | 194 | # Check for Ruby version of crowdin-cli |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 195 | if subprocess.check_output(['rvm', 'all', 'do', 'gem', 'list', 'crowdin-cli', '-i']) == 'true': |
| 196 | sys.exit('You have not installed crowdin-cli. Terminating.') |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 197 | else: |
| 198 | print('Found: crowdin-cli') |
Marco Brohet | 7165b4e | 2014-03-02 17:31:17 +0100 | [diff] [blame] | 199 | # Check for caf.xml |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 200 | if not os.path.isfile('caf.xml'): |
| 201 | sys.exit('You have no caf.xml. Terminating.') |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 202 | else: |
| 203 | print('Found: caf.xml') |
Marco Brohet | 7165b4e | 2014-03-02 17:31:17 +0100 | [diff] [blame] | 204 | # Check for default.xml |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 205 | if not os.path.isfile('default.xml'): |
| 206 | sys.exit('You have no default.xml. Terminating.') |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 207 | else: |
| 208 | print('Found: default.xml') |
Marco Brohet | 7165b4e | 2014-03-02 17:31:17 +0100 | [diff] [blame] | 209 | # Check for repo |
| 210 | try: |
| 211 | subprocess.check_output(['which', 'repo']) |
| 212 | except: |
| 213 | sys.exit('You have not installed repo. Terminating.') |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 214 | |
Marco Brohet | 25623ce | 2014-03-08 19:13:07 +0100 | [diff] [blame^] | 215 | print('\nSTEP 1: Create cm_caf.xml') |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 216 | # Load caf.xml |
Marco Brohet | 25623ce | 2014-03-08 19:13:07 +0100 | [diff] [blame^] | 217 | print('Loading caf.xml') |
| 218 | xml = minidom.parse('caf.xml') |
| 219 | items = xml.getElementsByTagName('item') |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 220 | |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 221 | # Store all created cm_caf.xml files in here. |
| 222 | # Easier to remove them afterwards, as they cannot be committed |
Marco Brohet | 25623ce | 2014-03-08 19:13:07 +0100 | [diff] [blame^] | 223 | cm_caf = [] |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 224 | |
Marco Brohet | 25623ce | 2014-03-08 19:13:07 +0100 | [diff] [blame^] | 225 | for item in items: |
| 226 | # Create tmp dir for download of AOSP base file |
| 227 | path_to_values = item.attributes["path"].value |
| 228 | subprocess.call(['mkdir', '-p', 'tmp/' + path_to_values]) |
| 229 | # Create cm_caf.xml - header |
| 230 | f = codecs.open(path_to_values + '/cm_caf.xml', 'w', 'utf-8') |
| 231 | f.write('<?xml version="1.0" encoding="utf-8"?>\n') |
| 232 | f.write('<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">\n') |
| 233 | # Create cm_caf.xml - contents |
| 234 | # This means we also support multiple base files (e.g. checking if strings.xml and arrays.xml are changed) |
| 235 | contents = [] |
| 236 | item_aosp = item.getElementsByTagName('aosp') |
| 237 | for aosp_item in item_aosp: |
| 238 | url = aosp_item.firstChild.nodeValue |
| 239 | xml_file = aosp_item.attributes["file"].value |
| 240 | path_to_base = 'tmp/' + path_to_values + '/' + xml_file |
| 241 | path_to_cm = path_to_values + '/' + xml_file |
| 242 | urlretrieve(url, path_to_base) |
| 243 | contents = contents + get_caf_additions(path_to_base, path_to_cm) |
| 244 | for addition in contents: |
| 245 | f.write(addition + '\n') |
| 246 | # Create cm_caf.xml - the end |
| 247 | f.write('</resources>') |
| 248 | f.close() |
| 249 | cm_caf.append(path_to_values + '/cm_caf.xml') |
| 250 | print('Created ' + path_to_values + '/cm_caf.xml') |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 251 | |
| 252 | print('\nSTEP 2: Upload Crowdin source translations') |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 253 | # Execute 'crowdin-cli upload sources' and show output |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 254 | print(subprocess.check_output(['crowdin-cli', 'upload', 'sources'])) |
| 255 | |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame] | 256 | print('STEP 3: Download Crowdin translations') |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 257 | # Execute 'crowdin-cli download' and show output |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame] | 258 | print(subprocess.check_output(['crowdin-cli', "download"])) |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 259 | |
Marco Brohet | 25623ce | 2014-03-08 19:13:07 +0100 | [diff] [blame^] | 260 | print('STEP 4A: Clean up of source cm_caf.xmls') |
| 261 | # Remove all cm_caf.xml files, which you can find in the list 'cm_caf' |
| 262 | for cm_caf_file in cm_caf: |
| 263 | print ('Removing ' + cm_caf_file) |
| 264 | os.remove(cm_caf_file) |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame] | 265 | |
Marco Brohet | 25623ce | 2014-03-08 19:13:07 +0100 | [diff] [blame^] | 266 | print('\nSTEP 4B: Clean up of temp dir') |
| 267 | # We are done with cm_caf.xml files, so remove tmp/ |
| 268 | shutil.rmtree(os.getcwd() + '/tmp') |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame] | 269 | |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 270 | print('\nSTEP 4C: Clean up of empty translations') |
| 271 | # Some line of code that I found to find all XML files |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 272 | 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'] |
| 273 | for xml_file in result: |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 274 | # We hate empty, useless files. Crowdin exports them with <resources/> (sometimes with xliff). |
| 275 | # That means: easy to find |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 276 | if '<resources/>' in open(xml_file).read(): |
| 277 | print ('Removing ' + xml_file) |
| 278 | os.remove(xml_file) |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame] | 279 | elif '<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>' in open(xml_file).read(): |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 280 | print ('Removing ' + xml_file) |
| 281 | os.remove(xml_file) |
| 282 | |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 283 | print('\nSTEP 5: Push translations to Git') |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 284 | # Get all files that Crowdin pushed |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 285 | proc = subprocess.Popen(['crowdin-cli', 'list', 'sources'],stdout=subprocess.PIPE) |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 286 | xml = minidom.parse('default.xml') |
| 287 | items = xml.getElementsByTagName('project') |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame] | 288 | all_projects = [] |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 289 | |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame] | 290 | for path in iter(proc.stdout.readline,''): |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 291 | # Remove the \n at the end of each line |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame] | 292 | path = path.rstrip() |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 293 | # Get project root dir from Crowdin's output |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame] | 294 | m = re.search('/(.*Superuser)/Superuser.*|/(.*LatinIME).*|/(frameworks/base).*|/(.*CMFileManager).*|/(device/.*/.*)/.*/res/values.*|/(hardware/.*/.*)/.*/res/values.*|/(.*)/res/values.*', path) |
| 295 | for good_path in m.groups(): |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 296 | # When a project has multiple translatable files, Crowdin will give duplicates. |
| 297 | # We don't want that (useless empty commits), so we save each project in all_projects |
| 298 | # and check if it's already in there. |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame] | 299 | if good_path is not None and not good_path in all_projects: |
| 300 | all_projects.append(good_path) |
| 301 | working = 'false' |
| 302 | for project_item in items: |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 303 | # We need to have the Github repository for the git push url. Obtain them from |
| 304 | # default.xml based on the project root dir. |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame] | 305 | if project_item.attributes["path"].value == good_path: |
| 306 | working = 'true' |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 307 | push_as_commit(good_path, project_item.attributes['name'].value) |
| 308 | print 'Committing ' + project_item.attributes['name'].value + ' (based on default.xml)' |
| 309 | # We also translate repositories that are not downloaded by default (e.g. device parts). |
| 310 | # This is just a fallback. |
| 311 | # WARNING: If the name is wrong, this will not stop the script. |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame] | 312 | if working == 'false': |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 313 | push_as_commit(good_path, 'CyanogenMod/android_' + good_path.replace('/', '_')) |
| 314 | print 'Committing ' + project_item.attributes['name'].value + ' (workaround)' |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 315 | |
| 316 | print('STEP 6: Done!') |