# HG changeset patch # User Richard Taylor # Date 1267712217 0 # Node ID 976aca38ffe5ce30d7753ea77a8de44e5cbd0dc8 # Parent 502501837ac41cbf3b5bb53dd13a5e132dd2f2d6 add bzip2 filter into 2.12.4-rc2 diff -r 502501837ac4 -r 976aca38ffe5 sbsv2/raptor/python/plugins/filter_bz2log.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sbsv2/raptor/python/plugins/filter_bz2log.py Thu Mar 04 14:16:57 2010 +0000 @@ -0,0 +1,88 @@ +# Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). +# All rights reserved. +# This component and the accompanying materials are made available +# under the terms of the License "Eclipse Public License v1.0" +# which accompanies this distribution, and is available +# at the URL "http://www.eclipse.org/legal/epl-v10.html". +# +# Initial Contributors: +# Nokia Corporation - initial contribution. +# +# Contributors: +# +# Description: +# Compress the full Raptor log file using the BZip2 algorithm, maximum compression. +# +# + +import os +import sys +import raptor +import filter_interface +import bz2 + +class StringListCompressor(object): + def __init__(self, complevel=5, filename="file.log.bz2"): + self.compressor = bz2.BZ2Compressor(complevel) + self.stringlist = [] + self.outputopenedok = False + self.filename = filename + try: + self.fh = open(self.filename, "wb") + self.outputopenedok = True + except: + self.outputopenedok = False + + def write(self, data): + if self.outputopenedok: + compresseddata = self.compressor.compress(data) + self.fh.write(compresseddata) + + def __del__(self): + if self.outputopenedok: + compresseddata = self.compressor.flush() + self.fh.write(compresseddata) + self.fh.close() + +class Bz2log(filter_interface.Filter): + def __init__(self): + self.__inRecipe = False + self.compressor = None + + def open(self, raptor_instance): + """Open a log file for the various I/O methods to write to.""" + + if raptor_instance.logFileName == None: + self.out = sys.stdout # Default to stdout if no log file is given + else: + logname = str(raptor_instance.logFileName.path.replace("%TIME", raptor_instance.timestring)) + + # Ensure that filename has the right extension; append ".bz2" if required + if not logname.lower().endswith(".bz2"): + logname += ".bz2" + + try: + dirname = str(raptor_instance.logFileName.Dir()) + if dirname and not os.path.isdir(dirname): + os.makedirs(dirname) + except: + self.formatError("cannot create directory %s", dirname) + return False + + # Use highest compression level 9 which corresponds to a 900KB dictionary + self.compressor = StringListCompressor(9, logname) + if not self.compressor.outputopenedok: + self.out = None + self.formatError("failed to initialise compression routines." ) + return False + return True + + def write(self, data): + """Write data compressed log""" + if self.compressor: + self.compressor.write(data) + return True + + def close(self): + """Close the log file""" + return True