Add italian to the list of language names.
[kugel-rb.git] / rbutil / rbutilqt / langstats.py
blob1e9d46a5e7532d3ad921da444020fca4b2a149d8
1 #!/usr/bin/python
2 # __________ __ ___.
3 # Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 # Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 # Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 # Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 # \/ \/ \/ \/ \/
8 # $Id$
10 # Copyright (c) 2010 Dominik Riebeling
12 # All files in this archive are subject to the GNU General Public License.
13 # See the file COPYING in the source tree root for full license agreement.
15 # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 # KIND, either express or implied.
19 # lrelease all rbutil translations and create a nice table from the output
20 # suited to paste in the wiki.
23 import subprocess
24 import re
25 import sys
26 import string
27 import pysvn
28 import tempfile
29 import os
30 import shutil
31 from datetime import date
34 langs = {
35 'cs' : 'Czech',
36 'de' : 'German',
37 'fi' : 'Finnish',
38 'fr' : 'French',
39 'gr' : 'Greek',
40 'he' : 'Hebrew',
41 'it' ; 'Italian',
42 'ja' : 'Japanese',
43 'nl' : 'Dutch',
44 'pl' : 'Polish',
45 'pt' : 'Portuguese',
46 'pt_BR' : 'Portuguese (Brasileiro)',
47 'ru' : 'Russian',
48 'tr' : 'Turkish',
49 'zh_CN' : 'Chinese',
50 'zh_TW' : 'Chinese (trad)'
53 # modules that are not part of python itself.
54 try:
55 import pysvn
56 except ImportError:
57 print "Fatal: This script requires the pysvn package to run."
58 print " See http://pysvn.tigris.org/."
59 sys.exit(-5)
62 svnserver = "svn://svn.rockbox.org/rockbox/trunk/"
63 langbase = "rbutil/rbutilqt/"
64 # Paths and files to retrieve from svn.
65 # This is a mixed list, holding both paths and filenames.
66 # Get cpp sources as well for lupdate to work.
67 svnpaths = [ langbase ]
69 def printhelp():
70 print "Usage:", sys.argv[0], "[options]"
71 print "Print translation statistics suitable for pasting in the wiki."
72 print "Options:"
73 print " --pretty: display pretty output instead of wiki-style"
74 print " --help: show this help"
77 def gettrunkrev(svnsrv):
78 '''Get the revision of trunk for svnsrv'''
79 client = pysvn.Client()
80 entries = client.info2(svnsrv, recurse=False)
81 return entries[0][1].rev.number
84 def getsources(svnsrv, filelist, dest):
85 '''Get the files listed in filelist from svnsrv and put it at dest.'''
86 client = pysvn.Client()
87 print "Checking out sources from %s, please wait." % svnsrv
89 for elem in filelist:
90 url = re.subn('/$', '', svnsrv + elem)[0]
91 destpath = re.subn('/$', '', dest + elem)[0]
92 # make sure the destination path does exist
93 d = os.path.dirname(destpath)
94 if not os.path.exists(d):
95 os.makedirs(d)
96 # get from svn
97 try:
98 client.export(url, destpath)
99 except:
100 print "SVN client error: %s" % sys.exc_value
101 print "URL: %s, destination: %s" % (url, destpath)
102 return -1
103 print "Checkout finished."
104 return 0
107 def main():
108 if len(sys.argv) > 1:
109 if sys.argv[1] == '--help':
110 printhelp()
111 sys.exit(0)
112 if len(sys.argv) > 1:
113 if sys.argv[1] == '--pretty':
114 pretty = 1
115 else:
116 pretty = 0
118 # get svnpaths to temporary folder
119 workfolder = tempfile.mkdtemp() + "/"
120 getsources(svnserver, svnpaths, workfolder)
122 projectfolder = workfolder + langbase
123 # lupdate translations and drop all obsolete translations
124 subprocess.Popen(["lupdate-qt4", "-no-obsolete", "rbutilqt.pro"], \
125 stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=projectfolder).communicate()
126 # lrelease translations to get status
127 output = subprocess.Popen(["lrelease-qt4", "rbutilqt.pro"], stdout=subprocess.PIPE, \
128 stderr=subprocess.PIPE, cwd=projectfolder).communicate()
129 lines = re.split(r"\n", output[0])
131 re_updating = re.compile(r"^Updating.*")
132 re_generated = re.compile(r"Generated.*")
133 re_ignored = re.compile(r"Ignored.*")
134 re_qmlang = re.compile(r"'.*/rbutil_(.*)\.qm'")
135 re_qmbase = re.compile(r"'.*/(rbutil_.*)\.qm'")
136 re_genout = re.compile(r"[^0-9]([0-9]+) .*[^0-9]([0-9]+) .*[^0-9]([0-9]+) ")
137 re_ignout = re.compile(r"([0-9]+) ")
139 # print header
140 titlemax = 0
141 for l in langs:
142 cur = len(langs[l])
143 if titlemax < cur:
144 titlemax = cur
146 if pretty == 1:
147 delim = "+-" + titlemax * "-" \
148 + "-+-------+-----+-----+-----+-----+--------------------+-----------------+"
149 head = "| Language" + (titlemax - 8) * " " \
150 + " | Code |Trans| Fin |Unfin| Untr| Updated | Done |"
151 print delim
152 print "|" + " " * (len(head) / 2 - 3) + str(gettrunkrev(svnserver)) \
153 + " " * (len(head) / 2 - 4) + "|"
154 print delim
155 print head
156 print delim
157 else:
158 print "| *Translation status as of revision " + str(gettrunkrev(svnserver)) + "* ||||||||"
159 print "| *Language* | *Language Code* | *Translations* | *Finished* | " \
160 "*Unfinished* | *Untranslated* | *Updated* | *Done* |"
162 client = pysvn.Client()
163 # scan output
164 i = 0
165 while i < len(lines):
166 line = lines[i]
167 if re_updating.search(line):
168 lang = re_qmlang.findall(line)
169 tsfile = "lang/" + re_qmbase.findall(line)[0] + ".ts"
170 fileinfo = client.info2(svnserver + langbase + tsfile)[0][1]
171 tsrev = fileinfo.last_changed_rev.number
172 tsdate = date.fromtimestamp(fileinfo.last_changed_date).isoformat()
174 line = lines[i + 1]
175 if re_generated.search(line):
176 values = re_genout.findall(line)
177 translations = string.atoi(values[0][0])
178 finished = string.atoi(values[0][1])
179 unfinished = string.atoi(values[0][2])
180 line = lines[i + 2]
181 if not line.strip():
182 line = lines[i + 3]
183 if re_ignored.search(line):
184 ignored = string.atoi(re_ignout.findall(line)[0])
185 else:
186 ignored = 0
187 if langs.has_key(lang[0]):
188 name = langs[lang[0]].strip()
189 else:
190 name = '(unknown)'
192 percent = (float(finished + unfinished) * 100 / float(translations + ignored))
193 bar = "#" * int(percent / 10)
194 if (percent % 10) > 5:
195 bar += "+"
196 bar += " " * (10 - len(bar))
197 if pretty == 1:
198 fancylang = lang[0] + " " * (5 - len(lang[0]))
199 else:
200 fancylang = lang[0]
201 tsversion = str(tsrev) + " (" + tsdate + ")"
202 status = [fancylang, translations, finished, unfinished, ignored, tsversion, percent, bar]
203 if pretty == 1:
204 thisname = name + (titlemax - len(name)) * " "
205 print "| " + thisname + " | %5s | %3s | %3s | %3s | %3s | %6s | %3i%% %s |" % tuple(status)
206 else:
207 if percent > 90:
208 color = '%%GREEN%%'
209 else:
210 if percent > 50:
211 color = '%%ORANGE%%'
212 else:
213 color = '%%RED%%'
215 text = "| " + name + " | %s | %s | %s | %s | %s | %s | " + color + "%3i%%%%ENDCOLOR%% %s |"
216 print text % tuple(status)
217 i += 1
219 if pretty == 1:
220 print delim
222 shutil.rmtree(workfolder)
225 if __name__ == "__main__":
226 main()