author | timothy.murphy@nokia.com |
Wed, 02 Dec 2009 00:21:12 +0000 | |
branch | wip |
changeset 29 | ee00c00df073 |
parent 3 | e1eecf4d390d |
permissions | -rw-r--r-- |
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 |
||
29
ee00c00df073
Catchup to Perforce WIP with timing, python24
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
19 |
import errno |
3 | 20 |
import os |
21 |
import sys |
|
22 |
import raptor |
|
23 |
import filter_interface |
|
24 |
||
25 |
class FilterSplitlog(filter_interface.Filter): |
|
26 |
||
27 |
def open(self, raptor_instance): |
|
28 |
"""Open a log file for the various I/O methods to write to.""" |
|
29 |
||
30 |
self.raptor = raptor_instance |
|
31 |
self.logFileName = self.raptor.logFileName |
|
32 |
# insert the time into the log file name |
|
33 |
if self.logFileName: |
|
34 |
self.logFileName.path = self.logFileName.path.replace("%TIME", |
|
35 |
self.raptor.timestring) |
|
36 |
||
37 |
try: |
|
38 |
dirname = str(self.raptor.logFileName.Dir()) |
|
39 |
if dirname and not os.path.isdir(dirname): |
|
40 |
os.makedirs(dirname) |
|
41 |
except os.error, e: |
|
29
ee00c00df073
Catchup to Perforce WIP with timing, python24
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
42 |
if e.errno != errno.EEXIST: |
3 | 43 |
sys.stderr.write("%s : error: cannot create directory " + |
44 |
"%s\n" % (raptor.name, dirname)) |
|
45 |
return False |
|
46 |
try: |
|
47 |
self.out = open(str(self.logFileName), "w") |
|
48 |
except: |
|
49 |
self.out = None |
|
50 |
sys.stderr.write("%s : error: cannot write log %s\n" %\ |
|
51 |
(raptor.name, self.logFileName.GetShellPath())) |
|
52 |
return False |
|
53 |
||
54 |
# Add extra streams for splitting logfile |
|
55 |
self.log = str(self.logFileName) |
|
56 |
self.index = self.log.rfind(".") |
|
57 |
# If there is no dot, append to the end |
|
58 |
if self.index < 0: |
|
59 |
self.index = len(self.log) |
|
60 |
self.streams = [self.out] |
|
61 |
||
62 |
# Append this list for extra files |
|
63 |
stream_list = ["clean", "whatlog", "recipe"] |
|
64 |
||
65 |
for stream in stream_list: |
|
66 |
||
67 |
path = self.log[:self.index] + "." + stream + \ |
|
68 |
self.log[self.index:] |
|
69 |
try: |
|
70 |
handle = open(path, "w") |
|
71 |
self.streams.append(handle) |
|
72 |
except: |
|
73 |
self.streams.append(self.out) |
|
74 |
sys.stderr.write("%s : error: cannot write log %s\n" %\ |
|
75 |
(str(raptor.name), path)) |
|
76 |
# self.out = self.streams[0] |
|
77 |
self.clean = self.streams[1] |
|
78 |
self.whatlog = self.streams[2] |
|
79 |
self.recipe = self.streams[3] |
|
80 |
self.block = self.out |
|
81 |
||
82 |
else: |
|
83 |
# Change output stream to stdout and override 'write' function |
|
84 |
self.out = sys.stdout |
|
85 |
def stdout_write(text): |
|
86 |
self.out.write(text) |
|
87 |
return True |
|
88 |
self.write = stdout_write |
|
89 |
||
90 |
return True |
|
91 |
||
92 |
||
93 |
def write(self, text): |
|
94 |
"""Write text into relevant log file""" |
|
95 |
||
96 |
for textLine in text.splitlines(): |
|
97 |
textLine = textLine + '\n' |
|
98 |
if textLine.startswith("<?xml ") or textLine.startswith("<buildlog ") \ |
|
99 |
or textLine.startswith("</buildlog"): |
|
100 |
for stream in self.streams: |
|
101 |
stream.write(textLine) |
|
102 |
# Split 'CLEAN' output into clean file |
|
103 |
elif textLine.startswith("<clean"): |
|
104 |
if self.block != self.out: |
|
105 |
sys.stderr.write("%s : error: invalid xml. <clean> tag found " \ |
|
106 |
+ "before previous block closed %s\n" %\ |
|
107 |
(raptor.name, self.logFileName)) |
|
108 |
self.block = self.clean |
|
109 |
self.block.write(textLine) |
|
110 |
||
111 |
# Split 'WHATLOG' output into whatlog file |
|
112 |
elif textLine.startswith("<whatlog"): |
|
113 |
if self.block != self.out: |
|
114 |
sys.stderr.write("%s : error: invalid xml. <whatlog> tag " + \ |
|
115 |
"found before previous block closed\n" %\ |
|
116 |
(raptor.name, self.logFileName.GetShellPath())) |
|
117 |
self.block = self.whatlog |
|
118 |
self.block.write(textLine) |
|
119 |
||
120 |
# Split 'RECIPE' output into recipe file |
|
121 |
elif textLine.startswith("<recipe"): |
|
122 |
if self.block != self.out: |
|
123 |
sys.stderr.write("%s : error: invalid xml. <recipe> tag " + \ |
|
124 |
"found before previous block closed %s\n" %\ |
|
125 |
(raptor.name, self.logFileName.GetShellPath())) |
|
126 |
self.block = self.recipe |
|
127 |
self.block.write(textLine) |
|
128 |
||
129 |
# End of block found. Reset block to standard logfile |
|
130 |
elif textLine.startswith("</clean>") or textLine.startswith("</whatlog>") \ |
|
131 |
or textLine.startswith("</recipe>"): |
|
132 |
self.block.write(textLine) |
|
133 |
self.block = self.out |
|
134 |
||
135 |
# Everything else goes to logfile associated with current block |
|
136 |
else: |
|
137 |
self.block.write(textLine) |
|
138 |
return True |
|
139 |
||
140 |
||
141 |
def summary(self): |
|
142 |
"""Write Summary""" |
|
143 |
if self.logFileName and not self.raptor.quiet: |
|
144 |
sys.stdout.write("sbs: build log in %s\n" % self.logFileName) |
|
145 |
return True |
|
146 |
||
147 |
||
148 |
def close(self): |
|
149 |
"""Close the log file(s)""" |
|
150 |
||
151 |
try: |
|
152 |
self.out.close |
|
153 |
for stream in self.streams: |
|
154 |
stream.close() |
|
155 |
return True |
|
156 |
except: |
|
157 |
self.out = None |
|
158 |
return False |