Statistics
| Branch: | Tag: | Revision:

root / image_creator / dialog_util.py @ bf3a282c

History | View | Annotate | Download (5.6 kB)

1 023e1217 Nikos Skalkotos
#!/usr/bin/env python
2 023e1217 Nikos Skalkotos
3 023e1217 Nikos Skalkotos
# Copyright 2012 GRNET S.A. All rights reserved.
4 023e1217 Nikos Skalkotos
#
5 023e1217 Nikos Skalkotos
# Redistribution and use in source and binary forms, with or
6 023e1217 Nikos Skalkotos
# without modification, are permitted provided that the following
7 023e1217 Nikos Skalkotos
# conditions are met:
8 023e1217 Nikos Skalkotos
#
9 023e1217 Nikos Skalkotos
#   1. Redistributions of source code must retain the above
10 023e1217 Nikos Skalkotos
#      copyright notice, this list of conditions and the following
11 023e1217 Nikos Skalkotos
#      disclaimer.
12 023e1217 Nikos Skalkotos
#
13 023e1217 Nikos Skalkotos
#   2. Redistributions in binary form must reproduce the above
14 023e1217 Nikos Skalkotos
#      copyright notice, this list of conditions and the following
15 023e1217 Nikos Skalkotos
#      disclaimer in the documentation and/or other materials
16 023e1217 Nikos Skalkotos
#      provided with the distribution.
17 023e1217 Nikos Skalkotos
#
18 023e1217 Nikos Skalkotos
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
19 023e1217 Nikos Skalkotos
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 023e1217 Nikos Skalkotos
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 023e1217 Nikos Skalkotos
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
22 023e1217 Nikos Skalkotos
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 023e1217 Nikos Skalkotos
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 023e1217 Nikos Skalkotos
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
25 023e1217 Nikos Skalkotos
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 023e1217 Nikos Skalkotos
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 023e1217 Nikos Skalkotos
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 023e1217 Nikos Skalkotos
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 023e1217 Nikos Skalkotos
# POSSIBILITY OF SUCH DAMAGE.
30 023e1217 Nikos Skalkotos
#
31 023e1217 Nikos Skalkotos
# The views and conclusions contained in the software and
32 023e1217 Nikos Skalkotos
# documentation are those of the authors and should not be
33 023e1217 Nikos Skalkotos
# interpreted as representing official policies, either expressed
34 023e1217 Nikos Skalkotos
# or implied, of GRNET S.A.
35 023e1217 Nikos Skalkotos
36 023e1217 Nikos Skalkotos
import os
37 0a116b9d Nikos Skalkotos
from image_creator.output.dialog import GaugeOutput
38 0a116b9d Nikos Skalkotos
from image_creator.util import MD5
39 023e1217 Nikos Skalkotos
40 023e1217 Nikos Skalkotos
SMALL_WIDTH = 60
41 023e1217 Nikos Skalkotos
WIDTH = 70
42 023e1217 Nikos Skalkotos
43 023e1217 Nikos Skalkotos
44 023e1217 Nikos Skalkotos
def update_background_title(session):
45 88f83027 Nikos Skalkotos
    """Update the backgroud title of the dialog page"""
46 023e1217 Nikos Skalkotos
    d = session['dialog']
47 b044e29e Nikos Skalkotos
    disk = session['disk']
48 f5174d2c Nikos Skalkotos
    image = session['image']
49 023e1217 Nikos Skalkotos
50 023e1217 Nikos Skalkotos
    MB = 2 ** 20
51 023e1217 Nikos Skalkotos
52 f5174d2c Nikos Skalkotos
    size = (image.size + MB - 1) // MB
53 023e1217 Nikos Skalkotos
    shrinked = 'shrinked' in session and session['shrinked']
54 023e1217 Nikos Skalkotos
    postfix = " (shrinked)" if shrinked else ''
55 023e1217 Nikos Skalkotos
56 b044e29e Nikos Skalkotos
    title = "OS: %s, Distro: %s, Size: %dMB%s, Source: %s" % \
57 f5174d2c Nikos Skalkotos
            (image.ostype, image.distro, size, postfix,
58 b044e29e Nikos Skalkotos
             os.path.abspath(disk.source))
