From 4ac116828c6c42d9f749cf095ba2994e8b9b9a2d Mon Sep 17 00:00:00 2001 From: Sunny Patel Date: Sun, 27 Oct 2024 14:30:52 +0530 Subject: [PATCH] Add `copy_tree_with_update` method for efficient directory copy Introduces `copy_tree_with_update` method which ensures that only new or updated files are copied from source to destination. This replaces the previous `copytree` call in the `inject_vendor_files` method, improving the efficiency of file copying operations. --- zappa/core.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/zappa/core.py b/zappa/core.py index 12a143be..ee0d6d53 100644 --- a/zappa/core.py +++ b/zappa/core.py @@ -357,6 +357,24 @@ def __init__( self.cf_api_resources = [] self.cf_parameters = {} + def copy_tree_with_update(self, src, dest): + src_path = Path(src) + dest_path = Path(dest) + + # Ensure destination path exists + dest_path.mkdir(parents=True, exist_ok=True) + + for item in src_path.rglob('*'): + dest_item = dest_path / item.relative_to(src_path) + + # Check if the item is a directory + if item.is_dir(): + dest_item.mkdir(parents=True, exist_ok=True) + else: + # Copy file if it does not exist in destination or is newer + if not dest_item.exists() or os.path.getmtime(item) > os.path.getmtime(dest_item): + shutil.copy2(item, dest_item) + def configure_boto_session_method_kwargs(self, service, kw): """Allow for custom endpoint urls for non-AWS (testing and bootleg cloud) deployments""" if service in self.endpoint_urls and "endpoint_url" not in kw: @@ -686,7 +704,7 @@ def splitpath(path): if egg_links: self.copy_editable_packages(egg_links, temp_package_path) - copytree(temp_package_path, temp_project_path, metadata=False, symlinks=False) + self.copy_tree_with_update(temp_package_path, temp_project_path) # Then the pre-compiled packages.. if use_precompiled_packages: