summaryrefslogtreecommitdiffstats
path: root/scripts/lib/wic/creator.py
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/lib/wic/creator.py')
-rw-r--r--scripts/lib/wic/creator.py187
1 files changed, 187 insertions, 0 deletions
diff --git a/scripts/lib/wic/creator.py b/scripts/lib/wic/creator.py
new file mode 100644
index 0000000000..2219377b38
--- /dev/null
+++ b/scripts/lib/wic/creator.py
@@ -0,0 +1,187 @@
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
18import os, sys, re
19from optparse import SUPPRESS_HELP
20
21from wic import msger
22from wic.utils import cmdln, errors
23from wic.conf import configmgr
24from wic.plugin import pluginmgr
25
26
27class Creator(cmdln.Cmdln):
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 cmdln.Cmdln.__init__(self, *args, **kwargs)
41 self._subcmds = []
42
43 # get cmds from pluginmgr
44 # mix-in do_subcmd interface
45 for subcmd, klass in pluginmgr.get_plugins('imager').iteritems():
46 if not hasattr(klass, 'do_create'):
47 msger.warning("Unsupported subcmd: %s" % subcmd)
48 continue
49
50 func = getattr(klass, 'do_create')
51 setattr(self.__class__, "do_"+subcmd, func)
52 self._subcmds.append(subcmd)
53
54 def get_optparser(self):
55 optparser = cmdln.CmdlnOptionParser(self)
56 optparser.add_option('-d', '--debug', action='store_true',
57 dest='debug',
58 help=SUPPRESS_HELP)
59 optparser.add_option('-v', '--verbose', action='store_true',
60 dest='verbose',
61 help=SUPPRESS_HELP)
62 optparser.add_option('', '--logfile', type='string', dest='logfile',
63 default=None,
64 help='Path of logfile')
65 optparser.add_option('-c', '--config', type='string', dest='config',
66 default=None,
67 help='Specify config file for wic')
68 optparser.add_option('-o', '--outdir', type='string', action='store',
69 dest='outdir', default=None,
70 help='Output directory')
71 optparser.add_option('', '--tmpfs', action='store_true', dest='enabletmpfs',
72 help='Setup tmpdir as tmpfs to accelerate, experimental'
73 ' feature, use it if you have more than 4G memory')
74 return optparser
75
76 def preoptparse(self, argv):
77 optparser = self.get_optparser()
78
79 largs = []
80 rargs = []
81 while argv:
82 arg = argv.pop(0)
83
84 if arg in ('-h', '--help'):
85 rargs.append(arg)
86
87 elif optparser.has_option(arg):
88 largs.append(arg)
89
90 if optparser.get_option(arg).takes_value():
91 try:
92 largs.append(argv.pop(0))
93 except IndexError:
94 raise errors.Usage("option %s requires arguments" % arg)
95
96 else:
97 if arg.startswith("--"):
98 if "=" in arg:
99 opt = arg.split("=")[0]
100 else:
101 opt = None
102 elif arg.startswith("-") and len(arg) > 2:
103 opt = arg[0:2]
104 else:
105 opt = None
106
107 if opt and optparser.has_option(opt):
108 largs.append(arg)
109 else:
110 rargs.append(arg)
111
112 return largs + rargs
113
114 def postoptparse(self):
115 abspath = lambda pth: os.path.abspath(os.path.expanduser(pth))
116
117 if self.options.verbose:
118 msger.set_loglevel('verbose')
119 if self.options.debug:
120 msger.set_loglevel('debug')
121
122 if self.options.logfile:
123 logfile_abs_path = abspath(self.options.logfile)
124 if os.path.isdir(logfile_abs_path):
125 raise errors.Usage("logfile's path %s should be file"
126 % self.options.logfile)
127 if not os.path.exists(os.path.dirname(logfile_abs_path)):
128 os.makedirs(os.path.dirname(logfile_abs_path))
129 msger.set_interactive(False)
130 msger.set_logfile(logfile_abs_path)
131 configmgr.create['logfile'] = self.options.logfile
132
133 if self.options.config:
134 configmgr.reset()
135 configmgr._siteconf = self.options.config
136
137 if self.options.outdir is not None:
138 configmgr.create['outdir'] = abspath(self.options.outdir)
139
140 cdir = 'outdir'
141 if os.path.exists(configmgr.create[cdir]) \
142 and not os.path.isdir(configmgr.create[cdir]):
143 msger.error('Invalid directory specified: %s' \
144 % configmgr.create[cdir])
145
146 if self.options.enabletmpfs:
147 configmgr.create['enabletmpfs'] = self.options.enabletmpfs
148
149 def main(self, argv=None):
150 if argv is None:
151 argv = sys.argv
152 else:
153 argv = argv[:] # don't modify caller's list
154
155 self.optparser = self.get_optparser()
156 if self.optparser:
157 try:
158 argv = self.preoptparse(argv)
159 self.options, args = self.optparser.parse_args(argv)
160
161 except cmdln.CmdlnUserError, ex:
162 msg = "%s: %s\nTry '%s help' for info.\n"\
163 % (self.name, ex, self.name)
164 msger.error(msg)
165
166 except cmdln.StopOptionProcessing, ex:
167 return 0
168 else:
169 # optparser=None means no process for opts
170 self.options, args = None, argv[1:]
171
172 if not args:
173 return self.emptyline()
174
175 self.postoptparse()
176
177 return self.cmd(args)
178
179 def precmd(self, argv): # check help before cmd
180
181 if '-h' in argv or '?' in argv or '--help' in argv or 'help' in argv:
182 return argv
183
184 if len(argv) == 1:
185 return ['help', argv[0]]
186
187 return argv