diff --git a/variable_grabbler.py b/variable_grabbler.py index 2c69b9c..7c9592c 100644 --- a/variable_grabbler.py +++ b/variable_grabbler.py @@ -1,14 +1,32 @@ #!/usr/bin/python2 print """ -Variable Grabbler - version 3.3 +Variable Grabbler - version 3.4 -------------------------------""" +################################################################ +# Changes in this version: +#=============================================================== +# * added support for command execution +# * some internal technicalities that should not impact features +# * Yes. +# + +################################################################ +# Exit codes: +#=============================================================== +# 0 => normal exit +# 1 => argument error +# 2 => error while processing +# # definitions -import os, json, sys +import os, json, sys, subprocess + +def print_err(text): + print >> sys.stderr, text if not len(sys.argv)==3: - print >> sys.stderr, "Error: Exactly two arguments required: \"python variable_grabbler.py \"" + print_err("Error: Exactly two arguments required: \"python variable_grabbler.py \"") sys.exit(1) print "Reading config file...", @@ -30,9 +48,21 @@ for variable in config_values: file_replacement.close() elif config_values[variable][0] == "exec": print "Found array. Executing command instead: " + config_values[variable][1] - #Add code to replace variable with command output here - print >> sys.stderr, "Error: This option is not yet supported." - replacement = "" + process = subprocess.Popen(config_values[variable][1], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout, stderr = process.communicate() + if process.returncode == 0: + print "Process exited normally. Replacing variable with the output from STDOUT..." + print_err("Subprocess exited normally.\nThe following error messages were produced:") + print_err(stderr) + replacement = stdout + else: + print "Process exited abnormally. Exiting. No changes will be made." + print_err("Subprocess exited abnormally. Exiting.\nThe following output was produced:") + print_err(stdout) + print_err("The following error messages were produced:") + print_err(stderr) + print "Exiting now." + sys.exit(2) else: replacement = config_values[variable] print repr(replacement)