BmapCopy: have a Logger object
authorSimon McVittie <smcv@debian.org>
Tue, 25 Jun 2013 11:54:45 +0000 (12:54 +0100)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Wed, 3 Jul 2013 13:15:44 +0000 (16:15 +0300)
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 <artem.bityutskiy@intel.com>
bmaptool
bmaptools/BmapCopy.py

index 978bd15..24356b9 100755 (executable)
--- 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)
index 5c45b86..211ce17 100644 (file)
@@ -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