Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not raise if chmod fails #2429

Merged
merged 5 commits into from
Jul 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/huggingface_hub/file_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -1951,6 +1951,11 @@ def _chmod_and_move(src: Path, dst: Path) -> None:
tmp_file.touch()
cache_dir_mode = Path(tmp_file).stat().st_mode
os.chmod(str(src), stat.S_IMODE(cache_dir_mode))
except OSError as e:
logger.warning(
f"Could not set the permissions on the file '{src}'. "
f"Error: {e}.\nContinuing without setting permissions."
)
finally:
try:
tmp_file.unlink()
Expand All @@ -1959,7 +1964,21 @@ def _chmod_and_move(src: Path, dst: Path) -> None:
# See https://github.com/huggingface/huggingface_hub/issues/2359
pass

shutil.move(str(src), str(dst))
shutil.move(str(src), str(dst), copy_function=_copy_no_matter_what)


def _copy_no_matter_what(src: str, dst: str) -> None:
"""Copy file from src to dst.

If `shutil.copy2` fails, fallback to `shutil.copyfile`.
"""
try:
# Copy file with metadata and permission
# Can fail e.g. if dst is an S3 mount
shutil.copy2(src, dst)
except OSError:
# Copy only file content
shutil.copyfile(src, dst)


def _get_pointer_path(storage_folder: str, revision: str, relative_filename: str) -> str:
Expand Down
Loading