From 46aed1f13dea1b5a3b5af5376fa0e8a90be62ec4 Mon Sep 17 00:00:00 2001 From: Sergio Date: Sun, 8 Feb 2026 13:37:00 +0100 Subject: [PATCH] creat script --- files.py | 4 +++ watcher.py | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 files.py create mode 100644 watcher.py diff --git a/files.py b/files.py new file mode 100644 index 0000000..5e63610 --- /dev/null +++ b/files.py @@ -0,0 +1,4 @@ +FILES = [ + "/sustancia/home/saves/Beetle PSX HW/Suikoden II (USA) (Bug Fix) (Pyriel) (2.02.078).1.mcr", + "/sustancia/home/saves/Beetle PSX HW/Suikoden II (USA) (Bug Fix) (Pyriel) (2.02.078).srm", +] diff --git a/watcher.py b/watcher.py new file mode 100644 index 0000000..53bec2b --- /dev/null +++ b/watcher.py @@ -0,0 +1,76 @@ +import os +import time +import shutil +import hashlib +from datetime import datetime +from files import FILES + +CHECK_INTERVAL = 60 # segundos + +def sha256sum(path): + """Devuelve el hash SHA256 del fichero.""" + h = hashlib.sha256() + with open(path, "rb") as f: + for chunk in iter(lambda: f.read(4096), b""): + h.update(chunk) + return h.hexdigest() + +def ensure_backup_dir(file_path): + """Crea backup// si no existe.""" + file_name = os.path.basename(file_path) + name, _ = os.path.splitext(file_name) + backup_dir = os.path.join("backup", name) + os.makedirs(backup_dir, exist_ok=True) + return backup_dir + +def get_latest_backup(file_path): + """Devuelve la ruta del backup más reciente o None si no hay.""" + backup_dir = ensure_backup_dir(file_path) + files = sorted(os.listdir(backup_dir), reverse=True) + if not files: + return None + return os.path.join(backup_dir, files[0]) + +def backup_file(file_path): + """Crea una copia con timestamp.""" + backup_dir = ensure_backup_dir(file_path) + file_name = os.path.basename(file_path) + name, ext = os.path.splitext(file_name) + timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") + dest = os.path.join(backup_dir, f"{name}_{timestamp}{ext}") + shutil.copy2(file_path, dest) + print(f"[BACKUP] Copia creada: {dest}") + +def process_file(file_path): + """Comprueba si hay cambios y crea backup si es necesario.""" + if not os.path.isfile(file_path): + print(f"[WARN] No existe: {file_path}") + return + + latest = get_latest_backup(file_path) + + if latest is None: + print(f"[INIT] No hay copia previa de {file_path}, creando primera copia...") + backup_file(file_path) + return + + # Comparamos hashes + current_hash = sha256sum(file_path) + latest_hash = sha256sum(latest) + + if current_hash != latest_hash: + print(f"[CHANGE] Detectado cambio en {file_path}, creando nueva copia...") + backup_file(file_path) + else: + print(f"[OK] Sin cambios en {file_path}") + +def main(): + print("Iniciando watcher de saves...") + + while True: + for f in FILES: + process_file(f) + time.sleep(CHECK_INTERVAL) + +if __name__ == "__main__": + main()