Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 1 | #!/usr/bin/python2 |
| 2 | # -*- coding: utf-8 -*- |
Michael Bestas | 1ab959b | 2014-07-26 16:01:01 +0300 | [diff] [blame] | 3 | # crowdin_sync.py |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 4 | # |
| 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 Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 22 | ############################################# IMPORTS ############################################## |
| 23 | |
| 24 | import argparse |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 25 | import codecs |
| 26 | import git |
| 27 | import os |
| 28 | import os.path |
| 29 | import re |
| 30 | import shutil |
| 31 | import subprocess |
| 32 | import sys |
| 33 | from urllib import urlretrieve |
| 34 | from xml.dom import minidom |
| 35 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 36 | ############################################ FUNCTIONS ############################################# |
| 37 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 38 | def get_default_branch(xml): |
| 39 | xml_default = xml.getElementsByTagName('default')[0] |
| 40 | xml_default_revision = xml_default.attributes['revision'].value |
| 41 | return re.search('refs/heads/(.*)', xml_default_revision).groups()[0] |
| 42 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 43 | def push_as_commit(path, name, branch, username): |
| 44 | print('Committing ' + name + ' on branch ' + branch) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 45 | |
| 46 | # Get path |
| 47 | path = os.getcwd() + '/' + path |
| 48 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 49 | # Create repo object |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 50 | repo = git.Repo(path) |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 51 | |
| 52 | # Remove previously deleted files from Git |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 53 | removed_files = repo.git.ls_files(d=True).split('\n') |
| 54 | try: |
| 55 | repo.git.rm(removed_files) |
| 56 | except: |
| 57 | pass |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 58 | |
| 59 | # Add all files to commit |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 60 | repo.git.add('-A') |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 61 | |
| 62 | # Create commit; if it fails, probably empty so skipping |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 63 | try: |
| 64 | repo.git.commit(m='Automatic translation import') |
| 65 | except: |
| 66 | print('Failed to create commit for ' + name + ', probably empty: skipping') |
| 67 | return |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 68 | |
| 69 | # Push commit |
Michael Bestas | f96f67b | 2014-10-21 00:43:37 +0300 | [diff] [blame] | 70 | try: |
| 71 | repo.git.push('ssh://' + username + '@review.cyanogenmod.org:29418/' + name, 'HEAD:refs/for/' + branch + '%topic=translation') |
| 72 | print('Succesfully pushed commit for ' + name) |
| 73 | except: |
| 74 | print('Failed to push commit for ' + name) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 75 | |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 76 | #################################################################################################### |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 77 | |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 78 | print('Welcome to the CM Crowdin sync script!') |
| 79 | |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 80 | #################################################################################################### |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 81 | |
| 82 | parser = argparse.ArgumentParser(description='Synchronising CyanogenMod\'s translations with Crowdin') |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 83 | sync = parser.add_mutually_exclusive_group() |
| 84 | parser.add_argument('-u', '--username', help='Gerrit username', required=True) |
| 85 | sync.add_argument('--no-upload', action='store_true', help='Only download CM translations from Crowdin') |
| 86 | sync.add_argument('--no-download', action='store_true', help='Only upload CM source translations to Crowdin') |
| 87 | args = parser.parse_args() |
| 88 | argsdict = vars(args) |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 89 | |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 90 | username = argsdict['username'] |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 91 | |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 92 | ############################################# PREPARE ############################################## |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 93 | |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame^] | 94 | print('\nSTEP 0: Checking dependencies & define shared variables') |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 95 | # Check for Ruby version of crowdin-cli |
| 96 | if subprocess.check_output(['rvm', 'all', 'do', 'gem', 'list', 'crowdin-cli', '-i']) == 'true': |
| 97 | sys.exit('You have not installed crowdin-cli. Terminating.') |
| 98 | else: |
| 99 | print('Found: crowdin-cli') |
| 100 | |
| 101 | # Check for repo |
| 102 | try: |
| 103 | subprocess.check_output(['which', 'repo']) |
| 104 | except: |
| 105 | sys.exit('You have not installed repo. Terminating.') |
| 106 | |
| 107 | # Check for android/default.xml |
| 108 | if not os.path.isfile('android/default.xml'): |
| 109 | sys.exit('You have no android/default.xml. Terminating.') |
| 110 | else: |
| 111 | print('Found: android/default.xml') |
| 112 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 113 | # Variables regarding android/default.xml |
| 114 | print('Loading: android/default.xml') |
| 115 | xml_android = minidom.parse('android/default.xml') |
| 116 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 117 | # Default branch |
| 118 | default_branch = get_default_branch(xml_android) |
| 119 | print('Default branch: ' + default_branch) |
| 120 | |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame^] | 121 | # Check for crowdin/extra_packages_' + default_branch + '.xml |
| 122 | if not os.path.isfile('crowdin/extra_packages_' + default_branch + '.xml'): |
| 123 | sys.exit('You have no crowdin/extra_packages_' + default_branch + '.xml. Terminating.') |
| 124 | else: |
| 125 | print('Found: crowdin/extra_packages_' + default_branch + '.xml') |
| 126 | |
| 127 | # Check for crowdin/config.yaml |
| 128 | if not os.path.isfile('crowdin/config.yaml'): |
| 129 | sys.exit('You have no crowdin/config.yaml. Terminating.') |
| 130 | else: |
| 131 | print('Found: crowdin/config.yaml') |
| 132 | |
| 133 | # Check for crowdin/config_aosp.yaml |
| 134 | if not os.path.isfile('crowdin/config_aosp.yaml'): |
| 135 | sys.exit('You have no crowdin/config_aosp.yaml. Terminating.') |
| 136 | else: |
| 137 | print('Found: crowdin/config_aosp.yaml') |
| 138 | |
| 139 | # Check for crowdin/crowdin_' + default_branch + '.yaml |
| 140 | if not os.path.isfile('crowdin/crowdin_' + default_branch + '.yaml'): |
| 141 | sys.exit('You have no crowdin/crowdin_' + default_branch + '.yaml. Terminating.') |
| 142 | else: |
| 143 | print('Found: crowdin/crowdin_' + default_branch + '.yaml') |
| 144 | |
| 145 | # Check for crowdin/crowdin_' + default_branch + '_aosp.yaml |
| 146 | if not os.path.isfile('crowdin/crowdin_' + default_branch + '_aosp.yaml'): |
| 147 | sys.exit('You have no crowdin/crowdin_' + default_branch + '_aosp.yaml. Terminating.') |
| 148 | else: |
| 149 | print('Found: crowdin/crowdin_' + default_branch + '_aosp.yaml') |
| 150 | |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 151 | ############################################### MAIN ############################################### |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 152 | |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 153 | if not args.no_upload: |
| 154 | print('\nSTEP 1: Upload Crowdin source translations') |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 155 | print('Uploading Crowdin source translations (AOSP supported languages)') |
| 156 | # Execute 'crowdin-cli upload sources' and show output |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame^] | 157 | print(subprocess.check_output(['crowdin-cli', '--config=crowdin/crowdin_' + default_branch + '.yaml', '--identity=crowdin/config.yaml', 'upload', 'sources'])) |
| 158 | |
| 159 | print('Uploading Crowdin source translations (non-AOSP supported languages)') |
| 160 | # Execute 'crowdin-cli upload sources' and show output |
| 161 | print(subprocess.check_output(['crowdin-cli', '--config=crowdin/crowdin_' + default_branch + '_aosp.yaml', '--identity=crowdin/config_aosp.yaml', 'upload', 'sources'])) |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 162 | else: |
| 163 | print('\nSkipping source translations upload') |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 164 | |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 165 | if not args.no_download: |
| 166 | print('\nSTEP 2: Download Crowdin translations') |
| 167 | print('Downloading Crowdin translations (AOSP supported languages)') |
| 168 | # Execute 'crowdin-cli download' and show output |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame^] | 169 | print(subprocess.check_output(['crowdin-cli', '--config=crowdin/crowdin_' + default_branch + '.yaml', '--identity=crowdin/config.yaml', 'download'])) |
Michael Bestas | 50579d2 | 2014-08-09 17:49:14 +0300 | [diff] [blame] | 170 | |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 171 | print('Downloading Crowdin translations (non-AOSP supported languages)') |
| 172 | # Execute 'crowdin-cli download' and show output |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame^] | 173 | print(subprocess.check_output(['crowdin-cli', '--config=crowdin/crowdin_' + default_branch + '_aosp.yaml', '--identity=crowdin/config_aosp.yaml', 'download'])) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 174 | |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 175 | print('\nSTEP 3: Remove useless empty translations') |
| 176 | # Some line of code that I found to find all XML files |
| 177 | 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'] |
| 178 | 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"/>'} |
| 179 | for xml_file in result: |
| 180 | for line in empty_contents: |
| 181 | if line in open(xml_file).read(): |
| 182 | print('Removing ' + xml_file) |
| 183 | os.remove(xml_file) |
| 184 | break |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 185 | |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 186 | print('\nSTEP 4: Create a list of pushable translations') |
| 187 | # Get all files that Crowdin pushed |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame^] | 188 | 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 Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 189 | proc.wait() # Wait for the above to finish |
Michael Bestas | 50579d2 | 2014-08-09 17:49:14 +0300 | [diff] [blame] | 190 | |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 191 | print('\nSTEP 5: Upload to Gerrit') |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame^] | 192 | xml_extra = minidom.parse('crowdin/extra_packages_' + default_branch + '.xml') |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 193 | items = xml_android.getElementsByTagName('project') |
| 194 | items += xml_extra.getElementsByTagName('project') |
| 195 | all_projects = [] |
| 196 | |
| 197 | for path in iter(proc.stdout.readline,''): |
| 198 | # Remove the \n at the end of each line |
| 199 | path = path.rstrip() |
| 200 | |
| 201 | if not path: |
| 202 | continue |
| 203 | |
| 204 | # Get project root dir from Crowdin's output by regex |
| 205 | m = re.search('/(.*Superuser)/Superuser.*|/(.*LatinIME).*|/(frameworks/base).*|/(.*CMFileManager).*|/(.*CMHome).*|/(device/.*/.*)/.*/res/values.*|/(hardware/.*/.*)/.*/res/values.*|/(.*)/res/values.*', path) |
| 206 | |
| 207 | if not m.groups(): |
| 208 | # Regex result is empty, warn the user |
| 209 | print('WARNING: Cannot determine project root dir of [' + path + '], skipping') |
| 210 | continue |
| 211 | |
| 212 | for i in m.groups(): |
| 213 | if not i: |
| 214 | continue |
| 215 | result = i |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 216 | break |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 217 | |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 218 | if result in all_projects: |
| 219 | # Already committed for this project, go to next project |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 220 | continue |
| 221 | |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 222 | # When a project has multiple translatable files, Crowdin will give duplicates. |
| 223 | # We don't want that (useless empty commits), so we save each project in all_projects |
| 224 | # and check if it's already in there. |
| 225 | all_projects.append(result) |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 226 | |
Michael Bestas | 4b26c4e | 2014-10-23 23:21:59 +0300 | [diff] [blame^] | 227 | # Search in android/default.xml or crowdin/extra_packages_' + default_branch + '.xml for the project's name |
Michael Bestas | 919053f | 2014-10-20 23:30:54 +0300 | [diff] [blame] | 228 | for project_item in items: |
| 229 | if project_item.attributes['path'].value != result: |
| 230 | # No match found, go to next item |
| 231 | continue |
| 232 | |
| 233 | # Define branch (custom branch if defined in xml file, otherwise the default one) |
| 234 | if project_item.hasAttribute('revision'): |
| 235 | branch = project_item.attributes['revision'].value |
| 236 | else: |
| 237 | branch = default_branch |
| 238 | |
| 239 | push_as_commit(result, project_item.attributes['name'].value, branch, username) |
| 240 | else: |
| 241 | print('\nSkipping translations download') |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 242 | |
| 243 | ############################################### DONE ############################################### |
Michael Bestas | 50579d2 | 2014-08-09 17:49:14 +0300 | [diff] [blame] | 244 | |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 245 | print('\nDone!') |