summaryrefslogtreecommitdiffstats
path: root/rbutil/rbutilqt/langstats.py
blob: 7a1fc4f703ecf6bb965975af2d5da59e21c2ac87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/python3
#             __________               __   ___.
#   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
#   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
#   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
#   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
#                     \/            \/     \/    \/            \/
#
# Copyright (c) 2010 Dominik Riebeling
#
# All files in this archive are subject to the GNU General Public License.
# See the file COPYING in the source tree root for full license agreement.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
#
# lrelease all rbutil translations and create a nice table from the output
# suited to paste in the wiki.
#

import subprocess
import re
import sys
import tempfile
import os
import shutil
import argparse

# extend search path for gitscraper
sys.path.append(os.path.abspath(os.path.join(
    os.path.dirname(os.path.realpath(__file__)), "../../utils/common")))
import gitscraper


LANGS = {
    'cs':       'Czech',
    'de':       'German',
    'fi':       'Finnish',
    'fr':       'French',
    'gr':       'Greek',
    'he':       'Hebrew',
    'it':       'Italian',
    'ja':       'Japanese',
    'nl':       'Dutch',
    'pl':       'Polish',
    'pt':       'Portuguese',
    'pt_BR':    'Portuguese (Brasileiro)',
    'ru':       'Russian',
    'tr':       'Turkish',
    'zh_CN':    'Chinese',
    'zh_TW':    'Chinese (trad)'
}


LANGBASE = "rbutil/rbutilqt/"
# Paths and files to retrieve from svn.
# This is a mixed list, holding both paths and filenames.
# Get cpp sources as well for lupdate to work.
GITPATHS = [LANGBASE]


def main():
    parser = argparse.ArgumentParser(
        description='Print translation statistics for pasting in the wiki.')
    parser.add_argument('-p', '--pretty', action='store_true',
                        help='Display pretty output instead of wiki-style')

    args = parser.parse_args()

    langstat(args.pretty)


def langstat(pretty=True):
    '''Get translation stats and print to stdout.'''
    # get gitpaths to temporary folder
    workfolder = tempfile.mkdtemp() + "/"
    repo = os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))
    tree = gitscraper.get_refs(repo)['refs/remotes/origin/master']
    filesprops = gitscraper.scrape_files(
        repo, tree, GITPATHS, dest=workfolder,
        timestamp_files=["rbutil/rbutilqt/lang"])

    projectfolder = workfolder + LANGBASE
    # lupdate translations and drop all obsolete translations
    subprocess.Popen(["lupdate", "-no-obsolete", "rbutilqt.pro"],
                     stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                     cwd=projectfolder).communicate()
    # lrelease translations to get status
    output = subprocess.Popen(["lrelease", "rbutilqt.pro"],
                              stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                              cwd=projectfolder).communicate()
    lines = re.split(r"\n", output[0].decode())

    re_updating = re.compile(r"^Updating.*")
    re_generated = re.compile(r"Generated.*")
    re_ignored = re.compile(r"Ignored.*")
    re_qmlang = 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_ignout = re.compile(r"([0-9]+) ")

    # print header
    titlemax = 0
    for lang in LANGS:
        cur = len(LANGS[lang])
        if titlemax < cur:
            titlemax = cur

    if pretty:
        delim = "+--" + titlemax * "-"
        for spc in [7, 5, 5, 5, 5, 27, 17]:
            delim += "+" + "-" * spc
        delim += "+"
        head = ("| {:%s} | {:6}|{:5}|{:5}|{:5}|{:5}| {:26}| {:16}|"
                % titlemax).format("Language", "Code", "Trans", "Fin", "Unfin",
                                   "Untr", "Updated", "Done")
        print(delim)
        print(("| {:^%s} |" % (len(head) - 4)).format(tree))
        print(delim)
        print(head)
        print(delim)
    else:
        rev = "%s (%s)" % (
            tree, gitscraper.get_file_timestamp(repo, tree, "."))
        print("|  *Translation status as of revision %s*  ||||||||" % rev)
        print("| *Language* | *Language Code* | *Translations* "
              "| *Finished* | *Unfinished* | *Untranslated* | *Updated* "
              "| *Done* |")

    # scan output
    for i in range(len(lines)):
        line = lines[i]
        if re_updating.search(line):
            lang = re_qmlang.findall(line)
            tsfile = "rbutil/rbutilqt/lang/%s.ts" % re_qmbase.findall(line)[0]
            tsdate = filesprops[1][tsfile]

            line = lines[i + 1]
            if re_generated.search(line):
                values = re_genout.findall(line)
                translations = int(values[0][0])
                finished = int(values[0][1])
                unfinished = int(values[0][2])
                line = lines[i + 2]
                if not line.strip():
                    line = lines[i + 3]
                if re_ignored.search(line):
                    ignored = int(re_ignout.findall(line)[0])
                else:
                    ignored = 0
            if lang[0] in LANGS:
                name = LANGS[lang[0]].strip()
            else:
                name = '(unknown)'

            percent = (finished + unfinished) * 100. / (translations + ignored)
            progress = "#" * int(percent / 10)
            if (percent % 10) > 5:
                progress += "+"
            progress += " " * (10 - len(progress))
            if pretty:
                fancylang = lang[0] + " " * (5 - len(lang[0]))
            else:
                fancylang = lang[0]
            if pretty:
                print(("| {:%i} | {:5} | {:3} | {:3} | {:3} | {:3} | {:25} | "
                       "{:3}%% {} |"
                       % titlemax).format(
                           name, fancylang, translations, finished, unfinished,
                           ignored, tsdate, int(percent), progress))
            else:
                if percent > 90:
                    color = r'%GREEN%'
                else:
                    if percent > 50:
                        color = r'%ORANGE%'
                    else:
                        color = r'%RED%'

                print("| %s | %s | %s | %s | %s | %s | %s | %s %i%% "
                      "%%ENDCOLOR%% %s |" %
                      (name, fancylang, translations, finished, unfinished,
                       ignored, tsdate, color, percent, progress))

    if pretty:
        print(delim)

    shutil.rmtree(workfolder)


if __name__ == "__main__":
    main()