Introduce BmapHelpers.py
authorArtem Bityutskiy <artem.bityutskiy@intel.com>
Thu, 1 Nov 2012 07:54:32 +0000 (09:54 +0200)
committerArtem Bityutskiy <artem.bityutskiy@intel.com>
Thu, 1 Nov 2012 07:56:07 +0000 (09:56 +0200)
This module will contain shared helper functions.

Change-Id: I7c0fe807f9587508bdfbacc22820edbcf970103d
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@intel.com>
bmap-flasher
bmaptools/BmapHelpers.py [new file with mode: 0644]

index 85dc310..d61e220 100755 (executable)
@@ -32,7 +32,7 @@ import sys
 import stat
 import time
 import logging
-from bmaptools import BmapFlasher
+from bmaptools import BmapFlasher, BmapHelpers
 
 def parse_arguments():
     """ A helper function which parses the input arguments. """
@@ -73,30 +73,6 @@ def parse_arguments():
 
     return parser.parse_args()
 
-def human_size(size):
-    """ Transform size in bytes into a human-readable form. """
-
-    for modifier in ["KiB", "MiB", "GiB", "TiB"]:
-        size /= 1024.0
-        if size < 1024:
-            return "%.1f %s" % (size, modifier)
-
-    return "%.1f %s" % (size, 'EiB')
-
-def human_time(seconds):
-    """ Transform time in seconds to the HH:MM:SS format. """
-
-    (minutes, seconds) = divmod(seconds, 60)
-    (hours, minutes) = divmod(minutes, 60)
-
-    result = ""
-    if hours:
-        result = "%dh " % hours
-    if minutes:
-        result += "%dm " % minutes
-
-    return result + "%.1fs" % seconds
-
 def setup_logger(loglevel):
     """ A helper function which sets up and configures the logger. The log
         level is initialized to 'loglevel'. Returns the logger object. """
@@ -151,9 +127,9 @@ def main():
         log.info("block map format version %s" % flasher.bmap_version)
         log.info("%d blocks of size %d (%s), mapped %d blocks (%s or %.1f%%)" \
                  % (flasher.bmap_blocks_cnt, flasher.bmap_block_size,
-                    human_size(flasher.bmap_total_size),
+                    BmapHelpers.human_size(flasher.bmap_total_size),
                     flasher.bmap_mapped_cnt,
-                    human_size(flasher.bmap_mapped_size),
+                    BmapHelpers.human_size(flasher.bmap_mapped_size),
                     flasher.bmap_mapped_percent))
         log.info("writing the image to '%s' using bmap file '%s'" \
                  % (args.bdev, args.bmap))
@@ -176,7 +152,8 @@ def main():
     flashing_time = time.time() - start_time
     flashing_speed = flasher.bmap_total_size / flashing_time
     log.info("flashing time: %s, flashing speed %s/sec" \
-             % (human_time(flashing_time), human_size(flashing_speed)))
+             % (BmapHelpers.human_time(flashing_time), \
+                BmapHelpers.human_size(flashing_speed)))
 
 if __name__ == "__main__":
     sys.exit(main())
diff --git a/bmaptools/BmapHelpers.py b/bmaptools/BmapHelpers.py
new file mode 100644 (file)
index 0000000..605d421
--- /dev/null
@@ -0,0 +1,28 @@
+"""
+This module contains various helper functions which are shared between
+BmapFlasher and BmapCreator or which are useful for users of bmaptools.
+"""
+
+def human_size(size):
+    """ Transform size in bytes into a human-readable form. """
+
+    for modifier in ["KiB", "MiB", "GiB", "TiB"]:
+        size /= 1024.0
+        if size < 1024:
+            return "%.1f %s" % (size, modifier)
+
+    return "%.1f %s" % (size, 'EiB')
+
+def human_time(seconds):
+    """ Transform time in seconds to the HH:MM:SS format. """
+
+    (minutes, seconds) = divmod(seconds, 60)
+    (hours, minutes) = divmod(minutes, 60)
+
+    result = ""
+    if hours:
+        result = "%dh " % hours
+    if minutes:
+        result += "%dm " % minutes
+
+    return result + "%.1fs" % seconds