Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | #!/usr/bin/env 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 OptionParser, SUPPRESS_HELP |
| 20 | |
| 21 | from wic import msger |
| 22 | from wic.utils import errors |
| 23 | from wic.conf import configmgr |
| 24 | from wic.plugin import pluginmgr |
| 25 | |
| 26 | |
| 27 | class Creator(object): |
| 28 | """${name}: create an image |
| 29 | |
| 30 | Usage: |
| 31 | ${name} SUBCOMMAND <ksfile> [OPTS] |
| 32 | |
| 33 | ${command_list} |
| 34 | ${option_list} |
| 35 | """ |
| 36 | |
| 37 | name = 'wic create(cr)' |
| 38 | |
| 39 | def __init__(self, *args, **kwargs): |
| 40 | self._subcmds = {} |
| 41 | |
| 42 | # get cmds from pluginmgr |
| 43 | # mix-in do_subcmd interface |
| 44 | for subcmd, klass in pluginmgr.get_plugins('imager').iteritems(): |
| 45 | if not hasattr(klass, 'do_create'): |
| 46 | msger.warning("Unsupported subcmd: %s" % subcmd) |
| 47 | continue |
| 48 | |
| 49 | func = getattr(klass, 'do_create') |
| 50 | self._subcmds[subcmd] = func |
| 51 | |
| 52 | def get_optparser(self): |
| 53 | optparser = OptionParser() |
| 54 | optparser.add_option('-d', '--debug', action='store_true', |
| 55 | dest='debug', |
| 56 | help=SUPPRESS_HELP) |
| 57 | optparser.add_option('-v', '--verbose', action='store_true', |
| 58 | dest='verbose', |
| 59 | help=SUPPRESS_HELP) |
| 60 | optparser.add_option('', '--logfile', type='string', dest='logfile', |
| 61 | default=None, |
| 62 | help='Path of logfile') |
| 63 | optparser.add_option('-c', '--config', type='string', dest='config', |
| 64 | default=None, |
| 65 | help='Specify config file for wic') |
| 66 | optparser.add_option('-o', '--outdir', type='string', action='store', |
| 67 | dest='outdir', default=None, |
| 68 | help='Output directory') |
| 69 | optparser.add_option('', '--tmpfs', action='store_true', dest='enabletmpfs', |
| 70 | help='Setup tmpdir as tmpfs to accelerate, experimental' |
| 71 | ' feature, use it if you have more than 4G memory') |
| 72 | return optparser |
| 73 | |
| 74 | def postoptparse(self, options): |
| 75 | abspath = lambda pth: os.path.abspath(os.path.expanduser(pth)) |
| 76 | |
| 77 | if options.verbose: |
| 78 | msger.set_loglevel('verbose') |
| 79 | if options.debug: |
| 80 | msger.set_loglevel('debug') |
| 81 | |
| 82 | if options.logfile: |
| 83 | logfile_abs_path = abspath(options.logfile) |
| 84 | if os.path.isdir(logfile_abs_path): |
| 85 | raise errors.Usage("logfile's path %s should be file" |
| 86 | % options.logfile) |
| 87 | if not os.path.exists(os.path.dirname(logfile_abs_path)): |
| 88 | os.makedirs(os.path.dirname(logfile_abs_path)) |
| 89 | msger.set_interactive(False) |
| 90 | msger.set_logfile(logfile_abs_path) |
| 91 | configmgr.create['logfile'] = options.logfile |
| 92 | |
| 93 | if options.config: |
| 94 | configmgr.reset() |
| 95 | configmgr._siteconf = options.config |
| 96 | |
| 97 | if options.outdir is not None: |
| 98 | configmgr.create['outdir'] = abspath(options.outdir) |
| 99 | |
| 100 | cdir = 'outdir' |
| 101 | if os.path.exists(configmgr.create[cdir]) \ |
| 102 | and not os.path.isdir(configmgr.create[cdir]): |
| 103 | msger.error('Invalid directory specified: %s' \ |
| 104 | % configmgr.create[cdir]) |
| 105 | |
| 106 | if options.enabletmpfs: |
| 107 | configmgr.create['enabletmpfs'] = options.enabletmpfs |
| 108 | |
| 109 | def main(self, argv=None): |
| 110 | if argv is None: |
| 111 | argv = sys.argv |
| 112 | else: |
| 113 | argv = argv[:] # don't modify caller's list |
| 114 | |
| 115 | pname = argv[0] |
| 116 | if pname not in self._subcmds: |
| 117 | msger.error('Unknown plugin: %s' % pname) |
| 118 | |
| 119 | optparser = self.get_optparser() |
| 120 | options, args = optparser.parse_args(argv) |
| 121 | |
| 122 | self.postoptparse(options) |
| 123 | |
| 124 | return self._subcmds[pname](options, *args[1:]) |