From 3484e09be4f46d95660a462cfab5b982bdd2063b Mon Sep 17 00:00:00 2001 From: Derisis13 Date: Wed, 27 Apr 2022 17:27:45 +0200 Subject: [PATCH] Refactor: extracted announce_date and open_backup_file functions --- main.py | 91 ++++++++++++++++++++++++++------------------------------- 1 file changed, 42 insertions(+), 49 deletions(-) diff --git a/main.py b/main.py index 7a376e4..7d1cb9d 100644 --- a/main.py +++ b/main.py @@ -18,7 +18,28 @@ import getopt import os import subprocess import sys -from typing import TextIO + + +def announce_date(type): + try: + f = open(".backupdone", "r") + date = f.read(10) + f.close() + except FileNotFoundError: + date = "Unknown" + print("Restoring " + type + " from " + date) + + +def open_backup_file(filename): + global backupfile + try: + backupfile = open(filename, "r") + except FileNotFoundError: + print("No " + filename + " in your backup directory! \n" + "Did you specify the right directory? Please check for correct order of arguments: -d/--backup-dir" + " -r/--restore -a/--action!") + exit(2) + return backupfile def run_daily(): @@ -26,8 +47,8 @@ def run_daily(): f = open('.backupdone', "r+") except FileNotFoundError: f = open('.backupdone', "w+") - olddate = f.read(10) - if (olddate == str(datetime.datetime.now().date())) and (len(olddate) > 0): + old_date = f.read(10) + if (old_date == str(datetime.datetime.now().date())) and (len(old_date) > 0): exit() f.seek(0, 0) f.write(str(datetime.datetime.now().date())) @@ -54,59 +75,31 @@ def save_dconf_settings(): def restore_apt_packages(): if os.getuid() == 0: - try: - packagelist = open("packages.txt", "r") - 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: - f = open(".backupdone", "r") - date = f.read(10) - f.close() - except FileNotFoundError: - date = "Unknown" - print("Restoring programs and settings from ", date) - subprocess.run(["dpkg", "--set-selections"], stdin=packagelist) - subprocess.run(["apt-get", "dselect-upgrade"]) - print("Restoration of packages complete") + packagelist = open_backup_file("packages.txt") + announce_date("apt packages") + subprocess.run(["dpkg", "--set-selections"], stdin=packagelist) + subprocess.run(["apt-get", "dselect-upgrade"]) + print("Done!") else: print("You're not root! You can't restore apt packages unless you are root!") def restore_flatpak_apps(): - try: - f = open("flatpaks.txt", "r") - except FileNotFoundError: - print("No flatpaks.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: - f = open(".backupdone", "r") - date = f.read(10) - f.close() - except FileNotFoundError: - date = "Unknown" - print("Restoring flatpaks and from ", date, "...") - app = f.readline() - while app: - app = f.readline() - subprocess.run(args=["flatpak", "install", "--user", "--assumeyes", "app"]) - print("Done!") + flatpaklist = open_backup_file("flatpaks.txt") + announce_date("flatpak apps") + app = flatpaklist.readline() + while app: + app = flatpaklist.readline() + subprocess.run(args=["flatpak", "install", "--user", "--assumeyes", "app"]) + print("Done!") def restore_dconf_settings(): - 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") + config = open_backup_file("dconf_out.txt") + announce_date("dconf settings") + subprocess.run(['dconf', 'load', '/'], stdin=config) + config.close() + print("Done!") def restore(): @@ -120,7 +113,7 @@ def restore(): restore_apt_packages() if actions.__contains__("dconf"): restore_dconf_settings() - print("Exitting...") + print("Exiting...") exit()