sbsv2/raptor/python/raptor_utilities.py
changeset 641 8dd670a9f34f
parent 616 24e4ef208cca
--- a/sbsv2/raptor/python/raptor_utilities.py	Mon Sep 13 13:11:19 2010 +0800
+++ b/sbsv2/raptor/python/raptor_utilities.py	Mon Sep 13 14:04:04 2010 +0100
@@ -239,3 +239,56 @@
 		raise IOError(message)
 
 	return 
+
+
+
+## Commandline processing utilities ##
+
+fullCommandOption = "--command"
+miniCommandOption = "--co"  # update this if another "co" option is added
+
+def read_command_file(filename, used):
+	"""Read commandline options in from a file"""
+	if filename in used:
+		raise IOError("command file '%s' refers to itself" % filename)
+
+	args = []
+	try:
+		file = open(filename, "r")
+		for line in file.readlines():
+			args.extend(line.split())
+		file.close()
+	except:
+		raise IOError("couldn't read command file '%s'" % filename)
+
+	# expand any command files in the options we just read.
+	# making sure we don't get stuck in a loop.
+	usedPlusThis = used[:]
+	usedPlusThis.append(filename)
+	return expand_command_options(args, usedPlusThis)
+
+def expand_command_options(args, files = []):
+	"""process commandline options to recursively expand command files (--command options) into a full list of options."""
+	expanded = []
+	previousWasOpt = False
+
+	for a in args:
+		if previousWasOpt: # then this one is the filename
+			expanded.extend(read_command_file(a, files))
+			previousWasOpt = False
+			continue
+
+		if a.startswith(miniCommandOption):
+			if "=" in a: # then this is opt=filename
+				opt = a.split("=")
+				if fullCommandOption.startswith(opt[0]):
+					expanded.extend(read_command_file(opt[1], files))
+					continue
+			else: # the next one is the filename
+				if fullCommandOption.startswith(a):
+					previousWasOpt = True
+					continue
+
+		expanded.append(a) # an ordinary arg, nothing to do with command files
+
+	return expanded