|
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 # Squash a raptor log file by removing commands from successful recipes |
|
16 # |
|
17 |
|
18 import os |
|
19 import sys |
|
20 import raptor |
|
21 import filter_interface |
|
22 |
|
23 class FilterSquashlog(filter_interface.Filter): |
|
24 |
|
25 def __init__(self): |
|
26 self.__inRecipe = False |
|
27 |
|
28 def open(self, raptor_instance): |
|
29 """Open a log file for the various I/O methods to write to.""" |
|
30 |
|
31 if raptor_instance.logFileName == None: |
|
32 self.out = sys.stdout |
|
33 else: |
|
34 try: |
|
35 dirname = str(raptor_instance.logFileName.Dir()) |
|
36 if dirname and not os.path.isdir(dirname): |
|
37 os.makedirs(dirname) |
|
38 except: |
|
39 sys.stderr.write(str(raptor.name) + \ |
|
40 ": error: cannot create directory %s\n", dirname) |
|
41 return False |
|
42 |
|
43 try: |
|
44 logname = str(raptor_instance.logFileName) |
|
45 self.out = open(logname, "w") |
|
46 except: |
|
47 self.out = None |
|
48 sys.stderr.write(str(raptor.name) + \ |
|
49 ": error: cannot write log %s\n", \ |
|
50 str(raptor_instance.logFileName)) |
|
51 return False |
|
52 |
|
53 return True |
|
54 |
|
55 def write(self, line): |
|
56 """Write text into a squashed log file by removing commands from successful recipes""" |
|
57 |
|
58 # escape % characters otherwise print will fail |
|
59 line = line.replace("%", "%%") |
|
60 |
|
61 # detect the start of a recipe |
|
62 if line.startswith("<recipe "): |
|
63 self.__inRecipe = True |
|
64 self.__recipeLines = [line] |
|
65 self.__squashRecipe = True |
|
66 return |
|
67 |
|
68 # detect the status report from a recipe |
|
69 if line.startswith("<status "): |
|
70 if not "exit='ok'" in line: |
|
71 # only squash ok recipes |
|
72 self.__squashRecipe = False |
|
73 self.__recipeLines.append(line) |
|
74 return |
|
75 |
|
76 # detect the end of a recipe |
|
77 if line.startswith("</recipe>"): |
|
78 # print the recipe |
|
79 if self.__squashRecipe: |
|
80 for text in self.__recipeLines: |
|
81 if not text.startswith("+"): |
|
82 self.out.write(text) |
|
83 else: |
|
84 for text in self.__recipeLines: |
|
85 self.out.write(text) |
|
86 |
|
87 self.out.write(line) |
|
88 self.__inRecipe = False |
|
89 return |
|
90 |
|
91 # remember the lines during a recipe |
|
92 if self.__inRecipe: |
|
93 self.__recipeLines.append(line) |
|
94 else: |
|
95 # print all lines outside a recipe |
|
96 self.out.write(line) |
|
97 |
|
98 return True |
|
99 |
|
100 def close(self): |
|
101 """Close the log file""" |
|
102 |
|
103 try: |
|
104 self.out.close() |
|
105 return True |
|
106 except: |
|
107 self.out = None |
|
108 return False |