File: vftsum.py

package info (click to toggle)
veryfasttree 4.0.4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,308 kB
  • sloc: cpp: 7,403; python: 209; sh: 38; makefile: 36
file content (107 lines) | stat: -rw-r--r-- 3,225 bytes parent folder | download
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
#!/usr/bin/env python3

import sys
import os


def show(name, secs, out):
    d = int(secs / 3600 / 24)
    h = int(secs / 3600) % 24
    m = int(secs / 60) % 60
    s = round(secs % 60, 2)

    row = name + ": "
    if d > 0:
        row += str(d) + " days, "
    if h > 0:
        row += str(h) + " hours, "
    if m > 0:
        row += str(m) + " minutes, "
    row += str(s) + " seconds"
    print(row, file=out)


def parse(file, short):
    total = float(0)
    table = []
    row_check = False
    relative = False
    last_secs = 0

    with open(file) as finput, open(file + '.sum', 'w') as out:
        for line in finput:
            if "seconds" in line and line.strip()[0].isdigit():
                secs_s, name = line.split("seconds")[0:2]
                name = name[1:].strip()
                secs = float(secs_s)
                if not relative:
                    secs, last_secs = secs - last_secs, secs
                total += secs
                if len(table) == 0:
                    row_check = True
                    table.append((name, secs))
                else:
                    rname = table[-1][0]
                    rsecs = table[-1][1]

                    if short and "Tree partitioned" in name:
                        table[-1] = (rname, rsecs + secs)
                        row_check = True
                        continue

                    if row_check:
                        for i in range(min(len(rname), len(name))):
                            if rname[i] != name[i]:
                                break

                        if i > 0 and "," in name and not short:
                            i = name.index(',') + 1

                        if i > 2 and name[i - 2] == ' ' and short:
                            i -= 1

                        if i > 4 and (name[i - 1] == ' ' or name[i - 1] == ','):
                            table[-1] = (rname[:i].strip(), rsecs + secs)
                            row_check = short
                            continue

                    if len(rname) <= len(name) and rname == name[:len(rname)]:
                        table[-1] = (rname, rsecs + secs)
                    else:
                        row_check = True
                        table.append((name.split(",")[0], secs))
            elif len(table) == 0:
                relative = relative or "-relative-progress" in line
                print(line.strip(), file=out)

        tab = 0
        for name, secs in table:
            if len(name) > tab:
                tab = len(name)

        for name, secs in table:
            show(" " * (tab - len(name)) + name, secs, out)
        show(" " * (tab - len("Total")) + "Total", total, out)


def main():
    if len(sys.argv) < 2:
        print("use <file|folder> [-s]\n  -s: shorter summary")
        exit(-1)

    path = sys.argv[1]
    short = len(sys.argv) > 2 and "-s" == sys.argv[2]

    if os.path.isfile(path):
        parse(path, short)
    else:
        for file in os.listdir(path):
            if file.endswith(".out"):
                try:
                    parse(os.path.join(path, file), short)
                except:
                    pass


if __name__ == '__main__':
    main()