blob: df217578e69fbafc79f70ac8c290462d55b56993 [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
21print('STEP 0: Checking dependencies\n')
22if subprocess.check_output(['rvm', 'all', 'do', 'gem', 'list', 'crowdin-cli', '-i']) == 'true':
23 sys.exit('You have not installed crowdin-cli. Terminating.')
24if not os.path.isfile('caf.xml'):
25 sys.exit('You have no caf.xml. Terminating.')
26if not os.path.isfile('default.xml'):
27 sys.exit('You have no default.xml. Terminating.')
28
29print('STEP 1: Create cm_caf.xml')
30xml = minidom.parse('caf.xml')
31items = xml.getElementsByTagName('item')
32
33cm_caf = []
34
35for item in items:
36 subprocess.call(['mkdir', '-p', 'tmp/' + item.attributes["path"].value])
37 item_aosp = item.getElementsByTagName('aosp')
38 for aosp_item in item_aosp:
39 url = aosp_item.firstChild.nodeValue
40 path_to_base = 'tmp/' + item.attributes["path"].value + '/' + aosp_item.attributes["file"].value
41 path_to_cm = item.attributes["path"].value + '/' + aosp_item.attributes["file"].value
42 path = item.attributes["path"].value
43 urlretrieve(url, path_to_base)
44 create_cm_caf_xml.create_cm_caf_xml(path_to_base, path_to_cm, path)
45 cm_caf.append(path + '/cm_caf.xml')
46 print('Created ' + path + '/cm_caf.xml')
47
48print('\nSTEP 2: Upload Crowdin source translations')
49print(subprocess.check_output(['crowdin-cli', 'upload', 'sources']))
50
51#print('STEP 3: Download Crowdin translations')
52#print(subprocess.check_output(['crowdin-cli', "download"]))
53
54print('STEP 4A: Clean up of empty translations')
55# Search for all XML files
56result = [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']
57for xml_file in result:
58 if '<resources/>' in open(xml_file).read():
59 print ('Removing ' + xml_file)
60 os.remove(xml_file)
61 if '<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"/>' in open(xml_file).read():
62 print ('Removing ' + xml_file)
63 os.remove(xml_file)
64
65print('\nSTEP 4B: Clean up of source cm_caf.xmls')
66for cm_caf_file in cm_caf:
67 print ('Removing ' + cm_caf_file)
68 os.remove(cm_caf_file)
69
70print('\nSTEP 4C: Clean up of temp dir')
71for cm_caf_file in cm_caf:
72 print ('Removing ' + cm_caf_file)
73 shutil.rmtree(os.getcwd() + '/tmp')
74
75print('\nSTEP 5: Push translations to Git')
76
77proc = subprocess.Popen(['crowdin-cli', 'list', 'sources'],stdout=subprocess.PIPE)
78
79for source in iter(proc.stdout.readline,''):
80 path = os.getcwd() + source
81 path = path.rstrip()
82 all_projects = []
83 if os.path.isfile(path):
84 m = re.search('/(.*)/res/values.*', source)
85 path_this = m.group(1)
86 if not path_this in all_projects:
87 all_projects.append(path_this)
88
89xml = minidom.parse('default.xml')
90items = xml.getElementsByTagName('project')
91
92for project in all_projects:
93 path_repo = os.getcwd() + '/' + project
94 repo = git.Repo(path_repo)
95 print repo.git.add(path_repo)
96 print repo.git.commit(m='Automatic translations import')
97 for project_item in items:
98 if project_item.attributes["path"].value == project:
99 print repo.git.push('ssh://cobjeM@review.cyanogenmod.org:29418/' + project_item.attributes['name'].value, 'HEAD:refs/for/cm-11.0')
100
101print('STEP 6: Done!')