Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 1 | #!/usr/bin/python2 |
| 2 | # |
| 3 | # cm_crowdin_sync.py |
| 4 | # |
| 5 | # Updates Crowdin source translations and pulls translations |
| 6 | # directly to CyanogenMod's Git |
| 7 | |
| 8 | import create_cm_caf_xml |
| 9 | import git |
| 10 | import mmap |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame^] | 11 | import os |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 12 | import os.path |
| 13 | import re |
| 14 | import shutil |
| 15 | import subprocess |
| 16 | import sys |
| 17 | from urllib import urlretrieve |
| 18 | from xml.dom import minidom |
| 19 | |
| 20 | print('Welcome to the CM Crowdin sync script!\n') |
| 21 | |
| 22 | print('STEP 0: Checking dependencies\n') |
| 23 | if subprocess.check_output(['rvm', 'all', 'do', 'gem', 'list', 'crowdin-cli', '-i']) == 'true': |
| 24 | sys.exit('You have not installed crowdin-cli. Terminating.') |
| 25 | if not os.path.isfile('caf.xml'): |
| 26 | sys.exit('You have no caf.xml. Terminating.') |
| 27 | if not os.path.isfile('default.xml'): |
| 28 | sys.exit('You have no default.xml. Terminating.') |
| 29 | |
| 30 | print('STEP 1: Create cm_caf.xml') |
| 31 | xml = minidom.parse('caf.xml') |
| 32 | items = xml.getElementsByTagName('item') |
| 33 | |
| 34 | cm_caf = [] |
| 35 | |
| 36 | for item in items: |
| 37 | subprocess.call(['mkdir', '-p', 'tmp/' + item.attributes["path"].value]) |
| 38 | item_aosp = item.getElementsByTagName('aosp') |
| 39 | for aosp_item in item_aosp: |
| 40 | url = aosp_item.firstChild.nodeValue |
| 41 | path_to_base = 'tmp/' + item.attributes["path"].value + '/' + aosp_item.attributes["file"].value |
| 42 | path_to_cm = item.attributes["path"].value + '/' + aosp_item.attributes["file"].value |
| 43 | path = item.attributes["path"].value |
| 44 | urlretrieve(url, path_to_base) |
| 45 | create_cm_caf_xml.create_cm_caf_xml(path_to_base, path_to_cm, path) |
| 46 | cm_caf.append(path + '/cm_caf.xml') |
| 47 | print('Created ' + path + '/cm_caf.xml') |
| 48 | |
| 49 | print('\nSTEP 2: Upload Crowdin source translations') |
| 50 | print(subprocess.check_output(['crowdin-cli', 'upload', 'sources'])) |
| 51 | |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame^] | 52 | print('STEP 3: Download Crowdin translations') |
| 53 | print(subprocess.check_output(['crowdin-cli', "download"])) |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 54 | |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame^] | 55 | print('STEP 4A: Clean up of source cm_caf.xmls') |
| 56 | for cm_caf_file in cm_caf: |
| 57 | print ('Removing ' + cm_caf_file) |
| 58 | os.remove(cm_caf_file) |
| 59 | |
| 60 | print('\nSTEP 4B: Clean up of temp dir') |
| 61 | shutil.rmtree(os.getcwd() + '/tmp') |
| 62 | |
| 63 | print('STEP 4C: Clean up of empty translations') |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 64 | 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'] |
| 65 | for xml_file in result: |
| 66 | if '<resources/>' in open(xml_file).read(): |
| 67 | print ('Removing ' + xml_file) |
| 68 | os.remove(xml_file) |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame^] | 69 | 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] | 70 | print ('Removing ' + xml_file) |
| 71 | os.remove(xml_file) |
| 72 | |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 73 | print('\nSTEP 5: Push translations to Git') |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 74 | proc = subprocess.Popen(['crowdin-cli', 'list', 'sources'],stdout=subprocess.PIPE) |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 75 | xml = minidom.parse('default.xml') |
| 76 | items = xml.getElementsByTagName('project') |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame^] | 77 | all_projects = [] |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 78 | |
Marco Brohet | cf4069b | 2014-02-28 18:48:17 +0100 | [diff] [blame^] | 79 | for path in iter(proc.stdout.readline,''): |
| 80 | path = path.rstrip() |
| 81 | m = re.search('/(.*Superuser)/Superuser.*|/(.*LatinIME).*|/(frameworks/base).*|/(.*CMFileManager).*|/(device/.*/.*)/.*/res/values.*|/(hardware/.*/.*)/.*/res/values.*|/(.*)/res/values.*', path) |
| 82 | for good_path in m.groups(): |
| 83 | if good_path is not None and not good_path in all_projects: |
| 84 | all_projects.append(good_path) |
| 85 | working = 'false' |
| 86 | for project_item in items: |
| 87 | if project_item.attributes["path"].value == good_path: |
| 88 | working = 'true' |
| 89 | create_cm_caf_xml.push_as_commit(good_path, project_item.attributes['name'].value) |
| 90 | print 'WORKING: ' + project_item.attributes['name'].value |
| 91 | if working == 'false': |
| 92 | create_cm_caf_xml.push_as_commit(good_path, 'CyanogenMod/android_' + good_path.replace('/', '_')) |
| 93 | print 'WORKAROUND: ' + good_path |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 94 | |
| 95 | print('STEP 6: Done!') |