diff options
Diffstat (limited to 'bitbake/lib/bb/ui/crumbs/hig/deployimagedialog.py')
-rw-r--r-- | bitbake/lib/bb/ui/crumbs/hig/deployimagedialog.py | 215 |
1 files changed, 215 insertions, 0 deletions
diff --git a/bitbake/lib/bb/ui/crumbs/hig/deployimagedialog.py b/bitbake/lib/bb/ui/crumbs/hig/deployimagedialog.py new file mode 100644 index 0000000000..bc1efbbfaf --- /dev/null +++ b/bitbake/lib/bb/ui/crumbs/hig/deployimagedialog.py | |||
@@ -0,0 +1,215 @@ | |||
1 | # | ||
2 | # BitBake Graphical GTK User Interface | ||
3 | # | ||
4 | # Copyright (C) 2011-2012 Intel Corporation | ||
5 | # | ||
6 | # Authored by Joshua Lock <josh@linux.intel.com> | ||
7 | # Authored by Dongxiao Xu <dongxiao.xu@intel.com> | ||
8 | # Authored by Shane Wang <shane.wang@intel.com> | ||
9 | # | ||
10 | # This program is free software; you can redistribute it and/or modify | ||
11 | # it under the terms of the GNU General Public License version 2 as | ||
12 | # published by the Free Software Foundation. | ||
13 | # | ||
14 | # This program is distributed in the hope that it will be useful, | ||
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | # GNU General Public License for more details. | ||
18 | # | ||
19 | # You should have received a copy of the GNU General Public License along | ||
20 | # with this program; if not, write to the Free Software Foundation, Inc., | ||
21 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
22 | |||
23 | import glob | ||
24 | import gtk | ||
25 | import gobject | ||
26 | import os | ||
27 | import re | ||
28 | import shlex | ||
29 | import subprocess | ||
30 | import tempfile | ||
31 | from bb.ui.crumbs.hobwidget import hic, HobButton | ||
32 | from bb.ui.crumbs.progressbar import HobProgressBar | ||
33 | import bb.ui.crumbs.utils | ||
34 | import bb.process | ||
35 | from bb.ui.crumbs.hig.crumbsdialog import CrumbsDialog | ||
36 | from bb.ui.crumbs.hig.crumbsmessagedialog import CrumbsMessageDialog | ||
37 | |||
38 | """ | ||
39 | The following are convenience classes for implementing GNOME HIG compliant | ||
40 | BitBake GUI's | ||
41 | In summary: spacing = 12px, border-width = 6px | ||
42 | """ | ||
43 | |||
44 | class DeployImageDialog (CrumbsDialog): | ||
45 | |||
46 | __dummy_usb__ = "--select a usb drive--" | ||
47 | |||
48 | def __init__(self, title, image_path, parent, flags, buttons=None, standalone=False): | ||
49 | super(DeployImageDialog, self).__init__(title, parent, flags, buttons) | ||
50 | |||
51 | self.image_path = image_path | ||
52 | self.standalone = standalone | ||
53 | |||
54 | self.create_visual_elements() | ||
55 | self.connect("response", self.response_cb) | ||
56 | |||
57 | def create_visual_elements(self): | ||
58 | self.set_size_request(600, 400) | ||
59 | label = gtk.Label() | ||
60 | label.set_alignment(0.0, 0.5) | ||
61 | markup = "<span font_desc='12'>The image to be written into usb drive:</span>" | ||
62 | label.set_markup(markup) | ||
63 | self.vbox.pack_start(label, expand=False, fill=False, padding=2) | ||
64 | |||
65 | table = gtk.Table(2, 10, False) | ||
66 | table.set_col_spacings(5) | ||
67 | table.set_row_spacings(5) | ||
68 | self.vbox.pack_start(table, expand=True, fill=True) | ||
69 | |||
70 | scroll = gtk.ScrolledWindow() | ||
71 | scroll.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC) | ||
72 | scroll.set_shadow_type(gtk.SHADOW_IN) | ||
73 | tv = gtk.TextView() | ||
74 | tv.set_editable(False) | ||
75 | tv.set_wrap_mode(gtk.WRAP_WORD) | ||
76 | tv.set_cursor_visible(False) | ||
77 | self.buf = gtk.TextBuffer() | ||
78 | self.buf.set_text(self.image_path) | ||
79 | tv.set_buffer(self.buf) | ||
80 | scroll.add(tv) | ||
81 | table.attach(scroll, 0, 10, 0, 1) | ||
82 | |||
83 | # There are 2 ways to use DeployImageDialog | ||
84 | # One way is that called by HOB when the 'Deploy Image' button is clicked | ||
85 | # The other way is that called by a standalone script. | ||
86 | # Following block of codes handles the latter way. It adds a 'Select Image' button and | ||
87 | # emit a signal when the button is clicked. | ||
88 | if self.standalone: | ||
89 | gobject.signal_new("select_image_clicked", self, gobject.SIGNAL_RUN_FIRST, | ||
90 | gobject.TYPE_NONE, ()) | ||
91 | icon = gtk.Image() | ||
92 | pix_buffer = gtk.gdk.pixbuf_new_from_file(hic.ICON_IMAGES_DISPLAY_FILE) | ||
93 | icon.set_from_pixbuf(pix_buffer) | ||
94 | button = gtk.Button("Select Image") | ||
95 | button.set_image(icon) | ||
96 | #button.set_size_request(140, 50) | ||
97 | table.attach(button, 9, 10, 1, 2, gtk.FILL, 0, 0, 0) | ||
98 | button.connect("clicked", self.select_image_button_clicked_cb) | ||
99 | |||
100 | separator = gtk.HSeparator() | ||
101 | self.vbox.pack_start(separator, expand=False, fill=False, padding=10) | ||
102 | |||
103 | self.usb_desc = gtk.Label() | ||
104 | self.usb_desc.set_alignment(0.0, 0.5) | ||
105 | markup = "<span font_desc='12'>You haven't chosen any USB drive.</span>" | ||
106 | self.usb_desc.set_markup(markup) | ||
107 | |||
108 | self.usb_combo = gtk.combo_box_new_text() | ||
109 | self.usb_combo.connect("changed", self.usb_combo_changed_cb) | ||
110 | model = self.usb_combo.get_model() | ||
111 | model.clear() | ||
112 | self.usb_combo.append_text(self.__dummy_usb__) | ||
113 | for usb in self.find_all_usb_devices(): | ||
114 | self.usb_combo.append_text("/dev/" + usb) | ||
115 | self.usb_combo.set_active(0) | ||
116 | self.vbox.pack_start(self.usb_combo, expand=False, fill=False) | ||
117 | self.vbox.pack_start(self.usb_desc, expand=False, fill=False, padding=2) | ||
118 | |||
119 | self.progress_bar = HobProgressBar() | ||
120 | self.vbox.pack_start(self.progress_bar, expand=False, fill=False) | ||
121 | separator = gtk.HSeparator() | ||
122 | self.vbox.pack_start(separator, expand=False, fill=True, padding=10) | ||
123 | |||
124 | self.vbox.show_all() | ||
125 | self.progress_bar.hide() | ||
126 | |||
127 | def set_image_text_buffer(self, image_path): | ||
128 | self.buf.set_text(image_path) | ||
129 | |||
130 | def set_image_path(self, image_path): | ||
131 | self.image_path = image_path | ||
132 | |||
133 | def popen_read(self, cmd): | ||
134 | tmpout, errors = bb.process.run("%s" % cmd) | ||
135 | return tmpout.strip() | ||
136 | |||
137 | def find_all_usb_devices(self): | ||
138 | usb_devs = [ os.readlink(u) | ||
139 | for u in glob.glob('/dev/disk/by-id/usb*') | ||
140 | if not re.search(r'part\d+', u) ] | ||
141 | return [ '%s' % u[u.rfind('/')+1:] for u in usb_devs ] | ||
142 | |||
143 | def get_usb_info(self, dev): | ||
144 | return "%s %s" % \ | ||
145 | (self.popen_read('cat /sys/class/block/%s/device/vendor' % dev), | ||
146 | self.popen_read('cat /sys/class/block/%s/device/model' % dev)) | ||
147 | |||
148 | def select_image_button_clicked_cb(self, button): | ||
149 | self.emit('select_image_clicked') | ||
150 | |||
151 | def usb_combo_changed_cb(self, usb_combo): | ||
152 | combo_item = self.usb_combo.get_active_text() | ||
153 | if not combo_item or combo_item == self.__dummy_usb__: | ||
154 | markup = "<span font_desc='12'>You haven't chosen any USB drive.</span>" | ||
155 | self.usb_desc.set_markup(markup) | ||
156 | else: | ||
157 | markup = "<span font_desc='12'>" + self.get_usb_info(combo_item.lstrip("/dev/")) + "</span>" | ||
158 | self.usb_desc.set_markup(markup) | ||
159 | |||
160 | def response_cb(self, dialog, response_id): | ||
161 | if response_id == gtk.RESPONSE_YES: | ||
162 | lbl = '' | ||
163 | combo_item = self.usb_combo.get_active_text() | ||
164 | if combo_item and combo_item != self.__dummy_usb__ and self.image_path: | ||
165 | cmdline = bb.ui.crumbs.utils.which_terminal() | ||
166 | if cmdline: | ||
167 | tmpfile = tempfile.NamedTemporaryFile() | ||
168 | cmdline += "\"sudo dd if=" + self.image_path + \ | ||
169 | " of=" + combo_item + "; echo $? > " + tmpfile.name + "\"" | ||
170 | subprocess.call(shlex.split(cmdline)) | ||
171 | |||
172 | if int(tmpfile.readline().strip()) == 0: | ||
173 | lbl = "<b>Deploy image successfully.</b>" | ||
174 | else: | ||
175 | lbl = "<b>Failed to deploy image.</b>\nPlease check image <b>%s</b> exists and USB device <b>%s</b> is writable." % (self.image_path, combo_item) | ||
176 | tmpfile.close() | ||
177 | else: | ||
178 | if not self.image_path: | ||
179 | lbl = "<b>No selection made.</b>\nYou have not selected an image to deploy." | ||
180 | else: | ||
181 | lbl = "<b>No selection made.</b>\nYou have not selected a USB device." | ||
182 | if len(lbl): | ||
183 | crumbs_dialog = CrumbsMessageDialog(self, lbl, gtk.STOCK_DIALOG_INFO) | ||
184 | button = crumbs_dialog.add_button("Close", gtk.RESPONSE_OK) | ||
185 | HobButton.style_button(button) | ||
186 | crumbs_dialog.run() | ||
187 | crumbs_dialog.destroy() | ||
188 | |||
189 | def update_progress_bar(self, title, fraction, status=None): | ||
190 | self.progress_bar.update(fraction) | ||
191 | self.progress_bar.set_title(title) | ||
192 | self.progress_bar.set_rcstyle(status) | ||
193 | |||
194 | def write_file(self, ifile, ofile): | ||
195 | self.progress_bar.reset() | ||
196 | self.progress_bar.show() | ||
197 | |||
198 | f_from = os.open(ifile, os.O_RDONLY) | ||
199 | f_to = os.open(ofile, os.O_WRONLY) | ||
200 | |||
201 | total_size = os.stat(ifile).st_size | ||
202 | written_size = 0 | ||
203 | |||
204 | while True: | ||
205 | buf = os.read(f_from, 1024*1024) | ||
206 | if not buf: | ||
207 | break | ||
208 | os.write(f_to, buf) | ||
209 | written_size += 1024*1024 | ||
210 | self.update_progress_bar("Writing to usb:", written_size * 1.0/total_size) | ||
211 | |||
212 | self.update_progress_bar("Writing completed:", 1.0) | ||
213 | os.close(f_from) | ||
214 | os.close(f_to) | ||
215 | self.progress_bar.hide() | ||