#!/usr/bin/env python3 """ Apply the memory handler fix to all components This script backs up original files and replaces them with memory-based versions """ import os import shutil from datetime import datetime def backup_and_replace(original_file, new_file): """Backup original file and replace with new version.""" if not os.path.exists(original_file): print(f"āŒ Original file not found: {original_file}") return False if not os.path.exists(new_file): print(f"āŒ New file not found: {new_file}") return False # Create backup backup_file = f"{original_file}.backup_{datetime.now().strftime('%Y%m%d_%H%M%S')}" shutil.copy2(original_file, backup_file) print(f"šŸ“‹ Backed up: {original_file} → {backup_file}") # Replace with new version shutil.copy2(new_file, original_file) print(f"āœ… Replaced: {original_file}") return True def main(): """Apply memory handler updates to all components.""" print("šŸ”„ Applying Memory Handler Fix") print("=" * 60) print("This will backup original files and replace with memory-based versions") print("=" * 60) # Files to update updates = [ ("web_app/handlers/frequency_handlers.py", "web_app/handlers/frequency_handlers_updated.py"), ("web_app/handlers/analysis_handlers.py", "web_app/handlers/analysis_handlers_updated.py"), ] # Confirm with user response = input("\nProceed with updates? (y/n): ").lower() if response != 'y': print("āŒ Cancelled") return print("\nšŸš€ Starting updates...") success_count = 0 for original, updated in updates: if backup_and_replace(original, updated): success_count += 1 print("\n" + "=" * 60) print(f"āœ… Successfully updated {success_count}/{len(updates)} files") # Additional components that need manual updates print("\nāš ļø The following files still need manual updates:") manual_updates = [ "web_app/handlers/pos_handlers.py", "web_app/reference_manager.py", "web_app/config_manager.py", "web_app/debug_utils.py" ] for file in manual_updates: print(f" - {file}") print("\nšŸ’” To complete the migration:") print("1. Update the remaining files manually") print("2. Test the application thoroughly") print("3. Remove the *_updated.py files after verification") print("\nšŸ“ Key changes to make in remaining files:") print("- Replace: from web_app.utils import FileUploadHandler") print(" With: from web_app.utils import MemoryFileHandler") print("- Replace: FileUploadHandler.save_to_temp() + read_from_temp()") print(" With: MemoryFileHandler.process_uploaded_file()") print("- Remove: All cleanup_temp_file() calls") if __name__ == "__main__": main()