From 8a0e288f719faac6dca1e671a71ede36d431bd39 Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Thu, 6 Feb 2025 11:14:19 -0800 Subject: [PATCH] Add path_ops.update_directory_tree: Copy missing and newer files from src to dest --- reflex/utils/path_ops.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/reflex/utils/path_ops.py b/reflex/utils/path_ops.py index 07a541201..c85810544 100644 --- a/reflex/utils/path_ops.py +++ b/reflex/utils/path_ops.py @@ -260,3 +260,29 @@ def find_replace(directory: str | Path, find: str, replace: str): text = filepath.read_text(encoding="utf-8") text = re.sub(find, replace, text) filepath.write_text(text, encoding="utf-8") + + +def update_directory_tree(src: Path, dest: Path): + """Recursively copies a directory tree from src to dest. + Only copies files if the destination file is missing or modified earlier than the source file. + + :param src: Source directory (Path object) + :param dest: Destination directory (Path object) + """ + if not src.is_dir(): + raise ValueError(f"Source {src} is not a directory") + + # Ensure the destination directory exists + dest.mkdir(parents=True, exist_ok=True) + + for item in src.iterdir(): + dest_item = dest / item.name + + if item.is_dir(): + # Recursively copy subdirectories + update_directory_tree(item, dest_item) + elif item.is_file() and ( + not dest_item.exists() or item.stat().st_mtime > dest_item.stat().st_mtime + ): + # Copy file if it doesn't exist in the destination or is older than the source + shutil.copy2(item, dest_item)