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 |
Chirayu Desai | d49a47f | 2014-08-01 18:36:39 +0530 | [diff] [blame] | 70 | repo.git.push('ssh://' + username + '@review.cyanogenmod.org:29418/' + name, 'HEAD:refs/for/' + branch + '%topic=translation') |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 71 | |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 72 | print('Succesfully pushed commit for ' + name) |
| 73 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 74 | ################################################################################################### |
| 75 | |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 76 | print('Welcome to the CM Crowdin sync script!') |
| 77 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 78 | ################################################################################################### |
| 79 | |
| 80 | parser = argparse.ArgumentParser(description='Synchronising CyanogenMod\'s translations with Crowdin') |
| 81 | parser.add_argument('--username', help='Gerrit username', required=True) |
| 82 | #parser.add_argument('--upload-only', help='Only upload CM source translations to Crowdin', required=False) |
| 83 | args = vars(parser.parse_args()) |
| 84 | |
| 85 | username = args['username'] |
| 86 | |
| 87 | ############################################## STEP 0 ############################################## |
| 88 | |
| 89 | print('\nSTEP 0A: Checking dependencies') |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 90 | # Check for Ruby version of crowdin-cli |
| 91 | if subprocess.check_output(['rvm', 'all', 'do', 'gem', 'list', 'crowdin-cli', '-i']) == 'true': |
| 92 | sys.exit('You have not installed crowdin-cli. Terminating.') |
| 93 | else: |
| 94 | print('Found: crowdin-cli') |
| 95 | |
| 96 | # Check for repo |
| 97 | try: |
| 98 | subprocess.check_output(['which', 'repo']) |
| 99 | except: |
| 100 | sys.exit('You have not installed repo. Terminating.') |
| 101 | |
| 102 | # Check for android/default.xml |
| 103 | if not os.path.isfile('android/default.xml'): |
| 104 | sys.exit('You have no android/default.xml. Terminating.') |
| 105 | else: |
| 106 | print('Found: android/default.xml') |
| 107 | |
Michael Bestas | 55ae81a | 2014-07-26 19:22:19 +0300 | [diff] [blame] | 108 | # Check for crowdin/config_aosp.yaml |
| 109 | if not os.path.isfile('crowdin/config_aosp.yaml'): |
| 110 | sys.exit('You have no crowdin/config_aosp.yaml. Terminating.') |
| 111 | else: |
| 112 | print('Found: crowdin/config_aosp.yaml') |
| 113 | |
| 114 | # Check for crowdin/config_cm.yaml |
| 115 | if not os.path.isfile('crowdin/config_cm.yaml'): |
| 116 | sys.exit('You have no crowdin/config_cm.yaml. Terminating.') |
| 117 | else: |
| 118 | print('Found: crowdin/config_cm.yaml') |
| 119 | |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 120 | # Check for crowdin/crowdin_aosp.yaml |
| 121 | if not os.path.isfile('crowdin/crowdin_aosp.yaml'): |
| 122 | sys.exit('You have no crowdin/crowdin_aosp.yaml. Terminating.') |
| 123 | else: |
| 124 | print('Found: crowdin/crowdin_aosp.yaml') |
| 125 | |
| 126 | # Check for crowdin/crowdin_cm.yaml |
| 127 | if not os.path.isfile('crowdin/crowdin_cm.yaml'): |
| 128 | sys.exit('You have no crowdin/crowdin_cm.yaml. Terminating.') |
| 129 | else: |
| 130 | print('Found: crowdin/crowdin_cm.yaml') |
| 131 | |
| 132 | # Check for crowdin/extra_packages.xml |
| 133 | if not os.path.isfile('crowdin/extra_packages.xml'): |
| 134 | sys.exit('You have no crowdin/extra_packages.xml. Terminating.') |
| 135 | else: |
| 136 | print('Found: crowdin/extra_packages.xml') |
| 137 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 138 | print('\nSTEP 0B: Define shared variables') |
| 139 | |
| 140 | # Variables regarding android/default.xml |
| 141 | print('Loading: android/default.xml') |
| 142 | xml_android = minidom.parse('android/default.xml') |
| 143 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 144 | # Default branch |
| 145 | default_branch = get_default_branch(xml_android) |
| 146 | print('Default branch: ' + default_branch) |
| 147 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 148 | ############################################## STEP 1 ############################################## |
| 149 | |
Michael Bestas | 805c53b | 2014-10-20 20:35:16 +0300 | [diff] [blame^] | 150 | print('\nSTEP 1A: Upload Crowdin source translations (non-AOSP supported languages)') |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 151 | # Execute 'crowdin-cli upload sources' and show output |
Michael Bestas | 3cf378d | 2014-07-26 15:47:29 +0300 | [diff] [blame] | 152 | print(subprocess.check_output(['crowdin-cli', '--config=crowdin/crowdin_aosp.yaml', '--identity=crowdin/config_aosp.yaml', 'upload', 'sources'])) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 153 | |
Michael Bestas | 805c53b | 2014-10-20 20:35:16 +0300 | [diff] [blame^] | 154 | print('\nSTEP 1B: Upload Crowdin source translations (AOSP supported languages)') |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 155 | # Execute 'crowdin-cli upload sources' and show output |
Michael Bestas | 3cf378d | 2014-07-26 15:47:29 +0300 | [diff] [blame] | 156 | print(subprocess.check_output(['crowdin-cli', '--config=crowdin/crowdin_cm.yaml', '--identity=crowdin/config_cm.yaml', 'upload', 'sources'])) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 157 | |
Michael Bestas | 805c53b | 2014-10-20 20:35:16 +0300 | [diff] [blame^] | 158 | ############################################## STEP 2 ############################################## |
Michael Bestas | 50579d2 | 2014-08-09 17:49:14 +0300 | [diff] [blame] | 159 | |
Michael Bestas | 805c53b | 2014-10-20 20:35:16 +0300 | [diff] [blame^] | 160 | print('\nSTEP 2A: Download Crowdin translations (AOSP supported languages)') |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 161 | # Execute 'crowdin-cli download' and show output |
Michael Bestas | 3cf378d | 2014-07-26 15:47:29 +0300 | [diff] [blame] | 162 | print(subprocess.check_output(['crowdin-cli', '--config=crowdin/crowdin_cm.yaml', '--identity=crowdin/config_cm.yaml', 'download'])) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 163 | |
Michael Bestas | 805c53b | 2014-10-20 20:35:16 +0300 | [diff] [blame^] | 164 | print('\nSTEP 2B: Download Crowdin translations (non-AOSP supported languages)') |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 165 | # Execute 'crowdin-cli download' and show output |
Michael Bestas | 3cf378d | 2014-07-26 15:47:29 +0300 | [diff] [blame] | 166 | print(subprocess.check_output(['crowdin-cli', '--config=crowdin/crowdin_aosp.yaml', '--identity=crowdin/config_aosp.yaml', 'download'])) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 167 | |
Michael Bestas | 805c53b | 2014-10-20 20:35:16 +0300 | [diff] [blame^] | 168 | ############################################## STEP 3 ############################################## |
Michael Bestas | 50579d2 | 2014-08-09 17:49:14 +0300 | [diff] [blame] | 169 | |
Michael Bestas | 805c53b | 2014-10-20 20:35:16 +0300 | [diff] [blame^] | 170 | print('\nSTEP 3: Remove useless empty translations') |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 171 | # Some line of code that I found to find all XML files |
| 172 | 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'] |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 173 | 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"/>'} |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 174 | for xml_file in result: |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 175 | for line in empty_contents: |
| 176 | if line in open(xml_file).read(): |
| 177 | print('Removing ' + xml_file) |
| 178 | os.remove(xml_file) |
| 179 | break |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 180 | |
Michael Bestas | 805c53b | 2014-10-20 20:35:16 +0300 | [diff] [blame^] | 181 | ############################################## STEP 4 ############################################## |
Michael Bestas | 50579d2 | 2014-08-09 17:49:14 +0300 | [diff] [blame] | 182 | |
Michael Bestas | 805c53b | 2014-10-20 20:35:16 +0300 | [diff] [blame^] | 183 | print('\nSTEP 4: Create a list of pushable translations') |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 184 | # Get all files that Crowdin pushed |
Michael Bestas | 3cf378d | 2014-07-26 15:47:29 +0300 | [diff] [blame] | 185 | proc = subprocess.Popen(['crowdin-cli --config=crowdin/crowdin_cm.yaml --identity=crowdin/config_cm.yaml list sources && crowdin-cli --config=crowdin/crowdin_aosp.yaml --identity=crowdin/config_aosp.yaml list sources'], stdout=subprocess.PIPE, shell=True) |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 186 | proc.wait() # Wait for the above to finish |
| 187 | |
Michael Bestas | 805c53b | 2014-10-20 20:35:16 +0300 | [diff] [blame^] | 188 | ############################################## STEP 5 ############################################## |
Michael Bestas | 50579d2 | 2014-08-09 17:49:14 +0300 | [diff] [blame] | 189 | |
Michael Bestas | 805c53b | 2014-10-20 20:35:16 +0300 | [diff] [blame^] | 190 | print('\nSTEP 5: Commit to Gerrit') |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 191 | xml_extra = minidom.parse('crowdin/extra_packages.xml') |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 192 | items = xml_android.getElementsByTagName('project') |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 193 | items += xml_extra.getElementsByTagName('project') |
| 194 | all_projects = [] |
| 195 | |
| 196 | for path in iter(proc.stdout.readline,''): |
| 197 | # Remove the \n at the end of each line |
| 198 | path = path.rstrip() |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 199 | |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 200 | if not path: |
| 201 | continue |
| 202 | |
| 203 | # Get project root dir from Crowdin's output by regex |
| 204 | m = re.search('/(.*Superuser)/Superuser.*|/(.*LatinIME).*|/(frameworks/base).*|/(.*CMFileManager).*|/(.*CMHome).*|/(device/.*/.*)/.*/res/values.*|/(hardware/.*/.*)/.*/res/values.*|/(.*)/res/values.*', path) |
| 205 | |
| 206 | if not m.groups(): |
| 207 | # Regex result is empty, warn the user |
| 208 | print('WARNING: Cannot determine project root dir of [' + path + '], skipping') |
| 209 | continue |
| 210 | |
| 211 | for i in m.groups(): |
| 212 | if not i: |
| 213 | continue |
| 214 | result = i |
| 215 | break |
| 216 | |
| 217 | if result in all_projects: |
| 218 | # Already committed for this project, go to next project |
| 219 | continue |
| 220 | |
| 221 | # When a project has multiple translatable files, Crowdin will give duplicates. |
| 222 | # We don't want that (useless empty commits), so we save each project in all_projects |
| 223 | # and check if it's already in there. |
| 224 | all_projects.append(result) |
| 225 | |
| 226 | # Search in android/default.xml or crowdin/extra_packages.xml for the project's name |
| 227 | for project_item in items: |
| 228 | if project_item.attributes['path'].value != result: |
| 229 | # No match found, go to next item |
| 230 | continue |
| 231 | |
Michael Bestas | 55ae81a | 2014-07-26 19:22:19 +0300 | [diff] [blame] | 232 | # Define branch (custom branch if defined in xml file, otherwise the default one) |
Marco Brohet | 6b6b4e5 | 2014-07-20 00:05:16 +0200 | [diff] [blame] | 233 | if project_item.hasAttribute('revision'): |
| 234 | branch = project_item.attributes['revision'].value |
| 235 | else: |
| 236 | branch = default_branch |
| 237 | |
| 238 | push_as_commit(result, project_item.attributes['name'].value, branch, username) |
| 239 | |
| 240 | ############################################### DONE ############################################### |
Michael Bestas | 50579d2 | 2014-08-09 17:49:14 +0300 | [diff] [blame] | 241 | |
Marco Brohet | cb5cdb4 | 2014-07-11 22:41:53 +0200 | [diff] [blame] | 242 | print('\nDone!') |