Revision 7bf93524

b/image_creator/output/dialog.py
1
# Copyright 2012 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
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.
15
#
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.
28
#
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.
33

  
34
from image_creator.output import Output
35
import time
36
import fcntl
37

  
38

  
39
class GaugeOutput(Output):
40
    def __init__(self, dialog, title, msg=''):
41
        self.d = dialog
42
        self.msg = msg
43
        self.percent = 0
44
        self.d.gauge_start(self.msg, title=title)
45

  
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)
56

  
57
    def output(self, msg='', new_line=True):
58
        self.msg = msg
59
        self.percent = 0
60
        self.d.gauge_update(self.percent, self.msg, update_text=True)
61
        time.sleep(0.4)
62

  
63
    def success(self, result, new_line=True):
64
        self.percent = 100
65
        self.d.gauge_update(self.percent, "%s %s" % (self.msg, result),
66
                            update_text=True)
67
        time.sleep(0.4)
68

  
69
    def warn(self, msg, new_line=True):
70
        self.d.gauge_update(self.index, "%s Warning: %s" % (self.msg, msg),
71
                            update_text=True)
72
        time.sleep(0.4)
73

  
74
    def cleanup(self):
75
        self.d.gauge_stop()
76

  
77
    class _Progress(Output._Progress):
78
        template = {
79
            'default': '%(index)d/%(size)d',
80
            'percent': '',
81
            'b': "%(index)d/%(size)d B",
82
            'kb': "%(index)d/%(size)d KB",
83
            'mb': "%(index)d/%(size)d MB"
84
        }
85

  
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
90
            self.goto(0)
91

  
92
        def _postfix(self):
93
            return self.template[self.bar_type] % self.output.__dict__
94

  
95
        def goto(self, dest):
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,
100
                                       update_text=True)
101

  
102
        def next(self):
103
            self.goto(self.output.index + 1)
104

  
105

  
106
class InitializationOutput(GaugeOutput):
107
    def __init__(self, dialog):
108
        dialog.setBackgroundTitle('snf-image-creator')
109
        super(InitializationOutput, self).__init__(dialog, "Initialization",
110
                                                   "Initializing...")
111

  
112
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :

Also available in: Unified diff