remove arch key from default mic.conf
[tools/mic.git] / mic / creator.py
1 #!/usr/bin/python -tt
2 #
3 # Copyright 2011 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 from optparse import SUPPRESS_HELP
22
23 from mic import configmgr, pluginmgr, msger
24 from mic.utils import cmdln, errors
25
26 class Creator(cmdln.Cmdln):
27     """${name}: create an image
28
29     usage:
30         ${name} SUBCOMMAND [OPTS] [ARGS..]
31
32     ${command_list}
33     ${option_list}
34     """
35
36     name = 'mic create(cr)'
37
38     def __init__(self, *args, **kwargs):
39         cmdln.Cmdln.__init__(self, *args, **kwargs)
40
41         # load configmgr
42         self.configmgr = configmgr.getConfigMgr()
43
44         # load pluginmgr
45         self.pluginmgr = pluginmgr.PluginMgr()
46         self.plugincmds = self.pluginmgr.get_plugins('imager')
47
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)
52                 continue
53
54             func = getattr(klass, 'do_create')
55             setattr(self.__class__, "do_"+subcmd, func)
56
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')
66         return optparser
67
68     def preoptparse(self, argv):
69         optparser = self.get_optparser()
70
71         largs = []
72         rargs = []
73         while argv:
74             arg = argv.pop(0)
75
76             if arg in ('-h', '--help'):
77                 rargs.append(arg)
78
79             elif optparser.has_option(arg):
80                 largs.append(arg)
81
82                 if optparser.get_option(arg).takes_value():
83                     try:
84                         largs.append(argv.pop(0))
85                     except IndexError:
86                         raise errors.Usage("%s option requires an argument" % arg)
87
88             else:
89                 if arg.startswith("--"):
90                     if "=" in arg:
91                         opt = arg.split("=")[0]
92                     else:
93                         opt = None
94                 elif arg.startswith("-") and len(arg) > 2:
95                     opt = arg[0:2]
96                 else:
97                     opt = None
98
99                 if opt and optparser.has_option(opt):
100                     largs.append(arg)
101                 else:
102                     rargs.append(arg)
103
104         return largs + rargs
105
106     def postoptparse(self):
107         if self.options.verbose:
108             msger.set_loglevel('verbose')
109         if self.options.debug:
110             msger.set_loglevel('debug')
111
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
116
117         if self.options.release:
118             self.configmgr.create['release'] = self.options.release
119
120         if self.options.arch is not None:
121             self.configmgr.create['arch'] = self.options.arch
122
123         if self.options.logfile:
124             msger.set_interactive(False)
125             msger.set_logfile(self.options.logfile)
126
127     def main(self, argv=None):
128         if argv is None:
129             argv = sys.argv
130         else:
131             argv = argv[:] # don't modify caller's list
132
133         self.optparser = self.get_optparser()
134         if self.optparser:
135             try:
136                 argv = self.preoptparse(argv)
137                 self.options, args = self.optparser.parse_args(argv)
138
139             except cmdln.CmdlnUserError, ex:
140                 msg = "%s: %s\nTry '%s help' for info.\n"\
141                       % (self.name, ex, self.name)
142                 msger.error(msg)
143
144             except cmdln.StopOptionProcessing, ex:
145                 return 0
146         else:
147             # optparser=None means no process for opts
148             self.options, args = None, argv[1:]
149
150         self.postoptparse()
151
152         if not args:
153             return self.emptyline()
154             
155         if os.geteuid() != 0:
156             msger.error('Root permission is required to continue, abort')
157
158         return self.cmd(args)
159