From b07fdd91ae86863b61405984952406fbc9951fa4 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Tue, 25 Jun 2013 12:54:45 +0100 Subject: [PATCH] BmapCopy: have a Logger object Artem: In some situations we may want inform about various happenings, see the next patch for example. So let's teach the BmapCopy class to accept a 'logger' object. Change-Id: Idcfe6d7acfb8e327be39496898415e9f4b08d0dd Signed-off-by: Artem Bityutskiy --- bmaptool | 4 ++-- bmaptools/BmapCopy.py | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/bmaptool b/bmaptool index 978bd15..24356b9 100755 --- a/bmaptool +++ b/bmaptool @@ -176,11 +176,11 @@ def copy_command(args, log): dest_str = "block device '%s'" % args.dest # For block devices, use the specialized class writer = BmapCopy.BmapBdevCopy(image_obj, dest_obj, bmap_obj, - image_size) + image_size, logger=log) else: dest_str = "file '%s'" % os.path.basename(args.dest) writer = BmapCopy.BmapCopy(image_obj, dest_obj, bmap_obj, - image_size) + image_size, logger=log) except BmapCopy.Error as err: log.error(str(err)) raise SystemExit(1) diff --git a/bmaptools/BmapCopy.py b/bmaptools/BmapCopy.py index 5c45b86..211ce17 100644 --- a/bmaptools/BmapCopy.py +++ b/bmaptools/BmapCopy.py @@ -38,6 +38,7 @@ import os import stat import sys import hashlib +import logging import Queue import thread import datetime @@ -203,7 +204,8 @@ class BmapCopy: # Bmap file checksum appeard in format 1.3 self._verify_bmap_checksum() - def __init__(self, image, dest, bmap = None, image_size = None): + def __init__(self, image, dest, bmap = None, image_size = None, + logger = None): """ The class constructor. The parameters are: image - file-like object of the image which should be copied, should only support 'read()' and 'seek()' methods, @@ -211,7 +213,13 @@ class BmapCopy: dest - file object of the destination file to copy the image to. bmap - file object of the bmap file to use for copying. - image_size - size of the image in bytes. """ + image_size - size of the image in bytes. + logger - the logger object to use for printing messages. + """ + + self._logger = logger + if self._logger is None: + self._logger = logging.getLogger(__name__) self._xml = None @@ -618,12 +626,13 @@ class BmapBdevCopy(BmapCopy): finally: self._restore_bdev_settings() - def __init__(self, image, dest, bmap = None, image_size = None): + def __init__(self, image, dest, bmap = None, image_size = None, + logger = None): """ The same as the constructor of the 'BmapCopy' base class, but adds useful guard-checks specific to block devices. """ # Call the base class constructor first - BmapCopy.__init__(self, image, dest, bmap, image_size) + BmapCopy.__init__(self, image, dest, bmap, image_size, logger=logger) self._batch_bytes = 1024 * 1024 self._batch_blocks = self._batch_bytes / self.block_size -- 2.7.4