|
1 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 # All rights reserved. |
|
3 # This component and the accompanying materials are made available |
|
4 # under the terms of the License "Eclipse Public License v1.0" |
|
5 # which accompanies this distribution, and is available |
|
6 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 # |
|
8 # Initial Contributors: |
|
9 # Nokia Corporation - initial contribution. |
|
10 # |
|
11 # Contributors: |
|
12 # |
|
13 # Description: |
|
14 # Compress the full Raptor log file using the BZip2 algorithm, maximum compression. |
|
15 # |
|
16 # |
|
17 |
|
18 import os |
|
19 import sys |
|
20 import raptor |
|
21 import filter_interface |
|
22 import bz2 |
|
23 |
|
24 class StringListCompressor(object): |
|
25 def __init__(self, complevel=5, filename="file.log.bz2"): |
|
26 self.compressor = bz2.BZ2Compressor(complevel) |
|
27 self.stringlist = [] |
|
28 self.outputopenedok = False |
|
29 self.filename = filename |
|
30 try: |
|
31 self.fh = open(self.filename, "wb") |
|
32 self.outputopenedok = True |
|
33 except: |
|
34 self.outputopenedok = False |
|
35 |
|
36 def write(self, data): |
|
37 if self.outputopenedok: |
|
38 compresseddata = self.compressor.compress(data) |
|
39 self.fh.write(compresseddata) |
|
40 |
|
41 def __del__(self): |
|
42 if self.outputopenedok: |
|
43 compresseddata = self.compressor.flush() |
|
44 self.fh.write(compresseddata) |
|
45 self.fh.close() |
|
46 |
|
47 class Bz2log(filter_interface.Filter): |
|
48 def __init__(self): |
|
49 self.__inRecipe = False |
|
50 self.compressor = None |
|
51 |
|
52 def open(self, raptor_instance): |
|
53 """Open a log file for the various I/O methods to write to.""" |
|
54 |
|
55 if raptor_instance.logFileName == None: |
|
56 self.out = sys.stdout # Default to stdout if no log file is given |
|
57 else: |
|
58 logname = str(raptor_instance.logFileName.path.replace("%TIME", raptor_instance.timestring)) |
|
59 |
|
60 # Ensure that filename has the right extension; append ".bz2" if required |
|
61 if not logname.lower().endswith(".bz2"): |
|
62 logname += ".bz2" |
|
63 |
|
64 try: |
|
65 dirname = str(raptor_instance.logFileName.Dir()) |
|
66 if dirname and not os.path.isdir(dirname): |
|
67 os.makedirs(dirname) |
|
68 except: |
|
69 self.formatError("cannot create directory %s", dirname) |
|
70 return False |
|
71 |
|
72 # Use highest compression level 9 which corresponds to a 900KB dictionary |
|
73 self.compressor = StringListCompressor(9, logname) |
|
74 if not self.compressor.outputopenedok: |
|
75 self.out = None |
|
76 self.formatError("failed to initialise compression routines." ) |
|
77 return False |
|
78 return True |
|
79 |
|
80 def write(self, data): |
|
81 """Write data compressed log""" |
|
82 if self.compressor: |
|
83 self.compressor.write(data) |
|
84 return True |
|
85 |
|
86 def close(self): |
|
87 """Close the log file""" |
|
88 return True |