59 023e1217 Nikos Skalkotos
60 023e1217 Nikos Skalkotos
    d.setBackgroundTitle(title)
61 023e1217 Nikos Skalkotos
62 023e1217 Nikos Skalkotos
63 023e1217 Nikos Skalkotos
def confirm_exit(d, msg=''):
64 88f83027 Nikos Skalkotos
    """Ask the user to confirm when exiting the program"""
65 023e1217 Nikos Skalkotos
    return not d.yesno("%s Do you want to exit?" % msg, width=SMALL_WIDTH)
66 023e1217 Nikos Skalkotos
67 023e1217 Nikos Skalkotos
68 023e1217 Nikos Skalkotos
def confirm_reset(d):
69 88f83027 Nikos Skalkotos
    """Ask the user to confirm a reset action"""
70 023e1217 Nikos Skalkotos
    return not d.yesno("Are you sure you want to reset everything?",
71 023e1217 Nikos Skalkotos
                       width=SMALL_WIDTH, defaultno=1)
72 023e1217 Nikos Skalkotos
73 023e1217 Nikos Skalkotos
74 023e1217 Nikos Skalkotos
class Reset(Exception):
75 88f83027 Nikos Skalkotos
    """Exception used to reset the program"""
76 023e1217 Nikos Skalkotos
    pass
77 023e1217 Nikos Skalkotos
78 023e1217 Nikos Skalkotos
79 0a116b9d Nikos Skalkotos
def extract_metadata_string(session):
80 88f83027 Nikos Skalkotos
    """Convert image metadata to text"""
81 0a116b9d Nikos Skalkotos
    metadata = ['%s=%s' % (k, v) for (k, v) in session['metadata'].items()]
82 0a116b9d Nikos Skalkotos
83 0a116b9d Nikos Skalkotos
    if 'task_metadata' in session:
84 0a116b9d Nikos Skalkotos
        metadata.extend("%s=yes" % m for m in session['task_metadata'])
85 0a116b9d Nikos Skalkotos
86 0a116b9d Nikos Skalkotos
    return '\n'.join(metadata) + '\n'
87 0a116b9d Nikos Skalkotos
88 0a116b9d Nikos Skalkotos
89 023e1217 Nikos Skalkotos
def extract_image(session):
90 88f83027 Nikos Skalkotos
    """Dump the image to a local file"""
91 023e1217 Nikos Skalkotos
    d = session['dialog']
92 023e1217 Nikos Skalkotos
    dir = os.getcwd()
93 023e1217 Nikos Skalkotos
    while 1:
94 023e1217 Nikos Skalkotos
        if dir and dir[-1] != os.sep:
95 023e1217 Nikos Skalkotos
            dir = dir + os.sep
96 023e1217 Nikos Skalkotos
97 023e1217 Nikos Skalkotos
        (code, path) = d.fselect(dir, 10, 50, title="Save image as...")
98 023e1217 Nikos Skalkotos
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
99 023e1217 Nikos Skalkotos
            return False
100 023e1217 Nikos Skalkotos
101 023e1217 Nikos Skalkotos
        if os.path.isdir(path):
102 023e1217 Nikos Skalkotos
            dir = path
103 023e1217 Nikos Skalkotos
            continue
104 023e1217 Nikos Skalkotos
105 023e1217 Nikos Skalkotos
        if os.path.isdir("%s.meta" % path):
106 023e1217 Nikos Skalkotos
            d.msgbox("Can't overwrite directory `%s.meta'" % path,
107 023e1217 Nikos Skalkotos
                     width=SMALL_WIDTH)
108 023e1217 Nikos Skalkotos
            continue
109 023e1217 Nikos Skalkotos
110 023e1217 Nikos Skalkotos
        if os.path.isdir("%s.md5sum" % path):
111 023e1217 Nikos Skalkotos
            d.msgbox("Can't overwrite directory `%s.md5sum'" % path,
112 023e1217 Nikos Skalkotos
                     width=SMALL_WIDTH)
113 023e1217 Nikos Skalkotos
            continue
114 023e1217 Nikos Skalkotos
115 023e1217 Nikos Skalkotos
        basedir = os.path.dirname(path)
