Created actions option to increase compatibility

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

56
main.py
View File

@@ -53,6 +53,8 @@ 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 actions.__contains__("apt-get"):
if os.getuid() == 0: if os.getuid() == 0:
try: try:
packagelist = open("packages.txt", "r") packagelist = open("packages.txt", "r")
@@ -73,6 +75,7 @@ def restore():
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!")
if actions.__contains__("dconf"):
try: try:
config = open("dconf_out.txt", "r") config = open("dconf_out.txt", "r")
except FileNotFoundError: except FileNotFoundError:
@@ -86,22 +89,22 @@ def restore():
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()
if actions.__contains__("apt-get"):
list_pkgs() list_pkgs()
if actions.__contains__("dconf"):
save_settings() save_settings()