1
0
Fork 0
forked from len0rd/rockbox

Rework langstat.

- Generally improve code and make it compliant to PEP8.
- Make it work with Python3 as well.

Change-Id: I9e99999c59dc501664c36dd38fcb85fb60d6d9e6
This commit is contained in:
Dominik Riebeling 2013-06-08 23:56:33 +02:00
parent 43d8d6fece
commit 6b3c4beba4
2 changed files with 58 additions and 52 deletions

View file

@ -26,12 +26,10 @@ import string
import tempfile import tempfile
import os import os
import shutil import shutil
from datetime import date
import time
# extend search path for gitscraper # extend search path for gitscraper
sys.path.append(os.path.abspath(os.path.dirname(os.path.realpath(__file__)) sys.path.append(os.path.abspath(os.path.join(
+ "/../../utils/common")) os.path.dirname(os.path.realpath(__file__)), "../../utils/common")))
import gitscraper import gitscraper
@ -63,11 +61,11 @@ gitpaths = [langbase]
def printhelp(): def printhelp():
print "Usage:", sys.argv[0], "[options]" print("Usage:", sys.argv[0], "[options]")
print "Print translation statistics suitable for pasting in the wiki." print("Print translation statistics suitable for pasting in the wiki.")
print "Options:" print("Options:")
print " --pretty: display pretty output instead of wiki-style" print(" --pretty: display pretty output instead of wiki-style")
print " --help: show this help" print(" --help: show this help")
def main(): def main():
@ -77,34 +75,36 @@ def main():
sys.exit(0) sys.exit(0)
if len(sys.argv) > 1: if len(sys.argv) > 1:
if sys.argv[1] == '--pretty': if sys.argv[1] == '--pretty':
pretty = 1 pretty = True
else: else:
pretty = 0 pretty = False
# get gitpaths to temporary folder # get gitpaths to temporary folder
workfolder = tempfile.mkdtemp() + "/" workfolder = tempfile.mkdtemp() + "/"
repo = os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")) repo = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
tree = gitscraper.get_refs(repo)['refs/remotes/origin/master'] tree = gitscraper.get_refs(repo)['refs/remotes/origin/master']
filesprops = gitscraper.scrape_files(repo, tree, gitpaths, dest=workfolder, filesprops = gitscraper.scrape_files(
timestamp_files=["rbutil/rbutilqt/lang"]) repo, tree, gitpaths, dest=workfolder,
timestamp_files=["rbutil/rbutilqt/lang"])
projectfolder = workfolder + langbase projectfolder = workfolder + langbase
# lupdate translations and drop all obsolete translations # lupdate translations and drop all obsolete translations
subprocess.Popen(["lupdate-qt4", "-no-obsolete", "rbutilqt.pro"], subprocess.Popen(["lupdate-qt4", "-no-obsolete", "rbutilqt.pro"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
cwd=projectfolder).communicate() cwd=projectfolder).communicate()
# lrelease translations to get status # lrelease translations to get status
output = subprocess.Popen(["lrelease-qt4", "rbutilqt.pro"], output = subprocess.Popen(["lrelease-qt4", "rbutilqt.pro"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
cwd=projectfolder).communicate() cwd=projectfolder).communicate()
lines = re.split(r"\n", output[0]) lines = re.split(r"\n", output[0].decode())
re_updating = re.compile(r"^Updating.*") re_updating = re.compile(r"^Updating.*")
re_generated = re.compile(r"Generated.*") re_generated = re.compile(r"Generated.*")
re_ignored = re.compile(r"Ignored.*") re_ignored = re.compile(r"Ignored.*")
re_qmlang = re.compile(r"'.*/rbutil_(.*)\.qm'") re_qmlang = re.compile(r"'.*/rbutil_(.*)\.qm'")
re_qmbase = re.compile(r"'.*/(rbutil_.*)\.qm'") re_qmbase = re.compile(r"'.*/(rbutil_.*)\.qm'")
re_genout = re.compile(r"[^0-9]([0-9]+) .*[^0-9]([0-9]+) .*[^0-9]([0-9]+) ") re_genout = re.compile(
r"[^0-9]([0-9]+) .*[^0-9]([0-9]+) .*[^0-9]([0-9]+) ")
re_ignout = re.compile(r"([0-9]+) ") re_ignout = re.compile(r"([0-9]+) ")
# print header # print header
@ -114,25 +114,27 @@ def main():
if titlemax < cur: if titlemax < cur:
titlemax = cur titlemax = cur
if pretty == 1: if pretty:
spaces = [7, 5, 5, 5, 5, 27, 17] spaces = [7, 5, 5, 5, 5, 27, 17]
delim = "+--" + titlemax * "-" delim = "+--" + titlemax * "-"
for s in spaces: for s in spaces:
delim += "+" + "-" * s delim += "+" + "-" * s
delim += "+" delim += "+"
head = "| Language" + (titlemax - 8) * " " \ head = ("| {:%s} | {:6}|{:5}|{:5}|{:5}|{:5}| {:26}| {:16}|"
+ " | Code |Trans| Fin |Unfin| Untr| Updated | Done |" % titlemax).format("Language", "Code", "Trans", "Fin", "Unfin",
print delim "Untr", "Updated", "Done")
print "|" + " " * ((len(head) / 2 - len(tree) / 2) - 1) + str(tree) \ print(delim)
+ " " * ((len(head) / 2 - len(tree) / 2) - 1) + "|" print(("| {:^%s} |" % (len(head) - 4)).format(tree))
print delim print(delim)
print head print(head)
print delim print(delim)
else: else:
r = str(tree) + " (" + gitscraper.get_file_timestamp(repo, tree, ".") + ")" r = "%s (%s)" % (str(tree),
print "| *Translation status as of revision " + r + "* ||||||||" gitscraper.get_file_timestamp(repo, tree, "."))
print "| *Language* | *Language Code* | *Translations* | *Finished* | " \ print("| *Translation status as of revision %s* ||||||||" % r)
"*Unfinished* | *Untranslated* | *Updated* | *Done* |" print("| *Language* | *Language Code* | *Translations* "
"| *Finished* | *Unfinished* | *Untranslated* | *Updated* "
"| *Done* |")
# scan output # scan output
i = 0 i = 0
@ -140,20 +142,20 @@ def main():
line = lines[i] line = lines[i]
if re_updating.search(line): if re_updating.search(line):
lang = re_qmlang.findall(line) lang = re_qmlang.findall(line)
tsfile = "rbutil/rbutilqt/lang/" + re_qmbase.findall(line)[0] + ".ts" tsfile = "rbutil/rbutilqt/lang/%s.ts" % re_qmbase.findall(line)[0]
tsdate = filesprops[1][tsfile] tsdate = filesprops[1][tsfile]
line = lines[i + 1] line = lines[i + 1]
if re_generated.search(line): if re_generated.search(line):
values = re_genout.findall(line) values = re_genout.findall(line)
translations = string.atoi(values[0][0]) translations = int(values[0][0])
finished = string.atoi(values[0][1]) finished = int(values[0][1])
unfinished = string.atoi(values[0][2]) unfinished = int(values[0][2])
line = lines[i + 2] line = lines[i + 2]
if not line.strip(): if not line.strip():
line = lines[i + 3] line = lines[i + 3]
if re_ignored.search(line): if re_ignored.search(line):
ignored = string.atoi(re_ignout.findall(line)[0]) ignored = int(re_ignout.findall(line)[0])
else: else:
ignored = 0 ignored = 0
if lang[0] in langs: if lang[0] in langs:
@ -161,34 +163,38 @@ def main():
else: else:
name = '(unknown)' name = '(unknown)'
percent = (float(finished + unfinished) * 100 / float(translations + ignored)) percent = (finished + unfinished) * 100. / (translations + ignored)
bar = "#" * int(percent / 10) bar = "#" * int(percent / 10)
if (percent % 10) > 5: if (percent % 10) > 5:
bar += "+" bar += "+"
bar += " " * (10 - len(bar)) bar += " " * (10 - len(bar))
if pretty == 1: if pretty:
fancylang = lang[0] + " " * (5 - len(lang[0])) fancylang = lang[0] + " " * (5 - len(lang[0]))
else: else:
fancylang = lang[0] fancylang = lang[0]
status = [fancylang, translations, finished, unfinished, ignored, tsdate, percent, bar] if pretty:
if pretty == 1: print(("| {:%i} | {:5} | {:3} | {:3} | {:3} | {:3} | {:25} | "
thisname = name + (titlemax - len(name)) * " " "{:3}%% {} |"
print "| " + thisname + " | %5s | %3s | %3s | %3s | %3s | %25s | %3i%% %s |" % tuple(status) % titlemax).format(
name, fancylang, translations, finished, unfinished,
ignored, tsdate, int(percent), bar))
else: else:
if percent > 90: if percent > 90:
color = '%%GREEN%%' color = r'%GREEN%'
else: else:
if percent > 50: if percent > 50:
color = '%%ORANGE%%' color = r'%ORANGE%'
else: else:
color = '%%RED%%' color = r'%RED%'
text = "| " + name + " | %s | %s | %s | %s | %s | %s | " + color + "%3i%%%%ENDCOLOR%% %s |" print("| %s | %s | %s | %s | %s | %s | %s | %s %i%% "
print text % tuple(status) "%%ENDCOLOR%% %s |" %
(name, fancylang, translations, finished, unfinished,
ignored, tsdate, color, percent, bar))
i += 1 i += 1
if pretty == 1: if pretty:
print delim print(delim)
shutil.rmtree(workfolder) shutil.rmtree(workfolder)

View file

@ -95,7 +95,7 @@ def get_lstree(repo, start, filterlist=[]):
if rf[3] in objects: if rf[3] in objects:
print("FATAL: key already exists in dict!") print("FATAL: key already exists in dict!")
return {} return {}
objects[rf[3]] = rf[2] objects[rf[3].decode()] = rf[2].decode()
return objects return objects
@ -172,7 +172,7 @@ def scrape_files(repo, treehash, filelist, dest="", timestamp_files=[]):
treeobjects = get_lstree(repo, treehash, filelist) treeobjects = get_lstree(repo, treehash, filelist)
timestamps = {} timestamps = {}
for obj in treeobjects: for obj in treeobjects:
get_object(repo, treeobjects[obj], os.path.join(dest.encode(), obj)) get_object(repo, treeobjects[obj], os.path.join(dest, obj))
for f in timestamp_files: for f in timestamp_files:
if obj.find(f) == 0: if obj.find(f) == 0:
timestamps[obj] = get_file_timestamp(repo, treehash, obj) timestamps[obj] = get_file_timestamp(repo, treehash, obj)