more accurate help message for subcommands
[tools/mic.git] / mic / creator.py
1 #!/usr/bin/python -tt
2 #
3 # Copyright (c) 2011 Intel, Inc.
4 #
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation; version 2 of the License
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 # for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc., 59
16 # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18 import os, sys
19 from optparse import SUPPRESS_HELP
20
21 from mic import msger
22 from mic.utils import cmdln, errors, rpmmisc
23 from conf import configmgr
24 from plugin import pluginmgr
25
26 class Creator(cmdln.Cmdln):
27     """${name}: create an image
28
29     Usage:
30         ${name} SUBCOMMAND <ksfile> [OPTS]
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         # get cmds from pluginmgr
42         # mix-in do_subcmd interface
43         for subcmd, klass in pluginmgr.get_plugins('imager').iteritems():
44             if not hasattr(klass, 'do_create'):
45                 msger.warning("Unsurpport subcmd: %s" % subcmd)
46                 continue
47
48             func = getattr(klass, 'do_create')
49             setattr(self.__class__, "do_"+subcmd, func)
50
51     def get_optparser(self):
52         optparser = cmdln.CmdlnOptionParser(self)
53         optparser.add_option('-d', '--debug', action='store_true', dest='debug',
54                              help=SUPPRESS_HELP)
55         optparser.add_option('-v', '--verbose', action='store_true',
56                              dest='verbose',
57                              help=SUPPRESS_HELP)
58         optparser.add_option('', '--logfile', type='string', dest='logfile',
59                              default=None,
60                              help='Path of logfile')
61         optparser.add_option('-c', '--config', type='string', dest='config',
62                              default=None,
63                              help='Specify config file for mic')
64         optparser.add_option('-k', '--cachedir', type='string', action='store',
65                              dest='cachedir', default=None,
66                              help='Cache directory to store the downloaded')
67         optparser.add_option('-o', '--outdir', type='string', action='store',
68                              dest='outdir', default=None,
69                              help='Output directory')
70         optparser.add_option('-A', '--arch', type='string', dest='arch',
71                              default=None,
72                              help='Specify repo architecture')
73         optparser.add_option('', '--release', type='string', dest='release',
74                              default=None, metavar='RID',
75                              help='Generate a release of RID with all necessary'
76                                   ' files, when @BUILD_ID@ is contained in '
77                                   'kickstart file, it will be replaced by RID')
78         optparser.add_option("", "--record-pkgs", type="string",
79                              dest="record_pkgs", default=None,
80                              help='Record the info of installed packages, '
81                                   'multiple values can be specified which '
82                                   'joined by ",", valid values: "name", '
83                                   '"content", "license"')
84         optparser.add_option('', '--pkgmgr', type='string', dest='pkgmgr',
85                              default=None,
86                              help='Specify backend package manager')
87         optparser.add_option('', '--local-pkgs-path', type='string',
88                              dest='local_pkgs_path', default=None,
89                              help='Path for local pkgs(rpms) to be installed')
90         optparser.add_option('', '--runtime', type='string',
91                              dest='runtime', default=None,
92                              help='Specify  runtime mode, avaiable: bootstrap')
93         optparser.add_option('', '--compress-disk-image', type='string',
94                              dest='compress_disk_image', default=None,
95                              help='Sets the disk image compression. NOTE: The '
96                                   'available values might depend on the used '
97                                   'filesystem type.')
98         return optparser
99
100     def preoptparse(self, argv):
101         optparser = self.get_optparser()
102
103         largs = []
104         rargs = []
105         while argv:
106             arg = argv.pop(0)
107
108             if arg in ('-h', '--help'):
109                 rargs.append(arg)
110
111             elif optparser.has_option(arg):
112                 largs.append(arg)
113
114                 if optparser.get_option(arg).takes_value():
115                     try:
116                         largs.append(argv.pop(0))
117                     except IndexError:
118                         raise errors.Usage("%s option requires an argument" % arg)
119
120             else:
121                 if arg.startswith("--"):
122                     if "=" in arg:
123                         opt = arg.split("=")[0]
124                     else:
125                         opt = None
126                 elif arg.startswith("-") and len(arg) > 2:
127                     opt = arg[0:2]
128                 else:
129                     opt = None
130
131                 if opt and optparser.has_option(opt):
132                     largs.append(arg)
133                 else:
134                     rargs.append(arg)
135
136         return largs + rargs
137
138     def postoptparse(self):
139         if self.options.verbose:
140             msger.set_loglevel('verbose')
141         if self.options.debug:
142             msger.set_loglevel('debug')
143
144         if self.options.logfile:
145             msger.set_interactive(False)
146             msger.set_logfile(self.options.logfile)
147             configmgr.create['logfile'] = self.options.logfile
148
149         if self.options.config:
150             configmgr.reset()
151             configmgr._siteconf = self.options.config
152
153         if self.options.outdir is not None:
154             configmgr.create['outdir'] = self.options.outdir
155         if self.options.cachedir is not None:
156             configmgr.create['cachedir'] = self.options.cachedir
157         os.environ['ZYPP_LOCKFILE_ROOT'] = configmgr.create['cachedir']
158         if self.options.local_pkgs_path is not None:
159             configmgr.create['local_pkgs_path'] = self.options.local_pkgs_path
160
161         if self.options.release:
162             configmgr.create['release'] = self.options.release
163
164         if self.options.record_pkgs:
165             configmgr.create['record_pkgs'] = []
166             for infotype in self.options.record_pkgs.split(','):
167                 if infotype not in ('name', 'content', 'license'):
168                     raise errors.Usage('Invalid pkg recording: %s, valid ones:'
169                                        ' "name", "content", "license"' \
170                                        % infotype)
171
172                 configmgr.create['record_pkgs'].append(infotype)
173
174         if self.options.arch is not None:
175             supported_arch = sorted(rpmmisc.archPolicies.keys(), reverse=True)
176             if self.options.arch in supported_arch:
177                 configmgr.create['arch'] = self.options.arch
178             else:
179                 raise errors.Usage('Invalid architecture: "%s".\n'
180                                    '  Supported architectures are: \n'
181                                    '  %s\n' % (self.options.arch,
182                                                ', '.join(supported_arch)))
183
184         if self.options.pkgmgr is not None:
185             configmgr.create['pkgmgr'] = self.options.pkgmgr
186
187         if self.options.runtime:
188             configmgr.create['runtime'] = self.options.runtime
189
190         if self.options.compress_disk_image is not None:
191             configmgr.create['compress_disk_image'] = \
192                                                 self.options.compress_disk_image
193
194     def main(self, argv=None):
195         if argv is None:
196             argv = sys.argv
197         else:
198             argv = argv[:] # don't modify caller's list
199
200         self.optparser = self.get_optparser()
201         if self.optparser:
202             try:
203                 argv = self.preoptparse(argv)
204                 self.options, args = self.optparser.parse_args(argv)
205
206             except cmdln.CmdlnUserError, ex:
207                 msg = "%s: %s\nTry '%s help' for info.\n"\
208                       % (self.name, ex, self.name)
209                 msger.error(msg)
210
211             except cmdln.StopOptionProcessing, ex:
212                 return 0
213         else:
214             # optparser=None means no process for opts
215             self.options, args = None, argv[1:]
216
217         if not args:
218             return self.emptyline()
219
220         self.postoptparse()
221
222         if os.geteuid() != 0 and args[0] != 'help':
223             msger.error('root permission is required to continue, abort')
224
225         return self.cmd(args)
226