2020-02-23 21:40:16 +01:00
|
|
|
#!/usr/bin/python2
|
|
|
|
|
|
|
|
print """
|
2020-04-08 02:58:43 +02:00
|
|
|
Variable Grabbler - version 4.0_pre1
|
|
|
|
------------------------------------"""
|
2020-04-08 02:06:32 +02:00
|
|
|
################################################################
|
|
|
|
# Changes in this version:
|
|
|
|
#===============================================================
|
2020-04-08 03:02:54 +02:00
|
|
|
# *"\?" in a variable will now be replaced with a simple ?
|
|
|
|
# before processing, remember to double escape that because
|
|
|
|
# json (=> \\?)
|
|
|
|
# *commands are now not run over and over again if not needed
|
2020-04-08 02:06:32 +02:00
|
|
|
#
|
|
|
|
|
|
|
|
################################################################
|
|
|
|
# Exit codes:
|
|
|
|
#===============================================================
|
|
|
|
# 0 => normal exit
|
|
|
|
# 1 => argument error
|
|
|
|
# 2 => error while processing
|
|
|
|
#
|
2020-02-23 21:40:16 +01:00
|
|
|
|
|
|
|
# definitions
|
2020-04-08 02:06:32 +02:00
|
|
|
import os, json, sys, subprocess
|
|
|
|
|
|
|
|
def print_err(text):
|
|
|
|
print >> sys.stderr, text
|
2020-02-23 21:40:16 +01:00
|
|
|
|
2020-02-24 00:56:44 +01:00
|
|
|
if not len(sys.argv)==3:
|
2020-04-08 02:06:32 +02:00
|
|
|
print_err("Error: Exactly two arguments required: \"python variable_grabbler.py <file to be rewritten> <config file>\"")
|
2020-02-24 00:56:44 +01:00
|
|
|
sys.exit(1)
|
2020-02-23 21:40:16 +01:00
|
|
|
|
2020-02-24 00:04:25 +01:00
|
|
|
print "Reading config file...",
|
2020-02-24 01:04:44 +01:00
|
|
|
config_file = open(sys.argv[2],"r")
|
|
|
|
config_values = json.loads(config_file.read().decode("utf-8"))
|
|
|
|
config_file.close()
|
2020-02-24 00:04:25 +01:00
|
|
|
print "Done."
|
2020-02-23 21:40:16 +01:00
|
|
|
|
2020-04-08 02:58:43 +02:00
|
|
|
|
2020-02-24 00:56:44 +01:00
|
|
|
print "Working on file: "+sys.argv[1]
|
2020-02-24 01:04:44 +01:00
|
|
|
for variable in config_values:
|
2020-04-08 02:58:43 +02:00
|
|
|
print "> "+variable.upper()+" => ",
|
2020-02-24 00:56:44 +01:00
|
|
|
file_in = open(sys.argv[1], "r")
|
|
|
|
file_content = file_in.read()
|
|
|
|
file_in.close()
|
2020-03-03 14:59:28 +01:00
|
|
|
if config_values[variable][0] == "file":
|
|
|
|
print "Found array. Adding file instead: " + config_values[variable][1]
|
|
|
|
file_replacement = open(config_values[variable][1], "r")
|
|
|
|
replacement = file_replacement.read()
|
|
|
|
file_replacement.close()
|
|
|
|
elif config_values[variable][0] == "exec":
|
|
|
|
print "Found array. Executing command instead: " + config_values[variable][1]
|
2020-04-08 02:58:43 +02:00
|
|
|
if variable.upper() in file_content:
|
|
|
|
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)
|
2020-04-08 02:06:32 +02:00
|
|
|
else:
|
2020-04-08 02:58:43 +02:00
|
|
|
print variable.upper(),
|
|
|
|
print "was not found in the file. Skipping command execution."
|
2020-03-03 14:59:28 +01:00
|
|
|
else:
|
2020-04-08 02:58:43 +02:00
|
|
|
replacement = repr(config_values[variable])
|
2020-03-05 10:35:53 +01:00
|
|
|
print repr(replacement)
|
2020-04-08 02:58:43 +02:00
|
|
|
# look for variable sections that contain question marks
|
|
|
|
argument_positions = []
|
|
|
|
i = 0
|
|
|
|
while i<len(replacement):
|
|
|
|
if replacement[i] == '?' and (i==0 or not replacement[i-1]=='\\'):
|
|
|
|
argument_positions = argument_positions + [ i ]
|
|
|
|
print "DEBUG: " + argument_positions
|
|
|
|
replacement = replacement.replace("\\?", "?")
|
2020-02-24 01:04:44 +01:00
|
|
|
file_out = open(sys.argv[1], "w")
|
2020-03-07 19:46:34 +01:00
|
|
|
file_out.write(file_content.decode('utf-8').replace("%" + variable.upper() + "%", replacement).encode('utf-8'))
|
2020-02-24 00:56:44 +01:00
|
|
|
file_out.close()
|
2020-03-03 14:59:28 +01:00
|
|
|
print "Done."
|