[docs]defensure_agents_file(source_path:Path,target_path:Path,*,force:bool,managed_symlink_sources:tuple[Path,...],)->None:ifnotsource_path.is_file():raiseFileNotFoundError(f"Missing source file for {AGENTS_FILENAME}: {source_path}")ifworkspace_fs.path_exists(target_path):iftarget_path.is_symlink():ifany(workspace_fs.symlink_matches(target_path,source_candidate)forsource_candidateinmanaged_symlink_sources):workspace_fs.remove_path(target_path)elifnotforce:raiseFileExistsError(f"Conflict at {target_path}: already exists. ""Use --force to overwrite.")else:workspace_fs.remove_path(target_path)eliftarget_path.is_file():ifworkspace_fs.files_match(target_path,source_path):returnifnotforce:raiseFileExistsError(f"Conflict at {target_path}: local file content differs from "f"managed {AGENTS_FILENAME}. Use --force to overwrite.")workspace_fs.remove_path(target_path)else:ifnotforce:raiseFileExistsError(f"Conflict at {target_path}: already exists. ""Use --force to overwrite.")workspace_fs.remove_path(target_path)target_path.parent.mkdir(parents=True,exist_ok=True)shutil.copy2(source_path,target_path)
[docs]defremove_managed_agents_file(target_path:Path,expected_source:Path,*,force:bool,managed_symlink_sources:tuple[Path,...],)->None:ifnotworkspace_fs.path_exists(target_path):returnifforce:workspace_fs.remove_path(target_path)returniftarget_path.is_symlink():ifany(workspace_fs.symlink_matches(target_path,source_candidate)forsource_candidateinmanaged_symlink_sources):workspace_fs.remove_path(target_path)returnraiseFileExistsError(f"Conflict at {target_path}: expected managed {AGENTS_FILENAME} symlink. ""Use --force to remove anyway.")iftarget_path.is_file():ifworkspace_fs.files_match(target_path,expected_source):workspace_fs.remove_path(target_path)returnraiseFileExistsError(f"Conflict at {target_path}: expected managed {AGENTS_FILENAME} file ""content. Use --force to remove anyway.")raiseFileExistsError(f"Conflict at {target_path}: expected managed {AGENTS_FILENAME} file/symlink. ""Use --force to remove anyway.")