Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 | msg = '' |
| 164 | combo_item = self.usb_combo.get_active_text() |
| 165 | if combo_item and combo_item != self.__dummy_usb__ and self.image_path: |
| 166 | cmdline = bb.ui.crumbs.utils.which_terminal() |
| 167 | if cmdline: |
| 168 | tmpfile = tempfile.NamedTemporaryFile() |
| 169 | cmdline += "\"sudo dd if=" + self.image_path + \ |
| 170 | " of=" + combo_item + " && sync; echo $? > " + tmpfile.name + "\"" |
| 171 | subprocess.call(shlex.split(cmdline)) |
| 172 | |
| 173 | if int(tmpfile.readline().strip()) == 0: |
| 174 | lbl = "<b>Deploy image successfully.</b>" |
| 175 | else: |
| 176 | lbl = "<b>Failed to deploy image.</b>" |
| 177 | msg = "Please check image <b>%s</b> exists and USB device <b>%s</b> is writable." % (self.image_path, combo_item) |
| 178 | tmpfile.close() |
| 179 | else: |
| 180 | if not self.image_path: |
| 181 | lbl = "<b>No selection made.</b>" |
| 182 | msg = "You have not selected an image to deploy." |
| 183 | else: |
| 184 | lbl = "<b>No selection made.</b>" |
| 185 | msg = "You have not selected a USB device." |
| 186 | if len(lbl): |
| 187 | crumbs_dialog = CrumbsMessageDialog(self, lbl, gtk.MESSAGE_INFO, msg) |
| 188 | button = crumbs_dialog.add_button("Close", gtk.RESPONSE_OK) |
| 189 | HobButton.style_button(button) |
| 190 | crumbs_dialog.run() |
| 191 | crumbs_dialog.destroy() |
| 192 | |
| 193 | def update_progress_bar(self, title, fraction, status=None): |
| 194 | self.progress_bar.update(fraction) |
| 195 | self.progress_bar.set_title(title) |
| 196 | self.progress_bar.set_rcstyle(status) |
| 197 | |
| 198 | def write_file(self, ifile, ofile): |
| 199 | self.progress_bar.reset() |
| 200 | self.progress_bar.show() |
| 201 | |
| 202 | f_from = os.open(ifile, os.O_RDONLY) |
| 203 | f_to = os.open(ofile, os.O_WRONLY) |
| 204 | |
| 205 | total_size = os.stat(ifile).st_size |
| 206 | written_size = 0 |
| 207 | |
| 208 | while True: |
| 209 | buf = os.read(f_from, 1024*1024) |
| 210 | if not buf: |
| 211 | break |
| 212 | os.write(f_to, buf) |
| 213 | written_size += 1024*1024 |
| 214 | self.update_progress_bar("Writing to usb:", written_size * 1.0/total_size) |
| 215 | |
| 216 | self.update_progress_bar("Writing completed:", 1.0) |
| 217 | os.close(f_from) |
| 218 | os.close(f_to) |
| 219 | self.progress_bar.hide() |