bmaptool: do not feed stdout to BmapCreate
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Fri, 3 May 2013 13:48:40 +0000 (16:48 +0300)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Mon, 6 May 2013 14:58:51 +0000 (17:58 +0300)
We are going to modify BmapCreate to support bmap file checksum. And we'll have
to seek the bmap file. However, the problem is that bmaptool may feed
BmapCreate with stdout which is not seekable.

This patch changes bmaptool and makes it create a temporary file when the user
does not specify the output file, give it to BmapCreate, and then print the
contents of the file to stdout. We use the 'NamedTemporaryFile' python method
which automatically removes the file when the program crashes or is interrupted
with Ctrl-C.

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

index 395c3d5eabc93f5e921037b00a670ee255740820..3b5907303375bde4fa3f176818f279f4d5d111b1 100755 (executable)
--- a/bmaptool
+++ b/bmaptool
@@ -44,6 +44,7 @@ import os
 import stat
 import time
 import logging
+import tempfile
 import traceback
 from bmaptools import BmapCreate, BmapCopy, BmapHelpers, TransRead
 
@@ -265,7 +266,12 @@ def create_command(args, log):
                       % (args.output, err))
             raise SystemExit(1)
     else:
-        output = sys.stdout
+        try:
+            # Create a temporary file for the bmap
+            output = tempfile.TemporaryFile("w+")
+        except IOError as err:
+            log.error("cannot create a temporary file: %s" % err)
+            raise SystemExit(1)
 
     try:
         creator = BmapCreate.BmapCreate(args.image, output)
@@ -274,6 +280,10 @@ def create_command(args, log):
         log.error(str(err))
         raise SystemExit(1)
 
+    if not args.output:
+        output.seek(0)
+        sys.stdout.write(output.read())
+
     if creator.mapped_cnt == creator.blocks_cnt:
         log.warning("all %s are mapped, no holes in '%s'" \
                     % (creator.image_size_human, args.image))