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