3 # Copyright 2008, 2009, 2010 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.
23 import mic.utils.cmdln as cmdln
24 import mic.configmgr as configmgr
25 import mic.pluginmgr as pluginmgr
28 class Creator(cmdln.Cmdln):
29 """${name}: create an image
32 ${name} SUBCOMMAND [OPTS] [ARGS..]
38 name = 'mic create(cr)'
40 def __init__(self, *args, **kwargs):
41 cmdln.Cmdln.__init__(self, *args, **kwargs)
44 self.configmgr = configmgr.getConfigMgr()
47 self.pluginmgr = pluginmgr.PluginMgr()
48 self.pluginmgr.loadPlugins()
49 self.plugincmds = self.pluginmgr.getImagerPlugins()
51 # mix-in do_subcmd interface
52 for subcmd, klass in self.plugincmds:
53 if not hasattr(klass, 'do_create'):
54 logging.warn("Unsurpport subcmd: %s" % subcmd)
56 func = getattr(klass, 'do_create')
57 setattr(self.__class__, "do_"+subcmd, func)
59 def get_optparser(self):
60 optparser = cmdln.CmdlnOptionParser(self)
61 optparser.add_option('-d', '--debug', action='store_true', help='print debug info')
62 optparser.add_option('-v', '--verbose', action='store_true', help='verbose output')
63 #optparser.add_option('-o', '--outdir', type='string', action='store', dest='outdir', default=None, help='output directory')
66 def preoptparse(self, argv):
69 def postoptparse(self):
70 if self.options.verbose is True:
71 logging.getLogger().setLevel(logging.INFO)
72 if self.options.debug is True:
73 logging.getLogger().setLevel(logging.DEBUG)
75 #if self.options.outdir is not None:
76 # self.configmgr.create['outdir'] = self.options.outdir
78 def main(self, argv=None):
82 argv = argv[:] # don't modify caller's list
84 self.optparser = self.get_optparser()
87 self.preoptparse(argv)
88 self.options, args = self.optparser.parse_args(argv)
89 except cmdln.CmdlnUserError, ex:
90 msg = "%s: %s\nTry '%s help' for info.\n"\
91 % (self.name, ex, self.name)
92 self.stderr.write(self._str(msg))
95 except cmdln.StopOptionProcessing, ex:
98 # optparser=None means no process for opts
99 self.options, args = None, argv[1:]
104 if os.geteuid() != 0:
105 msger.error('Need root permission to run this command')
107 return self.cmd(args)
110 return self.emptyline()