Refactor: apt and dconf restorations have separate functions

This commit is contained in:
2022-04-27 16:26:59 +02:00
parent dbc9f77fb5
commit 76eadce7b0

39
main.py
View File

@@ -18,6 +18,7 @@ import getopt
import os import os
import subprocess import subprocess
import sys import sys
from typing import TextIO
def run_daily(): def run_daily():
@@ -33,28 +34,19 @@ def run_daily():
f.close() f.close()
def list_pkgs(): def list_apt_packages():
f = open("packages.txt", "w") f = open("packages.txt", "w")
subprocess.run(args=["dpkg", "--get-selections"], stdout=f) subprocess.run(args=["dpkg", "--get-selections"], stdout=f)
f.close() f.close()
def save_settings(): def save_dconf_settings():
f = open("dconf_out.txt", "w") f = open("dconf_out.txt", "w")
subprocess.run(args=["dconf", "dump", "/"], stdout=f) subprocess.run(args=["dconf", "dump", "/"], stdout=f)
f.close() f.close()
def restore(): def restore_apt_packages():
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 actions.__contains__("apt-get"):
if os.getuid() == 0: if os.getuid() == 0:
try: try:
packagelist = open("packages.txt", "r") packagelist = open("packages.txt", "r")
@@ -74,8 +66,10 @@ def restore():
subprocess.run(["apt-get", "dselect-upgrade"]) subprocess.run(["apt-get", "dselect-upgrade"])
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 apt packages unless you are root!")
if actions.__contains__("dconf"):
def restore_dconf_settings():
try: try:
config = open("dconf_out.txt", "r") config = open("dconf_out.txt", "r")
except FileNotFoundError: except FileNotFoundError:
@@ -85,6 +79,19 @@ def restore():
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")
def restore():
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 actions.__contains__("apt-get"):
restore_apt_packages()
if actions.__contains__("dconf"):
restore_dconf_settings()
print("Exitting...") print("Exitting...")
exit() exit()
@@ -147,6 +154,6 @@ if __name__ == '__main__':
exit(0) exit(0)
run_daily() run_daily()
if actions.__contains__("apt-get"): if actions.__contains__("apt-get"):
list_pkgs() list_apt_packages()
if actions.__contains__("dconf"): if actions.__contains__("dconf"):
save_settings() save_dconf_settings()