blob: ddffd5b107d5b84812067191a6d8c011c0fc643d [file] [log] [blame]
Marco Brohet4683bee2014-02-28 01:06:03 +01001#!/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
8import create_cm_caf_xml
9import git
10import mmap
Marco Brohetcf4069b2014-02-28 18:48:17 +010011import os
Marco Brohet4683bee2014-02-28 01:06:03 +010012import os.path
13import re
14import shutil
15import subprocess
16import sys
17from urllib import urlretrieve
18from xml.dom import minidom
19
20print('Welcome to the CM Crowdin sync script!\n')
21
22print('STEP 0: Checking dependencies\n')
23if subprocess.check_output(['rvm', 'all', 'do', 'gem', 'list', 'crowdin-cli', '-i']) == 'true':
24 sys.exit('You have not installed crowdin-cli. Terminating.')
25if not os.path.isfile('caf.xml'):
26 sys.exit('You have no caf.xml. Terminating.')
27if not os.path.isfile('default.xml'):
28 sys.exit('You have no default.xml. Terminating.')
29
30print('STEP 1: Create cm_caf.xml')
31xml = minidom.parse('caf.xml')
32items = xml.getElementsByTagName('item')
33
34cm_caf = []
35
36for 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
49print('\nSTEP 2: Upload Crowdin source translations')
50print(subprocess.check_output(['crowdin-cli', 'upload', 'sources']))
51
Marco Brohetcf4069b2014-02-28 18:48:17 +010052print('STEP 3: Download Crowdin translations')
53print(subprocess.check_output(['crowdin-cli', "download"]))
Marco Brohet4683bee2014-02-28 01:06:03 +010054
Marco Brohetcf4069b2014-02-28 18:48:17 +010055print('STEP 4A: Clean up of source cm_caf.xmls')
56for cm_caf_file in cm_caf:
57 print ('Removing ' + cm_caf_file)
58 os.remove(cm_caf_file)
59
60print('\nSTEP 4B: Clean up of temp dir')
61shutil.rmtree(os.getcwd() + '/tmp')
62
63print('STEP 4C: Clean up of empty translations')
Marco Brohet4683bee2014-02-28 01:06:03 +010064result = [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']
65for xml_file in result:
66 if '<resources/>' in open(xml_file).read():
67 print ('Removing ' + xml_file)
68 os.remove(xml_file)
Marco Brohetcf4069b2014-02-28 18:48:17 +010069 elif '<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>' in open(xml_file).read():
Marco Brohet4683bee2014-02-28 01:06:03 +010070 print ('Removing ' + xml_file)
71 os.remove(xml_file)
72
Marco Brohet4683bee2014-02-28 01:06:03 +010073print('\nSTEP 5: Push translations to Git')
Marco Brohet4683bee2014-02-28 01:06:03 +010074proc = subprocess.Popen(['crowdin-cli', 'list', 'sources'],stdout=subprocess.PIPE)
Marco Brohet4683bee2014-02-28 01:06:03 +010075xml = minidom.parse('default.xml')
76items = xml.getElementsByTagName('project')
Marco Brohetcf4069b2014-02-28 18:48:17 +010077all_projects = []
Marco Brohet4683bee2014-02-28 01:06:03 +010078
Marco Brohetcf4069b2014-02-28 18:48:17 +010079for 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 Brohet4683bee2014-02-28 01:06:03 +010094
95print('STEP 6: Done!')