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 |
| 11 | import os.path |
| 12 | import re |
| 13 | import shutil |
| 14 | import subprocess |
| 15 | import sys |
| 16 | from urllib import urlretrieve |
| 17 | from xml.dom import minidom |
| 18 | |
| 19 | print('Welcome to the CM Crowdin sync script!\n') |
| 20 | |
Marco Brohet | aec28ed | 2014-02-28 01:34:03 +0100 | [diff] [blame] | 21 | |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 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 | |
| 52 | #print('STEP 3: Download Crowdin translations') |
| 53 | #print(subprocess.check_output(['crowdin-cli', "download"])) |
| 54 | |
| 55 | print('STEP 4A: Clean up of empty translations') |
| 56 | # Search for all XML files |
| 57 | 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'] |
| 58 | for xml_file in result: |
| 59 | if '<resources/>' in open(xml_file).read(): |
| 60 | print ('Removing ' + xml_file) |
| 61 | os.remove(xml_file) |
| 62 | if '<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>' in open(xml_file).read(): |
| 63 | print ('Removing ' + xml_file) |
| 64 | os.remove(xml_file) |
| 65 | |
| 66 | print('\nSTEP 4B: Clean up of source cm_caf.xmls') |
| 67 | for cm_caf_file in cm_caf: |
| 68 | print ('Removing ' + cm_caf_file) |
| 69 | os.remove(cm_caf_file) |
| 70 | |
| 71 | print('\nSTEP 4C: Clean up of temp dir') |
| 72 | for cm_caf_file in cm_caf: |
| 73 | print ('Removing ' + cm_caf_file) |
| 74 | shutil.rmtree(os.getcwd() + '/tmp') |
| 75 | |
| 76 | print('\nSTEP 5: Push translations to Git') |
| 77 | |
| 78 | proc = subprocess.Popen(['crowdin-cli', 'list', 'sources'],stdout=subprocess.PIPE) |
| 79 | |
| 80 | for source in iter(proc.stdout.readline,''): |
| 81 | path = os.getcwd() + source |
| 82 | path = path.rstrip() |
| 83 | all_projects = [] |
| 84 | if os.path.isfile(path): |
Marco Brohet | aec28ed | 2014-02-28 01:34:03 +0100 | [diff] [blame] | 85 | m = re.search('/(.*CMFileManager)/themes/res/values.*|/(device/.*/.*)/.*/res/values.*|/(hardware/.*/.*)/.*/res/values.*|/(.*)/res/values.*', source) |
Marco Brohet | 4683bee | 2014-02-28 01:06:03 +0100 | [diff] [blame] | 86 | path_this = m.group(1) |
| 87 | if not path_this in all_projects: |
| 88 | all_projects.append(path_this) |
| 89 | |
| 90 | xml = minidom.parse('default.xml') |
| 91 | items = xml.getElementsByTagName('project') |
| 92 | |
| 93 | for project in all_projects: |
| 94 | path_repo = os.getcwd() + '/' + project |
| 95 | repo = git.Repo(path_repo) |
| 96 | print repo.git.add(path_repo) |
| 97 | print repo.git.commit(m='Automatic translations import') |
| 98 | for project_item in items: |
| 99 | if project_item.attributes["path"].value == project: |
| 100 | print repo.git.push('ssh://cobjeM@review.cyanogenmod.org:29418/' + project_item.attributes['name'].value, 'HEAD:refs/for/cm-11.0') |
| 101 | |
| 102 | print('STEP 6: Done!') |