blob: 6daadf3448b3b2f71f77dbf563aa7fd65cea77f3 [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
Chirayu Desaid49a47f2014-08-01 18:36:39 +053070 repo.git.push('ssh://' + username + '@review.cyanogenmod.org:29418/' + name, 'HEAD:refs/for/' + branch + '%topic=translation')
Marco Brohet6b6b4e52014-07-20 00:05:16 +020071
Marco Brohetcb5cdb42014-07-11 22:41:53 +020072 print('Succesfully pushed commit for ' + name)
73
Marco Brohet6b6b4e52014-07-20 00:05:16 +020074###################################################################################################
75
Marco Brohetcb5cdb42014-07-11 22:41:53 +020076print('Welcome to the CM Crowdin sync script!')
77
Marco Brohet6b6b4e52014-07-20 00:05:16 +020078###################################################################################################
79
80parser = argparse.ArgumentParser(description='Synchronising CyanogenMod\'s translations with Crowdin')
81parser.add_argument('--username', help='Gerrit username', required=True)
82#parser.add_argument('--upload-only', help='Only upload CM source translations to Crowdin', required=False)
83args = vars(parser.parse_args())
84
85username = args['username']
86
87############################################## STEP 0 ##############################################
88
89print('\nSTEP 0A: Checking dependencies')
Marco Brohetcb5cdb42014-07-11 22:41:53 +020090# Check for Ruby version of crowdin-cli
91if subprocess.check_output(['rvm', 'all', 'do', 'gem', 'list', 'crowdin-cli', '-i']) == 'true':
92 sys.exit('You have not installed crowdin-cli. Terminating.')
93else:
94 print('Found: crowdin-cli')
95
96# Check for repo
97try:
98 subprocess.check_output(['which', 'repo'])
99except:
100 sys.exit('You have not installed repo. Terminating.')
101
102# Check for android/default.xml
103if not os.path.isfile('android/default.xml'):
104 sys.exit('You have no android/default.xml. Terminating.')
105else:
106 print('Found: android/default.xml')
107
Michael Bestas55ae81a2014-07-26 19:22:19 +0300108# Check for crowdin/config_aosp.yaml
109if not os.path.isfile('crowdin/config_aosp.yaml'):
110 sys.exit('You have no crowdin/config_aosp.yaml. Terminating.')
111else:
112 print('Found: crowdin/config_aosp.yaml')
113
114# Check for crowdin/config_cm.yaml
115if not os.path.isfile('crowdin/config_cm.yaml'):
116 sys.exit('You have no crowdin/config_cm.yaml. Terminating.')
117else:
118 print('Found: crowdin/config_cm.yaml')
119
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200120# Check for crowdin/crowdin_aosp.yaml
121if not os.path.isfile('crowdin/crowdin_aosp.yaml'):
122 sys.exit('You have no crowdin/crowdin_aosp.yaml. Terminating.')
123else:
124 print('Found: crowdin/crowdin_aosp.yaml')
125
126# Check for crowdin/crowdin_cm.yaml
127if not os.path.isfile('crowdin/crowdin_cm.yaml'):
128 sys.exit('You have no crowdin/crowdin_cm.yaml. Terminating.')
129else:
130 print('Found: crowdin/crowdin_cm.yaml')
131
132# Check for crowdin/extra_packages.xml
133if not os.path.isfile('crowdin/extra_packages.xml'):
134 sys.exit('You have no crowdin/extra_packages.xml. Terminating.')
135else:
136 print('Found: crowdin/extra_packages.xml')
137
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200138print('\nSTEP 0B: Define shared variables')
139
140# Variables regarding android/default.xml
141print('Loading: android/default.xml')
142xml_android = minidom.parse('android/default.xml')
143
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200144# Default branch
145default_branch = get_default_branch(xml_android)
146print('Default branch: ' + default_branch)
147
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200148############################################## STEP 1 ##############################################
149
Michael Bestas805c53b2014-10-20 20:35:16 +0300150print('\nSTEP 1A: Upload Crowdin source translations (non-AOSP supported languages)')
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200151# Execute 'crowdin-cli upload sources' and show output
Michael Bestas3cf378d2014-07-26 15:47:29 +0300152print(subprocess.check_output(['crowdin-cli', '--config=crowdin/crowdin_aosp.yaml', '--identity=crowdin/config_aosp.yaml', 'upload', 'sources']))
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200153
Michael Bestas805c53b2014-10-20 20:35:16 +0300154print('\nSTEP 1B: Upload Crowdin source translations (AOSP supported languages)')
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200155# Execute 'crowdin-cli upload sources' and show output
Michael Bestas3cf378d2014-07-26 15:47:29 +0300156print(subprocess.check_output(['crowdin-cli', '--config=crowdin/crowdin_cm.yaml', '--identity=crowdin/config_cm.yaml', 'upload', 'sources']))
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200157
Michael Bestas805c53b2014-10-20 20:35:16 +0300158############################################## STEP 2 ##############################################
Michael Bestas50579d22014-08-09 17:49:14 +0300159
Michael Bestas805c53b2014-10-20 20:35:16 +0300160print('\nSTEP 2A: Download Crowdin translations (AOSP supported languages)')
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200161# Execute 'crowdin-cli download' and show output
Michael Bestas3cf378d2014-07-26 15:47:29 +0300162print(subprocess.check_output(['crowdin-cli', '--config=crowdin/crowdin_cm.yaml', '--identity=crowdin/config_cm.yaml', 'download']))
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200163
Michael Bestas805c53b2014-10-20 20:35:16 +0300164print('\nSTEP 2B: Download Crowdin translations (non-AOSP supported languages)')
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200165# Execute 'crowdin-cli download' and show output
Michael Bestas3cf378d2014-07-26 15:47:29 +0300166print(subprocess.check_output(['crowdin-cli', '--config=crowdin/crowdin_aosp.yaml', '--identity=crowdin/config_aosp.yaml', 'download']))
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200167
Michael Bestas805c53b2014-10-20 20:35:16 +0300168############################################## STEP 3 ##############################################
Michael Bestas50579d22014-08-09 17:49:14 +0300169
Michael Bestas805c53b2014-10-20 20:35:16 +0300170print('\nSTEP 3: Remove useless empty translations')
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200171# Some line of code that I found to find all XML files
172result = [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 Brohet6b6b4e52014-07-20 00:05:16 +0200173empty_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 Brohetcb5cdb42014-07-11 22:41:53 +0200174for xml_file in result:
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200175 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 Brohetcb5cdb42014-07-11 22:41:53 +0200180
Michael Bestas805c53b2014-10-20 20:35:16 +0300181############################################## STEP 4 ##############################################
Michael Bestas50579d22014-08-09 17:49:14 +0300182
Michael Bestas805c53b2014-10-20 20:35:16 +0300183print('\nSTEP 4: Create a list of pushable translations')
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200184# Get all files that Crowdin pushed
Michael Bestas3cf378d2014-07-26 15:47:29 +0300185proc = 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 Brohetcb5cdb42014-07-11 22:41:53 +0200186proc.wait() # Wait for the above to finish
187
Michael Bestas805c53b2014-10-20 20:35:16 +0300188############################################## STEP 5 ##############################################
Michael Bestas50579d22014-08-09 17:49:14 +0300189
Michael Bestas805c53b2014-10-20 20:35:16 +0300190print('\nSTEP 5: Commit to Gerrit')
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200191xml_extra = minidom.parse('crowdin/extra_packages.xml')
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200192items = xml_android.getElementsByTagName('project')
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200193items += xml_extra.getElementsByTagName('project')
194all_projects = []
195
196for path in iter(proc.stdout.readline,''):
197 # Remove the \n at the end of each line
198 path = path.rstrip()
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200199
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200200 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 Bestas55ae81a2014-07-26 19:22:19 +0300232 # Define branch (custom branch if defined in xml file, otherwise the default one)
Marco Brohet6b6b4e52014-07-20 00:05:16 +0200233 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 Bestas50579d22014-08-09 17:49:14 +0300241
Marco Brohetcb5cdb42014-07-11 22:41:53 +0200242print('\nDone!')