fix the last of the red... note to self, fix backdrop.h!
[kugel-rb.git] / rbutil / rbutilqt / langstats.py
blob8b7e4e037db2e5dc8bfa38f82ee4e5a1dcbd3072
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 'ja' : 'Japanese',
42 'nl' : 'Dutch',
43 'pl' : 'Polish',
44 'pt' : 'Portuguese',
45 'pt_BR' : 'Portuguese (Brasileiro)',
46 'ru' : 'Russian',
47 'tr' : 'Turkish',
48 'zh_CN' : 'Chinese',
49 'zh_TW' : 'Chinese (trad)'
52 # modules that are not part of python itself.
53 try:
54 import pysvn
55 except ImportError:
56 print "Fatal: This script requires the pysvn package to run."
57 print " See http://pysvn.tigris.org/."
58 sys.exit(-5)
61 svnserver = "svn://svn.rockbox.org/rockbox/trunk/"
62 langbase = "rbutil/rbutilqt/"
63 # Paths and files to retrieve from svn.
64 # This is a mixed list, holding both paths and filenames.
65 # Get cpp sources as well for lupdate to work.
66 svnpaths = [ langbase ]
68 def printhelp():
69 print "Usage:", sys.argv[0], "[options]"
70 print "Print translation statistics suitable for pasting in the wiki."
71 print "Options:"
72 print " --pretty: display pretty output instead of wiki-style"
73 print " --help: show this help"
76 def gettrunkrev(svnsrv):
77 '''Get the revision of trunk for svnsrv'''
78 client = pysvn.Client()
79 entries = client.info2(svnsrv, recurse=False)
80 return entries[0][1].rev.number
83 def getsources(svnsrv, filelist, dest):
84 '''Get the files listed in filelist from svnsrv and put it at dest.'''
85 client = pysvn.Client()
86 print "Checking out sources from %s, please wait." % svnsrv
88 for elem in filelist:
89 url = re.subn('/$', '', svnsrv + elem)[0]
90 destpath = re.subn('/$', '', dest + elem)[0]
91 # make sure the destination path does exist
92 d = os.path.dirname(destpath)
93 if not os.path.exists(d):
94 os.makedirs(d)
95 # get from svn
96 try:
97 client.export(url, destpath)
98 except:
99 print "SVN client error: %s" % sys.exc_value
100 print "URL: %s, destination: %s" % (url, destpath)
101 return -1
102 print "Checkout finished."
103 return 0
106 def main():
107 if len(sys.argv) > 1:
108 if sys.argv[1] == '--help':
109 printhelp()
110 sys.exit(0)
111 if len(sys.argv) > 1:
112 if sys.argv[1] == '--pretty':
113 pretty = 1
114 else:
115 pretty = 0
117 # get svnpaths to temporary folder
118 workfolder = tempfile.mkdtemp() + "/"
119 getsources(svnserver, svnpaths, workfolder)
121 projectfolder = workfolder + langbase
122 # lupdate translations and drop all obsolete translations
123 subprocess.Popen(["lupdate-qt4", "-no-obsolete", "rbutilqt.pro"], \
124 stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=projectfolder).communicate()
125 # lrelease translations to get status
126 output = subprocess.Popen(["lrelease-qt4", "rbutilqt.pro"], stdout=subprocess.PIPE, \
127 stderr=subprocess.PIPE, cwd=projectfolder).communicate()
128 lines = re.split(r"\n", output[0])
130 re_updating = re.compile(r"^Updating.*")
131 re_generated = re.compile(r"Generated.*")
132 re_ignored = re.compile(r"Ignored.*")
133 re_qmlang = re.compile(r"'.*/rbutil_(.*)\.qm'")
134 re_qmbase = re.compile(r"'.*/(rbutil_.*)\.qm'")
135 re_genout = re.compile(r"[^0-9]([0-9]+) .*[^0-9]([0-9]+) .*[^0-9]([0-9]+) ")
136 re_ignout = re.compile(r"([0-9]+) ")
138 # print header
139 titlemax = 0
140 for l in langs:
141 cur = len(langs[l])
142 if titlemax < cur:
143 titlemax = cur
145 if pretty == 1:
146 delim = "+-" + titlemax * "-" \
147 + "-+-------+-----+-----+-----+-----+--------------------+-----------------+"
148 head = "| Language" + (titlemax - 8) * " " \
149 + " | Code |Trans| Fin |Unfin| Untr| Updated | Done |"
150 print delim
151 print "|" + " " * (len(head) / 2 - 3) + str(gettrunkrev(svnserver)) \
152 + " " * (len(head) / 2 - 4) + "|"
153 print delim
154 print head
155 print delim
156 else:
157 print "| *Translation status as of revision " + str(gettrunkrev(svnserver)) + "* ||||||||"
158 print "| *Language* | *Language Code* | *Translations* | *Finished* | " \
159 "*Unfinished* | *Untranslated* | *Updated* | *Done* |"
161 client = pysvn.Client()
162 # scan output
163 i = 0
164 while i < len(lines):
165 line = lines[i]
166 if re_updating.search(line):
167 lang = re_qmlang.findall(line)
168 tsfile = "lang/" + re_qmbase.findall(line)[0] + ".ts"
169 fileinfo = client.info2(svnserver + langbase + tsfile)[0][1]
170 tsrev = fileinfo.last_changed_rev.number
171 tsdate = date.fromtimestamp(fileinfo.last_changed_date).isoformat()
173 line = lines[i + 1]
174 if re_generated.search(line):
175 values = re_genout.findall(line)
176 translations = string.atoi(values[0][0])
177 finished = string.atoi(values[0][1])
178 unfinished = string.atoi(values[0][2])
179 line = lines[i + 2]
180 if not line.strip():
181 line = lines[i + 3]
182 if re_ignored.search(line):
183 ignored = string.atoi(re_ignout.findall(line)[0])
184 else:
185 ignored = 0
186 if langs.has_key(lang[0]):
187 name = langs[lang[0]].strip()
188 else:
189 name = '(unknown)'
191 percent = (float(finished + unfinished) * 100 / float(translations + ignored))
192 bar = "#" * int(percent / 10)
193 if (percent % 10) > 5:
194 bar += "+"
195 bar += " " * (10 - len(bar))
196 if pretty == 1:
197 fancylang = lang[0] + " " * (5 - len(lang[0]))
198 else:
199 fancylang = lang[0]
200 tsversion = str(tsrev) + " (" + tsdate + ")"
201 status = [fancylang, translations, finished, unfinished, ignored, tsversion, percent, bar]
202 if pretty == 1:
203 thisname = name + (titlemax - len(name)) * " "
204 print "| " + thisname + " | %5s | %3s | %3s | %3s | %3s | %6s | %3i%% %s |" % tuple(status)
205 else:
206 if percent > 90:
207 color = '%%GREEN%%'
208 else:
209 if percent > 50:
210 color = '%%ORANGE%%'
211 else:
212 color = '%%RED%%'
214 text = "| " + name + " | %s | %s | %s | %s | %s | %s | " + color + "%3i%%%%ENDCOLOR%% %s |"
215 print text % tuple(status)
216 i += 1
218 if pretty == 1:
219 print delim
221 shutil.rmtree(workfolder)
224 if __name__ == "__main__":
225 main()