summaryrefslogtreecommitdiffstats
path: root/scripts/lib/wic/creator.py
diff options
context:
space:
mode:
authorEd Bartosh <ed.bartosh@linux.intel.com>2015-09-02 13:58:11 +0300
committerRichard Purdie <richard.purdie@linuxfoundation.org>2015-09-03 12:43:28 +0100
commite85f807b874a3e2a576e631fbc82767af00603ed (patch)
treec774bd6da1900887f0948f859e33363ef1a3146c /scripts/lib/wic/creator.py
parentf1bcf47ff4cf90e14e557587f22a169cad8783f7 (diff)
downloadpoky-e85f807b874a3e2a576e631fbc82767af00603ed.tar.gz
wic: use optparse instead of cmdln
cmdln.py https://pypi.python.org/pypi/cmdln was used in creator.py to parse image plugin options and arguments. There is no need in such a sofisticated API to do this simple task. Standard option parser optparse.OptionParser can do it just fine. Modified Creator class to work with option parser. Removed cmdln.py from the wic codebase. (From OE-Core rev: 1e5220f74830b99cf8340a4f6977399b5cf49871) Signed-off-by: Ed Bartosh <ed.bartosh@linux.intel.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'scripts/lib/wic/creator.py')
-rw-r--r--scripts/lib/wic/creator.py115
1 files changed, 26 insertions, 89 deletions
diff --git a/scripts/lib/wic/creator.py b/scripts/lib/wic/creator.py
index 760848f320..5231297282 100644
--- a/scripts/lib/wic/creator.py
+++ b/scripts/lib/wic/creator.py
@@ -16,15 +16,15 @@
16# Temple Place - Suite 330, Boston, MA 02111-1307, USA. 16# Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 17
18import os, sys 18import os, sys
19from optparse import SUPPRESS_HELP 19from optparse import OptionParser, SUPPRESS_HELP
20 20
21from wic import msger 21from wic import msger
22from wic.utils import cmdln, errors 22from wic.utils import errors
23from wic.conf import configmgr 23from wic.conf import configmgr
24from wic.plugin import pluginmgr 24from wic.plugin import pluginmgr
25 25
26 26
27class Creator(cmdln.Cmdln): 27class Creator(object):
28 """${name}: create an image 28 """${name}: create an image
29 29
30 Usage: 30 Usage:
@@ -37,8 +37,7 @@ class Creator(cmdln.Cmdln):
37 name = 'wic create(cr)' 37 name = 'wic create(cr)'
38 38
39 def __init__(self, *args, **kwargs): 39 def __init__(self, *args, **kwargs):
40 cmdln.Cmdln.__init__(self, *args, **kwargs) 40 self._subcmds = {}
41 self._subcmds = []
42 41
43 # get cmds from pluginmgr 42 # get cmds from pluginmgr
44 # mix-in do_subcmd interface 43 # mix-in do_subcmd interface
@@ -48,11 +47,10 @@ class Creator(cmdln.Cmdln):
48 continue 47 continue
49 48
50 func = getattr(klass, 'do_create') 49 func = getattr(klass, 'do_create')
51 setattr(self.__class__, "do_"+subcmd, func) 50 self._subcmds[subcmd] = func
52 self._subcmds.append(subcmd)
53 51
54 def get_optparser(self): 52 def get_optparser(self):
55 optparser = cmdln.CmdlnOptionParser(self) 53 optparser = OptionParser()
56 optparser.add_option('-d', '--debug', action='store_true', 54 optparser.add_option('-d', '--debug', action='store_true',
57 dest='debug', 55 dest='debug',
58 help=SUPPRESS_HELP) 56 help=SUPPRESS_HELP)
@@ -73,69 +71,31 @@ class Creator(cmdln.Cmdln):
73 ' feature, use it if you have more than 4G memory') 71 ' feature, use it if you have more than 4G memory')
74 return optparser 72 return optparser
75 73
76 def preoptparse(self, argv): 74 def postoptparse(self, options):
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)) 75 abspath = lambda pth: os.path.abspath(os.path.expanduser(pth))
116 76
117 if self.options.verbose: 77 if options.verbose:
118 msger.set_loglevel('verbose') 78 msger.set_loglevel('verbose')
119 if self.options.debug: 79 if options.debug:
120 msger.set_loglevel('debug') 80 msger.set_loglevel('debug')
121 81
122 if self.options.logfile: 82 if options.logfile:
123 logfile_abs_path = abspath(self.options.logfile) 83 logfile_abs_path = abspath(options.logfile)
124 if os.path.isdir(logfile_abs_path): 84 if os.path.isdir(logfile_abs_path):
125 raise errors.Usage("logfile's path %s should be file" 85 raise errors.Usage("logfile's path %s should be file"
126 % self.options.logfile) 86 % options.logfile)
127 if not os.path.exists(os.path.dirname(logfile_abs_path)): 87 if not os.path.exists(os.path.dirname(logfile_abs_path)):
128 os.makedirs(os.path.dirname(logfile_abs_path)) 88 os.makedirs(os.path.dirname(logfile_abs_path))
129 msger.set_interactive(False) 89 msger.set_interactive(False)
130 msger.set_logfile(logfile_abs_path) 90 msger.set_logfile(logfile_abs_path)
131 configmgr.create['logfile'] = self.options.logfile 91 configmgr.create['logfile'] = options.logfile
132 92
133 if self.options.config: 93 if options.config:
134 configmgr.reset() 94 configmgr.reset()
135 configmgr._siteconf = self.options.config 95 configmgr._siteconf = options.config
136 96
137 if self.options.outdir is not None: 97 if options.outdir is not None:
138 configmgr.create['outdir'] = abspath(self.options.outdir) 98 configmgr.create['outdir'] = abspath(options.outdir)
139 99
140 cdir = 'outdir' 100 cdir = 'outdir'
141 if os.path.exists(configmgr.create[cdir]) \ 101 if os.path.exists(configmgr.create[cdir]) \
@@ -143,8 +103,8 @@ class Creator(cmdln.Cmdln):
143 msger.error('Invalid directory specified: %s' \ 103 msger.error('Invalid directory specified: %s' \
144 % configmgr.create[cdir]) 104 % configmgr.create[cdir])
145 105
146 if self.options.enabletmpfs: 106 if options.enabletmpfs:
147 configmgr.create['enabletmpfs'] = self.options.enabletmpfs 107 configmgr.create['enabletmpfs'] = options.enabletmpfs
148 108
149 def main(self, argv=None): 109 def main(self, argv=None):
150 if argv is None: 110 if argv is None:
@@ -152,36 +112,13 @@ class Creator(cmdln.Cmdln):
152 else: 112 else:
153 argv = argv[:] # don't modify caller's list 113 argv = argv[:] # don't modify caller's list
154 114
155 self.optparser = self.get_optparser() 115 pname = argv[0]
156 if self.optparser: 116 if pname not in self._subcmds:
157 try: 117 msger.error('Unknown plugin: %s' % pname)
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 118
179 def precmd(self, argv): # check help before cmd 119 optparser = self.get_optparser()
180 120 options, args = optparser.parse_args(argv)
181 if '-h' in argv or '?' in argv or '--help' in argv or 'help' in argv:
182 return argv
183 121
184 if len(argv) == 1: 122 self.postoptparse(options)
185 return ['help', argv[0]]
186 123
187 return argv 124 return self._subcmds[pname](options, *args[1:])