Fix advanced EQ menu
[maemo-rb.git] / rbutil / rbutilqt / langstats.py
blob0dae9b93a6119277468dea36e1578652d69169df
1 #!/usr/bin/python
2 # __________ __ ___.
3 # Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 # Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 # Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 # Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 # \/ \/ \/ \/ \/
9 # Copyright (c) 2010 Dominik Riebeling
11 # All files in this archive are subject to the GNU General Public License.
12 # See the file COPYING in the source tree root for full license agreement.
14 # This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
15 # KIND, either express or implied.
18 # lrelease all rbutil translations and create a nice table from the output
19 # suited to paste in the wiki.
22 import subprocess
23 import re
24 import sys
25 import string
26 import tempfile
27 import os
28 import shutil
29 from datetime import date
30 import time
32 # extend search path for gitscraper
33 sys.path.append(os.path.abspath(os.path.dirname(os.path.realpath(__file__))
34 + "/../../utils/common"))
35 import gitscraper
38 langs = {
39 'cs': 'Czech',
40 'de': 'German',
41 'fi': 'Finnish',
42 'fr': 'French',
43 'gr': 'Greek',
44 'he': 'Hebrew',
45 'it': 'Italian',
46 'ja': 'Japanese',
47 'nl': 'Dutch',
48 'pl': 'Polish',
49 'pt': 'Portuguese',
50 'pt_BR': 'Portuguese (Brasileiro)',
51 'ru': 'Russian',
52 'tr': 'Turkish',
53 'zh_CN': 'Chinese',
54 'zh_TW': 'Chinese (trad)'
58 langbase = "rbutil/rbutilqt/"
59 # Paths and files to retrieve from svn.
60 # This is a mixed list, holding both paths and filenames.
61 # Get cpp sources as well for lupdate to work.
62 gitpaths = [langbase]
65 def printhelp():
66 print "Usage:", sys.argv[0], "[options]"
67 print "Print translation statistics suitable for pasting in the wiki."
68 print "Options:"
69 print " --pretty: display pretty output instead of wiki-style"
70 print " --help: show this help"
73 def main():
74 if len(sys.argv) > 1:
75 if sys.argv[1] == '--help':
76 printhelp()
77 sys.exit(0)
78 if len(sys.argv) > 1:
79 if sys.argv[1] == '--pretty':
80 pretty = 1
81 else:
82 pretty = 0
84 # get gitpaths to temporary folder
85 workfolder = tempfile.mkdtemp() + "/"
86 repo = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
87 tree = gitscraper.get_refs(repo)['refs/remotes/origin/master']
88 filesprops = gitscraper.scrape_files(repo, tree, gitpaths, dest=workfolder,
89 timestamp_files=["rbutil/rbutilqt/lang"])
91 projectfolder = workfolder + langbase
92 # lupdate translations and drop all obsolete translations
93 subprocess.Popen(["lupdate-qt4", "-no-obsolete", "rbutilqt.pro"],
94 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
95 cwd=projectfolder).communicate()
96 # lrelease translations to get status
97 output = subprocess.Popen(["lrelease-qt4", "rbutilqt.pro"],
98 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
99 cwd=projectfolder).communicate()
100 lines = re.split(r"\n", output[0])
102 re_updating = re.compile(r"^Updating.*")
103 re_generated = re.compile(r"Generated.*")
104 re_ignored = re.compile(r"Ignored.*")
105 re_qmlang = re.compile(r"'.*/rbutil_(.*)\.qm'")
106 re_qmbase = re.compile(r"'.*/(rbutil_.*)\.qm'")
107 re_genout = re.compile(r"[^0-9]([0-9]+) .*[^0-9]([0-9]+) .*[^0-9]([0-9]+) ")
108 re_ignout = re.compile(r"([0-9]+) ")
110 # print header
111 titlemax = 0
112 for l in langs:
113 cur = len(langs[l])
114 if titlemax < cur:
115 titlemax = cur
117 if pretty == 1:
118 spaces = [7, 5, 5, 5, 5, 27, 17]
119 delim = "+--" + titlemax * "-"
120 for s in spaces:
121 delim += "+" + "-" * s
122 delim += "+"
123 head = "| Language" + (titlemax - 8) * " " \
124 + " | Code |Trans| Fin |Unfin| Untr| Updated | Done |"
125 print delim
126 print "|" + " " * ((len(head) / 2 - len(tree) / 2) - 1) + str(tree) \
127 + " " * ((len(head) / 2 - len(tree) / 2) - 1) + "|"
128 print delim
129 print head
130 print delim
131 else:
132 r = str(tree) + " (" + gitscraper.get_file_timestamp(repo, tree, ".") + ")"
133 print "| *Translation status as of revision " + r + "* ||||||||"
134 print "| *Language* | *Language Code* | *Translations* | *Finished* | " \
135 "*Unfinished* | *Untranslated* | *Updated* | *Done* |"
137 # scan output
138 i = 0
139 while i < len(lines):
140 line = lines[i]
141 if re_updating.search(line):
142 lang = re_qmlang.findall(line)
143 tsfile = "rbutil/rbutilqt/lang/" + re_qmbase.findall(line)[0] + ".ts"
144 tsdate = filesprops[1][tsfile]
146 line = lines[i + 1]
147 if re_generated.search(line):
148 values = re_genout.findall(line)
149 translations = string.atoi(values[0][0])
150 finished = string.atoi(values[0][1])
151 unfinished = string.atoi(values[0][2])
152 line = lines[i + 2]
153 if not line.strip():
154 line = lines[i + 3]
155 if re_ignored.search(line):
156 ignored = string.atoi(re_ignout.findall(line)[0])
157 else:
158 ignored = 0
159 if lang[0] in langs:
160 name = langs[lang[0]].strip()
161 else:
162 name = '(unknown)'
164 percent = (float(finished + unfinished) * 100 / float(translations + ignored))
165 bar = "#" * int(percent / 10)
166 if (percent % 10) > 5:
167 bar += "+"
168 bar += " " * (10 - len(bar))
169 if pretty == 1:
170 fancylang = lang[0] + " " * (5 - len(lang[0]))
171 else:
172 fancylang = lang[0]
173 status = [fancylang, translations, finished, unfinished, ignored, tsdate, percent, bar]
174 if pretty == 1:
175 thisname = name + (titlemax - len(name)) * " "
176 print "| " + thisname + " | %5s | %3s | %3s | %3s | %3s | %25s | %3i%% %s |" % tuple(status)
177 else:
178 if percent > 90:
179 color = '%%GREEN%%'
180 else:
181 if percent > 50:
182 color = '%%ORANGE%%'
183 else:
184 color = '%%RED%%'
186 text = "| " + name + " | %s | %s | %s | %s | %s | %s | " + color + "%3i%%%%ENDCOLOR%% %s |"
187 print text % tuple(status)
188 i += 1
190 if pretty == 1:
191 print delim
193 shutil.rmtree(workfolder)
196 if __name__ == "__main__":
197 main()