Created actions option to increase compatibility

This commit is contained in:
2022-04-27 14:25:11 +02:00
parent 08f78a92e8
commit dbc9f77fb5

114
main.py
View File

@@ -53,55 +53,58 @@ def restore():
print("Backup directory not found! Does it really exist? Please check for correct order of arguments: " print("Backup directory not found! Does it really exist? Please check for correct order of arguments: "
"-d/--backup-dir -r/--restore!") "-d/--backup-dir -r/--restore!")
exit(2) exit(2)
if os.getuid() == 0:
try: if actions.__contains__("apt-get"):
packagelist = open("packages.txt", "r") if os.getuid() == 0:
except FileNotFoundError:
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!")
exit(2)
else:
try: try:
f = open(".backupdone", "r") packagelist = open("packages.txt", "r")
date = f.read(10)
f.close()
except FileNotFoundError: except FileNotFoundError:
date = "Unknown" print("No packages.txt in your backup directory! Did you specify the right directory? Please check for "
print("Restoring programs and settings from ", date) "correct order of arguments: first -d/--backup-dir then -r/--restore!")
subprocess.run(["dpkg", "--set-selections"], stdin=packagelist) exit(2)
subprocess.run(["apt-get", "dselect-upgrade"]) else:
print("Restoration of packages complete") try:
else: f = open(".backupdone", "r")
print("You're not root! You can't restore packages unless you are root!") date = f.read(10)
try: f.close()
config = open("dconf_out.txt", "r") except FileNotFoundError:
except FileNotFoundError: date = "Unknown"
print("No dconf_out.txt in your backup directory! Did you specify the right directory? Please check for " print("Restoring programs and settings from ", date)
"correct order of arguments: first -d/--backup-dir then -r/--restore!") subprocess.run(["dpkg", "--set-selections"], stdin=packagelist)
else: subprocess.run(["apt-get", "dselect-upgrade"])
subprocess.run(['dconf', 'load', '/'], stdin=config) print("Restoration of packages complete")
config.close() else:
print("Restoration of settings is complete") print("You're not root! You can't restore packages unless you are root!")
if actions.__contains__("dconf"):
try:
config = open("dconf_out.txt", "r")
except FileNotFoundError:
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!")
else:
subprocess.run(['dconf', 'load', '/'], stdin=config)
config.close()
print("Restoration of settings is complete")
print("Exitting...") print("Exitting...")
exit() exit()
def check_progs(): def check_progs(prog):
apt_present = distutils.spawn.find_executable("apt-get") if prog is False:
dconf_present = distutils.spawn.find_executable("dconf") return False
if apt_present is False: if distutils.spawn.find_executable(prog) is False:
print("Your system is not using apt as a package manager! Barusu uses apt to back up your packages. Barusu IS " print("Missing program:" + prog + "! It is removed from the list of actions to perform...")
"NOT designed to work with other packaging tools.") return False
if dconf_present is False: return True
print("Your system is not using dconf! Barusu uses dconf to back up your settings. Barusu IS "
"NOT designed to work with other settings manager.")
return apt_present, dconf_present
if __name__ == '__main__': if __name__ == '__main__':
backupdir = os.path.expanduser("~/.backup") backupdir = os.path.expanduser("~/.backup")
package_manager = "apt-get"
settings_editor = "dconf"
restore_mode = False
try: try:
options, values = getopt.getopt(sys.argv[1:], "hd:r", ["help", "backup-dir=", "restore"]) options, values = getopt.getopt(sys.argv[1:], "hd:ra:", ["help", "backup-dir=", "restore", "action="])
except getopt.GetoptError as err: except getopt.GetoptError as err:
print("Error: ", err.msg) print("Error: ", err.msg)
exit(1) exit(1)
@@ -111,26 +114,39 @@ if __name__ == '__main__':
print("usage: barusu [opts]\n" print("usage: barusu [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 "
"\t-r --restore: run the restoration (from backupdir)\n") "(default is ~/.backups)\n"
"\t-r --restore: run the restoration (from backupdir)\n"
"\t-a --action [a/d]: select actions to perform (default is all). Valid actions: "
"a - back up apt packages, d - back up dconf settings")
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 ("-a", "--action"):
package_manager = False
settings_editor = False
if value.__contains__('a'):
package_manager = "apt-get"
if value.__contains__('d'):
settings_editor = "dconf"
elif option in ("-r", "--restore"): elif option in ("-r", "--restore"):
restore() restore_mode = True
exit()
apt, dconf = check_progs() actions = [package_manager, settings_editor]
if (apt and dconf) is False: for i in actions:
print("System incompatibilities, exitting...") if check_progs(i) is False:
exit(3) actions.pop(actions.index(i))
try: try:
os.chdir(backupdir) os.chdir(backupdir)
except FileNotFoundError: except FileNotFoundError:
subprocess.call(["mkdir", backupdir]) subprocess.call(["mkdir", backupdir])
os.chdir(backupdir) os.chdir(backupdir)
if restore_mode:
restore()
exit(0)
run_daily() run_daily()
list_pkgs() if actions.__contains__("apt-get"):
save_settings() list_pkgs()
if actions.__contains__("dconf"):
save_settings()