forked from opensciencegrid/osg-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
osg-test-log-viewer
executable file
·199 lines (158 loc) · 6.59 KB
/
osg-test-log-viewer
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
195
196
197
198
199
#!/usr/bin/env python3
"""View the output from osg-test in a structured way"""
from tkinter import *
import tkinter.font
import tkinter.scrolledtext
import re
import sys
import urllib
import urllib.error
import urllib.request
OK_FAIL_COMMAND_PATTERNS = [
# These commands don't get highlighted if they return nonzero
"rpm --query",
"systemctl is-active",
r"service\s+\S+\s+status"
]
class Section(object):
"A section from the osg-test output"
def __init__(self, label, text="", color="black"):
self.label = label
self.text = text
self.color = color
def is_empty(text):
"True if a block of text is empty except for whitespace"
return not re.search(r'\S', text)
def load_logdata_from_handle(handle):
"Load profiler data from handle and return as a list of Section objects"
datetime_re = r"\d+-\d+-\d+[ ]\d+:\d+:\d+"
logdata = []
section = Section("TOP")
in_output = False
current_test = ""
for line in handle:
line = line.decode(errors="replace")
line = line.rstrip()
command_match = re.match(r'(?x)osgtest: \s* ' + datetime_re + r''': \s*
(?P<test>[^:]+:[^:]+):
(?P<lineno>\d+): \s*
(?P<command>.+?) \s* >>> \s*
(?P<result>.+?) \s* $''',
line)
match = (re.match(r'message:\s*%s:\s*(.+?)\s*$' % datetime_re, line) or
command_match or
re.match(r'((?:FAIL|ERROR|BAD SKIPS|OK SKIPS|EXCLUDED).+?)\s*$', line) or
re.match(r'(OSG-TEST LOG)', line))
output_delimiter = re.match(r'(?:STDOUT|STDERR):[{}]', line)
if match and not in_output:
if is_empty(section.text):
section.label += ' (no text)'
logdata.append(section)
if command_match:
if current_test != match.group('test'):
logdata.append(Section(match.group('test')))
current_test = match.group('test')
retcode, _, _ = match.group('result').split()
color = "black"
if int(retcode) != 0:
if not any(re.search(pattern, match.group('command')) for pattern in OK_FAIL_COMMAND_PATTERNS):
color = "red"
section = Section(' ' + match.group('lineno') + ':' + match.group('command'), color=color)
else:
section = Section(match.group(1))
if output_delimiter:
in_output = not in_output
section.text += line + "\n"
logdata.append(section)
return logdata
def load_logdata_from_file(filename):
try:
with open(filename) as filehandle:
return load_logdata_from_handle(filehandle)
except IOError as e:
print("Error reading file %s: %s" % (filename, e.strerror), file=sys.stderr)
sys.exit(1)
def load_logdata_from_url(url):
try:
urlhandle = urllib.request.urlopen(url)
return load_logdata_from_handle(urlhandle)
except urllib.error.HTTPError as e:
print("HTTP error code %d reading URL %s: %s" % (e.code, url, e.reason), file=sys.stderr)
sys.exit(1)
except urllib.error.URLError as e:
print("Error reading URL %s: %s" % (url, e.reason), file=sys.stderr)
sys.exit(1)
class Application(Frame):
"The GUI"
def __init__(self, master, logfile, logdata):
self.master = master
Frame.__init__(self, master)
self.pack(fill=BOTH, expand=True)
self.font = tkinter.font.Font(family='Sans', size=9)
self.textfont = tkinter.font.Font(family='Monospace', size=9)
self.top = Frame(self)
self.top.pack(side=TOP, fill=X)
self.quit_btn = Button(self.top, text="QUIT", fg="red", command=self.quit, font=self.font)
self.quit_btn.pack(side=LEFT)
self.copy_section_btn = Button(self.top, text="Copy Section to X Clipboard", command=self.copy_section, font=self.font)
self.copy_section_btn.pack(side=LEFT)
self.bot = Frame(self)
self.bot.pack(side=BOTTOM, fill=BOTH, expand=True)
self.section_scrollbar = Scrollbar(self.bot)
self.section_scrollbar.pack(side=LEFT, fill=Y)
self.section_lbx = Listbox(self.bot, selectmode=BROWSE, font=self.font)
self.section_lbx.pack(side=LEFT, fill=BOTH, expand=True)
self.current = None
self.section_lbx.config(yscrollcommand=self.section_scrollbar.set)
self.section_scrollbar.config(command=self.section_lbx.yview)
self.filelabel = Label(self.top, text=logfile, font=self.font)
self.filelabel.pack(side=RIGHT)
self.label = Label(self.top, text="", font=self.font)
self.label.pack(side=LEFT, fill=X)
self.text = tkinter.scrolledtext.ScrolledText(self.bot, font=self.textfont)
self.text.pack(side=RIGHT, fill=BOTH, expand=True)
self.logdata = logdata
self.populate()
self.poll()
def populate(self):
"Fill the section list box with the labels"
self.section_lbx.delete(0, END)
for index, section in enumerate(self.logdata):
self.section_lbx.insert(END, section.label)
self.section_lbx.itemconfig(index, foreground=section.color)
def poll(self):
"Check if selection has changed"
now = self.section_lbx.curselection()
if now != self.current:
self.list_has_changed(now)
self.current = now
self.after(250, self.poll)
def list_has_changed(self, now):
"Update the label and the text box"
if now:
idx = int(now[0])
self.label.configure(text=self.logdata[idx].label)
self.text.config(state=NORMAL)
self.text.delete(1.0, END)
self.text.insert(END, self.logdata[idx].text)
self.text.config(state=DISABLED)
def copy_section(self):
"Copy all text in section to the X clipboard"
self.master.clipboard_clear()
self.master.clipboard_append(self.text.get(1.0, END))
def main():
if len(sys.argv) != 2:
print("Usage: %s <LOG FILENAME OR URL>" % sys.argv[0])
print("View the output of osg-test")
sys.exit(2)
logpath = sys.argv[1]
if '://' not in logpath:
logdata = load_logdata_from_file(logpath)
else:
logdata = load_logdata_from_url(logpath)
root = Tk()
root.wm_title("osg-test log viewer: " + logpath)
Application(root, logpath, logdata)
root.mainloop()
if __name__ == '__main__':
main()