20 |
20 |
21 import types |
21 import types |
22 import raptor |
22 import raptor |
23 |
23 |
24 from optparse import OptionParser # for parsing command line parameters |
24 from optparse import OptionParser # for parsing command line parameters |
25 |
25 from raptor_utilities import expand_command_options |
26 fullCommandOption = "--command" |
|
27 miniCommandOption = "--co" # update this if another "co" option is added |
|
28 |
26 |
29 # raptor_cli module attributes |
27 # raptor_cli module attributes |
30 |
28 |
31 parser = OptionParser(prog = raptor.name, |
29 parser = OptionParser(prog = raptor.name, |
32 usage = """%prog [--help] [options] [target] ... |
30 usage = """%prog [--help] [options] [target] ... |
177 |
175 |
178 def GetArgs(Raptor, args): |
176 def GetArgs(Raptor, args): |
179 "Process command line arguments for a Raptor object" |
177 "Process command line arguments for a Raptor object" |
180 return DoRaptor(Raptor,args) |
178 return DoRaptor(Raptor,args) |
181 |
179 |
182 def ReadCommandFile(filename, used): |
|
183 if filename in used: |
|
184 raise IOError("command file '%s' refers to itself" % filename) |
|
185 |
|
186 args = [] |
|
187 try: |
|
188 file = open(filename, "r") |
|
189 for line in file.readlines(): |
|
190 args.extend(line.split()) |
|
191 file.close() |
|
192 except: |
|
193 raise IOError("couldn't read command file '%s'" % filename) |
|
194 |
|
195 # expand any command files in the options we just read. |
|
196 # making sure we don't get stuck in a loop. |
|
197 usedPlusThis = used[:] |
|
198 usedPlusThis.append(filename) |
|
199 return ExpandCommandOptions(args, usedPlusThis) |
|
200 |
|
201 def ExpandCommandOptions(args, files = []): |
|
202 """recursively expand --command options.""" |
|
203 expanded = [] |
|
204 previousWasOpt = False |
|
205 |
|
206 for a in args: |
|
207 if previousWasOpt: # then this one is the filename |
|
208 expanded.extend(ReadCommandFile(a, files)) |
|
209 previousWasOpt = False |
|
210 continue |
|
211 |
|
212 if a.startswith(miniCommandOption): |
|
213 if "=" in a: # then this is opt=filename |
|
214 opt = a.split("=") |
|
215 if fullCommandOption.startswith(opt[0]): |
|
216 expanded.extend(ReadCommandFile(opt[1], files)) |
|
217 continue |
|
218 else: # the next one is the filename |
|
219 if fullCommandOption.startswith(a): |
|
220 previousWasOpt = True |
|
221 continue |
|
222 |
|
223 expanded.append(a) # an ordinary arg, nothing to do with command files |
|
224 |
|
225 return expanded |
|
226 |
|
227 def DoRaptor(Raptor, args): |
180 def DoRaptor(Raptor, args): |
228 "Process raptor arguments" |
181 "Process raptor arguments" |
229 # |
182 # |
230 # This should parse the args list and call methods on |
183 # This should parse the args list and call methods on |
231 # the Raptor object to store the appropriate data. |
184 # the Raptor object to store the appropriate data. |