The agent that turns websites into APIs!
fix: sync shouldn't overwrite existing dir
kalil0321fffc959b5ab9
diff --git a/src/reverse_api/sync.py b/src/reverse_api/sync.pyindex 5757992..353b90f 100644--- a/src/reverse_api/sync.py+++ b/src/reverse_api/sync.py@@ -2,6 +2,7 @@import timeimport shutil+from datetime import datetimefrom pathlib import Pathfrom typing import Optional, Callablefrom threading import Thread, Event@@ -9,6 +10,27 @@ from watchdog.observers import Observerfrom watchdog.events import FileSystemEventHandler, FileSystemEvent+def get_available_directory(base_path: Path, base_name: str) -> Path:+ """+ Find an available directory name, appending timestamp if needed.++ If the base directory exists and is not empty, returns a new directory+ with timestamp suffix. Otherwise returns the base directory.+ """+ target_dir = base_path / base_name++ # If directory doesn't exist or is empty, use it+ if not target_dir.exists() or (+ target_dir.is_dir() and not any(target_dir.iterdir())+ ):+ return target_dir++ # Otherwise, create a new directory with timestamp+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")+ folder_name = f"{base_name}_{timestamp}"+ return base_path / folder_name++class SyncHandler(FileSystemEventHandler):"""Handle file system events for syncing."""@@ -180,12 +202,25 @@ class FileSyncWatcher:def sync_directory_once(source_dir: Path, dest_dir: Path):- """Perform a one-time sync of a directory."""- dest_dir.mkdir(parents=True, exist_ok=True)+ """+ Perform a one-time sync of a directory.++ If dest_dir already exists and is not empty, finds an available+ directory with _iter{i} suffix instead of overwriting.+ """+ # Extract base path and name from dest_dir+ base_path = dest_dir.parent+ base_name = dest_dir.name++ # Get available directory (won't overwrite existing non-empty dirs)+ final_dest_dir = get_available_directory(base_path, base_name)+ final_dest_dir.mkdir(parents=True, exist_ok=True)for item in source_dir.rglob("*"):if item.is_file():relative = item.relative_to(source_dir)- dest = dest_dir / relative+ dest = final_dest_dir / relativedest.parent.mkdir(parents=True, exist_ok=True)shutil.copy2(item, dest)++ return final_dest_dir