From: Nikos Skalkotos Date: Thu, 8 Mar 2012 15:32:04 +0000 (+0200) Subject: Add progress bar for the guestfs launch method X-Git-Tag: v0.1~142 X-Git-Url: https://code.grnet.gr/git/snf-image-creator/commitdiff_plain/586da0a0f6a8297eca2f66d81fe9389df2a7bd87 Add progress bar for the guestfs launch method --- diff --git a/image_creator/disk.py b/image_creator/disk.py index 0d5ed48..0d30f96 100644 --- a/image_creator/disk.py +++ b/image_creator/disk.py @@ -11,6 +11,7 @@ import guestfs import pbs from pbs import dd +from clint.textui import progress class DiskError(Exception): @@ -121,6 +122,14 @@ class Disk(object): 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. @@ -130,13 +139,18 @@ class DiskDevice(object): """Create a new DiskDevice.""" self.device = device self.bootable = bootable + self.progress_bar = None 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.delete_event_callback(eh) + 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() + 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) diff --git a/setup.py b/setup.py index d9e34e7..345c8c3 100755 --- a/setup.py +++ b/setup.py @@ -47,7 +47,7 @@ setup( 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'] }