From 22cc108e870b0ed2ced7bb53e17515658200ec5f Mon Sep 17 00:00:00 2001 From: boltgolt Date: Thu, 10 May 2018 15:05:27 +0200 Subject: [PATCH] Added auto config backup and restore for upgarades This does remove all comments from the config file --- autocomplete/howdy | 1 + debian/postinst | 30 ++++++++++++++++++++++++++++-- debian/preinst | 14 ++++++++++++++ src/compare.py | 2 +- src/config.ini | 2 ++ 5 files changed, 46 insertions(+), 3 deletions(-) diff --git a/autocomplete/howdy b/autocomplete/howdy index 339bccac..e3bde07d 100755 --- a/autocomplete/howdy +++ b/autocomplete/howdy @@ -42,4 +42,5 @@ _howdy() { return 0 } +# Register the autocomplete function complete -F _howdy howdy diff --git a/debian/postinst b/debian/postinst index efe76a18..5c40df6b 100755 --- a/debian/postinst +++ b/debian/postinst @@ -36,8 +36,34 @@ def handleStatus(status): sys.exit(1) -# We're not in fresh configuration mode (probably an upgrade), so exit +# We're not in fresh configuration mode so don't continue the setup if not os.path.exists("/tmp/howdy_picked_device"): + # Check if we have an older config we can restore + if len(sys.argv) > 2: + if os.path.exists("/tmp/howdy_config_backup_v" + sys.argv[2] + ".ini"): + # Get the config parser + import configparser + + # Load th old and new config files + oldConf = configparser.ConfigParser() + oldConf.read("/tmp/howdy_config_backup_v" + sys.argv[2] + ".ini") + newConf = configparser.ConfigParser() + newConf.read("/lib/security/howdy/config.ini") + + # Go through every setting in the old config and apply it to the new file + for section in oldConf.sections(): + for (key, value) in oldConf.items(section): + try: + newConf.set(section, key, value) + # Add a new section where needed + except configparser.NoSectionError as e: + newConf.add_section(section) + newConf.set(section, key, value) + + # Write it all to file + with open("/lib/security/howdy/config.ini", "w") as configfile: + newConf.write(configfile) + sys.exit(0) # Open the temporary file containing the device ID @@ -162,7 +188,7 @@ if "HOWDY_NO_PROMPT" not in os.environ: # Abort the whole thing if it's not if (ans.lower() != "y"): - print("Inerpeting as a \"NO\", aborting") + print("Interpreting as a \"NO\", aborting") sys.exit(1) print("Adding lines to PAM\n") diff --git a/debian/preinst b/debian/preinst index 02960a8d..33572beb 100755 --- a/debian/preinst +++ b/debian/preinst @@ -16,6 +16,20 @@ import os import re import signal +# Backup the config file if we're upgrading +if "upgrade" in sys.argv: + # Try to copy the config file as a backup + try: + subprocess.call(["cp /lib/security/howdy/config.ini /tmp/howdy_config_backup_v" + sys.argv[2] + ".ini"], shell=True) + + # Let the user know so he knows where to look on a failed install + print("Backup of Howdy config file created in /tmp/howdy_config_backup_v" + sys.argv[2] + ".ini") + except e: + print("Could not make an backup of old Howdy config file") + + # Don't continue setup when we're just upgrading + sys.exit(0) + # Don't run if we're not trying to install fresh if "install" not in sys.argv: sys.exit(0) diff --git a/src/compare.py b/src/compare.py index b25e80a5..a1a08697 100644 --- a/src/compare.py +++ b/src/compare.py @@ -61,7 +61,7 @@ def stop(status): video_capture = cv2.VideoCapture(int(config.get("video", "device_id"))) # Force MJPEG decoding if true -if config.get("debug", "force_mjpeg") == "true": +if config.get("video", "force_mjpeg") == "true": video_capture.set(cv2.CAP_PROP_FOURCC, 1196444237) # Capture a single frame so the camera becomes active diff --git a/src/config.ini b/src/config.ini index 146f1e87..ac5e06dd 100644 --- a/src/config.ini +++ b/src/config.ini @@ -46,3 +46,5 @@ dark_threshold = 50 [debug] # Show a short but detailed diagnostic report in console end_report = false + +dummy = true