From f6bdb3e92e2921bbe5db5b0c3f3418ceac6ef3a9 Mon Sep 17 00:00:00 2001 From: Artem Bityutskiy Date: Thu, 1 Nov 2012 09:54:32 +0200 Subject: [PATCH] Introduce BmapHelpers.py This module will contain shared helper functions. Change-Id: I7c0fe807f9587508bdfbacc22820edbcf970103d Signed-off-by: Artem Bityutskiy --- bmap-flasher | 33 +++++---------------------------- bmaptools/BmapHelpers.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 28 deletions(-) create mode 100644 bmaptools/BmapHelpers.py diff --git a/bmap-flasher b/bmap-flasher index 85dc310..d61e220 100755 --- a/bmap-flasher +++ b/bmap-flasher @@ -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 index 0000000..605d421 --- /dev/null +++ b/bmaptools/BmapHelpers.py @@ -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 -- 2.7.4