image-writer 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/env python
  2. # Copyright (c) 2012 Wind River Systems, Inc.
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License version 2 as
  6. # published by the Free Software Foundation.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. # See the GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software
  15. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. import os
  17. import sys
  18. sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname( \
  19. os.path.abspath(__file__))), 'lib'))
  20. try:
  21. import bb
  22. except RuntimeError as exc:
  23. sys.exit(str(exc))
  24. import gtk
  25. import optparse
  26. import pygtk
  27. from bb.ui.crumbs.hobwidget import HobAltButton, HobButton
  28. from bb.ui.crumbs.hig.crumbsmessagedialog import CrumbsMessageDialog
  29. from bb.ui.crumbs.hig.deployimagedialog import DeployImageDialog
  30. from bb.ui.crumbs.hig.imageselectiondialog import ImageSelectionDialog
  31. # I put all the fs bitbake supported here. Need more test.
  32. DEPLOYABLE_IMAGE_TYPES = ["jffs2", "cramfs", "ext2", "ext3", "ext4", "btrfs", "squashfs", "ubi", "vmdk"]
  33. Title = "USB Image Writer"
  34. class DeployWindow(gtk.Window):
  35. def __init__(self, image_path=''):
  36. super(DeployWindow, self).__init__()
  37. if len(image_path) > 0:
  38. valid = True
  39. if not os.path.exists(image_path):
  40. valid = False
  41. lbl = "<b>Invalid image file path: %s.</b>\nPress <b>Select Image</b> to select an image." % image_path
  42. else:
  43. image_path = os.path.abspath(image_path)
  44. extend_name = os.path.splitext(image_path)[1][1:]
  45. if extend_name not in DEPLOYABLE_IMAGE_TYPES:
  46. valid = False
  47. lbl = "<b>Undeployable imge type: %s</b>\nPress <b>Select Image</b> to select an image." % extend_name
  48. if not valid:
  49. image_path = ''
  50. crumbs_dialog = CrumbsMessageDialog(self, lbl, gtk.STOCK_DIALOG_INFO)
  51. button = crumbs_dialog.add_button("Close", gtk.RESPONSE_OK)
  52. HobButton.style_button(button)
  53. crumbs_dialog.run()
  54. crumbs_dialog.destroy()
  55. self.deploy_dialog = DeployImageDialog(Title, image_path, self,
  56. gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT
  57. | gtk.DIALOG_NO_SEPARATOR, None, standalone=True)
  58. close_button = self.deploy_dialog.add_button("Close", gtk.RESPONSE_NO)
  59. HobAltButton.style_button(close_button)
  60. close_button.connect('clicked', gtk.main_quit)
  61. write_button = self.deploy_dialog.add_button("Write USB image", gtk.RESPONSE_YES)
  62. HobAltButton.style_button(write_button)
  63. self.deploy_dialog.connect('select_image_clicked', self.select_image_clicked_cb)
  64. self.deploy_dialog.connect('destroy', gtk.main_quit)
  65. response = self.deploy_dialog.show()
  66. def select_image_clicked_cb(self, dialog):
  67. cwd = os.getcwd()
  68. dialog = ImageSelectionDialog(cwd, DEPLOYABLE_IMAGE_TYPES, Title, self, gtk.FILE_CHOOSER_ACTION_SAVE )
  69. button = dialog.add_button("Cancel", gtk.RESPONSE_NO)
  70. HobAltButton.style_button(button)
  71. button = dialog.add_button("Open", gtk.RESPONSE_YES)
  72. HobAltButton.style_button(button)
  73. response = dialog.run()
  74. if response == gtk.RESPONSE_YES:
  75. if not dialog.image_names:
  76. lbl = "<b>No selections made</b>\nClicked the radio button to select a image."
  77. crumbs_dialog = CrumbsMessageDialog(self, lbl, gtk.STOCK_DIALOG_INFO)
  78. button = crumbs_dialog.add_button("Close", gtk.RESPONSE_OK)
  79. HobButton.style_button(button)
  80. crumbs_dialog.run()
  81. crumbs_dialog.destroy()
  82. dialog.destroy()
  83. return
  84. # get the full path of image
  85. image_path = os.path.join(dialog.image_folder, dialog.image_names[0])
  86. self.deploy_dialog.set_image_text_buffer(image_path)
  87. self.deploy_dialog.set_image_path(image_path)
  88. dialog.destroy()
  89. def main():
  90. parser = optparse.OptionParser(
  91. usage = """%prog [-h] [image_file]
  92. %prog writes bootable images to USB devices. You can
  93. provide the image file on the command line or select it using the GUI.""")
  94. options, args = parser.parse_args(sys.argv)
  95. image_file = args[1] if len(args) > 1 else ''
  96. dw = DeployWindow(image_file)
  97. if __name__ == '__main__':
  98. try:
  99. main()
  100. gtk.main()
  101. except Exception:
  102. import traceback
  103. traceback.print_exc(3)