cleanup cmdln opts and update cmdln module to latest
[tools/mic.git] / mic / creator.py
1 #!/usr/bin/python -tt
2 #
3 # Copyright 2008, 2009, 2010 Intel, Inc.
4 #
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.
11 #
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.
18 #
19
20 import os, sys
21 import logging
22
23 from mic import configmgr
24 from mic import pluginmgr
25 from mic import msger
26 from mic.utils import cmdln
27
28 class Creator(cmdln.Cmdln):
29     """${name}: create an image
30
31     usage:
32         ${name} SUBCOMMAND [OPTS] [ARGS..]
33
34     ${command_list}
35     ${option_list}
36     """
37
38     name = 'mic create(cr)'
39
40     def __init__(self, *args, **kwargs):
41         cmdln.Cmdln.__init__(self, *args, **kwargs)
42
43         # load configmgr
44         self.configmgr = configmgr.getConfigMgr()
45
46         # load pluginmgr
47         self.pluginmgr = pluginmgr.PluginMgr()
48         self.pluginmgr.loadPlugins()
49         self.plugincmds = self.pluginmgr.getImagerPlugins()
50
51         # mix-in do_subcmd interface
52         for subcmd, klass in self.plugincmds:
53             if not hasattr(klass, 'do_create'):
54                 msger.warning("Unsurpport subcmd: %s" % subcmd)
55                 continue
56             func = getattr(klass, 'do_create')
57             setattr(self.__class__, "do_"+subcmd, func)
58
59     def get_optparser(self):
60         optparser = cmdln.CmdlnOptionParser(self)
61     #    #optparser.add_option('-o', '--outdir', type='string', action='store', dest='outdir', default=None, help='output directory')
62         return optparser
63
64     def preoptparse(self, argv):
65         pass
66
67     def postoptparse(self):
68         pass
69         #if self.options.outdir is not None:
70         #    self.configmgr.create['outdir'] = self.options.outdir
71
72     def main(self, argv=None):
73         if argv is None:
74             argv = sys.argv
75         else:
76             argv = argv[:] # don't modify caller's list
77
78         self.optparser = self.get_optparser()
79         if self.optparser:
80             try:
81                 self.preoptparse(argv)
82                 self.options, args = self.optparser.parse_args(argv)
83
84             except cmdln.CmdlnUserError, ex:
85                 msg = "%s: %s\nTry '%s help' for info.\n"\
86                       % (self.name, ex, self.name)
87                 msger.error(msg)
88
89             except cmdln.StopOptionProcessing, ex:
90                 return 0
91         else:
92             # optparser=None means no process for opts
93             self.options, args = None, argv[1:]
94
95         self.postoptparse()
96
97         if args:
98             if os.geteuid() != 0:
99                 msger.error('Need root permission to run this command')
100
101             return self.cmd(args)
102
103         else:
104             return self.emptyline()