blob: 0d3124f17558e76b5bf430124d18771323c851cf [file] [log] [blame]
Marco Brohetcb5cdb42014-07-11 22:41:53 +02001#!/usr/bin/python2
2# -*- coding: utf-8 -*-
Michael Bestas1ab959b2014-07-26 16:01:01 +03003# crowdin_sync.py
Marco Brohetcb5cdb42014-07-11 22:41:53 +02004#
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 Brohet6b6b4e52014-07-20 00:05:16 +020022############################################# IMPORTS ##############################################
23
24import argparse
Marco Brohetcb5cdb42014-07-11 22:41:53 +020025import codecs
26import git
27import os
28import os.path
29import re
30import shutil
31import subprocess
32import sys
33from urllib import urlretrieve
34from xml.dom import minidom
35
Marco Brohet6b6b4e52014-07-20 00:05:16 +020036############################################ FUNCTIONS #############################################
37
Marco Brohet6b6b4e52014-07-20 00:05:16 +020038def 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 Brohet6b6b4e52014-07-20 00:05:16 +020043def push_as_commit(path, name, branch, username):
44 print('Committing ' + name + ' on branch ' + branch)
Marco Brohetcb5cdb42014-07-11 22:41:53 +020045
46 # Get path
47 path = os.getcwd() + '/' + path
48
Marco Brohet6b6b4e52014-07-20 00:05:16 +020049 # Create repo object
Marco Brohetcb5cdb42014-07-11 22:41:53 +020050 repo = git.Repo(path)
Marco Brohet6b6b4e52014-07-20 00:05:16 +020051
52 # Remove previously deleted files from Git
Marco Brohetcb5cdb42014-07-11 22:41:53 +020053 removed_files = repo.git.ls_files(d=True).split('\n')
54 try:
55 repo.git.rm(removed_files)
56 except:
57 pass
Marco Brohet6b6b4e52014-07-20 00:05:16 +020058
59 # Add all files to commit
Marco Brohetcb5cdb42014-07-11 22:41:53 +020060 repo.git.add('-A')
Marco Brohet6b6b4e52014-07-20 00:05:16 +020061
62 # Create commit; if it fails, probably empty so skipping
Marco Brohetcb5cdb42014-07-11 22:41:53 +020063 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 Brohet6b6b4e52014-07-20 00:05:16 +020068
69 # Push commit
Michael Bestasf96f67b2014-10-21 00:43:37 +030070 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 Brohetcb5cdb42014-07-11 22:41:53 +020075
Michael Bestas919053f2014-10-20 23:30:54 +030076####################################################################################################
Marco Brohet6b6b4e52014-07-20 00:05:16 +020077
Marco Brohetcb5cdb42014-07-11 22:41:53 +020078print('Welcome to the CM Crowdin sync script!')
79
Michael Bestas919053f2014-10-20 23:30:54 +030080####################################################################################################
Marco Brohet6b6b4e52014-07-20 00:05:16 +020081
82parser = argparse.ArgumentParser(description='Synchronising CyanogenMod\'s translations with Crowdin')
Michael Bestas919053f2014-10-20 23:30:54 +030083sync = parser.add_mutually_exclusive_group()
84parser.add_argument('-u', '--username', help='Gerrit username', required=True)
85sync.add_argument('--no-upload', action='store_true', help='Only download CM translations from Crowdin')
86sync.add_argument('--no-download', action='store_true', help='Only upload CM source translations to Crowdin')
87args = parser.parse_args()
88argsdict = vars(args)
Marco Brohet6b6b4e52014-07-20 00:05:16 +020089
Michael Bestas919053f2014-10-20 23:30:54 +030090username = argsdict['username']
Marco Brohet6b6b4e52014-07-20 00:05:16 +020091
Michael Bestas919053f2014-10-20 23:30:54 +030092############################################# PREPARE ##############################################
Marco Brohet6b6b4e52014-07-20 00:05:16 +020093
94print('\nSTEP 0A: Checking dependencies')
Marco Brohetcb5cdb42014-07-11 22:41:53 +020095# Check for Ruby version of crowdin-cli
96if subprocess.check_output(['rvm', 'all', 'do', 'gem', 'list', 'crowdin-cli', '-i']) == 'true':
97 sys.exit('You have not installed crowdin-cli. Terminating.')
98else:
99 print('Found: crowdin-cli')
100
101# Check for repo
102try:
103 subprocess.check_output(['which', 'repo'])
104except:
105 sys.exit('You have not installed repo. Terminating.')
106
107# Check for android/default.xml
108if not os.path.isfile('android/default.xml'):
109 sys.exit('You have no android/default.xml. Terminating.')
110else:
111 print('Found: android/default.xml')
112
Michael Bestas55ae81a2014-07-26 19:22:19 +0300113# Check for crowdin/config_aosp.yaml
114if not os.path.isfile('crowdin/config_aosp.yaml'):
115 sys.exit('You have no crowdin/config_aosp.yaml. Terminating.')
116else:
117 print('Found: crowdin/config_aosp.yaml')
118
119# Check for crowdin/config_cm.yaml
120if not os.path.isfile('crowdin/config_cm.yaml'):
121 sys.exit('You have no crowdin/config_cm.yaml. Terminating.')
122else:
123 print('Found: crowdin/config_cm.yaml')
124
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200125# Check for crowdin/crowdin_aosp.yaml
126if not os.path.isfile('crowdin/crowdin_aosp.yaml'):
127 sys.exit('You have no crowdin/crowdin_aosp.yaml. Terminating.')
128else:
129 print('Found: crowdin/crowdin_aosp.yaml')
130
131# Check for crowdin/crowdin_cm.yaml
132if not os.path.isfile('crowdin/crowdin_cm.yaml'):
133 sys.exit('You have no crowdin/crowdin_cm.yaml. Terminating.')
134else:
135 print('Found: crowdin/crowdin_cm.yaml')
136
137# Check for crowdin/extra_packages.xml
138if not os.path.isfile('crowdin/extra_packages.xml'):
139 sys.exit('You have no crowdin/extra_packages.xml. Terminating.')
140else:
141 print('Found: crowdin/extra_packages.xml')
142
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200143print('\nSTEP 0B: Define shared variables')
144
145# Variables regarding android/default.xml
146print('Loading: android/default.xml')
147xml_android = minidom.parse('android/default.xml')
148
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200149# Default branch
150default_branch = get_default_branch(xml_android)
151print('Default branch: ' + default_branch)
152
Michael Bestas919053f2014-10-20 23:30:54 +0300153############################################### MAIN ###############################################
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200154
Michael Bestas919053f2014-10-20 23:30:54 +0300155if not args.no_upload:
156 print('\nSTEP 1: Upload Crowdin source translations')
157 print('Uploading Crowdin source translations (non-AOSP supported languages)')
158 # Execute 'crowdin-cli upload sources' and show output
159 print(subprocess.check_output(['crowdin-cli', '--config=crowdin/crowdin_aosp.yaml', '--identity=crowdin/config_aosp.yaml', 'upload', 'sources']))
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200160
Michael Bestas919053f2014-10-20 23:30:54 +0300161 print('Uploading Crowdin source translations (AOSP supported languages)')
162 # Execute 'crowdin-cli upload sources' and show output
163 print(subprocess.check_output(['crowdin-cli', '--config=crowdin/crowdin_cm.yaml', '--identity=crowdin/config_cm.yaml', 'upload', 'sources']))
164else:
165 print('\nSkipping source translations upload')
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200166
Michael Bestas919053f2014-10-20 23:30:54 +0300167if not args.no_download:
168 print('\nSTEP 2: Download Crowdin translations')
169 print('Downloading Crowdin translations (AOSP supported languages)')
170 # Execute 'crowdin-cli download' and show output
171 print(subprocess.check_output(['crowdin-cli', '--config=crowdin/crowdin_cm.yaml', '--identity=crowdin/config_cm.yaml', 'download']))
Michael Bestas50579d22014-08-09 17:49:14 +0300172
Michael Bestas919053f2014-10-20 23:30:54 +0300173 print('Downloading Crowdin translations (non-AOSP supported languages)')
174 # Execute 'crowdin-cli download' and show output
175 print(subprocess.check_output(['crowdin-cli', '--config=crowdin/crowdin_aosp.yaml', '--identity=crowdin/config_aosp.yaml', 'download']))
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200176
Michael Bestas919053f2014-10-20 23:30:54 +0300177 print('\nSTEP 3: Remove useless empty translations')
178 # Some line of code that I found to find all XML files
179 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']
180 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"/>'}
181 for xml_file in result:
182 for line in empty_contents:
183 if line in open(xml_file).read():
184 print('Removing ' + xml_file)
185 os.remove(xml_file)
186 break
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200187
Michael Bestas919053f2014-10-20 23:30:54 +0300188 print('\nSTEP 4: Create a list of pushable translations')
189 # Get all files that Crowdin pushed
190 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)
191 proc.wait() # Wait for the above to finish
Michael Bestas50579d22014-08-09 17:49:14 +0300192
Michael Bestas919053f2014-10-20 23:30:54 +0300193 print('\nSTEP 5: Upload to Gerrit')
194 xml_extra = minidom.parse('crowdin/extra_packages.xml')
195 items = xml_android.getElementsByTagName('project')
196 items += xml_extra.getElementsByTagName('project')
197 all_projects = []
198
199 for path in iter(proc.stdout.readline,''):
200 # Remove the \n at the end of each line
201 path = path.rstrip()
202
203 if not path:
204 continue
205
206 # Get project root dir from Crowdin's output by regex
207 m = re.search('/(.*Superuser)/Superuser.*|/(.*LatinIME).*|/(frameworks/base).*|/(.*CMFileManager).*|/(.*CMHome).*|/(device/.*/.*)/.*/res/values.*|/(hardware/.*/.*)/.*/res/values.*|/(.*)/res/values.*', path)
208
209 if not m.groups():
210 # Regex result is empty, warn the user
211 print('WARNING: Cannot determine project root dir of [' + path + '], skipping')
212 continue
213
214 for i in m.groups():
215 if not i:
216 continue
217 result = i
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200218 break
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200219
Michael Bestas919053f2014-10-20 23:30:54 +0300220 if result in all_projects:
221 # Already committed for this project, go to next project
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200222 continue
223
Michael Bestas919053f2014-10-20 23:30:54 +0300224 # When a project has multiple translatable files, Crowdin will give duplicates.
225 # We don't want that (useless empty commits), so we save each project in all_projects
226 # and check if it's already in there.
227 all_projects.append(result)
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200228
Michael Bestas919053f2014-10-20 23:30:54 +0300229 # Search in android/default.xml or crowdin/extra_packages.xml for the project's name
230 for project_item in items:
231 if project_item.attributes['path'].value != result:
232 # No match found, go to next item
233 continue
234
235 # Define branch (custom branch if defined in xml file, otherwise the default one)
236 if project_item.hasAttribute('revision'):
237 branch = project_item.attributes['revision'].value
238 else:
239 branch = default_branch
240
241 push_as_commit(result, project_item.attributes['name'].value, branch, username)
242else:
243 print('\nSkipping translations download')
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200244
245############################################### DONE ###############################################
Michael Bestas50579d22014-08-09 17:49:14 +0300246
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200247print('\nDone!')