sbsv2/raptor/python/raptor_cli.py
changeset 641 8dd670a9f34f
parent 590 360bd6b35136
child 674 37ee82a83d43
equal deleted inserted replaced
640:ac0bbc1e5d79 641:8dd670a9f34f
    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.
   234 	# command file.
   187 	# command file.
   235 
   188 
   236 	non_ascii_error = "Non-ASCII character in argument or command file"
   189 	non_ascii_error = "Non-ASCII character in argument or command file"
   237 
   190 
   238 	try:
   191 	try:
   239 		expanded_args = ExpandCommandOptions(args)
   192 		expanded_args = expand_command_options(args)
   240 		for arg in expanded_args:
   193 		for arg in expanded_args:
   241 			for c in arg:
   194 			for c in arg:
   242 				if ord(c) > 127:
   195 				if ord(c) > 127:
   243 					Raptor.Error(non_ascii_error)
   196 					Raptor.Error(non_ascii_error)
   244 					return False
   197 					return False