mirror of
https://github.com/Derisis13/barusu.git
synced 2025-12-07 03:52:48 +01:00
Refactor: extracted announce_date and open_backup_file functions
This commit is contained in:
91
main.py
91
main.py
@@ -18,7 +18,28 @@ import getopt
|
|||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
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():
|
def run_daily():
|
||||||
@@ -26,8 +47,8 @@ def run_daily():
|
|||||||
f = open('.backupdone', "r+")
|
f = open('.backupdone', "r+")
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
f = open('.backupdone', "w+")
|
f = open('.backupdone', "w+")
|
||||||
olddate = f.read(10)
|
old_date = f.read(10)
|
||||||
if (olddate == str(datetime.datetime.now().date())) and (len(olddate) > 0):
|
if (old_date == str(datetime.datetime.now().date())) and (len(old_date) > 0):
|
||||||
exit()
|
exit()
|
||||||
f.seek(0, 0)
|
f.seek(0, 0)
|
||||||
f.write(str(datetime.datetime.now().date()))
|
f.write(str(datetime.datetime.now().date()))
|
||||||
@@ -54,59 +75,31 @@ def save_dconf_settings():
|
|||||||
|
|
||||||
def restore_apt_packages():
|
def restore_apt_packages():
|
||||||
if os.getuid() == 0:
|
if os.getuid() == 0:
|
||||||
try:
|
packagelist = open_backup_file("packages.txt")
|
||||||
packagelist = open("packages.txt", "r")
|
announce_date("apt packages")
|
||||||
except FileNotFoundError:
|
subprocess.run(["dpkg", "--set-selections"], stdin=packagelist)
|
||||||
print("No packages.txt in your backup directory! Did you specify the right directory? Please check for "
|
subprocess.run(["apt-get", "dselect-upgrade"])
|
||||||
"correct order of arguments: first -d/--backup-dir then -r/--restore!")
|
print("Done!")
|
||||||
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")
|
|
||||||
else:
|
else:
|
||||||
print("You're not root! You can't restore apt packages unless you are root!")
|
print("You're not root! You can't restore apt packages unless you are root!")
|
||||||
|
|
||||||
|
|
||||||
def restore_flatpak_apps():
|
def restore_flatpak_apps():
|
||||||
try:
|
flatpaklist = open_backup_file("flatpaks.txt")
|
||||||
f = open("flatpaks.txt", "r")
|
announce_date("flatpak apps")
|
||||||
except FileNotFoundError:
|
app = flatpaklist.readline()
|
||||||
print("No flatpaks.txt in your backup directory! Did you specify the right directory? Please check for "
|
while app:
|
||||||
"correct order of arguments: first -d/--backup-dir then -r/--restore!")
|
app = flatpaklist.readline()
|
||||||
exit(2)
|
subprocess.run(args=["flatpak", "install", "--user", "--assumeyes", "app"])
|
||||||
else:
|
print("Done!")
|
||||||
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!")
|
|
||||||
|
|
||||||
|
|
||||||
def restore_dconf_settings():
|
def restore_dconf_settings():
|
||||||
try:
|
config = open_backup_file("dconf_out.txt")
|
||||||
config = open("dconf_out.txt", "r")
|
announce_date("dconf settings")
|
||||||
except FileNotFoundError:
|
subprocess.run(['dconf', 'load', '/'], stdin=config)
|
||||||
print("No dconf_out.txt in your backup directory! Did you specify the right directory? Please check for "
|
config.close()
|
||||||
"correct order of arguments: first -d/--backup-dir then -r/--restore!")
|
print("Done!")
|
||||||
else:
|
|
||||||
subprocess.run(['dconf', 'load', '/'], stdin=config)
|
|
||||||
config.close()
|
|
||||||
print("Restoration of settings is complete")
|
|
||||||
|
|
||||||
|
|
||||||
def restore():
|
def restore():
|
||||||
@@ -120,7 +113,7 @@ def restore():
|
|||||||
restore_apt_packages()
|
restore_apt_packages()
|
||||||
if actions.__contains__("dconf"):
|
if actions.__contains__("dconf"):
|
||||||
restore_dconf_settings()
|
restore_dconf_settings()
|
||||||
print("Exitting...")
|
print("Exiting...")
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user