in src/huggingface_hub/hf_file_system.py [0:0]
def cp_file(self, path1: str, path2: str, revision: Optional[str] = None, **kwargs) -> None:
"""
Copy a file within or between repositories.
<Tip warning={true}>
Note: When possible, use `HfApi.upload_file()` for better performance.
</Tip>
Args:
path1 (`str`):
Source path to copy from.
path2 (`str`):
Destination path to copy to.
revision (`str`, *optional*):
The git revision to copy from.
"""
resolved_path1 = self.resolve_path(path1, revision=revision)
resolved_path2 = self.resolve_path(path2, revision=revision)
same_repo = (
resolved_path1.repo_type == resolved_path2.repo_type and resolved_path1.repo_id == resolved_path2.repo_id
)
if same_repo:
commit_message = f"Copy {path1} to {path2}"
self._api.create_commit(
repo_id=resolved_path1.repo_id,
repo_type=resolved_path1.repo_type,
revision=resolved_path2.revision,
commit_message=kwargs.get("commit_message", commit_message),
commit_description=kwargs.get("commit_description", ""),
operations=[
CommitOperationCopy(
src_path_in_repo=resolved_path1.path_in_repo,
path_in_repo=resolved_path2.path_in_repo,
src_revision=resolved_path1.revision,
)
],
)
else:
with self.open(path1, "rb", revision=resolved_path1.revision) as f:
content = f.read()
commit_message = f"Copy {path1} to {path2}"
self._api.upload_file(
path_or_fileobj=content,
path_in_repo=resolved_path2.path_in_repo,
repo_id=resolved_path2.repo_id,
token=self.token,
repo_type=resolved_path2.repo_type,
revision=resolved_path2.revision,
commit_message=kwargs.get("commit_message", commit_message),
commit_description=kwargs.get("commit_description"),
)
self.invalidate_cache(path=resolved_path1.unresolve())
self.invalidate_cache(path=resolved_path2.unresolve())