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')
63 optparser.add_option('', '--logfile', type='string', dest='logfile', default=None, help='Path of logfile')
64 optparser.add_option('', '--release', type='string', dest='release', default=None, help='Generate release package')
65 optparser.add_option('-A', '--arch', type='string', dest='arch', default=None, help='Specify repo architecture')
68 def preoptparse(self, argv):
69 optparser = self.get_optparser()
76 if arg in ('-h', '--help'):
79 elif optparser.has_option(arg):
82 if optparser.get_option(arg).takes_value():
84 largs.append(argv.pop(0))
86 raise errors.Usage("%s option requires an argument" % arg)
89 if arg.startswith("--"):
91 opt = arg.split("=")[0]
94 elif arg.startswith("-") and len(arg) > 2:
99 if opt and optparser.has_option(opt):
106 def postoptparse(self):
107 if self.options.verbose:
108 msger.set_loglevel('verbose')
109 if self.options.debug:
110 msger.set_loglevel('debug')
112 if self.options.outdir is not None:
113 self.configmgr.create['outdir'] = self.options.outdir
114 if self.options.local_pkgs_path is not None:
115 self.configmgr.create['local_pkgs_path'] = self.options.local_pkgs_path
117 if self.options.release:
118 self.configmgr.create['release'] = self.options.release
120 if self.options.arch is not None:
121 self.configmgr.create['arch'] = self.options.arch
123 if self.options.logfile:
124 msger.set_interactive(False)
125 msger.set_logfile(self.options.logfile)
127 def main(self, argv=None):
131 argv = argv[:] # don't modify caller's list
133 self.optparser = self.get_optparser()
136 argv = self.preoptparse(argv)
137 self.options, args = self.optparser.parse_args(argv)
139 except cmdln.CmdlnUserError, ex:
140 msg = "%s: %s\nTry '%s help' for info.\n"\
141 % (self.name, ex, self.name)
144 except cmdln.StopOptionProcessing, ex:
147 # optparser=None means no process for opts
148 self.options, args = None, argv[1:]
153 return self.emptyline()
155 if os.geteuid() != 0:
156 msger.error('Root permission is required to continue, abort')
158 return self.cmd(args)