BmapFlasher: provide a target_is_block_device attribute
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Tue, 6 Nov 2012 13:01:49 +0000 (15:01 +0200)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Thu, 8 Nov 2012 08:01:01 +0000 (10:01 +0200)
Which is useful for bmap-flasher, because it does not have find this out
itslelf.

Note, this is rather bad approach, and later I'll introduce a base class
for flashing anywhere and a child class for flashing to block devices.

Change-Id: I1959359f357a70fa45d09cbad7b16104023070cd
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
bmap-flasher
bmaptools/BmapFlasher.py

index 00a59ef..9acaacb 100755 (executable)
@@ -27,9 +27,7 @@ file which have to be copied to the block device.
 VERSION = "0.1.0"
 
 import argparse
-import os
 import sys
-import stat
 import time
 import logging
 from bmaptools import BmapFlasher, BmapHelpers
@@ -104,21 +102,14 @@ def main():
         log = setup_logger(logging.INFO)
 
     try:
-        is_block_device = stat.S_ISBLK(os.stat(args.bdev).st_mode)
-    except OSError as err:
-        log.error("cannot access block device '%s': %s" \
-                   % (args.bdev, err.strerror))
-        raise SystemExit(1)
-
-    if not is_block_device:
-        log.warning("'%s' is not a block device!" % args.bdev)
-
-    try:
         flasher = BmapFlasher.BmapFlasher(args.image, args.bdev, args.bmap)
     except BmapFlasher.Error as err:
         log.error(str(err))
         raise SystemExit(1)
 
+    if not flasher.target_is_block_device:
+        log.warning("'%s' is not a block device!" % args.bdev)
+
     start_time = time.time()
     if not args.bmap:
         log.info("no block map given (see the --bmap option)")
index 101f266..bd67167 100644 (file)
@@ -176,6 +176,14 @@ class BmapFlasher:
             raise Error("cannot open block device '%s' in exclusive mode: %s" \
                         % (self._bdev_path, err.strerror))
 
+        try:
+            st_mode = os.fstat(self._f_bdev).st_mode
+        except OSError as err:
+            raise Error("cannot access block device '%s': %s" \
+                        % (self._bdev_path, err.strerror))
+
+        self.target_is_block_device = stat.S_ISBLK(st_mode)
+
         # Turn the block device file descriptor into a file object
         try:
             self._f_bdev = os.fdopen(self._f_bdev, "wb")
@@ -214,6 +222,7 @@ class BmapFlasher:
         self.bmap_mapped_size = None
         self.bmap_mapped_size_human = None
         self.bmap_mapped_percent = None
+        self.target_is_block_device = None
 
         self._open_block_device()
         self._open_image_file()