VERSIONES.py
Archivo real del paquete para implementar la función. Descárgalo del ZIP para editarlo; no contiene claves privadas.
#!/usr/bin/env python3
"""Crea una copia local y permite restaurar solo outputs del módulo. Confirmación explícita.
No restaura Worker remoto ni modifica datos de Play Console.
"""
import json, zipfile
from datetime import datetime,timezone
from pathlib import Path
MOD=Path(__file__).resolve().parent;ROOT=MOD.parent
BACKUPS=MOD/'backups/versiones';TARGETS=['datos','ia','mcp','index.html']
def backup(quiet=False):
BACKUPS.mkdir(parents=True,exist_ok=True)
dst=BACKUPS/('snapshot_'+datetime.now(timezone.utc).strftime('%Y%m%dT%H%M%S%fZ')+'.zip')
with zipfile.ZipFile(dst,'w',compression=zipfile.ZIP_DEFLATED,compresslevel=5) as z:
for k in TARGETS:
path=ROOT/k
if path.is_file():z.write(path,k)
elif path.is_dir():
for f in path.rglob('*'):
if f.is_file() and not f.is_symlink() and f.stat().st_size<20_000_000:z.write(f,f.relative_to(ROOT).as_posix())
z.writestr('INFORMACION_BACKUP.json',json.dumps({'fecha':datetime.now(timezone.utc).isoformat(),'contenido':'solo carpeta datos, ia, mcp e index.html; sin Worker remoto'}))
archives=sorted(BACKUPS.glob('snapshot_*.zip'))
for old in archives[:-5]:old.unlink()
if not quiet:print('Copia de seguridad local:',dst)
return dst
def restore():
archives=sorted(BACKUPS.glob('snapshot_*.zip'),reverse=True)
if not archives:
print('No hay copias locales.');return
for i,p in enumerate(archives,1):print(i,p.name)
try:idx=int(input('Número de copia a restaurar (0 cancela): ').strip())
except ValueError:return
if idx<1 or idx>len(archives):return
target=archives[idx-1]
with zipfile.ZipFile(target) as z:
members=[n for n in z.namelist() if n!='INFORMACION_BACKUP.json']
if any(Path(n).is_absolute() or '..' in Path(n).parts or not (n in TARGETS or n.split('/')[0] in TARGETS[:3]) for n in members):
raise RuntimeError('Archivo de respaldo contiene una ruta inesperada; operación cancelada')
print('La restauración sobrescribirá SOLO archivos presentes en el respaldo. No eliminará páginas ajenas al módulo.')
if input('Escribe RESTAURAR para confirmar: ')!='RESTAURAR':return
backup()
for n in members:
dst=ROOT/n;dst.parent.mkdir(parents=True,exist_ok=True)
dst.write_bytes(z.read(n))
print('Restauración local terminada. Worker remoto debe restaurarse manualmente si cambió.')
if __name__=='__main__':
print('1) Crear respaldo 2) Restaurar')
choice=input('Opción: ').strip()
if choice=='1':backup()
elif choice=='2':restore()