BmapCopy.py: fix a bug when copying without bmap
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Wed, 21 Nov 2012 14:38:21 +0000 (16:38 +0200)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Wed, 21 Nov 2012 14:40:56 +0000 (16:40 +0200)
Fix a regression introduced in version 0.5: when copying without bmap the
script fails with the following error:

AttributeError: BmapCopy instance has no attribute '_f_bmap'

This is because we never define this attribute when there is no bmap, but still
use it. The fix is:

1. Define all attributes in the __init__ constructor.
2. Always check if _f_bmap is not None before using it.

While on it, do a minor space clean-up.

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

index ce7df6e..ede64ad 100644 (file)
@@ -247,11 +247,19 @@ class BmapCopy:
         self._f_image_needs_close = False
         self._f_bmap_needs_close = False
 
+        self._f_dest = None
+        self._f_image = None
+        self._f_bmap = None
+
+        self._dest_path  = None
+        self._image_path = None
+        self._bmap_path = None
+
         if hasattr(dest, "write"):
-            self._f_dest  = dest
-            self._dest_path  = dest.name
+            self._f_dest = dest
+            self._dest_path = dest.name
         else:
-            self._dest_path  = dest
+            self._dest_path = dest
             self._open_destination_file()
 
         if hasattr(image, "read"):
@@ -428,7 +436,8 @@ class BmapCopy:
         # Save file positions in order to restore them at the end
         image_pos = self._f_image.tell()
         dest_pos = self._f_dest.tell()
-        bmap_pos = self._f_bmap.tell()
+        if self._f_bmap:
+            bmap_pos = self._f_bmap.tell()
 
         # Create the queue for block batches and start the reader thread, which
         # will read the image in batches and put the results to '_batch_queue'.
@@ -495,7 +504,8 @@ class BmapCopy:
         # Restore file positions
         self._f_image.seek(image_pos)
         self._f_dest.seek(dest_pos)
-        self._f_bmap.seek(bmap_pos)
+        if self._f_bmap:
+            self._f_bmap.seek(bmap_pos)
 
     def sync(self):
         """ Synchronize the destination file to make sure all the data are