summaryrefslogtreecommitdiff
path: root/utils/rbutilqt/langstats.py
diff options
context:
space:
mode:
Diffstat (limited to 'utils/rbutilqt/langstats.py')
-rwxr-xr-xutils/rbutilqt/langstats.py195
1 files changed, 195 insertions, 0 deletions
diff --git a/utils/rbutilqt/langstats.py b/utils/rbutilqt/langstats.py
new file mode 100755
index 0000000000..cbf49dce08
--- /dev/null
+++ b/utils/rbutilqt/langstats.py
@@ -0,0 +1,195 @@
1#!/usr/bin/python3
2# __________ __ ___.
3# Open \______ \ ____ ____ | | _\_ |__ _______ ___
4# Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5# Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6# Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7# \/ \/ \/ \/ \/
8#
9# Copyright (c) 2010 Dominik Riebeling
10#
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.
13#
14# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
15# KIND, either express or implied.
16#
17#
18# lrelease all rbutil translations and create a nice table from the output
19# suited to paste in the wiki.
20#
21
22import subprocess
23import re
24import sys
25import tempfile
26import os
27import shutil
28import argparse
29
30# extend search path for gitscraper
31sys.path.append(os.path.abspath(os.path.join(
32 os.path.dirname(os.path.realpath(__file__)), "../../utils/common")))
33import gitscraper
34
35
36LANGS = {
37 'cs': 'Czech',
38 'de': 'German',
39 'fi': 'Finnish',
40 'fr': 'French',
41 'gr': 'Greek',
42 'he': 'Hebrew',
43 'it': 'Italian',
44 'ja': 'Japanese',
45 'nl': 'Dutch',
46 'pl': 'Polish',
47 'pt': 'Portuguese',
48 'pt_BR': 'Portuguese (Brasileiro)',
49 'ru': 'Russian',
50 'tr': 'Turkish',
51 'zh_CN': 'Chinese',
52 'zh_TW': 'Chinese (trad)'
53}
54
55
56LANGBASE = "rbutil/rbutilqt/"
57# Paths and files to retrieve from svn.
58# This is a mixed list, holding both paths and filenames.
59# Get cpp sources as well for lupdate to work.
60GITPATHS = [LANGBASE]
61
62
63def main():
64 parser = argparse.ArgumentParser(
65 description='Print translation statistics for pasting in the wiki.')
66 parser.add_argument('-p', '--pretty', action='store_true',
67 help='Display pretty output instead of wiki-style')
68 parser.add_argument('-c', '--commit', nargs='?', help='Git commit hash')
69
70 args = parser.parse_args()
71
72 langstat(args.pretty, args.commit)
73
74
75def langstat(pretty=True, tree=None):
76 '''Get translation stats and print to stdout.'''
77 # get gitpaths to temporary folder
78 workfolder = tempfile.mkdtemp() + "/"
79 repo = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
80 if tree is None:
81 tree = gitscraper.get_refs(repo)['HEAD']
82 filesprops = gitscraper.scrape_files(
83 repo, tree, GITPATHS, dest=workfolder,
84 timestamp_files=["rbutil/rbutilqt/lang"])
85
86 projectfolder = workfolder + LANGBASE
87 # lupdate translations and drop all obsolete translations
88 subprocess.Popen(["lupdate", "-no-obsolete", "rbutilqt.pro"],
89 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
90 cwd=projectfolder).communicate()
91 # lrelease translations to get status
92 output = subprocess.Popen(["lrelease", "rbutilqt.pro"],
93 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
94 cwd=projectfolder).communicate()
95 lines = re.split(r"\n", output[0].decode())
96
97 re_updating = re.compile(r"^Updating.*")
98 re_generated = re.compile(r"Generated.*")
99 re_ignored = re.compile(r"Ignored.*")
100 re_qmlang = re.compile(r"'.*/rbutil_(.*)\.qm'")
101 re_qmbase = re.compile(r"'.*/(rbutil_.*)\.qm'")
102 re_genout = re.compile(
103 r"[^0-9]([0-9]+) .*[^0-9]([0-9]+) .*[^0-9]([0-9]+) ")
104 re_ignout = re.compile(r"([0-9]+) ")
105
106 # print header
107 titlemax = 0
108 for lang in LANGS:
109 cur = len(LANGS[lang])
110 if titlemax < cur:
111 titlemax = cur
112
113 if pretty:
114 delim = "+--" + titlemax * "-"
115 for spc in [7, 5, 5, 5, 5, 27, 17]:
116 delim += "+" + "-" * spc
117 delim += "+"
118 head = ("| {:%s} | {:6}|{:5}|{:5}|{:5}|{:5}| {:26}| {:16}|"
119 % titlemax).format("Language", "Code", "Trans", "Fin", "Unfin",
120 "Untr", "Updated", "Done")
121 print(delim)
122 print(("| {:^%s} |" % (len(head) - 4)).format(tree))
123 print(delim)
124 print(head)
125 print(delim)
126 else:
127 rev = "%s (%s)" % (
128 tree, gitscraper.get_file_timestamp(repo, tree, "."))
129 print("| *Translation status as of revision %s* ||||||||" % rev)
130 print("| *Language* | *Language Code* | *Translations* "
131 "| *Finished* | *Unfinished* | *Untranslated* | *Updated* "
132 "| *Done* |")
133
134 # scan output
135 for i, line in enumerate(lines):
136 if re_updating.search(line):
137 lang = re_qmlang.findall(line)
138 tsfile = "rbutil/rbutilqt/lang/%s.ts" % re_qmbase.findall(line)[0]
139 tsdate = filesprops[1][tsfile]
140
141 line = lines[i + 1]
142 if re_generated.search(line):
143 values = re_genout.findall(line)
144 translations = int(values[0][0])
145 finished = int(values[0][1])
146 unfinished = int(values[0][2])
147 line = lines[i + 2]
148 if not line.strip():
149 line = lines[i + 3]
150 if re_ignored.search(line):
151 ignored = int(re_ignout.findall(line)[0])
152 else:
153 ignored = 0
154 if lang[0] in LANGS:
155 name = LANGS[lang[0]].strip()
156 else:
157 name = '(unknown)'
158
159 percent = (finished + unfinished) * 100. / (translations + ignored)
160 progress = "#" * int(percent / 10)
161 if (percent % 10) > 5:
162 progress += "+"
163 progress += " " * (10 - len(progress))
164 if pretty:
165 fancylang = lang[0] + " " * (5 - len(lang[0]))
166 else:
167 fancylang = lang[0]
168 if pretty:
169 print(("| {:%i} | {:5} | {:3} | {:3} | {:3} | {:3} | {:25} | "
170 "{:3}%% {} |"
171 % titlemax).format(
172 name, fancylang, translations, finished, unfinished,
173 ignored, tsdate, int(percent), progress))
174 else:
175 if percent > 90:
176 color = r'%GREEN%'
177 else:
178 if percent > 50:
179 color = r'%ORANGE%'
180 else:
181 color = r'%RED%'
182
183 print("| %s | %s | %s | %s | %s | %s | %s | %s %i%% "
184 "%%ENDCOLOR%% %s |" %
185 (name, fancylang, translations, finished, unfinished,
186 ignored, tsdate, color, percent, progress))
187
188 if pretty:
189 print(delim)
190
191 shutil.rmtree(workfolder)
192
193
194if __name__ == "__main__":
195 main()