-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgreptest.py
More file actions
executable file
·84 lines (66 loc) · 2.32 KB
/
Copy pathgreptest.py
File metadata and controls
executable file
·84 lines (66 loc) · 2.32 KB
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
#!/usr/bin/python
from __future__ import print_function
import argparse
import mmap
import os
import re
import sys
message_start_pattern = re.compile(r"^\d{2}:\d{2}:\d{2},\d{3} ", re.M)
def main():
args = parse_args()
file_name = args.file[0]
tests = args.tests
result_files = dict()
test_patterns = dict()
try:
for test_name in tests:
test_file_name = gen_file_name(test_name)
print("Opening output file %s for %s" % (test_file_name, test_name))
result_files[test_name] = open(test_file_name, "w")
test_patterns[test_name] = re.compile(test_name)
match_start = 0
match_end = 0
data = mmap_view(file_name)
for m in log_message_iter(data):
match_end = m.start()
#print("Found match: %s:%s" % (match_start, match_end))
message = data[match_start:match_end]
match_start = match_end
#print("Found log message %s" % message)
match_message(message, test_patterns, result_files)
message = data[match_start:]
match_message(message, test_patterns, result_files)
finally:
for test_file in result_files.itervalues():
test_file.close()
def match_message(message, test_patterns, result_files):
for (test_name, test_pattern) in test_patterns.iteritems():
if re.search(test_pattern, message):
short_message = re.sub("(" + test_name + ")-Node", "Node", message)
result_files[test_name].write(short_message)
def parse_args():
parser = argparse.ArgumentParser("greptest.py")
parser.add_argument('file', nargs=1,
help='Log file')
parser.add_argument('tests', metavar='test', nargs='+',
help='Test names')
args = parser.parse_args()
#print(args)
return args
def gen_file_name(test_name):
return re.sub(r"[^A-Z0-9]", "", test_name).lower() + ".log"
def mmap_view(file_name):
print("Opening input file %s" % file_name)
f = open(file_name)
if not f:
print("Invalid input file: %s" % file_name)
exit(1)
size = os.stat(file_name).st_size
if size > 2000000000:
raise Exception("Log is too big")
data = mmap.mmap(f.fileno(), size, access=mmap.ACCESS_READ)
return data
def log_message_iter(data):
return re.finditer(message_start_pattern, data)
if __name__ == "__main__":
main()