BmapCreate.py: stop using logger for the bmap output
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Tue, 20 Nov 2012 11:19:33 +0000 (13:19 +0200)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Tue, 20 Nov 2012 14:55:20 +0000 (16:55 +0200)
It is an overkill to use a logger object to output the bmap, and it is also
rather difficult for the users. Instead, make 'BmapCreate' accept a file-like
object for the output, not a logger object.

Change-Id: I7cf83dc8e0c43f762449e7fcf200b1f8b66359c5
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
bmaptool
bmaptools/BmapCreate.py

index 040d136..6c6cf8d 100755 (executable)
--- a/bmaptool
+++ b/bmaptool
@@ -141,13 +141,15 @@ def create_command(args, log):
     target device. """
 
     # Create and setup the output stream
-    output = logging.getLogger('bmap-create-output')
-    output.setLevel(logging.INFO)
     if args.output:
-        where = logging.FileHandler(args.output)
+        try:
+            output = open(args.output, "w+")
+        except IOError as err:
+            log.error("cannot open the output file '%s': %s" \
+                      % (args.output, err))
+            raise SystemExit(1)
     else:
-        where = logging.StreamHandler(sys.stdout)
-    output.addHandler(where)
+        output = sys.stdout
 
     try:
         creator = BmapCreate.BmapCreate(args.image, output)
index 44caf57..a8151eb 100644 (file)
@@ -74,7 +74,8 @@ _BMAP_START_TEMPLATE = \
     <!-- The block map which consists of elements which may either be a
          range of blocks or a single block. The 'sha1' attribute (if present)
          is the SHA1 checksum of this blocks range. -->
-    <BlockMap>"""
+    <BlockMap>
+"""
 
 class Error(Exception):
     """ A class for exceptions generated by the 'BmapCreate' module. We
@@ -95,7 +96,7 @@ class BmapCreate:
     create an instance of 'BmapCreate' and provide:
 
     * full path to the image to create bmap for
-    * a logger object to output the generated bmap to
+    * file-like object to write the output to
 
     Then you should invoke the 'generate()' method of this class. It will use
     the FIEMAP ioctl to generate the bmap, and fall-back to the FIBMAP ioctl if
@@ -104,7 +105,7 @@ class BmapCreate:
     def __init__(self, image_path, output):
         """ Initialize a class instance:
         * image_path - full path to the image file to generate bmap for
-        * output - a logger object to write the generated bmap to """
+        * output - file-like object to write the generated bmap to """
 
         self._image_path = image_path
         self._output = output
@@ -178,7 +179,7 @@ class BmapCreate:
                   self.bmap_image_size_human, self.bmap_image_size,
                   self.bmap_block_size, self.bmap_blocks_cnt)
 
-        self._output.info(xml)
+        self._output.write(xml)
 
     def _is_mapped_fibmap(self, block):
         """ A helper function which returns 'True' if block number 'block' of
@@ -272,9 +273,9 @@ class BmapCreate:
                % (self.bmap_mapped_size_human, self.bmap_mapped_percent)
         xml += "    <MappedBlocksCount> %u </MappedBlocksCount>\n" \
                % self.bmap_mapped_cnt
-        xml += "</bmap>"
+        xml += "</bmap>\n"
 
-        self._output.info(xml)
+        self._output.write(xml)
 
     def _calculate_sha1(self, first, last):
         """ A helper function which calculates SHA1 checksum for the range of
@@ -329,18 +330,21 @@ class BmapCreate:
                 sha1 = ""
 
             if first != last:
-                self._output.info("        <Range%s> %s-%s </Range>" \
-                                  % (sha1, first, last))
+                self._output.write("        <Range%s> %s-%s </Range>\n" \
+                                   % (sha1, first, last))
             else:
-                self._output.info("        <Range%s> %s </Range>" \
-                                  % (sha1, first))
+                self._output.write("        <Range%s> %s </Range>\n" \
+                                   % (sha1, first))
 
         self.bmap_mapped_size = self.bmap_mapped_cnt * self.bmap_block_size
         self.bmap_mapped_size_human = human_size(self.bmap_mapped_size)
         self.bmap_mapped_percent = self.bmap_mapped_cnt * 100.0
         self.bmap_mapped_percent /= self.bmap_blocks_cnt
+
         self._bmap_file_end()
 
+        self._output.flush()
+
     def __del__(self):
         """ The class destructor which closes the opened files. """