#!/usr/bin/python -tt # # Copyright 2008, 2009, 2010, 2011 Intel, Inc. # # This copyrighted material is made available to anyone wishing to use, modify, # copy, or redistribute it subject to the terms and conditions of the GNU # General Public License v.2. This program is distributed in the hope that it # will be useful, but WITHOUT ANY WARRANTY expressed or implied, including the # implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # See the GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along with # this program; if not, write to the Free Software Foundation, Inc., 51 # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat # trademarks that are incorporated in the source code or documentation are not # subject to the GNU General Public License and may only be used or replicated # with the express permission of Red Hat, Inc. # import os, sys from mic import msger, creator, configmgr, pluginmgr from mic.utils import cmdln, misc, errors from mic.__version__ import VERSION class Mic(cmdln.Cmdln): """ Usage: mic SUBCOMMAND [OPTS] [ARGS...] Mic Image Creation Tool. Try 'mic help SUBCOMAND' for help on a specific subcommand. ${command_list} global ${option_list} ${help_list} """ name = 'mic' version = VERSION def get_optparser(self): optparser = cmdln.CmdlnOptionParser(self, version=self.version) optparser.add_option('-d', '--debug', action='store_true', dest='debug', help='print debug message') optparser.add_option('-v', '--verbose', action='store_true', dest='verbose', help='verbose infomation') return optparser def postoptparse(self): if self.options.verbose: msger.set_loglevel('verbose') if self.options.debug: try: import rpm rpm.setVerbosity(rpm.RPMLOG_NOTICE) except ImportError: pass msger.set_loglevel('debug') def help_create(self): cr = creator.Creator() cr.optparser = cr.get_optparser() doc = cr.__doc__ doc = cr._help_reindent(doc) doc = cr._help_preprocess(doc, None) doc = doc.replace(cr.name, "${cmd_name}", 1) doc = doc.rstrip() + '\n' return doc @cmdln.alias("cr") def do_create(self, argv): try: cr = creator.Creator() cr.main(argv[1:]) except: raise def _root_confirm(self): if os.geteuid() != 0: msger.error('Root permission is required to continue, abort') @cmdln.alias("cv") def do_convert(self, subcmd, opts, *args): """${cmd_name}: convert image format usage: mic convert ${cmd_option_list} """ if not args: # print help handler = self._get_cmd_handler('convert') if hasattr(handler, "optparser"): handler.optparser.print_help() return 1 if len(args) == 1: raise errors.Usage("It need 2 arguments (1 given)") elif len(args) == 2: (srcimg, destformat) = args else: raise errors.Usage("Extra argument given") self._root_confirm() srcformat = misc.get_image_type(srcimg) if srcformat == "ext3fsimg": srcformat = "loop" srcimager = None destimager = None for iname, icls in pluginmgr.PluginMgr().get_plugins('imager').iteritems(): if iname == srcformat and hasattr(icls, "do_unpack"): srcimager = icls if iname == destformat and hasattr(icls, "do_pack"): destimager = icls if (srcimager and destimager) is None: raise errors.CreatorError("Can't convert from %s to %s" %(srcformat, destformat)) else: base_on = srcimager.do_unpack(srcimg) destimager.do_pack(base_on) @cmdln.alias("ch") def do_chroot(self, subcmd, opts, *args): """${cmd_name}: chroot into an image usage: mic chroot ${cmd_option_list} """ if not args: # print help handler = self._get_cmd_handler('chroot') if hasattr(handler, "optparser"): handler.optparser.print_help() return 1 if len(args) == 1: targetimage = args[0] else: raise errors.Usage("Extra argument given") self._root_confirm() imagetype = misc.get_image_type(targetimage) if not imagetype: imagetype = "fs" if imagetype == "ext3fsimg": imagetype = "loop" chrootclass = None for pname, pcls in pluginmgr.PluginMgr().get_plugins('imager').iteritems(): if pname == imagetype and hasattr(pcls, "do_chroot"): chrootclass = pcls break if not chrootclass: raise errors.CreatorError("Cannot support image type: %s" % imagetype) chrootclass.do_chroot(targetimage) if __name__ == "__main__": try: mic = Mic() sys.exit(mic.main()) except KeyboardInterrupt: msger.error('\n^C catched, program aborted.') except errors.Usage, usage: msger.error(str(usage)) except errors.CreatorError, err: if msger.get_loglevel() == 'debug': import traceback msger.error(traceback.format_exc()) else: msger.error(str(err))