[docs]defensure_symlink(source_path:Path,target_path:Path,force:bool)->None:ifnotsource_path.exists():raiseFileNotFoundError(f"Missing source path for symlink: {source_path}")ifpath_exists(target_path):ifsymlink_matches(target_path,source_path):returnifnotforce:raiseFileExistsError(f"Conflict at {target_path}: already exists. Use --force to overwrite.")remove_path(target_path)target_path.parent.mkdir(parents=True,exist_ok=True)relative_target=Path(os.path.relpath(source_path,start=target_path.parent))target_path.symlink_to(relative_target,target_is_directory=source_path.is_dir())
[docs]defremove_managed_symlink(target_path:Path,expected_source:Path,force:bool=False)->None:ifnotpath_exists(target_path):returnifforce:remove_path(target_path)returnifsymlink_matches(target_path,expected_source):remove_path(target_path)returnraiseFileExistsError(f"Conflict at {target_path}: expected symlink to {expected_source}. ""Use --force to remove anyway.")
[docs]defensure_copied_directory(source_path:Path,target_path:Path,*,force:bool)->None:ifnotsource_path.is_dir():raiseFileNotFoundError(f"Missing source directory for managed copy: {source_path}")ifpath_exists(target_path):iftarget_path.is_symlink():ifsymlink_matches(target_path,source_path):remove_path(target_path)elifnotforce:raiseFileExistsError(f"Conflict at {target_path}: already exists. ""Use --force to overwrite.")else:remove_path(target_path)eliftarget_path.is_dir():ifdirectories_match(target_path,source_path):returnifnotforce:raiseFileExistsError(f"Conflict at {target_path}: local directory content differs ""from managed copy. Use --force to overwrite.")remove_path(target_path)else:ifnotforce:raiseFileExistsError(f"Conflict at {target_path}: already exists. ""Use --force to overwrite.")remove_path(target_path)target_path.parent.mkdir(parents=True,exist_ok=True)shutil.copytree(source_path,target_path)
[docs]defremove_managed_copied_directory(target_path:Path,expected_source:Path,*,force:bool,)->None:ifnotpath_exists(target_path):returnifforce:remove_path(target_path)returniftarget_path.is_symlink():ifsymlink_matches(target_path,expected_source):remove_path(target_path)returnraiseFileExistsError(f"Conflict at {target_path}: expected managed copied directory "f"or symlink to {expected_source}. Use --force to remove anyway.")iftarget_path.is_dir():ifdirectories_match(target_path,expected_source):remove_path(target_path)returnraiseFileExistsError(f"Conflict at {target_path}: expected managed copied directory content. ""Use --force to remove anyway.")raiseFileExistsError(f"Conflict at {target_path}: expected managed copied directory. ""Use --force to remove anyway.")