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