summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominik Riebeling <Dominik.Riebeling@gmail.com>2013-06-08 23:56:33 +0200
committerDominik Riebeling <Dominik.Riebeling@gmail.com>2013-06-09 16:54:59 +0200
commit6b3c4beba4135681f0484d41ac096e1599939bd4 (patch)
tree274beced5c3fb3d59d8a935fb12b887a1ee35cac
parent43d8d6fece984b5ee3d4121a8982ac4919225d68 (diff)
downloadrockbox-6b3c4beba4135681f0484d41ac096e1599939bd4.tar.gz
rockbox-6b3c4beba4135681f0484d41ac096e1599939bd4.zip
Rework langstat.
- Generally improve code and make it compliant to PEP8. - Make it work with Python3 as well. Change-Id: I9e99999c59dc501664c36dd38fcb85fb60d6d9e6
-rwxr-xr-xrbutil/rbutilqt/langstats.py106
-rwxr-xr-xutils/common/gitscraper.py4
2 files changed, 58 insertions, 52 deletions
diff --git a/rbutil/rbutilqt/langstats.py b/rbutil/rbutilqt/langstats.py
index 0dae9b93a6..76f47450bd 100755
--- a/rbutil/rbutilqt/langstats.py
+++ b/rbutil/rbutilqt/langstats.py
@@ -26,12 +26,10 @@ import string
26import tempfile 26import tempfile
27import os 27import os
28import shutil 28import shutil
29from datetime import date
30import time
31 29
32# extend search path for gitscraper 30# extend search path for gitscraper
33sys.path.append(os.path.abspath(os.path.dirname(os.path.realpath(__file__)) 31sys.path.append(os.path.abspath(os.path.join(
34 + "/../../utils/common")) 32 os.path.dirname(os.path.realpath(__file__)), "../../utils/common")))
35import gitscraper 33import gitscraper
36 34
37 35
@@ -63,11 +61,11 @@ gitpaths = [langbase]
63 61
64 62
65def printhelp(): 63def printhelp():
66 print "Usage:", sys.argv[0], "[options]" 64 print("Usage:", sys.argv[0], "[options]")
67 print "Print translation statistics suitable for pasting in the wiki." 65 print("Print translation statistics suitable for pasting in the wiki.")
68 print "Options:" 66 print("Options:")
69 print " --pretty: display pretty output instead of wiki-style" 67 print(" --pretty: display pretty output instead of wiki-style")
70 print " --help: show this help" 68 print(" --help: show this help")
71 69
72 70
73def main(): 71def main():
@@ -77,34 +75,36 @@ def main():
77 sys.exit(0) 75 sys.exit(0)
78 if len(sys.argv) > 1: 76 if len(sys.argv) > 1:
79 if sys.argv[1] == '--pretty': 77 if sys.argv[1] == '--pretty':
80 pretty = 1 78 pretty = True
81 else: 79 else:
82 pretty = 0 80 pretty = False
83 81
84 # get gitpaths to temporary folder 82 # get gitpaths to temporary folder
85 workfolder = tempfile.mkdtemp() + "/" 83 workfolder = tempfile.mkdtemp() + "/"
86 repo = os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")) 84 repo = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
87 tree = gitscraper.get_refs(repo)['refs/remotes/origin/master'] 85 tree = gitscraper.get_refs(repo)['refs/remotes/origin/master']
88 filesprops = gitscraper.scrape_files(repo, tree, gitpaths, dest=workfolder, 86 filesprops = gitscraper.scrape_files(
89 timestamp_files=["rbutil/rbutilqt/lang"]) 87 repo, tree, gitpaths, dest=workfolder,
88 timestamp_files=["rbutil/rbutilqt/lang"])
90 89
91 projectfolder = workfolder + langbase 90 projectfolder = workfolder + langbase
92 # lupdate translations and drop all obsolete translations 91 # lupdate translations and drop all obsolete translations
93 subprocess.Popen(["lupdate-qt4", "-no-obsolete", "rbutilqt.pro"], 92 subprocess.Popen(["lupdate-qt4", "-no-obsolete", "rbutilqt.pro"],
94 stdout=subprocess.PIPE, stderr=subprocess.PIPE, 93 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
95 cwd=projectfolder).communicate() 94 cwd=projectfolder).communicate()
96 # lrelease translations to get status 95 # lrelease translations to get status
97 output = subprocess.Popen(["lrelease-qt4", "rbutilqt.pro"], 96 output = subprocess.Popen(["lrelease-qt4", "rbutilqt.pro"],
98 stdout=subprocess.PIPE, stderr=subprocess.PIPE, 97 stdout=subprocess.PIPE, stderr=subprocess.PIPE,
99 cwd=projectfolder).communicate() 98 cwd=projectfolder).communicate()
100 lines = re.split(r"\n", output[0]) 99 lines = re.split(r"\n", output[0].decode())
101 100
102 re_updating = re.compile(r"^Updating.*") 101 re_updating = re.compile(r"^Updating.*")
103 re_generated = re.compile(r"Generated.*") 102 re_generated = re.compile(r"Generated.*")
104 re_ignored = re.compile(r"Ignored.*") 103 re_ignored = re.compile(r"Ignored.*")
105 re_qmlang = re.compile(r"'.*/rbutil_(.*)\.qm'") 104 re_qmlang = re.compile(r"'.*/rbutil_(.*)\.qm'")
106 re_qmbase = re.compile(r"'.*/(rbutil_.*)\.qm'") 105 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]+) ") 106 re_genout = re.compile(
107 r"[^0-9]([0-9]+) .*[^0-9]([0-9]+) .*[^0-9]([0-9]+) ")
108 re_ignout = re.compile(r"([0-9]+) ") 108 re_ignout = re.compile(r"([0-9]+) ")
109 109
110 # print header 110 # print header
@@ -114,25 +114,27 @@ def main():
114 if titlemax < cur: 114 if titlemax < cur:
115 titlemax = cur 115 titlemax = cur
116 116
117 if pretty == 1: 117 if pretty:
118 spaces = [7, 5, 5, 5, 5, 27, 17] 118 spaces = [7, 5, 5, 5, 5, 27, 17]
119 delim = "+--" + titlemax * "-" 119 delim = "+--" + titlemax * "-"
120 for s in spaces: 120 for s in spaces:
121 delim += "+" + "-" * s 121 delim += "+" + "-" * s
122 delim += "+" 122 delim += "+"
123 head = "| Language" + (titlemax - 8) * " " \ 123 head = ("| {:%s} | {:6}|{:5}|{:5}|{:5}|{:5}| {:26}| {:16}|"
124 + " | Code |Trans| Fin |Unfin| Untr| Updated | Done |" 124 % titlemax).format("Language", "Code", "Trans", "Fin", "Unfin",
125 print delim 125 "Untr", "Updated", "Done")
126 print "|" + " " * ((len(head) / 2 - len(tree) / 2) - 1) + str(tree) \ 126 print(delim)
127 + " " * ((len(head) / 2 - len(tree) / 2) - 1) + "|" 127 print(("| {:^%s} |" % (len(head) - 4)).format(tree))
128 print delim 128 print(delim)
129 print head 129 print(head)
130 print delim 130 print(delim)
131 else: 131 else:
132 r = str(tree) + " (" + gitscraper.get_file_timestamp(repo, tree, ".") + ")" 132 r = "%s (%s)" % (str(tree),
133 print "| *Translation status as of revision " + r + "* ||||||||" 133 gitscraper.get_file_timestamp(repo, tree, "."))
134 print "| *Language* | *Language Code* | *Translations* | *Finished* | " \ 134 print("| *Translation status as of revision %s* ||||||||" % r)
135 "*Unfinished* | *Untranslated* | *Updated* | *Done* |" 135 print("| *Language* | *Language Code* | *Translations* "
136 "| *Finished* | *Unfinished* | *Untranslated* | *Updated* "
137 "| *Done* |")
136 138
137 # scan output 139 # scan output
138 i = 0 140 i = 0
@@ -140,20 +142,20 @@ def main():
140 line = lines[i] 142 line = lines[i]
141 if re_updating.search(line): 143 if re_updating.search(line):
142 lang = re_qmlang.findall(line) 144 lang = re_qmlang.findall(line)
143 tsfile = "rbutil/rbutilqt/lang/" + re_qmbase.findall(line)[0] + ".ts" 145 tsfile = "rbutil/rbutilqt/lang/%s.ts" % re_qmbase.findall(line)[0]
144 tsdate = filesprops[1][tsfile] 146 tsdate = filesprops[1][tsfile]
145 147
146 line = lines[i + 1] 148 line = lines[i + 1]
147 if re_generated.search(line): 149 if re_generated.search(line):
148 values = re_genout.findall(line) 150 values = re_genout.findall(line)
149 translations = string.atoi(values[0][0]) 151 translations = int(values[0][0])
150 finished = string.atoi(values[0][1]) 152 finished = int(values[0][1])
151 unfinished = string.atoi(values[0][2]) 153 unfinished = int(values[0][2])
152 line = lines[i + 2] 154 line = lines[i + 2]
153 if not line.strip(): 155 if not line.strip():
154 line = lines[i + 3] 156 line = lines[i + 3]
155 if re_ignored.search(line): 157 if re_ignored.search(line):
156 ignored = string.atoi(re_ignout.findall(line)[0]) 158 ignored = int(re_ignout.findall(line)[0])
157 else: 159 else:
158 ignored = 0 160 ignored = 0
159 if lang[0] in langs: 161 if lang[0] in langs:
@@ -161,34 +163,38 @@ def main():
161 else: 163 else:
162 name = '(unknown)' 164 name = '(unknown)'
163 165
164 percent = (float(finished + unfinished) * 100 / float(translations + ignored)) 166 percent = (finished + unfinished) * 100. / (translations + ignored)
165 bar = "#" * int(percent / 10) 167 bar = "#" * int(percent / 10)
166 if (percent % 10) > 5: 168 if (percent % 10) > 5:
167 bar += "+" 169 bar += "+"
168 bar += " " * (10 - len(bar)) 170 bar += " " * (10 - len(bar))
169 if pretty == 1: 171 if pretty:
170 fancylang = lang[0] + " " * (5 - len(lang[0])) 172 fancylang = lang[0] + " " * (5 - len(lang[0]))
171 else: 173 else:
172 fancylang = lang[0] 174 fancylang = lang[0]
173 status = [fancylang, translations, finished, unfinished, ignored, tsdate, percent, bar] 175 if pretty:
174 if pretty == 1: 176 print(("| {:%i} | {:5} | {:3} | {:3} | {:3} | {:3} | {:25} | "
175 thisname = name + (titlemax - len(name)) * " " 177 "{:3}%% {} |"
176 print "| " + thisname + " | %5s | %3s | %3s | %3s | %3s | %25s | %3i%% %s |" % tuple(status) 178 % titlemax).format(
179 name, fancylang, translations, finished, unfinished,
180 ignored, tsdate, int(percent), bar))
177 else: 181 else:
178 if percent > 90: 182 if percent > 90:
179 color = '%%GREEN%%' 183 color = r'%GREEN%'
180 else: 184 else:
181 if percent > 50: 185 if percent > 50:
182 color = '%%ORANGE%%' 186 color = r'%ORANGE%'
183 else: 187 else:
184 color = '%%RED%%' 188 color = r'%RED%'
185 189
186 text = "| " + name + " | %s | %s | %s | %s | %s | %s | " + color + "%3i%%%%ENDCOLOR%% %s |" 190 print("| %s | %s | %s | %s | %s | %s | %s | %s %i%% "
187 print text % tuple(status) 191 "%%ENDCOLOR%% %s |" %
192 (name, fancylang, translations, finished, unfinished,
193 ignored, tsdate, color, percent, bar))
188 i += 1 194 i += 1
189 195
190 if pretty == 1: 196 if pretty:
191 print delim 197 print(delim)
192 198
193 shutil.rmtree(workfolder) 199 shutil.rmtree(workfolder)
194 200
diff --git a/utils/common/gitscraper.py b/utils/common/gitscraper.py
index 9660acb83a..86d6a980cd 100755
--- a/utils/common/gitscraper.py
+++ b/utils/common/gitscraper.py
@@ -95,7 +95,7 @@ def get_lstree(repo, start, filterlist=[]):
95 if rf[3] in objects: 95 if rf[3] in objects:
96 print("FATAL: key already exists in dict!") 96 print("FATAL: key already exists in dict!")
97 return {} 97 return {}
98 objects[rf[3]] = rf[2] 98 objects[rf[3].decode()] = rf[2].decode()
99 return objects 99 return objects
100 100
101 101
@@ -172,7 +172,7 @@ def scrape_files(repo, treehash, filelist, dest="", timestamp_files=[]):
172 treeobjects = get_lstree(repo, treehash, filelist) 172 treeobjects = get_lstree(repo, treehash, filelist)
173 timestamps = {} 173 timestamps = {}
174 for obj in treeobjects: 174 for obj in treeobjects:
175 get_object(repo, treeobjects[obj], os.path.join(dest.encode(), obj)) 175 get_object(repo, treeobjects[obj], os.path.join(dest, obj))
176 for f in timestamp_files: 176 for f in timestamp_files:
177 if obj.find(f) == 0: 177 if obj.find(f) == 0:
178 timestamps[obj] = get_file_timestamp(repo, treehash, obj) 178 timestamps[obj] = get_file_timestamp(repo, treehash, obj)