Add size param in the Output.Progress constructor
authorNikos Skalkotos <skalkoto@grnet.gr>
Mon, 11 Jun 2012 09:02:04 +0000 (12:02 +0300)
committerNikos Skalkotos <skalkoto@grnet.gr>
Mon, 11 Jun 2012 09:02:04 +0000 (12:02 +0300)
image_creator/disk.py
image_creator/output.py
image_creator/util.py

index e404a06..38df2fd 100644 (file)
@@ -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:
index 4eabc06..040029c 100644 (file)
@@ -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)
index 161b73d..184dada 100644 (file)
@@ -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)