From 148b3d83f811870080f75128be82b57adf038b1e Mon Sep 17 00:00:00 2001 From: Krateng Date: Sat, 14 Dec 2019 13:43:01 +0100 Subject: [PATCH] Moved backup to own file --- maloja/backup.py | 34 ++++++++++++++++++++++++++ maloja/controller.py | 55 +++++++------------------------------------ maloja/fixexisting.py | 2 +- setup.py | 1 - 4 files changed, 44 insertions(+), 48 deletions(-) create mode 100644 maloja/backup.py diff --git a/maloja/backup.py b/maloja/backup.py new file mode 100644 index 0000000..93ff2b7 --- /dev/null +++ b/maloja/backup.py @@ -0,0 +1,34 @@ +import tarfile +from datetime import datetime +import glob +import os + + +user_files = { + "minimal":[ + "rules/*.tsv", + "scrobbles" + ], + "full":[ + "clients/authenticated_machines.tsv", + "images/artists", + "images/tracks", + "settings/settings.ini" + ] +} + +def backup(folder,level="full"): + + selected_files = user_files["minimal"] if level == "minimal" else user_files["minimal"] + user_files["full"] + real_files = [] + for g in selected_files: + real_files += glob.glob(g) + + now = datetime.utcnow() + timestr = now.strftime("%Y_%m_%d_%H_%M_%S") + filename = "maloja_backup_" + timestr + ".tar.gz" + archivefile = os.path.join(folder,filename) + assert not os.path.exists(archivefile) + with tarfile.open(name=archivefile,mode="x:gz") as archive: + for f in real_files: + archive.add(f) diff --git a/maloja/controller.py b/maloja/controller.py index f6b28e9..794ca5c 100755 --- a/maloja/controller.py +++ b/maloja/controller.py @@ -10,16 +10,10 @@ import stat import pathlib import pkg_resources from doreah.control import mainfunction +from doreah.io import col from .__init__ import DATA_DIR - - - -def blue(txt): return "\033[94m" + txt + "\033[0m" -def green(txt): return "\033[92m" + txt + "\033[0m" -def yellow(txt): return "\033[93m" + txt + "\033[0m" - - +from .backup import backup @@ -71,7 +65,7 @@ def setup(): key = "" for i in range(64): key += str(random.choice(list(range(10)) + list("abcdefghijklmnopqrstuvwxyz") + list("ABCDEFGHIJKLMNOPQRSTUVWXYZ"))) - print("Your API Key: " + yellow(key)) + print("Your API Key: " + col["yellow"](key)) with open("./clients/authenticated_machines.tsv","w") as keyfile: keyfile.write(key + "\t" + "Default Generated Key") elif answer.lower() in ["n","no","nay","0","negative","false"]: @@ -115,15 +109,15 @@ def start(): try: p = subprocess.Popen(["python3","-m","maloja.server"],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL,cwd=DATA_DIR) sp = subprocess.Popen(["python3","-m","maloja.supervisor"],stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL,cwd=DATA_DIR) - print(green("Maloja started!") + " PID: " + str(p.pid)) + print(col["green"]("Maloja started!") + " PID: " + str(p.pid)) from doreah import settings port = settings.get_settings("WEB_PORT") print("Visit your server address (Port " + str(port) + ") to see your web interface. Visit /setup to get started.") print("If you're installing this on your local machine, these links should get you there:") - print("\t" + blue("http://localhost:" + str(port))) - print("\t" + blue("http://localhost:" + str(port) + "/setup")) + print("\t" + col["blue"]("http://localhost:" + str(port))) + print("\t" + col["blue"]("http://localhost:" + str(port) + "/setup")) return True except: print("Error while starting Maloja.") @@ -170,39 +164,8 @@ def loadlastfm(filename): def direct(): from . import server -def backup(level="full",folder=origpath): - import tarfile - from datetime import datetime - import glob - - - - user_files = { - "minimal":[ - "rules/*.tsv", - "scrobbles" - ], - "full":[ - "clients/authenticated_machines.tsv", - "images/artists", - "images/tracks", - "settings/settings.ini" - ] - } - - user_files = user_files["minimal"] if level == "minimal" else user_files["minimal"] + user_files["full"] - real_files = [] - for g in user_files: - real_files += glob.glob(g) - - now = datetime.utcnow() - timestr = now.strftime("%Y_%m_%d_%H_%M_%S") - filename = "maloja_backup_" + timestr + ".tar.gz" - archivefile = os.path.join(folder,filename) - assert not os.path.exists(archivefile) - with tarfile.open(name=archivefile,mode="x:gz") as archive: - for f in real_files: - archive.add(f) +def backuphere(): + backup(folder=origpath) def update(): os.system("pip3 install malojaserver --upgrade --no-cache-dir") @@ -219,7 +182,7 @@ def main(action,*args,**kwargs): "stop":stop, "import":loadlastfm, "debug":direct, - "backup":backup, + "backup":backuphere, "update":update, "fix":fixdb } diff --git a/maloja/fixexisting.py b/maloja/fixexisting.py index 6f36f4d..a0de976 100644 --- a/maloja/fixexisting.py +++ b/maloja/fixexisting.py @@ -7,7 +7,7 @@ from .cleanup import CleanerAgent from doreah.logging import log import difflib import datetime -from .controller import backup +from .backup import backup wendigo = CleanerAgent() diff --git a/setup.py b/setup.py index fab6c6b..ef53a28 100644 --- a/setup.py +++ b/setup.py @@ -35,5 +35,4 @@ setuptools.setup( ) import os - os.system("git tag v" + ".".join(str(n) for n in module.version))