Statistics
| Branch: | Tag: | Revision:

root / image_creator / dialog_util.py @ e36b5b14

History | View | Annotate | Download (5.4 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 023e1217 Nikos Skalkotos
    d = session['dialog']
46 023e1217 Nikos Skalkotos
    dev = session['device']
47 b044e29e Nikos Skalkotos
    disk = session['disk']
48 023e1217 Nikos Skalkotos
49 023e1217 Nikos Skalkotos
    MB = 2 ** 20
50 023e1217 Nikos Skalkotos
51 023e1217 Nikos Skalkotos
    size = (dev.size + MB - 1) // MB
52 023e1217 Nikos Skalkotos
    shrinked = 'shrinked' in session and session['shrinked']
53 023e1217 Nikos Skalkotos
    postfix = " (shrinked)" if shrinked else ''
54 023e1217 Nikos Skalkotos
55 b044e29e Nikos Skalkotos
    title = "OS: %s, Distro: %s, Size: %dMB%s, Source: %s" % \
56 b044e29e Nikos Skalkotos
            (dev.ostype, dev.distro, size, postfix,
57 b044e29e Nikos Skalkotos
             os.path.abspath(disk.source))
58 023e1217 Nikos Skalkotos
59 023e1217 Nikos Skalkotos
    d.setBackgroundTitle(title)
60 023e1217 Nikos Skalkotos
61 023e1217 Nikos Skalkotos
62 023e1217 Nikos Skalkotos
def confirm_exit(d, msg=''):
63 023e1217 Nikos Skalkotos
    return not d.yesno("%s Do you want to exit?" % msg, width=SMALL_WIDTH)
64 023e1217 Nikos Skalkotos
65 023e1217 Nikos Skalkotos
66 023e1217 Nikos Skalkotos
def confirm_reset(d):
67 023e1217 Nikos Skalkotos
    return not d.yesno("Are you sure you want to reset everything?",
68 023e1217 Nikos Skalkotos
                       width=SMALL_WIDTH, defaultno=1)
69 023e1217 Nikos Skalkotos
70 023e1217 Nikos Skalkotos
71 023e1217 Nikos Skalkotos
class Reset(Exception):
72 023e1217 Nikos Skalkotos
    pass
73 023e1217 Nikos Skalkotos
74 023e1217 Nikos Skalkotos
75 0a116b9d Nikos Skalkotos
def extract_metadata_string(session):
76 0a116b9d Nikos Skalkotos
    metadata = ['%s=%s' % (k, v) for (k, v) in session['metadata'].items()]
77 0a116b9d Nikos Skalkotos
78 0a116b9d Nikos Skalkotos
    if 'task_metadata' in session:
79 0a116b9d Nikos Skalkotos
        metadata.extend("%s=yes" % m for m in session['task_metadata'])
80 0a116b9d Nikos Skalkotos
81 0a116b9d Nikos Skalkotos
    return '\n'.join(metadata) + '\n'
82 0a116b9d Nikos Skalkotos
83 0a116b9d Nikos Skalkotos
84 023e1217 Nikos Skalkotos
def extract_image(session):
85 023e1217 Nikos Skalkotos
    d = session['dialog']
86 023e1217 Nikos Skalkotos
    dir = os.getcwd()
87 023e1217 Nikos Skalkotos
    while 1:
88 023e1217 Nikos Skalkotos
        if dir and dir[-1] != os.sep:
89 023e1217 Nikos Skalkotos
            dir = dir + os.sep
90 023e1217 Nikos Skalkotos
91 023e1217 Nikos Skalkotos
        (code, path) = d.fselect(dir, 10, 50, title="Save image as...")
92 023e1217 Nikos Skalkotos
        if code in (d.DIALOG_CANCEL, d.DIALOG_ESC):
93 023e1217 Nikos Skalkotos
            return False
94 023e1217 Nikos Skalkotos
95 023e1217 Nikos Skalkotos
        if os.path.isdir(path):
96 023e1217 Nikos Skalkotos
            dir = path
97 023e1217 Nikos Skalkotos
            continue
98 023e1217 Nikos Skalkotos
99 023e1217 Nikos Skalkotos
        if os.path.isdir("%s.meta" % path):
100 023e1217 Nikos Skalkotos
            d.msgbox("Can't overwrite directory `%s.meta'" % path,
101 023e1217 Nikos Skalkotos
                     width=SMALL_WIDTH)
102 023e1217 Nikos Skalkotos
            continue
103 023e1217 Nikos Skalkotos
104 023e1217 Nikos Skalkotos
        if os.path.isdir("%s.md5sum" % path):
105 023e1217 Nikos Skalkotos
            d.msgbox("Can't overwrite directory `%s.md5sum'" % path,
106 023e1217 Nikos Skalkotos
                     width=SMALL_WIDTH)
107 023e1217 Nikos Skalkotos
            continue
108 023e1217 Nikos Skalkotos
109 023e1217 Nikos Skalkotos
        basedir = os.path.dirname(path)
110 023e1217 Nikos Skalkotos
        name = os.path.basename(path)
111 023e1217 Nikos Skalkotos
        if not os.path.exists(basedir):
112 023e1217 Nikos Skalkotos
            d.msgbox("Directory `%s' does not exist" % basedir,
113 023e1217 Nikos Skalkotos
                     width=SMALL_WIDTH)
114 023e1217 Nikos Skalkotos
            continue
115 023e1217 Nikos Skalkotos
116 023e1217 Nikos Skalkotos
        dir = basedir
117 023e1217 Nikos Skalkotos
        if len(name) == 0:
118 023e1217 Nikos Skalkotos
            continue
119 023e1217 Nikos Skalkotos
120 023e1217 Nikos Skalkotos
        files = ["%s%s" % (path, ext) for ext in ('', '.meta', '.md5sum')]
121 023e1217 Nikos Skalkotos
        overwrite = filter(os.path.exists, files)
122 023e1217 Nikos Skalkotos
123 023e1217 Nikos Skalkotos
        if len(overwrite) > 0:
124 023e1217 Nikos Skalkotos
            if d.yesno("The following file(s) exist:\n"
125 023e1217 Nikos Skalkotos
                       "%s\nDo you want to overwrite them?" %
126 023e1217 Nikos Skalkotos
                       "\n".join(overwrite), width=SMALL_WIDTH):
127 023e1217 Nikos Skalkotos
                continue
128 023e1217 Nikos Skalkotos
129 023e1217 Nikos Skalkotos
        gauge = GaugeOutput(d, "Image Extraction", "Extracting image...")
130 023e1217 Nikos Skalkotos
        try:
131 023e1217 Nikos Skalkotos
            dev = session['device']
132 023e1217 Nikos Skalkotos
            out = dev.out
133 023e1217 Nikos Skalkotos
            out.add(gauge)
134 023e1217 Nikos Skalkotos
            try:
135 023e1217 Nikos Skalkotos
                if "checksum" not in session:
136 023e1217 Nikos Skalkotos
                    size = dev.size
137 023e1217 Nikos Skalkotos
                    md5 = MD5(out)
138 023e1217 Nikos Skalkotos
                    session['checksum'] = md5.compute(session['snapshot'],
139 023e1217 Nikos Skalkotos
                                                      size)
140 023e1217 Nikos Skalkotos
141 023e1217 Nikos Skalkotos
                # Extract image file
142 023e1217 Nikos Skalkotos
                dev.dump(path)
143 023e1217 Nikos Skalkotos
144 023e1217 Nikos Skalkotos
                # Extract metadata file
145 023e1217 Nikos Skalkotos
                out.output("Extracting metadata file...")
146 023e1217 Nikos Skalkotos
                with open('%s.meta' % path, 'w') as f:
147 023e1217 Nikos Skalkotos
                    f.write(extract_metadata_string(session))
148 023e1217 Nikos Skalkotos
                out.success('done')
149 023e1217 Nikos Skalkotos
150 023e1217 Nikos Skalkotos
                # Extract md5sum file
151 023e1217 Nikos Skalkotos
                out.output("Extracting md5sum file...")
152 023e1217 Nikos Skalkotos
                md5str = "%s %s\n" % (session['checksum'], name)
153 023e1217 Nikos Skalkotos
                with open('%s.md5sum' % path, 'w') as f:
154 023e1217 Nikos Skalkotos
                    f.write(md5str)
155 023e1217 Nikos Skalkotos
                out.success("done")
156 023e1217 Nikos Skalkotos
            finally:
157 023e1217 Nikos Skalkotos
                out.remove(gauge)
158 023e1217 Nikos Skalkotos
        finally:
159 023e1217 Nikos Skalkotos
            gauge.cleanup()
160 023e1217 Nikos Skalkotos
        d.msgbox("Image file `%s' was successfully extracted!" % path,
161 023e1217 Nikos Skalkotos
                 width=SMALL_WIDTH)
162 023e1217 Nikos Skalkotos
        break
163 023e1217 Nikos Skalkotos
164 023e1217 Nikos Skalkotos
    return True
165 023e1217 Nikos Skalkotos
166 023e1217 Nikos Skalkotos
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :