cnlls.py 3.65 KB
Newer Older
1
2
3
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

Mario Hock's avatar
Mario Hock committed
4
import os
Mario Hock's avatar
Mario Hock committed
5
from collections import defaultdict, deque
6

Mario Hock's avatar
Mario Hock committed
7
from cnl_library import CNLParser
Mario Hock's avatar
Mario Hock committed
8
from summary import LogAnalyzer, show_match
9
10


Mario Hock's avatar
Mario Hock committed
11
12
13
14
15
16
17
def list_files_in_cur_dir():
    raw_list = os.listdir()
    no_invisible = filter(lambda filename: not filename.startswith("."), raw_list)

    return sorted( no_invisible )


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def get_begin(cnl_file):
    return cnl_file.get_general_header()["Date"][1]

def are_close(cnl_file1, cnl_file2):
    t1 = get_begin(cnl_file1)
    t2 = get_begin(cnl_file2)

    return abs(t1 - t2) < 2


def find_match(cnl_file, list_of_files):
    for f in list_of_files:
        if ( are_close(cnl_file, f) ):
            list_of_files.remove(f)
            return f

    return None


Mario Hock's avatar
Mario Hock committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

def merge_comments(left_file, right_file):
    if ( not right_file ):
        return left_file.get_comment()

    c1 = left_file.get_comment()
    c2 = right_file.get_comment()

    if ( c1.find(c2) >= 0 ):
        return c1

    if ( c2.find(c1) >= 0 ):
        return c2

    return c1 + " / " + c2



Mario Hock's avatar
Mario Hock committed
55
def print_line(left_file, right_file, long):
Mario Hock's avatar
Mario Hock committed
56
57
58
59
60
61
62
63
    out = ""

    if ( right_file ):
        out = "{}  {}".format(left_file.filename, right_file.filename)
    else:
        out = left_file.filename

    if ( long ):
Mario Hock's avatar
Mario Hock committed
64
65
66
67
68
69
70
71
        out += "   // "
        comm_offset = len(out)

        comments = merge_comments(left_file, right_file).split(";")
        out += comments[0]

        for c in comments[1:]:
            out += ";\n" + " "*comm_offset + c.strip()
Mario Hock's avatar
Mario Hock committed
72
73
74
75

    print( out )


Mario Hock's avatar
Mario Hock committed
76
77
78
79
def show_summary(left_file, right_file=None):
    ## BRANCH: No match -> fallback to show_brief()
    if ( not right_file ):
        log = LogAnalyzer(left_file)
Mario Hock's avatar
Mario Hock committed
80
        log.visualize_brief()
Mario Hock's avatar
Mario Hock committed
81
82
83
84
85
86
87
88
89
90
91
92

    ## BRANCH: Match -> Display both next to each other.
    else:
        log_left = LogAnalyzer(left_file)
        log_right = LogAnalyzer(right_file)

        show_match(log_left, log_right)


def show(left_file, right_file, long=False, summary=False):
    if ( summary ):
        show_summary(left_file, right_file)
Mario Hock's avatar
Mario Hock committed
93
        print()
Mario Hock's avatar
Mario Hock committed
94
95
96
    else:
        print_line(left_file, right_file, long)

Mario Hock's avatar
Mario Hock committed
97

98
99
## MAIN ##
if __name__ == "__main__":
Mario Hock's avatar
Mario Hock committed
100
101
102
103
    #import sys

    ## Command line arguments
    import argparse
104

Mario Hock's avatar
Mario Hock committed
105
106
107
108
    parser = argparse.ArgumentParser()

    parser.add_argument("files", nargs='*')
    parser.add_argument("-l", "--long", action="store_true")
Mario Hock's avatar
Mario Hock committed
109
    parser.add_argument("-s", "--summary", action="store_true")
Mario Hock's avatar
Mario Hock committed
110

Mario Hock's avatar
..    
Mario Hock committed
111
112
113
    ## TODO -e, --environment
    #   Specify which env-fields should be added to the comment

Mario Hock's avatar
Mario Hock committed
114
115
116
117
118
    args = parser.parse_args()


    if ( args.files ):
        filenames = sorted( args.files )
Mario Hock's avatar
Mario Hock committed
119
120
121
122
123
124
    else:
        filenames = list_files_in_cur_dir()




125
126
127
128
129
130
131
132
133
134
135
    cnl_files = defaultdict(deque)

    ## Parse files and store them in a dict (of lists) according to their hostname.
    for filename in filenames:
        cnl_file = CNLParser(filename)
        hostname = cnl_file.get_hostname()
        cnl_files[hostname].append( cnl_file )


    hostnames = sorted( cnl_files.keys() )

Mario Hock's avatar
Mario Hock committed
136
137
138
139
    ## BRANCH: Input from two hosts -> Matching.
    if ( len(hostnames) == 2 ):
        left_files = cnl_files[hostnames[0]]
        right_files = cnl_files[hostnames[1]]
140

Mario Hock's avatar
Mario Hock committed
141
142
143
        for left_file in left_files:
            matching_file = find_match(left_file, right_files)
            show(left_file, matching_file, args.long, args.summary)
144
145


Mario Hock's avatar
Mario Hock committed
146
147
148
149
150
        ## Print left over right files.
        if ( len(right_files) > 0 ):
            print()
            for f in right_files:
                show(f, None, args.long, args.summary)
151

Mario Hock's avatar
Mario Hock committed
152

Mario Hock's avatar
Mario Hock committed
153
154
155
    ## BRANCH: Only one (or more than two hosts) -> No matching.
    else:
        for h in hostnames:
Mario Hock's avatar
Mario Hock committed
156
            for f in cnl_files[h]:
Mario Hock's avatar
Mario Hock committed
157
                show(f, None, args.long, args.summary)
Mario Hock's avatar
Mario Hock committed
158
159


160