1 # Copyright 2012 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
11 # 2. Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following
13 # disclaimer in the documentation and/or other materials
14 # provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
34 from image_creator.output import Output
39 class GaugeOutput(Output):
40 def __init__(self, dialog, title, msg=''):
44 self.d.gauge_start(self.msg, title=title)
46 # Open pipe workaround. A fork will dublicate the open file descriptor.
47 # The FD_CLOEXEC flag makes sure that the gauge internal fd will be
48 # closed if execve is executed. This is needed because libguestfs will
49 # fork/exec the kvm process. If this fd stays open in the kvm process,
50 # then doing a gauge_stop will make this process wait forever for
51 # a dialog process that is blocked waiting for input from the kvm
52 # process's open file descriptor.
53 fd = self.d._gauge_process['stdin'].fileno()
54 flags = fcntl.fcntl(fd, fcntl.F_GETFL, 0)
55 fcntl.fcntl(fd, fcntl.F_SETFD, flags | fcntl.FD_CLOEXEC)
57 def output(self, msg='', new_line=True):
60 self.d.gauge_update(self.percent, self.msg, update_text=True)
63 def success(self, result, new_line=True):
65 self.d.gauge_update(self.percent, "%s %s" % (self.msg, result),
69 def warn(self, msg, new_line=True):
70 self.d.gauge_update(self.index, "%s Warning: %s" % (self.msg, msg),
77 class _Progress(Output._Progress):
79 'default': '%(index)d/%(size)d',
81 'b': "%(index)d/%(size)d B",
82 'kb': "%(index)d/%(size)d KB",
83 'mb': "%(index)d/%(size)d MB"
86 def __init__(self, size, title, bar_type='default'):
87 self.output.size = size
88 self.bar_type = bar_type
89 self.output.msg = "%s..." % title
93 return self.template[self.bar_type] % self.output.__dict__
96 self.output.index = dest
97 self.output.percent = self.output.index * 100 // self.output.size
98 msg = "%s %s" % (self.output.msg, self._postfix())
99 self.output.d.gauge_update(self.output.percent, msg,
103 self.goto(self.output.index + 1)
106 class InitializationOutput(GaugeOutput):
107 def __init__(self, dialog):
108 dialog.setBackgroundTitle('snf-image-creator')
109 super(InitializationOutput, self).__init__(dialog, "Initialization",
112 # vim: set sta sts=4 shiftwidth=4 sw=4 et ai :