#!/usr/bin/python -tt # # Copyright 2008, 2009, 2010 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 import logging from mic.__version__ import VERSION import mic.utils.cmdln as cmdln import mic.utils.misc as misc import mic.utils.errors as errors import mic.configmgr as configmgr import mic.pluginmgr as pluginmgr import mic.creator as creator class Mic(cmdln.Cmdln): """ Usage: mic SUBCOMMAND [OPTS] [ARGS...] MeeGo Image Creator Tool. ${command_list} ${help_list} global ${option_list} """ name = 'mic' version = VERSION @cmdln.alias("cr") def do_create(self, argv): """${cmd_name}: create image ${cmd_usage} ${cmd_option_list} """ cr = creator.Creator() ret = cr.main(argv[1:]) return ret @cmdln.alias("cv") def do_convert(self, subcmd, opts, *args): """${cmd_name}: convert an image format to another one usage: mic convert """ if len(args) == 0: self.emptyline() # print help return if len(args) == 1: raise errors.Usage("It takes 2 arguments (1 given)") elif len(args) == 2: srcimg = args[0] destformat = args[1] else: raise errors.Usage("Extra argument given") if os.geteuid() != 0: raise errors.Usage("You must run as root") srcformat = misc.get_image_type(srcimg) if srcformat == "ext3fsimg": srcformat = "loop" pkgmgr = pluginmgr.PluginMgr() pkgmgr.loadPlugins() imagers = pkgmgr.getImagerPlugins() srcimager = None destimager = None for iname, icls in imagers: 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.option("-d", "--debug", action="store_true", help="debug message") @cmdln.option("-v", "--verbose", action="store_true", help="verbose infomation") @cmdln.alias("ch") def do_chroot(self, subcmd, opts, *args): """${cmd_name}: chroot an image usage: mic chroot ${cmd_option_list} """ if len(args) == 0: self.emptyline() # print help return if len(args) == 1: targetimage = args[0] else: raise errors.Usage("Extra argument given") if os.geteuid() != 0: raise errors.Usage("You must run as root") # Fixeme? sub-logger to be used if opts.verbose: logging.getLogger().setLevel(logging.INFO) if opts.debug: logging.getLogger().setLevel(logging.DEBUG) imagetype = misc.get_image_type(targetimage) if not imagetype: imagetype = "fs" if imagetype == "ext3fsimg": imagetype = "loop" pkgmgr = pluginmgr.PluginMgr() pkgmgr.loadPlugins() chrootclass = None for (pname, pcls) in pkgmgr.getImagerPlugins(): if pname == imagetype and hasattr(pcls, "do_chroot"): chrootclass = pcls break if not chrootclass: raise errors.CreatorError("Don't support image type: %s" % imagetype) chrootclass.do_chroot(targetimage) if __name__ == "__main__": logging.getLogger().setLevel(logging.ERROR) mic = Mic() try: ret = mic.main() except errors.CreatorError, msg: ret = 2 print >> sys.stderr, msg except errors.Usage, msg: ret = 2 print >> sys.stderr, msg sys.exit(ret)