added support for command execution
parent
723d082116
commit
e2d684fff3
|
@ -1,14 +1,32 @@
|
||||||
#!/usr/bin/python2
|
#!/usr/bin/python2
|
||||||
|
|
||||||
print """
|
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
|
# definitions
|
||||||
import os, json, sys
|
import os, json, sys, subprocess
|
||||||
|
|
||||||
|
def print_err(text):
|
||||||
|
print >> sys.stderr, text
|
||||||
|
|
||||||
if not len(sys.argv)==3:
|
if not len(sys.argv)==3:
|
||||||
print >> sys.stderr, "Error: Exactly two arguments required: \"python variable_grabbler.py <file to be rewritten> <config file>\""
|
print_err("Error: Exactly two arguments required: \"python variable_grabbler.py <file to be rewritten> <config file>\"")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
print "Reading config file...",
|
print "Reading config file...",
|
||||||
|
@ -30,9 +48,21 @@ for variable in config_values:
|
||||||
file_replacement.close()
|
file_replacement.close()
|
||||||
elif config_values[variable][0] == "exec":
|
elif config_values[variable][0] == "exec":
|
||||||
print "Found array. Executing command instead: " + config_values[variable][1]
|
print "Found array. Executing command instead: " + config_values[variable][1]
|
||||||
#Add code to replace variable with command output here
|
process = subprocess.Popen(config_values[variable][1], shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||||
print >> sys.stderr, "Error: This option is not yet supported."
|
stdout, stderr = process.communicate()
|
||||||
replacement = ""
|
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:
|
else:
|
||||||
replacement = config_values[variable]
|
replacement = config_values[variable]
|
||||||
print repr(replacement)
|
print repr(replacement)
|
||||||
|
|
Loading…
Reference in New Issue