3 from __future__ import with_statement
10 import micng.utils.cmdln as cmdln
11 import micng.configmgr as configmgr
12 import micng.pluginmgr as pluginmgr
14 class Creator(cmdln.Cmdln):
20 name = 'micng create(cr)'
25 def __init__(self, *args, **kwargs):
26 cmdln.Cmdln.__init__(self, *args, **kwargs)
28 self.configmgr = configmgr.getConfigMgr()
30 self.pluginmgr = pluginmgr.PluginMgr()
31 self.pluginmgr.loadPlugins()
32 self.plugincmds = self.pluginmgr.getImagerPlugins()
33 # mix-in do_subcmd interface
34 for subcmd, klass in self.plugincmds:
35 if not hasattr(klass, 'do_create'):
36 logging.warn("Unsurpport subcmd: %s" % subcmd)
38 func = getattr(klass, 'do_create')
39 setattr(self.__class__, "do_"+subcmd, func)
41 def get_optparser(self):
42 optparser = cmdln.CmdlnOptionParser(self)
43 optparser.add_option('-d', '--debug', action='store_true', help='print debug info')
44 optparser.add_option('-v', '--verbose', action='store_true', help='verbose output')
45 #optparser.add_option('-o', '--outdir', type='string', action='store', dest='outdir', default=None, help='output directory')
48 def preoptparse(self, argv):
51 def postoptparse(self):
52 if self.options.verbose is True:
53 logging.getLogger().setLevel(logging.INFO)
54 if self.options.debug is True:
55 logging.getLogger().setLevel(logging.DEBUG)
56 #if self.options.outdir is not None:
57 # self.configmgr.create['outdir'] = self.options.outdir
59 def main(self, argv=None):
63 argv = argv[:] # don't modify caller's list
65 self.optparser = self.get_optparser()
66 if self.optparser: # i.e. optparser=None means don't process for opts
68 self.preoptparse(argv)
69 self.options, args = self.optparser.parse_args(argv)
70 except cmdln.CmdlnUserError, ex:
71 msg = "%s: %s\nTry '%s help' for info.\n"\
72 % (self.name, ex, self.name)
73 self.stderr.write(self._str(msg))
76 except cmdln.StopOptionProcessing, ex:
79 self.options, args = None, argv[1:]
84 print >> sys.stderr, "You must run %s as root" % sys.argv[0]
89 return self.emptyline()