From 1ee90cbdf8cf60e515322e343ae654c0b017e36b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Goinvic?= Date: Tue, 12 May 2026 15:12:53 +0000 Subject: [PATCH 1/2] Extra file: lazy loading of discovered files --- .../newsfragments/1298.feature.md | 1 + .../src/ess_migration_tool/extra_files.py | 22 +++++++++--- .../src/ess_migration_tool/migration.py | 34 +++++++++++++++++-- .../src/ess_migration_tool/models.py | 3 +- .../tests/test_extra_files.py | 22 ++++++++---- 5 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 packages/ess-migration-tool/newsfragments/1298.feature.md diff --git a/packages/ess-migration-tool/newsfragments/1298.feature.md b/packages/ess-migration-tool/newsfragments/1298.feature.md new file mode 100644 index 00000000..ae3cfb15 --- /dev/null +++ b/packages/ess-migration-tool/newsfragments/1298.feature.md @@ -0,0 +1 @@ +Lazy load extra files instead of loading them in memory before dumping them into our output directory. diff --git a/packages/ess-migration-tool/src/ess_migration_tool/extra_files.py b/packages/ess-migration-tool/src/ess_migration_tool/extra_files.py index 7c20f05b..94de84df 100644 --- a/packages/ess-migration-tool/src/ess_migration_tool/extra_files.py +++ b/packages/ess-migration-tool/src/ess_migration_tool/extra_files.py @@ -400,32 +400,44 @@ class ExtraFilesDiscovery: Validate that a file path exists and is readable. Args: - path: Path to validate + discovered_path: The discovered path to validate + override_path: Optional path to use instead of discovered_path.source_path Returns: - True if file is valid, False if it's a binary file + DiscoveredExtraFile with source_path set (content NOT loaded to save memory) Raises: ExtraFilesError: If file doesn't exist or isn't readable """ file_path = override_path or Path(discovered_path.source_path) - extra_file = DiscoveredExtraFile(filename=file_path.name, discovered_source_paths=[discovered_path]) + extra_file = DiscoveredExtraFile( + filename=file_path.name, + discovered_source_paths=[discovered_path], + source_path=file_path, # Store path for lazy loading + ) + # Validate file exists if not file_path.exists(): raise ExtraFilesError(f"Extra file does not exist: {file_path}") + # Validate it's a file (not a directory) if not file_path.is_file(): raise ExtraFilesError(f"Extra file path is not a file: {file_path}") - # Check if file is binary + # Check if file is binary BEFORE attempting to read if self._is_binary_file(file_path): extra_file.cleartext = False + # Don't read binary files - they can't be ConfigMaps anyway + return extra_file + # Validate we have permission to read the file try: with open(file_path): - extra_file.content = file_path.read_bytes() + pass # Just test opening, don't read content yet except PermissionError as err: raise ExtraFilesError(f"Permission denied when reading file: {file_path}") from err + + # DON'T read content here - defer to when ConfigMap is created (saves memory) return extra_file diff --git a/packages/ess-migration-tool/src/ess_migration_tool/migration.py b/packages/ess-migration-tool/src/ess_migration_tool/migration.py index ee268975..19e9df64 100644 --- a/packages/ess-migration-tool/src/ess_migration_tool/migration.py +++ b/packages/ess-migration-tool/src/ess_migration_tool/migration.py @@ -373,9 +373,39 @@ class ConfigValueTransformer: configmap_data = {} + # Maximum file size to load into ConfigMap (1MB) + # Larger files are skipped to prevent memory issues + MAX_EXTRA_FILE_SIZE = 1 * 1024 * 1024 + for discovered_extra_file in extra_files_discovery.discovered_extra_files.values(): - # Encode the file content - configmap_data[discovered_extra_file.filename] = discovered_extra_file.content.decode("utf-8") + # Skip non-cleartext files (binary files) + if not discovered_extra_file.cleartext: + logger.debug(f"Skipping non-cleartext file: {discovered_extra_file.filename}") + continue + + # Skip files that are too large + if ( + discovered_extra_file.source_path is not None + and discovered_extra_file.source_path.stat().st_size > MAX_EXTRA_FILE_SIZE + ): + logger.warning( + f"Skipping large file {discovered_extra_file.source_path} " + f"({discovered_extra_file.source_path.stat().st_size} bytes > {MAX_EXTRA_FILE_SIZE} bytes)" + ) + continue + + # Read content from source_path (lazy loading - content read once here, not during discovery) + if discovered_extra_file.source_path is not None: + try: + with open(discovered_extra_file.source_path, encoding="utf-8") as f: + content = f.read() + configmap_data[discovered_extra_file.filename] = content + except Exception as e: + logger.warning(f"Failed to read file {discovered_extra_file.source_path}: {e}") + continue + else: + logger.warning(f"No source_path for discovered extra file: {discovered_extra_file.filename}") + continue configmap = ConfigMap(name=configmap_name, data=configmap_data) diff --git a/packages/ess-migration-tool/src/ess_migration_tool/models.py b/packages/ess-migration-tool/src/ess_migration_tool/models.py index e5e53d44..c59bfb13 100644 --- a/packages/ess-migration-tool/src/ess_migration_tool/models.py +++ b/packages/ess-migration-tool/src/ess_migration_tool/models.py @@ -102,8 +102,7 @@ class DiscoveredExtraFile: init=True ) # Source configuration file (e.g., "synapse.yaml", "mas.yaml") filename: str = field(init=True) # Original file path in source file - # and the file was discovered by listing its content - content: bytes = field(default_factory=bytes) # Extra file content. File bigger than 100KiB will be skipped + source_path: Path | None = field(default=None) # Path to the source file (lazy-loaded, not content) cleartext: bool = field(default=True) # If true, the file will be stored in a ConfigMap diff --git a/packages/ess-migration-tool/tests/test_extra_files.py b/packages/ess-migration-tool/tests/test_extra_files.py index f3634bbb..40d2b02d 100644 --- a/packages/ess-migration-tool/tests/test_extra_files.py +++ b/packages/ess-migration-tool/tests/test_extra_files.py @@ -56,9 +56,12 @@ def test_validate_extra_files_success(tmp_path): assert len(discovery.discovered_extra_files) == 2 for file in discovery.discovered_extra_files.values(): if file.discovered_source_paths[0] == test1: - assert file.content == b"test content 1" + # Content is now lazy-loaded from source_path + assert file.source_path is not None + assert file.source_path.read_bytes() == b"test content 1" elif file.discovered_source_paths[0] == test2: - assert file.content == b"test: content 2" + assert file.source_path is not None + assert file.source_path.read_bytes() == b"test: content 2" else: pytest.fail("Unexpected file") @@ -309,12 +312,15 @@ def test_duplicate_file_paths(tmp_path): for path, discovered_file in discovery.discovered_extra_files.items(): if path == str(tmp_path / "templates"): - assert discovered_file.content == "header" + # Content is now lazy-loaded from source_path + assert discovered_file.source_path is not None + assert discovered_file.source_path.read_text() == "header" assert len(discovered_file.discovered_source_paths) == 3 for discovered in discovered_file.discovered_source_paths: assert discovered.source_path == str(tmp_path / "templates") elif path == str(tmp_path / "other"): - assert discovered_file.content == "other" + assert discovered_file.source_path is not None + assert discovered_file.source_path.read_text() == "other" assert discovered_file.discovered_source_paths[0].source_path == str(tmp_path / "other") @@ -359,10 +365,14 @@ def test_binary_file_detection(tmp_path): for discovered_file in discovery.discovered_extra_files.values(): if discovered_file.discovered_source_paths[0].source_path == text_file: assert discovered_file.cleartext - assert discovered_file.content == text_file.read_bytes() + # Text files have source_path set and can be read + assert discovered_file.source_path is not None + assert discovered_file.source_path.read_bytes() == text_file.read_bytes() if discovered_file.discovered_source_paths[0].source_path == binary_file: assert not discovered_file.cleartext - assert discovered_file.content == binary_file.read_bytes() + # Binary files have source_path set but cleartext=False + assert discovered_file.source_path is not None + assert discovered_file.source_path.read_bytes() == binary_file.read_bytes() def test_ignored_config_keys(): From c33469a848392eec7c0447d115d4116428699e0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Goinvic?= Date: Wed, 13 May 2026 09:23:17 +0200 Subject: [PATCH 2/2] press enter to continue on ignored extra file --- packages/ess-migration-tool/src/ess_migration_tool/migration.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/ess-migration-tool/src/ess_migration_tool/migration.py b/packages/ess-migration-tool/src/ess_migration_tool/migration.py index 19e9df64..6f15a2a2 100644 --- a/packages/ess-migration-tool/src/ess_migration_tool/migration.py +++ b/packages/ess-migration-tool/src/ess_migration_tool/migration.py @@ -28,6 +28,7 @@ from .secrets import SecretDiscovery from .utils import ( find_matching_schema_key, get_nested_value, + press_enter_to_continue, remove_nested_value, set_nested_value, sort_tracked_values_for_filtering, @@ -392,6 +393,7 @@ class ConfigValueTransformer: f"Skipping large file {discovered_extra_file.source_path} " f"({discovered_extra_file.source_path.stat().st_size} bytes > {MAX_EXTRA_FILE_SIZE} bytes)" ) + press_enter_to_continue(pretty_logger=self.pretty_logger) continue # Read content from source_path (lazy loading - content read once here, not during discovery)