ykit/buildtool/task/fileutil.py

38 lines
1.4 KiB
Python

from pathlib import Path
import shutil
def merge_into(src: Path | str, dest: Path | str, check_date: bool = True, touch_directories: bool = True):
"""Merges the contents of a directory into another
Args:
src (Path | str): The source directory to merge from
dest (Path | str): The destination directory to merge into
check_date (bool, optional): If true, only copies files which are newer in src than in dest. Defaults to True.
touch_directories (bool, optional): Updates the modified time of parent directories. Defaults to True.
Returns:
_type_: _description_
"""
src = Path(src)
dest = Path(dest)
changed = False
for (cd, _, files) in src.walk():
for file in files:
src_file = cd / file
dest_file = dest / src_file.relative_to(src)
# Don't update if dest is newer than source
if check_date and dest_file.exists() and src_file.stat().st_mtime < dest_file.stat().st_mtime:
continue
dest_file.parent.mkdir(parents=True, exist_ok=True)
_ = shutil.copyfile(src_file, dest_file)
changed = True
if touch_directories:
for prnt in dest_file.relative_to(dest).parents:
(dest / prnt).touch(exist_ok=True)
return changed