author | timothy.murphy@nokia.com |
Wed, 28 Apr 2010 01:01:42 +0100 | |
branch | fix |
changeset 534 | 3b10c85868b1 |
parent 533 | 408bfff46ad7 |
child 547 | 9fe7d0ab0f8f |
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 |
# Class to control array of defined logging filters |
|
16 |
# This class will act as a switch, controlling the list of filters iteratively |
|
17 |
# |
|
18 |
||
19 |
import os |
|
20 |
import sys |
|
21 |
import raptor |
|
22 |
import filter_interface |
|
23 |
import pluginbox |
|
24 |
import traceback |
|
25 |
||
26 |
class FilterWriteException(Exception): |
|
27 |
def __init__(self, value): |
|
28 |
self.parameter = value |
|
29 |
def __str__(self): |
|
30 |
return repr(self.parameter) |
|
31 |
||
32 |
class BootstrapFilter(filter_interface.Filter): |
|
33 |
def __init__(self): |
|
34 |
self.out = sys.stdout |
|
35 |
||
36 |
"""Use this until the CLI has supplied some real filters""" |
|
37 |
def open(self, raptor_instance): |
|
38 |
"""Set output to stdout for the various I/O methods to write to.""" |
|
39 |
self.raptor = raptor_instance |
|
40 |
return True |
|
41 |
||
42 |
def write(self, text): |
|
43 |
"""Write errors and warnings to stdout""" |
|
44 |
||
45 |
if text.startswith("<error>"): |
|
46 |
start = text.find(">") |
|
47 |
end = text.rfind("<") |
|
48 |
self.out.write(str(raptor.name) + ": error: %s\n" \ |
|
49 |
% text[(start + 1):end]) |
|
50 |
elif text.startswith("<warning"): |
|
51 |
start = text.find(">") |
|
52 |
end = text.rfind("<") |
|
53 |
self.out.write(str(raptor.name) + ": warning: %s\n" \ |
|
54 |
% text[(start + 1):end]) |
|
55 |
elif "error" in text or "warning" in text: |
|
56 |
self.out.write(text) |
|
57 |
return True |
|
58 |
||
59 |
def summary(self): |
|
60 |
"""Write Summary""" |
|
61 |
return False |
|
62 |
||
63 |
def close(self): |
|
64 |
"""Nothing to do for stdout""" |
|
65 |
return True |
|
66 |
||
67 |
||
68 |
||
69 |
class FilterList(filter_interface.Filter): |
|
70 |
||
71 |
def __init__(self): |
|
72 |
self.out = [BootstrapFilter()] |
|
73 |
self.filters = [] |
|
74 |
self.pbox = None |
|
75 |
||
76 |
def open(self, raptor_instance, filternames, pbox): |
|
77 |
""" |
|
78 |
Call open function on each filter using raptor parameters provided |
|
79 |
Returns: Boolean: Have the functions succeeded in opening the files? |
|
80 |
""" |
|
81 |
# Find all the filter plugins |
|
82 |
self.pbox = pbox |
|
83 |
possiblefilters = self.pbox.classesof(filter_interface.Filter) |
|
534
3b10c85868b1
fix resource deps: generate more cleanly - no targets if there are no prereqs. no empty lines.
timothy.murphy@nokia.com
parents:
533
diff
changeset
|
84 |
|
533
408bfff46ad7
fix performance: copy resources in the frontend. Helps cluster builds since remote copying is inefficient.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
85 |
filterdict = {} |
408bfff46ad7
fix performance: copy resources in the frontend. Helps cluster builds since remote copying is inefficient.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
86 |
for p in possiblefilters: |
408bfff46ad7
fix performance: copy resources in the frontend. Helps cluster builds since remote copying is inefficient.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
87 |
name = p.__name__.lower() |
408bfff46ad7
fix performance: copy resources in the frontend. Helps cluster builds since remote copying is inefficient.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
88 |
if name in filterdict: |
408bfff46ad7
fix performance: copy resources in the frontend. Helps cluster builds since remote copying is inefficient.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
89 |
raise ValueError("filters found in SBS_HOME/python/plugins which have duplicate name: %s " % p.__name__) |
408bfff46ad7
fix performance: copy resources in the frontend. Helps cluster builds since remote copying is inefficient.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
90 |
else: |
408bfff46ad7
fix performance: copy resources in the frontend. Helps cluster builds since remote copying is inefficient.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
91 |
filterdict[name] = p |
408bfff46ad7
fix performance: copy resources in the frontend. Helps cluster builds since remote copying is inefficient.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
92 |
|
3 | 93 |
unfound = [] |
94 |
self.filters = [] |
|
95 |
for f in filternames: |
|
533
408bfff46ad7
fix performance: copy resources in the frontend. Helps cluster builds since remote copying is inefficient.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
96 |
found = False |
408bfff46ad7
fix performance: copy resources in the frontend. Helps cluster builds since remote copying is inefficient.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
97 |
if f.lower() in filterdict: |
534
3b10c85868b1
fix resource deps: generate more cleanly - no targets if there are no prereqs. no empty lines.
timothy.murphy@nokia.com
parents:
533
diff
changeset
|
98 |
self.filters.append(filterdict[f.lower()]()) |
533
408bfff46ad7
fix performance: copy resources in the frontend. Helps cluster builds since remote copying is inefficient.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
99 |
else: |
408bfff46ad7
fix performance: copy resources in the frontend. Helps cluster builds since remote copying is inefficient.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
100 |
unfound.append(f) |
408bfff46ad7
fix performance: copy resources in the frontend. Helps cluster builds since remote copying is inefficient.
timothy.murphy@nokia.com
parents:
3
diff
changeset
|
101 |
|
3 | 102 |
if unfound != []: |
103 |
raise ValueError("requested filters not found: %s \ |
|
104 |
\nAvailable filters are: %s" % (str(unfound), self.format_output_list(possiblefilters))) |
|
105 |
||
106 |
if self.filters == []: |
|
107 |
self.out = [BootstrapFilter()] |
|
108 |
else: |
|
109 |
self.out=[] |
|
110 |
for filter in self.filters: |
|
111 |
if filter.open(raptor_instance): |
|
112 |
self.out.append(filter) |
|
113 |
else: |
|
114 |
sys.stderr.write(str(raptor.name) + \ |
|
115 |
": error: Cannot open filter: %s\n" % str(filter)) |
|
116 |
ok = False |
|
117 |
||
118 |
if self.out == []: |
|
119 |
sys.stderr.write(str(raptor.name) + \ |
|
120 |
": warning: All filters failed to open. " + \ |
|
121 |
"Defaulting to 'stdout'\n") |
|
122 |
self.out = [BootstrapFilter()] |
|
123 |
||
124 |
def write(self, text): |
|
125 |
""" |
|
126 |
Iterate through each filter, calling their write function |
|
127 |
""" |
|
128 |
||
129 |
if text is None: |
|
130 |
return |
|
131 |
||
132 |
badfilters = [] |
|
133 |
for filter in self.out: |
|
134 |
try: |
|
135 |
filter.write(text) |
|
136 |
except Exception,e: |
|
137 |
traceback.print_exc(file=sys.stdout) |
|
138 |
sys.stdout.write("Called from: \n") |
|
139 |
traceback.print_stack(file=sys.stdout) |
|
140 |
sys.stdout.write("\n") |
|
141 |
badfilters.append(filter) |
|
142 |
||
143 |
if len(badfilters) > 0: |
|
144 |
for f in badfilters: |
|
145 |
self.out.remove(f) # dump the filter in case it causes repeated exceptions |
|
146 |
sys.stdout.write("Removed filter %s because it generated an exception\n" % type(f)) |
|
147 |
||
148 |
if len(self.out) == 0: |
|
149 |
sys.stdout.write("Falling back to bootstrap filter\n") |
|
150 |
self.out = [BootstrapFilter()] # Try to fall back to something in the worst case |
|
151 |
||
152 |
def summary(self): |
|
153 |
""" |
|
154 |
Run the summaries of all filters (prior to log end) |
|
155 |
""" |
|
156 |
for filter in self.out: |
|
157 |
filter.summary() |
|
158 |
||
159 |
def close(self): |
|
160 |
""" |
|
161 |
Iterate through each filter, calling their close function |
|
162 |
Returns True if all the filters close properly |
|
163 |
""" |
|
164 |
returnVal = True |
|
165 |
||
166 |
for filter in self.out: |
|
167 |
if (filter != sys.stdout) and (filter != sys.stderr): |
|
168 |
returnVal = returnVal and filter.close() |
|
169 |
||
170 |
return returnVal |
|
171 |
||
172 |
def format_output_list(self, possiblefilters): |
|
173 |
""" |
|
174 |
formats available filters |
|
175 |
""" |
|
176 |
filters_formatted = "" |
|
177 |
for pl in possiblefilters: |
|
178 |
filters_formatted += "\n " + pl.__name__ |
|
179 |
return filters_formatted |
|
180 |