bmaptool: override bmap path
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Wed, 18 Sep 2013 06:59:23 +0000 (09:59 +0300)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Thu, 19 Sep 2013 10:28:47 +0000 (13:28 +0300)
Sometimes we create temporary files for the bmap file object, and the 'name'
attribute of the object contains the temporary file path in these cases. Then
we pass the bmap file object to, say, BmapCreate module, which uses the 'name'
attribute in various error messages. This, for example, leads to the following
confusing error message:

bmaptool: ERROR: cannot parse the bmap file '/tmp/tmpUkoTxA' which should be a proper XML file

It is a lot nicer to print the original path instead of the temporary path.
Let's do this with help of the 'NamedFile' class that we have. It helps
substituting the 'name' attribute with something we want the user to see.

Change-Id: I0503a1014d159b3bc6da8e00b16a4d194c2f68f3
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
bmaptool

index b0eb409b31a454a7c5c7c6538d4f94f26b148640..6b85cae2b59837fbfdcaa94949f78cf8ac1ecc0b 100755 (executable)
--- a/bmaptool
+++ b/bmaptool
@@ -60,6 +60,22 @@ import shutil
 import io
 from bmaptools import BmapCreate, BmapCopy, BmapHelpers, TransRead
 
+class NamedFile:
+    """
+    This simple class allows us to override the 'name' attribute of a file
+    object. The reason is that some classes use the 'name' attribute of the
+    file object to print file path. But, for example, 'os.fdopen()' sets the
+    name to "<fdopen>", which is not very user-friendly. Also, sometimes we
+    want to substitute the file name with something else.
+    """
+
+    def __init__(self, file_obj, name):
+        self._file_obj = file_obj
+        self.name = name
+
+    def __getattr__(self, name):
+        return getattr(self._file_obj, name)
+
 def open_block_device(path, log):
     """
     This is a helper function for 'open_files()' which is called if the
@@ -74,20 +90,6 @@ def open_block_device(path, log):
     Returns opened file object.
     """
 
-    class NamedFile:
-        """
-        This simple class allows us to override the 'name' attribute of a file
-        object. The problem is that 'os.fdopen()' sets the name to "<fdopen>",
-        which is not very user-friendly.
-        """
-
-        def __init__(self, file_obj, name):
-            self._file_obj = file_obj
-            self.name = name
-
-        def __getattr__(self, name):
-            return getattr(self._file_obj, name)
-
     try:
         descriptor = os.open(path, os.O_WRONLY | os.O_EXCL)
     except OSError as err:
@@ -427,11 +429,13 @@ def copy_command(args, log):
         if dest_is_blkdev:
             dest_str = "block device '%s'" % args.dest
             # For block devices, use the specialized class
-            writer = BmapCopy.BmapBdevCopy(image_obj, dest_obj, bmap_obj,
+            writer = BmapCopy.BmapBdevCopy(image_obj, dest_obj, args.dest,
+                                           NamedFile(bmap_obj, bmap_path),
                                            image_size, logger=log)
         else:
             dest_str = "file '%s'" % os.path.basename(args.dest)
-            writer = BmapCopy.BmapCopy(image_obj, dest_obj, bmap_obj,
+            writer = BmapCopy.BmapCopy(image_obj, dest_obj,
+                                       NamedFile(bmap_obj, bmap_path),
                                        image_size, logger=log)
     except BmapCopy.Error as err:
         log.error(str(err))