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 | |
Marco Brohet | 3cd7642 | 2014-03-08 21:12:09 +0100 | [diff] [blame] | 79 | # Store all found strings/arrays/plurals. |
| 80 | # Prevent duplicates with product attribute |
| 81 | found_string = [] |
| 82 | found_string_array = [] |
| 83 | found_plurals = [] |
| 84 | |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 85 | # Add all CAF additions to the list 'caf_additions' |
| 86 | for z in names_cm_string: |
Marco Brohet | 3cd7642 | 2014-03-08 21:12:09 +0100 | [diff] [blame] | 87 | if z not in names_base_string and z not in found_string: |
Marco Brohet | 25623ce | 2014-03-08 19:13:07 +0100 | [diff] [blame] | 88 | for string_item in list_cm_string: |
| 89 | if string_item.attributes['name'].value == z: |
| 90 | caf_additions.append(' ' + string_item.toxml()) |
Marco Brohet | 3cd7642 | 2014-03-08 21:12:09 +0100 | [diff] [blame] | 91 | found_string.append(z) |
Marco Brohet | 25623ce | 2014-03-08 19:13:07 +0100 | [diff] [blame] | 92 | for y in names_cm_string_array: |
Marco Brohet | 3cd7642 | 2014-03-08 21:12:09 +0100 | [diff] [blame] | 93 | if y not in names_base_string_array and y not in found_string_array: |
Marco Brohet | 25623ce | 2014-03-08 19:13:07 +0100 | [diff] [blame] | 94 | for string_array_item in list_cm_string_array: |
| 95 | if string_array_item.attributes['name'].value == y: |
| 96 | caf_additions.append(' ' + string_array_item.toxml()) |
Marco Brohet | 3cd7642 | 2014-03-08 21:12:09 +0100 | [diff] [blame] | 97 | found_string_array.append(y) |
Marco Brohet | 25623ce | 2014-03-08 19:13:07 +0100 | [diff] [blame] | 98 | for x in names_cm_plurals: |
Marco Brohet | 3cd7642 | 2014-03-08 21:12:09 +0100 | [diff] [blame] | 99 | if x not in names_base_plurals and x not in found_plurals: |
Marco Brohet | 25623ce | 2014-03-08 19:13:07 +0100 | [diff] [blame] | 100 | for plurals_item in list_cm_plurals: |
| 101 | if plurals_item.attributes['name'].value == x: |
| 102 | caf_additions.append(' ' + plurals_item.toxml()) |
Marco Brohet | 3cd7642 | 2014-03-08 21:12:09 +0100 | [diff] [blame] | 103 | found_plurals.append(x) |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 104 | |
| 105 | # Done :-) |
| 106 | return caf_additions |
| 107 | |
Marco Brohet | 7165b4e | 2014-03-02 17:31:17 +0100 | [diff] [blame] | 108 | def sync_js_translations(sync_type, path, lang=''): |
| 109 | # lang is necessary in download mode |
| 110 | if sync_type == 'download' and lang == '': |
| 111 | sys.exit('Invalid syntax. Language code is required in download mode.') |
| 112 | |
| 113 | # 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] | 114 | with codecs.open(path + 'en.js', 'r', 'utf-8') as f: |
Marco Brohet | 7165b4e | 2014-03-02 17:31:17 +0100 | [diff] [blame] | 115 | content = f.readlines() |
| 116 | |
| 117 | if sync_type == 'upload': |
| 118 | # Prepare XML file structure |
| 119 | doc = xml.dom.minidom.Document() |
| 120 | header = doc.createElement('resources') |
Marco Brohet | 44657ed | 2014-03-04 22:49:23 +0100 | [diff] [blame] | 121 | file_write = codecs.open(path + 'en.xml', 'w', 'utf-8') |
Marco Brohet | 7165b4e | 2014-03-02 17:31:17 +0100 | [diff] [blame] | 122 | else: |
| 123 | # Open translation files |
Marco Brohet | 44657ed | 2014-03-04 22:49:23 +0100 | [diff] [blame] | 124 | file_write = codecs.open(path + lang + '.js', 'w', 'utf-8') |
Marco Brohet | 7165b4e | 2014-03-02 17:31:17 +0100 | [diff] [blame] | 125 | xml_base = xml.dom.minidom.parse(path + lang + '.xml') |
| 126 | tags = xml_base.getElementsByTagName('string') |
| 127 | |
| 128 | # Read each line of en.js |
| 129 | for a_line in content: |
| 130 | # Regex to determine string id |
| 131 | m = re.search(' (.*): [\'|\"]', a_line) |
| 132 | if m is not None: |
| 133 | for string_id in m.groups(): |
| 134 | if string_id is not None: |
| 135 | # Find string id |
| 136 | string_id = string_id.replace(' ', '') |
| 137 | m2 = re.search('\'(.*)\'|"(.*)"', a_line) |
| 138 | # Find string contents |
| 139 | for string_content in m2.groups(): |
| 140 | if string_content is not None: |
| 141 | break |
| 142 | if sync_type == 'upload': |
| 143 | # In upload mode, create the appropriate string element. |
| 144 | contents = doc.createElement('string') |
| 145 | contents.attributes['name'] = string_id |
| 146 | contents.appendChild(doc.createTextNode(string_content)) |
| 147 | header.appendChild(contents) |
| 148 | else: |
| 149 | # In download mode, check if string_id matches a name attribute in the translation XML file. |
| 150 | # If it does, replace English text with the translation. |
| 151 | # If it does not, English text will remain and will be added to the file to retain the file structure. |
| 152 | for string in tags: |
| 153 | if string.attributes['name'].value == string_id: |
| 154 | a_line = a_line.replace(string_content.rstrip(), string.firstChild.nodeValue) |
| 155 | break |
| 156 | break |
| 157 | # In download mode do not write comments |
| 158 | if sync_type == 'download' and not '//' in a_line: |
| 159 | # Add language identifier (1) |
| 160 | if 'cmaccount.l10n.en' in a_line: |
| 161 | a_line = a_line.replace('l10n.en', 'l10n.' + lang) |
| 162 | # Add language identifier (2) |
| 163 | if 'l10n.add(\'en\'' in a_line: |
| 164 | a_line = a_line.replace('l10n.add(\'en\'', 'l10n.add(\'' + lang + '\'') |
| 165 | # Now write the line |
| 166 | file_write.write(a_line) |
Marco Brohet | 7165b4e | 2014-03-02 17:31:17 +0100 | [diff] [blame] | 167 | |
| 168 | # Create XML file structure |
| 169 | if sync_type == 'upload': |
| 170 | header.appendChild(contents) |
| 171 | contents = header.toxml().replace('<string', '\n <string').replace('</resources>', '\n</resources>') |
| 172 | file_write.write('<?xml version="1.0" encoding="utf-8"?>\n') |
| 173 | file_write.write('<!-- .JS CONVERTED TO .XML - DO NOT MERGE THIS FILE -->\n') |
| 174 | file_write.write(contents) |
| 175 | |
| 176 | # Close file |
| 177 | file_write.close() |
| 178 | |
Michael Bestas | 1000682 | 2014-04-17 15:36:46 +0300 | [diff] [blame] | 179 | def push_as_commit(path, name, branch): |
Michael Bestas | 3daf403 | 2014-04-17 16:38:20 +0300 | [diff] [blame] | 180 | # CM gerrit nickname |
| 181 | username = 'your_nickname' |
| 182 | |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 183 | # Get path |
| 184 | path = os.getcwd() + '/' + path |
| 185 | |
| 186 | # Create git commit |
| 187 | repo = git.Repo(path) |
| 188 | repo.git.add(path) |
Michael Bestas | f2b1090 | 2014-06-21 15:15:34 +0300 | [diff] [blame^] | 189 | removed_files = repo.git.ls_files(d=True).split('\n') |
| 190 | try: |
| 191 | repo.git.rm(removed_files) |
| 192 | except: |
| 193 | pass |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 194 | try: |
Michael Bestas | 3daf403 | 2014-04-17 16:38:20 +0300 | [diff] [blame] | 195 | repo.git.commit(m='Automatic translation import') |
Michael Bestas | 1000682 | 2014-04-17 15:36:46 +0300 | [diff] [blame] | 196 | repo.git.push('ssh://' + username + '@review.cyanogenmod.org:29418/' + name, 'HEAD:refs/for/' + branch) |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 197 | print 'Succesfully pushed commit for ' + name |
| 198 | except: |
| 199 | # If git commit fails, it's probably because of no changes. |
| 200 | # Just continue. |
| 201 | print 'No commit pushed (probably empty?) for ' + name |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 202 | |
| 203 | print('Welcome to the CM Crowdin sync script!') |
| 204 | |
| 205 | print('\nSTEP 0: Checking dependencies') |
Marco Brohet | 7165b4e | 2014-03-02 17:31:17 +0100 | [diff] [blame] | 206 | # Check for Ruby version of crowdin-cli |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 207 | if subprocess.check_output(['rvm', 'all', 'do', 'gem', 'list', 'crowdin-cli', '-i']) == 'true': |
| 208 | sys.exit('You have not installed crowdin-cli. Terminating.') |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 209 | else: |
| 210 | print('Found: crowdin-cli') |
Marco Brohet | 7165b4e | 2014-03-02 17:31:17 +0100 | [diff] [blame] | 211 | # Check for caf.xml |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 212 | if not os.path.isfile('caf.xml'): |
| 213 | sys.exit('You have no caf.xml. Terminating.') |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 214 | else: |
| 215 | print('Found: caf.xml') |
Michael Bestas | 7fd0f07 | 2014-03-29 20:29:13 +0200 | [diff] [blame] | 216 | # Check for android/default.xml |
| 217 | if not os.path.isfile('android/default.xml'): |
| 218 | sys.exit('You have no android/default.xml. Terminating.') |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 219 | else: |
Michael Bestas | 7fd0f07 | 2014-03-29 20:29:13 +0200 | [diff] [blame] | 220 | print('Found: android/default.xml') |
Michael Bestas | 6aeac31 | 2014-04-19 22:05:58 +0300 | [diff] [blame] | 221 | # Check for extra_packages.xml |
| 222 | if not os.path.isfile('extra_packages.xml'): |
| 223 | sys.exit('You have no extra_packages.xml. Terminating.') |
| 224 | else: |
| 225 | print('Found: extra_packages.xml') |
Marco Brohet | 7165b4e | 2014-03-02 17:31:17 +0100 | [diff] [blame] | 226 | # Check for repo |
| 227 | try: |
| 228 | subprocess.check_output(['which', 'repo']) |
| 229 | except: |
| 230 | sys.exit('You have not installed repo. Terminating.') |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 231 | |
Marco Brohet | 25623ce | 2014-03-08 19:13:07 +0100 | [diff] [blame] | 232 | print('\nSTEP 1: Create cm_caf.xml') |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 233 | # Load caf.xml |
Marco Brohet | 25623ce | 2014-03-08 19:13:07 +0100 | [diff] [blame] | 234 | print('Loading caf.xml') |
| 235 | xml = minidom.parse('caf.xml') |
| 236 | items = xml.getElementsByTagName('item') |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 237 | |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 238 | # Store all created cm_caf.xml files in here. |
| 239 | # Easier to remove them afterwards, as they cannot be committed |
Marco Brohet | 25623ce | 2014-03-08 19:13:07 +0100 | [diff] [blame] | 240 | cm_caf = [] |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 241 | |
Marco Brohet | 25623ce | 2014-03-08 19:13:07 +0100 | [diff] [blame] | 242 | for item in items: |
| 243 | # Create tmp dir for download of AOSP base file |
| 244 | path_to_values = item.attributes["path"].value |
| 245 | subprocess.call(['mkdir', '-p', 'tmp/' + path_to_values]) |
| 246 | # Create cm_caf.xml - header |
| 247 | f = codecs.open(path_to_values + '/cm_caf.xml', 'w', 'utf-8') |
| 248 | f.write('<?xml version="1.0" encoding="utf-8"?>\n') |
Michael Bestas | db8462d | 2014-03-25 15:11:50 +0200 | [diff] [blame] | 249 | f.write('<!--\n') |
| 250 | f.write(' Copyright (C) 2014 The CyanogenMod Project\n') |
| 251 | f.write('\n') |
| 252 | f.write(' Licensed under the Apache License, Version 2.0 (the "License");\n') |
| 253 | f.write(' you may not use this file except in compliance with the License.\n') |
| 254 | f.write(' You may obtain a copy of the License at\n') |
| 255 | f.write('\n') |
| 256 | f.write(' http://www.apache.org/licenses/LICENSE-2.0\n') |
| 257 | f.write('\n') |
| 258 | f.write(' Unless required by applicable law or agreed to in writing, software\n') |
| 259 | f.write(' distributed under the License is distributed on an "AS IS" BASIS,\n') |
| 260 | f.write(' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n') |
| 261 | f.write(' See the License for the specific language governing permissions and\n') |
| 262 | f.write(' limitations under the License.\n') |
| 263 | f.write('-->\n') |
Marco Brohet | 25623ce | 2014-03-08 19:13:07 +0100 | [diff] [blame] | 264 | f.write('<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">\n') |
| 265 | # Create cm_caf.xml - contents |
| 266 | # This means we also support multiple base files (e.g. checking if strings.xml and arrays.xml are changed) |
| 267 | contents = [] |
| 268 | item_aosp = item.getElementsByTagName('aosp') |
| 269 | for aosp_item in item_aosp: |
| 270 | url = aosp_item.firstChild.nodeValue |
| 271 | xml_file = aosp_item.attributes["file"].value |
| 272 | path_to_base = 'tmp/' + path_to_values + '/' + xml_file |
| 273 | path_to_cm = path_to_values + '/' + xml_file |
| 274 | urlretrieve(url, path_to_base) |
| 275 | contents = contents + get_caf_additions(path_to_base, path_to_cm) |
| 276 | for addition in contents: |
| 277 | f.write(addition + '\n') |
| 278 | # Create cm_caf.xml - the end |
| 279 | f.write('</resources>') |
| 280 | f.close() |
| 281 | cm_caf.append(path_to_values + '/cm_caf.xml') |
| 282 | print('Created ' + path_to_values + '/cm_caf.xml') |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 283 | |
| 284 | print('\nSTEP 2: Upload Crowdin source translations') |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 285 | # Execute 'crowdin-cli upload sources' and show output |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 286 | print(subprocess.check_output(['crowdin-cli', 'upload', 'sources'])) |
| 287 | |
Michael Bestas | b119b2c | 2014-04-04 18:55:16 +0300 | [diff] [blame] | 288 | print('\nSTEP 3: Download Crowdin translations') |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 289 | # Execute 'crowdin-cli download' and show output |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame] | 290 | print(subprocess.check_output(['crowdin-cli', "download"])) |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 291 | |
Michael Bestas | b119b2c | 2014-04-04 18:55:16 +0300 | [diff] [blame] | 292 | print('\nSTEP 4A: Clean up of source cm_caf.xmls') |
Marco Brohet | 25623ce | 2014-03-08 19:13:07 +0100 | [diff] [blame] | 293 | # Remove all cm_caf.xml files, which you can find in the list 'cm_caf' |
| 294 | for cm_caf_file in cm_caf: |
| 295 | print ('Removing ' + cm_caf_file) |
| 296 | os.remove(cm_caf_file) |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame] | 297 | |
Marco Brohet | 25623ce | 2014-03-08 19:13:07 +0100 | [diff] [blame] | 298 | print('\nSTEP 4B: Clean up of temp dir') |
| 299 | # We are done with cm_caf.xml files, so remove tmp/ |
| 300 | shutil.rmtree(os.getcwd() + '/tmp') |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame] | 301 | |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 302 | print('\nSTEP 4C: Clean up of empty translations') |
| 303 | # Some line of code that I found to find all XML files |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 304 | 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'] |
| 305 | for xml_file in result: |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 306 | # We hate empty, useless files. Crowdin exports them with <resources/> (sometimes with xliff). |
| 307 | # That means: easy to find |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 308 | if '<resources/>' in open(xml_file).read(): |
| 309 | print ('Removing ' + xml_file) |
| 310 | os.remove(xml_file) |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame] | 311 | 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] | 312 | print ('Removing ' + xml_file) |
Michael Bestas | 4bfe452 | 2014-05-24 01:22:05 +0300 | [diff] [blame] | 313 | os.remove(xml_file) |
| 314 | elif '<resources xmlns:android="http://schemas.android.com/apk/res/android" xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>' in open(xml_file).read(): |
| 315 | print ('Removing ' + xml_file) |
| 316 | os.remove(xml_file) |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 317 | |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 318 | print('\nSTEP 5: Push translations to Git') |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 319 | # Get all files that Crowdin pushed |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 320 | proc = subprocess.Popen(['crowdin-cli', 'list', 'sources'],stdout=subprocess.PIPE) |
Michael Bestas | 7fd0f07 | 2014-03-29 20:29:13 +0200 | [diff] [blame] | 321 | xml = minidom.parse('android/default.xml') |
Michael Bestas | 6aeac31 | 2014-04-19 22:05:58 +0300 | [diff] [blame] | 322 | xml_extra = minidom.parse('extra_packages.xml') |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 323 | items = xml.getElementsByTagName('project') |
Michael Bestas | 6aeac31 | 2014-04-19 22:05:58 +0300 | [diff] [blame] | 324 | items += xml_extra.getElementsByTagName('project') |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame] | 325 | all_projects = [] |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 326 | |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame] | 327 | for path in iter(proc.stdout.readline,''): |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 328 | # Remove the \n at the end of each line |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame] | 329 | path = path.rstrip() |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 330 | # Get project root dir from Crowdin's output |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame] | 331 | m = re.search('/(.*Superuser)/Superuser.*|/(.*LatinIME).*|/(frameworks/base).*|/(.*CMFileManager).*|/(device/.*/.*)/.*/res/values.*|/(hardware/.*/.*)/.*/res/values.*|/(.*)/res/values.*', path) |
| 332 | for good_path in m.groups(): |
Marco Brohet | 8b78a1b | 2014-02-28 21:01:26 +0100 | [diff] [blame] | 333 | # When a project has multiple translatable files, Crowdin will give duplicates. |
| 334 | # We don't want that (useless empty commits), so we save each project in all_projects |
| 335 | # and check if it's already in there. |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame] | 336 | if good_path is not None and not good_path in all_projects: |
| 337 | all_projects.append(good_path) |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame] | 338 | for project_item in items: |
Michael Bestas | 6aeac31 | 2014-04-19 22:05:58 +0300 | [diff] [blame] | 339 | # We need to have the Github repository for the git push url. |
| 340 | # Obtain them from android/default.xml or extra_packages.xml. |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame] | 341 | if project_item.attributes["path"].value == good_path: |
Michael Bestas | 1000682 | 2014-04-17 15:36:46 +0300 | [diff] [blame] | 342 | if project_item.hasAttribute('revision'): |
| 343 | branch = project_item.attributes['revision'].value |
| 344 | else: |
| 345 | branch = 'cm-11.0' |
| 346 | print 'Committing ' + project_item.attributes['name'].value + ' on branch ' + branch + ' (based on android/default.xml or extra_packages.xml)' |
| 347 | push_as_commit(good_path, project_item.attributes['name'].value, branch) |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 348 | |
Michael Bestas | 6aeac31 | 2014-04-19 22:05:58 +0300 | [diff] [blame] | 349 | print('\nSTEP 6: Done!') |