blob: 2ba48ed255104dd08a7183fd48efe5bfc8c0ea03 [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
11import os.path
12import re
13import shutil
14import subprocess
15import sys
16from urllib import urlretrieve
17from xml.dom import minidom
18
19print('Welcome to the CM Crowdin sync script!\n')
20
Marco Brohetaec28ed2014-02-28 01:34:03 +010021
Marco Brohet4683bee2014-02-28 01:06:03 +010022print('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
52#print('STEP 3: Download Crowdin translations')
53#print(subprocess.check_output(['crowdin-cli', "download"]))
54
55print('STEP 4A: Clean up of empty translations')
56# Search for all XML files
57result = [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']
58for 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
66print('\nSTEP 4B: Clean up of source cm_caf.xmls')
67for cm_caf_file in cm_caf:
68 print ('Removing ' + cm_caf_file)
69 os.remove(cm_caf_file)
70
71print('\nSTEP 4C: Clean up of temp dir')
72for cm_caf_file in cm_caf:
73 print ('Removing ' + cm_caf_file)
74 shutil.rmtree(os.getcwd() + '/tmp')
75
76print('\nSTEP 5: Push translations to Git')
77
78proc = subprocess.Popen(['crowdin-cli', 'list', 'sources'],stdout=subprocess.PIPE)
79
80for 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 Brohetaec28ed2014-02-28 01:34:03 +010085 m = re.search('/(.*CMFileManager)/themes/res/values.*|/(device/.*/.*)/.*/res/values.*|/(hardware/.*/.*)/.*/res/values.*|/(.*)/res/values.*', source)
Marco Brohet4683bee2014-02-28 01:06:03 +010086 path_this = m.group(1)
87 if not path_this in all_projects:
88 all_projects.append(path_this)
89
90xml = minidom.parse('default.xml')
91items = xml.getElementsByTagName('project')
92
93for 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
102print('STEP 6: Done!')