image-writer 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env python3
  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. from gi import pygtkcompat
  25. pygtkcompat.enable()
  26. pygtkcompat.enable_gtk(version='3.0')
  27. import gtk
  28. import optparse
  29. from bb.ui.crumbs.hobwidget import HobAltButton, HobButton
  30. from bb.ui.crumbs.hig.crumbsmessagedialog import CrumbsMessageDialog
  31. from bb.ui.crumbs.hig.deployimagedialog import DeployImageDialog
  32. from bb.ui.crumbs.hig.imageselectiondialog import ImageSelectionDialog
  33. # I put all the fs bitbake supported here. Need more test.
  34. DEPLOYABLE_IMAGE_TYPES = ["jffs2", "cramfs", "ext2", "ext3", "ext4", "btrfs", "squashfs", "ubi", "vmdk"]
  35. Title = "USB Image Writer"
  36. class DeployWindow(gtk.Window):
  37. def __init__(self, image_path=''):
  38. super(DeployWindow, self).__init__()
  39. if len(image_path) > 0:
  40. valid = True
  41. if not os.path.exists(image_path):
  42. valid = False
  43. lbl = "<b>Invalid image file path: %s.</b>\nPress <b>Select Image</b> to select an image." % image_path
  44. else:
  45. image_path = os.path.abspath(image_path)
  46. extend_name = os.path.splitext(image_path)[1][1:]
  47. if extend_name not in DEPLOYABLE_IMAGE_TYPES:
  48. valid = False
  49. lbl = "<b>Undeployable imge type: %s</b>\nPress <b>Select Image</b> to select an image." % extend_name
  50. if not valid:
  51. image_path = ''
  52. crumbs_dialog = CrumbsMessageDialog(self, lbl, gtk.STOCK_DIALOG_INFO)
  53. button = crumbs_dialog.add_button("Close", gtk.RESPONSE_OK)
  54. HobButton.style_button(button)
  55. crumbs_dialog.run()
  56. crumbs_dialog.destroy()
  57. self.deploy_dialog = DeployImageDialog(Title, image_path, self,
  58. gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT
  59. | gtk.DIALOG_NO_SEPARATOR, None, standalone=True)
  60. close_button = self.deploy_dialog.add_button("Close", gtk.RESPONSE_NO)
  61. HobAltButton.style_button(close_button)
  62. close_button.connect('clicked', gtk.main_quit)
  63. write_button = self.deploy_dialog.add_button("Write USB image", gtk.RESPONSE_YES)
  64. HobAltButton.style_button(write_button)
  65. self.deploy_dialog.connect('select_image_clicked', self.select_image_clicked_cb)
  66. self.deploy_dialog.connect('destroy', gtk.main_quit)
  67. response = self.deploy_dialog.show()
  68. def select_image_clicked_cb(self, dialog):
  69. cwd = os.getcwd()
  70. dialog = ImageSelectionDialog(cwd, DEPLOYABLE_IMAGE_TYPES, Title, self, gtk.FILE_CHOOSER_ACTION_SAVE )
  71. button = dialog.add_button("Cancel", gtk.RESPONSE_NO)
  72. HobAltButton.style_button(button)
  73. button = dialog.add_button("Open", gtk.RESPONSE_YES)
  74. HobAltButton.style_button(button)
  75. response = dialog.run()
  76. if response == gtk.RESPONSE_YES:
  77. if not dialog.image_names:
  78. lbl = "<b>No selections made</b>\nClicked the radio button to select a image."
  79. crumbs_dialog = CrumbsMessageDialog(self, lbl, gtk.STOCK_DIALOG_INFO)
  80. button = crumbs_dialog.add_button("Close", gtk.RESPONSE_OK)
  81. HobButton.style_button(button)
  82. crumbs_dialog.run()
  83. crumbs_dialog.destroy()
  84. dialog.destroy()
  85. return
  86. # get the full path of image
  87. image_path = os.path.join(dialog.image_folder, dialog.image_names[0])
  88. self.deploy_dialog.set_image_text_buffer(image_path)
  89. self.deploy_dialog.set_image_path(image_path)
  90. dialog.destroy()
  91. def main():
  92. parser = optparse.OptionParser(
  93. usage = """%prog [-h] [image_file]
  94. %prog writes bootable images to USB devices. You can
  95. provide the image file on the command line or select it using the GUI.""")
  96. options, args = parser.parse_args(sys.argv)
  97. image_file = args[1] if len(args) > 1 else ''
  98. dw = DeployWindow(image_file)
  99. if __name__ == '__main__':
  100. try:
  101. main()
  102. gtk.main()
  103. except Exception:
  104. import traceback
  105. traceback.print_exc()