From 96171db1ec35f7e5f600563a55e1c36d58edcdf8 Mon Sep 17 00:00:00 2001 From: Nikos Skalkotos Date: Mon, 11 Jun 2012 12:02:04 +0300 Subject: [PATCH] Add size param in the Output.Progress constructor --- image_creator/disk.py | 10 ++++------ image_creator/output.py | 9 +++++++-- image_creator/util.py | 9 +++++---- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/image_creator/disk.py b/image_creator/disk.py index e404a06..38df2fd 100644 --- a/image_creator/disk.py +++ b/image_creator/disk.py @@ -195,9 +195,8 @@ class DiskDevice(object): def enable(self): """Enable a newly created DiskDevice""" - self.progressbar = self.out.Progress("Launching helper VM", "percent") - self.progressbar.max = 100 - self.progressbar.goto(1) + self.progressbar = self.out.Progress(100, "Launching helper VM", + "percent") eh = self.g.set_event_callback(self.progress_callback, guestfs.EVENT_PROGRESS) self.g.launch() @@ -419,9 +418,8 @@ class DiskDevice(object): MB = 2 ** 20 blocksize = 4 * MB # 4MB size = self.meta['SIZE'] - progress_size = (size + MB - 1) // MB # in MB - progressbar = self.out.Progress("Dumping image file", 'mb') - progressbar.max = progress_size + progr_size = (size + MB - 1) // MB # in MB + progressbar = self.out.Progress(progr_size, "Dumping image file", 'mb') with open(self.real_device, 'r') as src: with open(outfile, "w") as dst: diff --git a/image_creator/output.py b/image_creator/output.py index 4eabc06..040029c 100644 --- a/image_creator/output.py +++ b/image_creator/output.py @@ -91,8 +91,9 @@ class Output(object): Progress = property(_get_progress) class _Progress(object): - def __init__(self, title, bar_type='default'): + def __init__(self, size, title, bar_type='default'): self.output.output("%s..." % title, False) + self.size = size def goto(self, dest): pass @@ -140,7 +141,7 @@ class Output_wth_progress(Output_wth_colors): 'mb': '%(index)d/%(max)d MB' } - def __init__(self, title, bar_type='default'): + def __init__(self, size, title, bar_type='default'): super(Output_wth_progress._Progress, self).__init__() self.title = title self.fill = '#' @@ -148,6 +149,10 @@ class Output_wth_progress(Output_wth_colors): self.bar_suffix = '] ' self.message = ("%s:" % self.title).ljust(self.MESSAGE_LENGTH) self.suffix = self.template[bar_type] + self.max = size + + # print empty progress bar workaround + self.goto(1) def success(self, result): self.output.output("\r%s... \033[K" % self.title, False) diff --git a/image_creator/util.py b/image_creator/util.py index 161b73d..184dada 100644 --- a/image_creator/util.py +++ b/image_creator/util.py @@ -60,10 +60,11 @@ class MD5: def compute(self, filename, size): - BLOCKSIZE = 2 ** 22 # 4MB + MB = 2 ** 20 + BLOCKSIZE = 4 * MB # 4MB - progressbar = self.out.Progress("Calculating md5sum:", 'mb') - progressbar.max = ((size + 2 ** 20 - 1) // (2 ** 20)) + prog_size = ((size + MB - 1) // MB) # in MB + progressbar = self.out.Progress(prog_size, "Calculating md5sum:", 'mb') md5 = hashlib.md5() with open(filename, "r") as src: left = size @@ -72,7 +73,7 @@ class MD5: data = src.read(length) md5.update(data) left -= length - progressbar.goto((size - left) // (2 ** 20)) + progressbar.goto((size - left) // MB) checksum = md5.hexdigest() progressbar.success(checksum) -- 1.7.10.4