3 # Copyright (c) 2011 Intel, Inc.
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; version 2 of the License
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc., 59
16 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 from mic import msger, creator
20 from mic.utils import cmdln, misc, errors
21 from mic.conf import configmgr
22 from mic.plugin import pluginmgr
23 from mic.__version__ import VERSION
25 class MicCmd(cmdln.Cmdln):
27 Usage: mic SUBCOMMAND [OPTS] [ARGS...]
29 mic Means the Image Creation tool
30 Try 'mic help SUBCOMMAND' for help on a specific subcommand.
40 def get_optparser(self):
41 optparser = cmdln.CmdlnOptionParser(self, version=self.version)
42 optparser.add_option('-d', '--debug', action='store_true',
44 help='print debug message')
45 optparser.add_option('-v', '--verbose', action='store_true',
47 help='verbose information')
50 def postoptparse(self):
51 if self.options.verbose:
52 msger.set_loglevel('verbose')
54 if self.options.debug:
57 rpm.setVerbosity(rpm.RPMLOG_NOTICE)
61 msger.set_loglevel('debug')
63 def help_create(self):
64 cr = creator.Creator()
65 cr.optparser = cr.get_optparser()
67 doc = cr._help_reindent(doc)
68 doc = cr._help_preprocess(doc, None)
69 doc = doc.replace(cr.name, "${cmd_name}", 1)
70 doc = doc.rstrip() + '\n'
74 def do_create(self, argv):
76 cr = creator.Creator()
81 def _root_confirm(self):
83 msger.error('Root permission is required to continue, abort')
86 @cmdln.option("-S", "--shell",
87 action="store_true", dest="shell", default=False,
88 help="Launch shell before packaging the converted image")
89 def do_convert(self, subcmd, opts, *args):
90 """${cmd_name}: convert image format
93 mic convert <imagefile> <destformat>
100 handler = self._get_cmd_handler('convert')
101 if hasattr(handler, "optparser"):
102 handler.optparser.print_help()
106 raise errors.Usage("It need 2 arguments (1 given)")
108 (srcimg, destformat) = args
110 raise errors.Usage("Extra argument given")
112 if not os.path.exists(srcimg):
113 raise errors.CreatorError("Cannot find the image: %s" % srcimg)
117 configmgr.convert['shell'] = opts.shell
119 srcformat = misc.get_image_type(srcimg)
120 if srcformat == "ext3fsimg":
125 for iname, icls in pluginmgr.get_plugins('imager').iteritems():
126 if iname == srcformat and hasattr(icls, "do_unpack"):
128 if iname == destformat and hasattr(icls, "do_pack"):
131 if (srcimager and destimager) is None:
132 raise errors.CreatorError("Can't convert from %s to %s" \
133 % (srcformat, destformat))
135 base_on = srcimager.do_unpack(srcimg)
136 destimager.do_pack(base_on)
139 @cmdln.option('-s', '--saveto',
140 action='store', dest='saveto', default=None,
141 help="Save the unpacked image to specified dir")
142 def do_chroot(self, subcmd, opts, *args):
143 """${cmd_name}: chroot into an image
146 mic chroot <imagefile>
153 handler = self._get_cmd_handler('chroot')
154 if hasattr(handler, "optparser"):
155 handler.optparser.print_help()
159 targetimage = args[0]
161 raise errors.Usage("Extra argument given")
163 if not os.path.exists(targetimage):
164 raise errors.CreatorError("Cannot find the image: %s"
169 configmgr.chroot['saveto'] = opts.saveto
171 imagetype = misc.get_image_type(targetimage)
172 if imagetype in ("ext3fsimg", "ext4fsimg", "btrfsimg"):
176 for pname, pcls in pluginmgr.get_plugins('imager').iteritems():
177 if pname == imagetype and hasattr(pcls, "do_chroot"):
182 raise errors.CreatorError("Cannot support image type: %s" \
185 chrootclass.do_chroot(targetimage)
187 if __name__ == "__main__":
192 except KeyboardInterrupt:
193 msger.error('\n^C catched, program aborted.')
195 # catch 'no space left' exception, etc
197 if e.errno == errno.ENOSPC:
198 msger.error('\nNo space left on device')
201 except errors.Usage, usage:
202 msger.error(str(usage))
204 except errors.Abort, msg:
207 except errors.CreatorError, err:
208 if msger.get_loglevel() == 'debug':
210 msger.error(traceback.format_exc())
212 msger.error('\n'+str(err))