116 023e1217 Nikos Skalkotos
        name = os.path.basename(path)
117 023e1217 Nikos Skalkotos
        if not os.path.exists(basedir):
118 023e1217 Nikos Skalkotos
            d.msgbox("Directory `%s' does not exist" % basedir,
119 023e1217 Nikos Skalkotos
                     width=SMALL_WIDTH)
120 023e1217 Nikos Skalkotos
            continue
121 023e1217 Nikos Skalkotos
122 023e1217 Nikos Skalkotos
        dir = basedir
123 023e1217 Nikos Skalkotos
        if len(name) == 0:
124 023e1217 Nikos Skalkotos
            continue
125 023e1217 Nikos Skalkotos
126 023e1217 Nikos Skalkotos
        files = ["%s%s" % (path, ext) for ext in ('', '.meta', '.md5sum')]
127 023e1217 Nikos Skalkotos
        overwrite = filter(os.path.exists, files)
128 023e1217 Nikos Skalkotos
129 023e1217 Nikos Skalkotos
        if len(overwrite) > 0:
130 023e1217 Nikos Skalkotos
            if d.yesno("The following file(s) exist:\n"
131 023e1217 Nikos Skalkotos
                       "%s\nDo you want to overwrite them?" %
132 023e1217 Nikos Skalkotos
                       "\n".join(overwrite), width=SMALL_WIDTH):
133 023e1217 Nikos Skalkotos
                continue
134 023e1217 Nikos Skalkotos
135 023e1217 Nikos Skalkotos
        gauge = GaugeOutput(d, "Image Extraction", "Extracting image...")
136 023e1217 Nikos Skalkotos
        try:
137 f5174d2c Nikos Skalkotos
            image = session['image']
138 f5174d2c Nikos Skalkotos
            out = image.out
139 023e1217 Nikos Skalkotos
            out.add(gauge)
140 023e1217 Nikos Skalkotos
            try:
141 023e1217 Nikos Skalkotos
                if "checksum" not in session:
142 023e1217 Nikos Skalkotos
                    md5 = MD5(out)
143 f5174d2c Nikos Skalkotos
                    session['checksum'] = md5.compute(image.device, image.size)
144 023e1217 Nikos Skalkotos
145 023e1217 Nikos Skalkotos
                # Extract image file
146 f5174d2c Nikos Skalkotos
                image.dump(path)
147 023e1217 Nikos Skalkotos
148 023e1217 Nikos Skalkotos
                # Extract metadata file
149 3afe6b44 Nikos Skalkotos
                out.output("Extracting metadata file ...")
150 023e1217 Nikos Skalkotos
                with open('%s.meta' % path, 'w') as f:
151 023e1217 Nikos Skalkotos
                    f.write(extract_metadata_string(session))
152 023e1217 Nikos Skalkotos
                out.success('done')
153 023e1217 Nikos Skalkotos
154 023e1217 Nikos Skalkotos
                # Extract md5sum file
155 3afe6b44 Nikos Skalkotos
                out.output("Extracting md5sum file ...")
156 023e1217 Nikos Skalkotos
                md5str = "%s %s\n" % (session['checksum'], name)
157 023e1217 Nikos Skalkotos
                with open('%s.md5sum' % path, 'w') as f:
158 023e1217 Nikos Skalkotos
                    f.write(md5str)
159 023e1217 Nikos Skalkotos
                out.success("done")
160 023e1217 Nikos Skalkotos
            finally:
161 023e1217 Nikos Skalkotos
                out.remove(gauge)
162 023e1217 Nikos Skalkotos
        finally:
163 023e1217 Nikos Skalkotos
            gauge.cleanup()
164 023e1217 Nikos Skalkotos
        d.msgbox("Image file `%s' was successfully extracted!" % path,
165 023e1217 Nikos Skalkotos
                 width=SMALL_WIDTH)
166 023e1217 Nikos Skalkotos
        break
167 023e1217 Nikos Skalkotos
168 023e1217 Nikos Skalkotos
    return True
169 023e1217 Nikos Skalkotos
170 023e1217 Nikos Skalkotos
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :