Minimize python use when replacing configs

This commit is contained in:
Ivan Kadochnikov 2016-09-13 03:33:27 +10:00
parent 47d0308b8e
commit 3388e470b8

View file

@ -1901,17 +1901,14 @@ __overwriteconfig() {
# Convert json string to a yaml string and write it to config file. Output is dumped into tempfile.
python -c "import json; import yaml; jsn=json.loads('$json'); yml=yaml.safe_dump(jsn, line_break='\n', default_flow_style=False); config_file=open('$target', 'w'); config_file.write(yml); config_file.close();" 2>$tempfile
# Check if there were any errors output to the tempfile
filesize=$(python -c "import os; print os.stat('$tempfile').st_size;")
# No python errors output to the tempfile
if [ "$filesize" -eq 0 ]; then
if [ ! -s "$tempfile" ]; then
rm -f "$tempfile"
return 0
fi
# Errors are present in the tempfile - let's expose the that to the user.
fullerror=$(python -c "tmp_file=open('$tempfile'); print tmp_file.read(); tmp_file.close()")
# Errors are present in the tempfile - let's expose them to the user.
fullerror=$(cat "$tempfile")
echodebug "$fullerror"
echoerror "Python error encountered. This is likely due to passing in a malformed JSON string. Please use -D to see stacktrace."