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