3 # Copyright 2011 Intel, Inc.
5 # This copyrighted material is made available to anyone wishing to use, modify,
6 # copy, or redistribute it subject to the terms and conditions of the GNU
7 # General Public License v.2. This program is distributed in the hope that it
8 # will be useful, but WITHOUT ANY WARRANTY expressed or implied, including the
9 # implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 # See the GNU General Public License for more details.
12 # You should have received a copy of the GNU General Public License along with
13 # this program; if not, write to the Free Software Foundation, Inc., 51
14 # Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat
15 # trademarks that are incorporated in the source code or documentation are not
16 # subject to the GNU General Public License and may only be used or replicated
17 # with the express permission of Red Hat, Inc.
21 from optparse import SUPPRESS_HELP
23 from mic import configmgr, pluginmgr, msger
24 from mic.utils import cmdln, errors
26 class Creator(cmdln.Cmdln):
27 """${name}: create an image
30 ${name} SUBCOMMAND [OPTS] [ARGS..]
36 name = 'mic create(cr)'
38 def __init__(self, *args, **kwargs):
39 cmdln.Cmdln.__init__(self, *args, **kwargs)
42 self.configmgr = configmgr.getConfigMgr()
45 self.pluginmgr = pluginmgr.PluginMgr()
46 self.plugincmds = self.pluginmgr.get_plugins('imager')
48 # mix-in do_subcmd interface
49 for subcmd, klass in self.plugincmds.iteritems():
50 if not hasattr(klass, 'do_create'):
51 msger.warning("Unsurpport subcmd: %s" % subcmd)
54 func = getattr(klass, 'do_create')
55 setattr(self.__class__, "do_"+subcmd, func)
57 def get_optparser(self):
58 optparser = cmdln.CmdlnOptionParser(self)
59 optparser.add_option('-d', '--debug', action='store_true', dest='debug', help=SUPPRESS_HELP)
60 optparser.add_option('-v', '--verbose', action='store_true', dest='verbose', help=SUPPRESS_HELP)
61 optparser.add_option('-o', '--outdir', type='string', action='store', dest='outdir', default=None, help='output directory')
62 optparser.add_option('', '--local-pkgs-path', type='string', dest='local_pkgs_path', default=None, help='Path for local pkgs(rpms) to be installed')
65 def preoptparse(self, argv):
66 optparser = self.get_optparser()
73 if arg in ('-h', '--help'):
76 elif optparser.has_option(arg):
79 if optparser.get_option(arg).takes_value():
81 largs.append(argv.pop(0))
83 raise errors.Usage("%s option requires an argument" % arg)
86 if arg.startswith("--"):
88 opt = arg.split("=")[0]
91 elif arg.startswith("-") and len(arg) > 2:
96 if opt and optparser.has_option(opt):
103 def postoptparse(self):
104 if self.options.verbose:
105 msger.set_loglevel('verbose')
106 if self.options.debug:
107 msger.set_loglevel('debug')
109 if self.options.outdir is not None:
110 self.configmgr.create['outdir'] = self.options.outdir
111 if self.options.local_pkgs_path is not None:
112 self.configmgr.create['local_pkgs_path'] = self.options.local_pkgs_path
114 def main(self, argv=None):
118 argv = argv[:] # don't modify caller's list
120 self.optparser = self.get_optparser()
123 argv = self.preoptparse(argv)
124 self.options, args = self.optparser.parse_args(argv)
126 except cmdln.CmdlnUserError, ex:
127 msg = "%s: %s\nTry '%s help' for info.\n"\
128 % (self.name, ex, self.name)
131 except cmdln.StopOptionProcessing, ex:
134 # optparser=None means no process for opts
135 self.options, args = None, argv[1:]
140 return self.emptyline()
142 if os.geteuid() != 0:
143 msger.error('Root permission is required to continue, abort')
145 return self.cmd(args)