Skip to content

The agent that turns websites into APIs!

fix: sync fallback

kalil032107527fd43806
diff --git a/src/reverse_api/sync.py b/src/reverse_api/sync.py
index 353b90f..a4fa452 100644
--- a/src/reverse_api/sync.py
+++ b/src/reverse_api/sync.py
@@ -51,19 +51,40 @@ class SyncHandler(FileSystemEventHandler):
self.last_sync_time = 0
self.file_count = 0
+ def _is_temporary_file(self, file_path: str) -> bool:
+ """Check if a file is a temporary file that should be ignored."""
+ path = Path(file_path)
+ name = path.name
+
+ # Check for temporary file patterns
+ if name.endswith('.tmp') or '.tmp.' in name:
+ return True
+
+ # Check for __pycache__ directories
+ if '__pycache__' in path.parts:
+ return True
+
+ # Check for other common temporary patterns
+ if name.startswith('.') and name.endswith('.swp'):
+ return True
+ if name.startswith('~'):
+ return True
+
+ return False
+
def on_created(self, event: FileSystemEvent):
"""Handle file creation."""
- if not event.is_directory:
+ if not event.is_directory and not self._is_temporary_file(event.src_path):
self._queue_sync(event.src_path)
def on_modified(self, event: FileSystemEvent):
"""Handle file modification."""
- if not event.is_directory:
+ if not event.is_directory and not self._is_temporary_file(event.src_path):
self._queue_sync(event.src_path)
def on_deleted(self, event: FileSystemEvent):
"""Handle file deletion."""
- if not event.is_directory:
+ if not event.is_directory and not self._is_temporary_file(event.src_path):
self._queue_sync(event.src_path, is_delete=True)
def _queue_sync(self, file_path: str, is_delete: bool = False):
@@ -104,11 +125,19 @@ class SyncHandler(FileSystemEventHandler):
dest.unlink()
self.file_count -= 1
else:
+ # Check if source file exists before attempting to copy
+ if not source.exists():
+ return
+
# Copy to destination
dest.parent.mkdir(parents=True, exist_ok=True)
- shutil.copy2(source, dest)
- if dest.exists():
- self.file_count += 1
+ try:
+ shutil.copy2(source, dest)
+ if dest.exists():
+ self.file_count += 1
+ except FileNotFoundError:
+ # File was deleted between existence check and copy, skip silently
+ return
# Update last sync time
self.last_sync_time = time.time()
@@ -166,11 +195,20 @@ class FileSyncWatcher:
def stop(self):
"""Stop watching and syncing."""
self.stop_event.set()
+
+ # Process any remaining pending events before stopping
+ # Give it a moment to process debounced events
+ time.sleep(self.debounce_ms / 1000.0 + 0.1)
+ self.handler.process_pending()
+
self.observer.stop()
self.observer.join(timeout=2)
if self.process_thread:
self.process_thread.join(timeout=2)
+
+ # Perform final sync of all existing files to ensure nothing is missed
+ self._final_sync()
def _process_loop(self):
"""Background loop to process pending sync events."""
@@ -178,6 +216,27 @@ class FileSyncWatcher:
self.handler.process_pending()
time.sleep(0.1) # Check every 100ms
+ def _final_sync(self):
+ """Perform a final sync of all existing files in source directory."""
+ if not self.source_dir.exists():
+ return
+
+ for item in self.source_dir.rglob("*"):
+ if item.is_file() and not self.handler._is_temporary_file(str(item)):
+ relative = item.relative_to(self.source_dir)
+ dest = self.dest_dir / relative
+
+ # Only sync if destination doesn't exist or is older
+ if not dest.exists() or item.stat().st_mtime > dest.stat().st_mtime:
+ try:
+ dest.parent.mkdir(parents=True, exist_ok=True)
+ shutil.copy2(item, dest)
+ if self.handler.on_sync:
+ self.handler.on_sync(f"Synced {item.name}")
+ except (FileNotFoundError, OSError):
+ # File was deleted or inaccessible, skip silently
+ pass
+
def get_status(self) -> dict:
"""Get current sync status."""
if not self.observer.is_alive():