|
1 # |
|
2 # Copyright (c) 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 # Base Class for defining filter classes |
|
16 # All filter classes that get defined should derive from this base class |
|
17 # |
|
18 |
|
19 import errno |
|
20 import filter_interface |
|
21 import os |
|
22 import raptor |
|
23 import raptor_timing |
|
24 import sys |
|
25 |
|
26 class FilterTiming(filter_interface.Filter): |
|
27 """ |
|
28 Writes a logfile containing the timings for each Raptor process |
|
29 """ |
|
30 |
|
31 def open(self, raptor_instance): |
|
32 """ |
|
33 Open a log file with the same name as the Raptor log file, with |
|
34 '.timings' appended. This will contain only 'progress' |
|
35 timing tags from the Raptor output |
|
36 Parameters: |
|
37 raptor_instance - Raptor |
|
38 Instance of Raptor. FilterList usually passes in a cut-down |
|
39 version of Raptor containing only a few attributes |
|
40 """ |
|
41 self.raptor = raptor_instance |
|
42 self.logFileName = self.raptor.logFileName |
|
43 # insert the time into the log file name |
|
44 if self.logFileName: |
|
45 self.path = (self.logFileName.path.replace("%TIME", |
|
46 self.raptor.timestring) + ".timings") |
|
47 |
|
48 try: |
|
49 dirname = str(self.raptor.logFileName.Dir()) |
|
50 if dirname and not os.path.isdir(dirname): |
|
51 os.makedirs(dirname) |
|
52 except os.error, e: |
|
53 if e.errno != errno.EEXIST: |
|
54 sys.stderr.write("%s : error: cannot create directory " + |
|
55 "%s\n" % (raptor.name, dirname)) |
|
56 return False |
|
57 try: |
|
58 self.out = open(str(self.path), "w") |
|
59 except: |
|
60 self.out = None |
|
61 sys.stderr.write("%s : error: cannot write log %s\n" %\ |
|
62 (raptor.name, self.path)) |
|
63 return False |
|
64 self.start_times = {} |
|
65 self.all_durations = [] |
|
66 self.namespace_written = False |
|
67 self.open_written = False |
|
68 return True |
|
69 |
|
70 |
|
71 def write(self, text): |
|
72 """ |
|
73 Write out any tags with a 'progress_' tagName |
|
74 """ |
|
75 if "<progress:discovery " in text: |
|
76 self.out.write(text) |
|
77 elif "<progress:start " in text: |
|
78 attributes = raptor_timing.Timing.extract_values(source = text) |
|
79 self.start_times[(attributes["object_type"] + attributes["task"] + |
|
80 attributes["key"])] = attributes["time"] |
|
81 elif "<progress:end " in text: |
|
82 attributes = raptor_timing.Timing.extract_values(source = text) |
|
83 duration = (float(attributes["time"]) - |
|
84 float(self.start_times[(attributes["object_type"] + |
|
85 attributes["task"] + attributes["key"])])) |
|
86 self.out.write(raptor_timing.Timing.custom_string(tag = "duration", |
|
87 object_type = attributes["object_type"], |
|
88 task = attributes["task"], key = attributes["key"], |
|
89 time = duration)) |
|
90 self.all_durations.append(duration) |
|
91 elif text.startswith("<?xml ") and not self.namespace_written: |
|
92 self.out.write(text) |
|
93 self.namespace_written = True |
|
94 elif text.startswith("<buildlog ") and not self.open_written: |
|
95 self.out.write(text) |
|
96 self.open_written = True |
|
97 return True |
|
98 |
|
99 |
|
100 def summary(self): |
|
101 """ |
|
102 Print out extra timing info |
|
103 """ |
|
104 total_time = 0.0 |
|
105 for duration in self.all_durations: |
|
106 total_time += duration |
|
107 self.out.write(raptor_timing.Timing.custom_string(tag = "duration", |
|
108 object_type = "all", task = "all", key = "all", |
|
109 time = total_time) + "</buildlog>\n") |
|
110 |
|
111 |
|
112 def close(self): |
|
113 """ |
|
114 Close the logfile |
|
115 """ |
|
116 try: |
|
117 self.out.close |
|
118 return True |
|
119 except: |
|
120 self.out = None |
|
121 return False |