BmapHelpers.py: introduce a 'get_block_size()' function
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Mon, 19 Nov 2012 15:02:40 +0000 (17:02 +0200)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Tue, 20 Nov 2012 14:55:16 +0000 (16:55 +0200)
Move the piece of code which finds out block size from 'BmapCreate.py'
to 'BmapHelpers.py' - we need this code in tests as well.

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

index 127036e..5ccc203 100644 (file)
@@ -31,7 +31,7 @@ import hashlib
 from fcntl import ioctl
 import struct
 from itertools import groupby
-from bmaptools.BmapHelpers import human_size
+from bmaptools.BmapHelpers import human_size, get_block_size
 import array
 
 # The bmap format version we generate
@@ -133,11 +133,8 @@ class BmapCreate:
             raise Error("cannot generate bmap for zero-sized image file '%s'" \
                         % image_path, err.errno)
 
-        # Get the block size of the host file-system for the image file by
-        # calling the FIGETBSZ ioctl (number 2).
         try:
-            binary_data = ioctl(self._f_image, 2, struct.pack('I', 0))
-            self.bmap_block_size = struct.unpack('I', binary_data)[0]
+            self.bmap_block_size = get_block_size(self._f_image)
         except IOError as err:
             raise Error("cannot get block size for '%s': %s" \
                         % (image_path, err), err.errno)
index 4d688fe..23204fb 100644 (file)
@@ -1,6 +1,5 @@
 """
-This module contains various helper functions which are shared between
-BmapFlash and BmapCreate or which are useful for users of bmaptools.
+This module contains various shared helper functions.
 """
 
 def human_size(size):
@@ -32,3 +31,15 @@ def human_time(seconds):
         result += "%dm " % minutes
 
     return result + "%.1fs" % seconds
+
+def get_block_size(file_obj):
+    """ Returns block size for file object 'file_obj'. Errors are indicated by
+    the 'IOError' exception. """
+
+    from fcntl import ioctl
+    import struct
+
+    # Get the block size of the host file-system for the image file by calling
+    # the FIGETBSZ ioctl (number 2).
+    binary_data = ioctl(file_obj, 2, struct.pack('I', 0))
+    return struct.unpack('I', binary_data)[0]