3
|
1 |
#
|
|
2 |
# Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
# All rights reserved.
|
|
4 |
# This component and the accompanying materials are made available
|
|
5 |
# under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
# which accompanies this distribution, and is available
|
|
7 |
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
#
|
|
9 |
# Initial Contributors:
|
|
10 |
# Nokia Corporation - initial contribution.
|
|
11 |
#
|
|
12 |
# Contributors:
|
|
13 |
#
|
|
14 |
# Description:
|
|
15 |
# Filter class for filtering XML logs and generate reports
|
|
16 |
# Will ultimately do everything that scanlog does
|
|
17 |
#
|
|
18 |
|
|
19 |
import os
|
|
20 |
import sys
|
|
21 |
import raptor
|
|
22 |
import filter_interface
|
|
23 |
|
|
24 |
class FilterSplitlog(filter_interface.Filter):
|
|
25 |
|
|
26 |
def open(self, raptor_instance):
|
|
27 |
"""Open a log file for the various I/O methods to write to."""
|
|
28 |
|
|
29 |
self.raptor = raptor_instance
|
|
30 |
self.logFileName = self.raptor.logFileName
|
|
31 |
# insert the time into the log file name
|
|
32 |
if self.logFileName:
|
|
33 |
self.logFileName.path = self.logFileName.path.replace("%TIME",
|
|
34 |
self.raptor.timestring)
|
|
35 |
|
|
36 |
try:
|
|
37 |
dirname = str(self.raptor.logFileName.Dir())
|
|
38 |
if dirname and not os.path.isdir(dirname):
|
|
39 |
os.makedirs(dirname)
|
|
40 |
except os.error, e:
|
|
41 |
if e.errno != os.errno.EEXIST:
|
|
42 |
sys.stderr.write("%s : error: cannot create directory " +
|
|
43 |
"%s\n" % (raptor.name, dirname))
|
|
44 |
return False
|
|
45 |
try:
|
|
46 |
self.out = open(str(self.logFileName), "w")
|
|
47 |
except:
|
|
48 |
self.out = None
|
|
49 |
sys.stderr.write("%s : error: cannot write log %s\n" %\
|
|
50 |
(raptor.name, self.logFileName.GetShellPath()))
|
|
51 |
return False
|
|
52 |
|
|
53 |
# Add extra streams for splitting logfile
|
|
54 |
self.log = str(self.logFileName)
|
|
55 |
self.index = self.log.rfind(".")
|
|
56 |
# If there is no dot, append to the end
|
|
57 |
if self.index < 0:
|
|
58 |
self.index = len(self.log)
|
|
59 |
self.streams = [self.out]
|
|
60 |
|
|
61 |
# Append this list for extra files
|
|
62 |
stream_list = ["clean", "whatlog", "recipe"]
|
|
63 |
|
|
64 |
for stream in stream_list:
|
|
65 |
|
|
66 |
path = self.log[:self.index] + "." + stream + \
|
|
67 |
self.log[self.index:]
|
|
68 |
try:
|
|
69 |
handle = open(path, "w")
|
|
70 |
self.streams.append(handle)
|
|
71 |
except:
|
|
72 |
self.streams.append(self.out)
|
|
73 |
sys.stderr.write("%s : error: cannot write log %s\n" %\
|
|
74 |
(str(raptor.name), path))
|
|
75 |
# self.out = self.streams[0]
|
|
76 |
self.clean = self.streams[1]
|
|
77 |
self.whatlog = self.streams[2]
|
|
78 |
self.recipe = self.streams[3]
|
|
79 |
self.block = self.out
|
|
80 |
|
|
81 |
else:
|
|
82 |
# Change output stream to stdout and override 'write' function
|
|
83 |
self.out = sys.stdout
|
|
84 |
def stdout_write(text):
|
|
85 |
self.out.write(text)
|
|
86 |
return True
|
|
87 |
self.write = stdout_write
|
|
88 |
|
|
89 |
return True
|
|
90 |
|
|
91 |
|
|
92 |
def write(self, text):
|
|
93 |
"""Write text into relevant log file"""
|
|
94 |
|
|
95 |
for textLine in text.splitlines():
|
|
96 |
textLine = textLine + '\n'
|
|
97 |
if textLine.startswith("<?xml ") or textLine.startswith("<buildlog ") \
|
|
98 |
or textLine.startswith("</buildlog"):
|
|
99 |
for stream in self.streams:
|
|
100 |
stream.write(textLine)
|
|
101 |
# Split 'CLEAN' output into clean file
|
|
102 |
elif textLine.startswith("<clean"):
|
|
103 |
if self.block != self.out:
|
|
104 |
sys.stderr.write("%s : error: invalid xml. <clean> tag found " \
|
|
105 |
+ "before previous block closed %s\n" %\
|
|
106 |
(raptor.name, self.logFileName))
|
|
107 |
self.block = self.clean
|
|
108 |
self.block.write(textLine)
|
|
109 |
|
|
110 |
# Split 'WHATLOG' output into whatlog file
|
|
111 |
elif textLine.startswith("<whatlog"):
|
|
112 |
if self.block != self.out:
|
|
113 |
sys.stderr.write("%s : error: invalid xml. <whatlog> tag " + \
|
|
114 |
"found before previous block closed\n" %\
|
|
115 |
(raptor.name, self.logFileName.GetShellPath()))
|
|
116 |
self.block = self.whatlog
|
|
117 |
self.block.write(textLine)
|
|
118 |
|
|
119 |
# Split 'RECIPE' output into recipe file
|
|
120 |
elif textLine.startswith("<recipe"):
|
|
121 |
if self.block != self.out:
|
|
122 |
sys.stderr.write("%s : error: invalid xml. <recipe> tag " + \
|
|
123 |
"found before previous block closed %s\n" %\
|
|
124 |
(raptor.name, self.logFileName.GetShellPath()))
|
|
125 |
self.block = self.recipe
|
|
126 |
self.block.write(textLine)
|
|
127 |
|
|
128 |
# End of block found. Reset block to standard logfile
|
|
129 |
elif textLine.startswith("</clean>") or textLine.startswith("</whatlog>") \
|
|
130 |
or textLine.startswith("</recipe>"):
|
|
131 |
self.block.write(textLine)
|
|
132 |
self.block = self.out
|
|
133 |
|
|
134 |
# Everything else goes to logfile associated with current block
|
|
135 |
else:
|
|
136 |
self.block.write(textLine)
|
|
137 |
return True
|
|
138 |
|
|
139 |
|
|
140 |
def summary(self):
|
|
141 |
"""Write Summary"""
|
|
142 |
if self.logFileName and not self.raptor.quiet:
|
|
143 |
sys.stdout.write("sbs: build log in %s\n" % self.logFileName)
|
|
144 |
return True
|
|
145 |
|
|
146 |
|
|
147 |
def close(self):
|
|
148 |
"""Close the log file(s)"""
|
|
149 |
|
|
150 |
try:
|
|
151 |
self.out.close
|
|
152 |
for stream in self.streams:
|
|
153 |
stream.close()
|
|
154 |
return True
|
|
155 |
except:
|
|
156 |
self.out = None
|
|
157 |
return False
|