import os,sys
import re
-__ALL__ = ['set_mode', 'set_loglevel', 'raw' 'debug', 'verbose', 'info', 'warning', 'error', 'ask', 'pause']
+__ALL__ = ['set_mode', 'get_loglevel', 'set_loglevel', 'raw' 'debug', 'verbose', 'info', 'warning', 'error', 'ask', 'pause']
# COLORs in ANSI
INFO_COLOR = 32 # green
return head, msg
+def get_loglevel():
+ return (k for k,v in LOG_LEVELS.items() if v==LOG_LEVEL).next()
+
def set_loglevel(level):
global LOG_LEVEL
if level not in LOG_LEVELS:
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-class Usage(Exception):
- def __init__(self, msg=None):
- Exception.__init__(self, msg)
-
-class ConfigError(Exception):
- def __init__(self, msg=None):
- Exception.__init__(self, msg)
-
class CreatorError(Exception):
"""An exception base class for all imgcreate errors."""
+ keyword = '<creator>'
+
def __init__(self, msg):
- Exception.__init__(self, msg)
+ self.msg = msg
+
+ def __str__(self):
+ return self.keyword + repr(self.msg)
+
+class ConfigError(CreatorError):
+ keyword = '<config>'
+
+class Usage(CreatorError):
+ keyword = '<usage>'
class KsError(CreatorError):
- pass
+ keyword = '<kickstart>'
class MountError(CreatorError):
- pass
+ keyword = '<mount>'
class SnapshotError(CreatorError):
- pass
+ keyword = '<snapshot>'
class SquashfsError(CreatorError):
- pass
+ keyword = '<squashfs>'
#!/usr/bin/python -tt
#
-# Copyright 2008, 2009, 2010 Intel, Inc.
+# 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
#
import os, sys
-
-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
-from mic import msger
-from mic.utils import cmdln
-try:
- from mic.__version__ import VERSION
-except:
- VERSION = 'Unknown'
+from mic import msger, creator, configmgr, pluginmgr
+from mic.utils import cmdln, misc, errors
+from mic.__version__ import VERSION
class Mic(cmdln.Cmdln):
"""
def postoptparse(self):
if self.options.verbose:
- msger.set_loglevel('verbose')
+ msger.set_loglevel('verbose')
if self.options.debug:
- msger.set_loglevel('debug')
+ msger.set_loglevel('debug')
@cmdln.alias("cr")
def do_create(self, argv):
doc = doc.rstrip() + '\n'
return doc
+ 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
${cmd_option_list}
"""
- if len(args) == 0:
+
+ if not args:
# print help
handler = self._get_cmd_handler('convert')
if hasattr(handler, "optparser"):
handler.optparser.print_help()
return
+
if len(args) == 1:
- raise errors.Usage("It takes 2 arguments (1 given)")
+ raise errors.Usage("It need 2 arguments (1 given)")
elif len(args) == 2:
- srcimg = args[0]
- destformat = args[1]
+ (srcimg, destformat) = args
else:
raise errors.Usage("Extra argument given")
- if os.geteuid() != 0:
- raise errors.Usage("You must run as root")
+ self._root_confirm()
srcformat = misc.get_image_type(srcimg)
if srcformat == "ext3fsimg":
${cmd_option_list}
"""
- if len(args) == 0:
+
+ if not args:
# print help
handler = self._get_cmd_handler('chroot')
if hasattr(handler, "optparser"):
handler.optparser.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")
+ self._root_confirm()
imagetype = misc.get_image_type(targetimage)
if not imagetype:
break
if not chrootclass:
- raise errors.CreatorError("Don't support image type: %s" % imagetype)
+ raise errors.CreatorError("Cannot support image type: %s" % imagetype)
chrootclass.do_chroot(targetimage)
mic = Mic()
sys.exit(mic.main())
- except errors.Usage, msg:
- msger.error("<usage> %s\n" % msg)
-
- except errors.ConfigError, msg:
- msger.error("<config> %s\n" % msg)
-
- except errors.CreatorError, msg:
- msger.error("<creator> %s\n" % msg)
+ except (errors.Usage, errors.ConfigError, errors.CreatorError) as err:
+ if msger.get_loglevel() == 'debug':
+ import traceback
+ msger.error(traceback.format_exc())
+ else:
+ msger.error(str(err))