Added configuration file checks in pybackup.py
This commit is contained in:
parent
a800cbc69d
commit
fc09d9b176
46
pybackup.py
46
pybackup.py
@ -3,27 +3,57 @@
|
||||
import sys
|
||||
import json
|
||||
|
||||
#CONFIGURATION_PATH = "/etc/pybackup.conf" # for "production"
|
||||
# CONFIGURATION_PATH = "/etc/pybackup.conf" # for "production"
|
||||
CONFIGURATION_PATH = "pybackup.conf" # for testing
|
||||
|
||||
|
||||
def log(message: str, log_level: int) -> bool:
|
||||
assignments = {0: "DEBUG", 1: "VERBOSE", 2: "INFO", 3: "ERROR", 4: "FATAL"}
|
||||
if(log_level not in assignments):
|
||||
if log_level not in assignments:
|
||||
return False # show that something went wrong
|
||||
print(f"[{assignments[log_level]}] {message}")
|
||||
print('\033[91m', end="") if log_level >= 3 else print("", end="") # ink the output red if error/fatal is logged
|
||||
print(f"[{assignments[log_level]}] {message}\033[0m")
|
||||
if log_level == 4:
|
||||
print("Exiting...")
|
||||
exit(1)
|
||||
return True
|
||||
|
||||
if not __name__ == "__main__": # just to make sure this program isn't used as a module
|
||||
|
||||
# just to make sure this program isn't used as a module
|
||||
if not __name__ == "__main__":
|
||||
print(f"[{sys.argv[0]}]: THIS BACKUP PROGRAM IS NOT A PYTHON MODULE!")
|
||||
exit(0)
|
||||
|
||||
|
||||
conf_f = open(CONFIGURATION_PATH, "r")
|
||||
try:
|
||||
try: # load the config if possible
|
||||
conf = json.load(conf_f)
|
||||
except json.decoder.JSONDecodeError:
|
||||
log("The configuration file is corrupted", 3)
|
||||
exit(1)
|
||||
log("The configuration file is corrupted.", 4)
|
||||
conf_f.close()
|
||||
|
||||
# test if the locations key exists in the config file
|
||||
try:
|
||||
conf["locations"]
|
||||
except KeyError:
|
||||
log("The configuration file has no locations inside. Maybe the root 'locations' key is wrongly spelled?", 4)
|
||||
|
||||
print(conf)
|
||||
# test for each location if the data was correctly written into the config
|
||||
counter = 0
|
||||
for _location in conf["locations"]:
|
||||
try:
|
||||
_location["name"]
|
||||
except KeyError:
|
||||
log(f"The {counter+1}th element of locations in the config has no name. Skipping this backup path...", 3)
|
||||
conf["locations"].pop(counter)
|
||||
|
||||
try:
|
||||
_location["path"]
|
||||
_location["backup_path"]
|
||||
_location["frequency"]
|
||||
_location["versions"]
|
||||
except KeyError:
|
||||
log(f"The {counter + 1}th element with the name \"{_location['name']}\" of locations in the config is corrupted. Skipping this backup path...", 3)
|
||||
conf["locations"].pop(counter)
|
||||
|
||||
counter += 1
|
||||
|
Loading…
x
Reference in New Issue
Block a user