From 9735919c2d0d38761e143e4d6c8481f4995c7129 Mon Sep 17 00:00:00 2001 From: "Jeison Yehuda Amihud (Blender Dumbass)" Date: Thu, 5 Jan 2023 11:51:32 +0000 Subject: [PATCH] Backing Up More Reliably ( I hope ) --- studio/backup_blends.py | 74 +++++++++++++++++++++++++++++++++++++ studio/studio_storyLayer.py | 15 ++++++++ 2 files changed, 89 insertions(+) create mode 100644 studio/backup_blends.py diff --git a/studio/backup_blends.py b/studio/backup_blends.py new file mode 100644 index 0000000..bbe3c20 --- /dev/null +++ b/studio/backup_blends.py @@ -0,0 +1,74 @@ +# (c) J.Y.Amihud 2022 +# This program is Free Software under the terms of +# GNU General Public License version 3 ( GPL3 ) or +# any later version of the program. + +# THIS PROGRAM HAS ABSOLUTELY NO WARRANTY. USE AT +# YOUR OWN RISK. SEE LICENSE FOR DETAILS. + +# License could be found in full text on gnu.org/licenses + +import os +import sys +import time +import hashlib + +# We need to know what folder to backup. + +if len(sys.argv) > 1: + project = sys.argv[1] +else: + project = input("Project: ") + +# To compare changes in files the best way is to look at their +# hashes. They are relatively fast to get. But they will be drastically +# different if the files have at least some differences. + +def hash_file(f): + try: + BLOCKSIZE = 65536 + hasher = hashlib.md5() + with open(f, 'rb') as afile: + buf = afile.read(BLOCKSIZE) + while len(buf) > 0: + hasher.update(buf) + buf = afile.read(BLOCKSIZE) + return str(hasher.hexdigest()) + except: + return "FOLDER" + +# This function walks ( recursively ) through the folder of the project, +# finds every un-backed up blend-file. And backs it up. + +def main(): + for i in os.walk(project): + for b in i[2]: + + + if b.endswith(".blend") and "_backup" not in b: + HASH = hash_file(i[0]+"/"+b) + F = open(i[0]+"/"+b, "rb") + F = F.read() + if not F: + print(b, "from", i[0].replace(project, ""), " seems corrupted! Skipping!") + continue + BACKNAME = b[:-6]+"_backup.blend" + if os.path.exists(i[0]+"/"+BACKNAME): + BACKHASH = hash_file(i[0]+"/"+BACKNAME) + else: + BACKHASH = "NO FILE" + if BACKHASH != HASH: + print("Backing up:", b, "from", i[0].replace(project, ""), "...") + #F = open(i[0]+"/"+b, "rb") + S = open(i[0]+"/"+BACKNAME, "wb") + S.write(F) + S.close() + + +# Main loop ( 30 seconds intervals ) + +while True: + print("Counting another 30 seconds...") + time.sleep(30) + print("Checking for changed files...") + main() diff --git a/studio/studio_storyLayer.py b/studio/studio_storyLayer.py index b43cc30..998826d 100644 --- a/studio/studio_storyLayer.py +++ b/studio/studio_storyLayer.py @@ -568,6 +568,21 @@ def layer(win): # Bottom + + # Backup launcher + def do(): + os.system("gnome-terminal -- python3 "+os.getcwd()+"/studio/backup_blends.py "+win.project) + + UI_elements.roundrect(layer, win, + 5, + win.current["h"]-145, + 40, + 40, + 10, + do, + "copy_file", + talk.text("back_up_tooltip"), + url="story_editor") # Multiuser def do():