Add progress bar for the guestfs launch method
authorNikos Skalkotos <skalkoto@grnet.gr>
Thu, 8 Mar 2012 15:32:04 +0000 (17:32 +0200)
committerNikos Skalkotos <skalkoto@grnet.gr>
Thu, 8 Mar 2012 15:32:04 +0000 (17:32 +0200)
image_creator/disk.py
setup.py

index 0d5ed48..0d30f96 100644 (file)
@@ -11,6 +11,7 @@ import guestfs
 
 import pbs
 from pbs import dd
 
 import pbs
 from pbs import dd
+from clint.textui import progress
 
 
 class DiskError(Exception):
 
 
 class DiskError(Exception):
@@ -121,6 +122,14 @@ class Disk(object):
         device.destroy()
 
 
         device.destroy()
 
 
+def progress_generator(total):
+    position = 0;
+    for i in progress.bar(range(total)):
+        if i < position:
+            continue
+        position = yield
+
+
 class DiskDevice(object):
     """This class represents a block device hosting an Operating System
     as created by the device-mapper.
 class DiskDevice(object):
     """This class represents a block device hosting an Operating System
     as created by the device-mapper.
@@ -130,13 +139,18 @@ class DiskDevice(object):
         """Create a new DiskDevice."""
         self.device = device
         self.bootable = bootable
         """Create a new DiskDevice."""
         self.device = device
         self.bootable = bootable
+        self.progress_bar = None
 
         self.g = guestfs.GuestFS()
 
         self.g = guestfs.GuestFS()
+        self.g.add_drive_opts(device, readonly=0)
 
 
-        self.g.set_trace(1)
+        #self.g.set_trace(1)
+        #self.g.set_verbose(1)
 
 
-        self.g.add_drive_opts(device, readonly=0)
+        eh = self.g.set_event_callback(self.progress_callback, guestfs.EVENT_PROGRESS)
         self.g.launch()
         self.g.launch()
+        self.g.delete_event_callback(eh)
+        
         roots = self.g.inspect_os()
         if len(roots) == 0:
             raise DiskError("No operating system found")
         roots = self.g.inspect_os()
         if len(roots) == 0:
             raise DiskError("No operating system found")
@@ -154,6 +168,21 @@ class DiskDevice(object):
         # Close the guestfs handler
         self.g.close()
 
         # Close the guestfs handler
         self.g.close()
 
+    def progress_callback(self, ev, eh, buf, array):
+        position = array[2]
+        total = array[3]
+        
+        if self.progress_bar is None:
+            self.progress_bar = progress_generator(total)
+            self.progress_bar.next()
+            if position == 1:
+                return
+
+        self.progress_bar.send(position)
+
+        if position == total:
+            self.progress_bar = None
+
     def mount(self):
         """Mount all disk partitions in a correct order."""
         mps = self.g.inspect_get_mountpoints(self.root)
     def mount(self):
         """Mount all disk partitions in a correct order."""
         mps = self.g.inspect_get_mountpoints(self.root)
index d9e34e7..345c8c3 100755 (executable)
--- a/setup.py
+++ b/setup.py
@@ -47,7 +47,7 @@ setup(
     license='BSD',
     packages=['image_creator'],
     include_package_data=True,
     license='BSD',
     packages=['image_creator'],
     include_package_data=True,
-    install_requires=['losetup', 'pbs'],
+    install_requires=['losetup', 'pbs', 'clint'],
     entry_points={
         'console_scripts': ['snf-image-creator = image_creator.main:main']
     }
     entry_points={
         'console_scripts': ['snf-image-creator = image_creator.main:main']
     }