Bugfixes:

- Long arguments not processed
 - closing files in except branches
 - exiting when apt and dconf are present (instead of when they aren't)
 - when running restoration as not root the change of directories didn't happen
 - no dir was specified for restoring dconf settings
This commit is contained in:
2021-12-10 21:00:07 +01:00
parent db2bad9417
commit fca39a93f6

35
main.py
View File

@@ -47,19 +47,18 @@ def save_settings():
def restore(): def restore():
global packagelist, config global packagelist, config
try:
os.chdir(backupdir)
except FileNotFoundError:
print("Backup directory not found! Does it really exist? Please check for correct order of arguments: "
"-d/--backup-dir -r/--restore!")
exit(2)
if os.getuid() == 0: if os.getuid() == 0:
try:
os.chdir(backupdir)
except FileNotFoundError:
print("Backup directory not found! Does it really exist? Please check for correct order of arguments: "
"-d/--backup-dir -r/--restore!")
exit(2)
try: try:
packagelist = open("packages.txt", "r") packagelist = open("packages.txt", "r")
except FileNotFoundError: except FileNotFoundError:
print("No packages.txt in your backup directory! Did you specify the right directory? Please check for " print("No packages.txt in your backup directory! Did you specify the right directory? Please check for "
"correct order of arguments: first -d/--backup-dir then -r/--restore!") "correct order of arguments: first -d/--backup-dir then -r/--restore!")
packagelist.close()
exit(2) exit(2)
else: else:
try: try:
@@ -71,7 +70,6 @@ def restore():
print("Restoring programs and settings from ", date) print("Restoring programs and settings from ", date)
subprocess.run(["dpkg", "--set-selections"], stdin=packagelist) subprocess.run(["dpkg", "--set-selections"], stdin=packagelist)
subprocess.run(["apt-get", "dselect-upgrade"]) subprocess.run(["apt-get", "dselect-upgrade"])
packagelist.close()
print("Restoration of packages complete") print("Restoration of packages complete")
else: else:
print("You're not root! You can't restore packages unless you are root!") print("You're not root! You can't restore packages unless you are root!")
@@ -80,9 +78,8 @@ def restore():
except FileNotFoundError: except FileNotFoundError:
print("No dconf_out.txt in your backup directory! Did you specify the right directory? Please check for " print("No dconf_out.txt in your backup directory! Did you specify the right directory? Please check for "
"correct order of arguments: first -d/--backup-dir then -r/--restore!") "correct order of arguments: first -d/--backup-dir then -r/--restore!")
config.close()
else: else:
subprocess.run(['dconf', 'load'], stdin=config) subprocess.run(['dconf', 'load', '/'], stdin=config)
config.close() config.close()
print("Restoration of settings is complete") print("Restoration of settings is complete")
print("Exitting...") print("Exitting...")
@@ -110,21 +107,21 @@ if __name__ == '__main__':
exit(1) exit(1)
else: else:
for option, value in options: for option, value in options:
if option in ("-h", "help"): if option in ("-h", "--help"):
print("usage: backup_assistant [opts]\n\ print("usage: backup_assistant [opts]\n"
Options:\n\ "Options:\n"
\t-h --help: show this\n\ "\t-h --help: show this\n"
\t-d --backup-dir [directory]: set the directory for the backup/restoration (default is ~/.backups)\n\ "\t-d --backup-dir [directory]: set the directory for the backup/restoration (default is ~/.backups)\n"
\t-r --restore: run the restoration (from backupdir)\n") "\t-r --restore: run the restoration (from backupdir)\n")
exit() exit()
elif option in ("-d", "backup-dir"): elif option in ("-d", "--backup-dir"):
backupdir = os.path.expanduser(value) backupdir = os.path.expanduser(value)
elif option in ("-r", "restore"): elif option in ("-r", "--restore"):
restore() restore()
exit() exit()
apt, dconf = check_progs() apt, dconf = check_progs()
if apt and dconf: if (apt and dconf) is False:
print("System incompatibilities, exitting...") print("System incompatibilities, exitting...")
exit(3) exit(3)