Skip to content

Commit

Permalink
Do not raise if chmod fails (#2429)
Browse files Browse the repository at this point in the history
* Do not raise if chmod fails

* just to test

* test

* comments
  • Loading branch information
Wauplin authored Jul 31, 2024
1 parent 59eacad commit c33a429
Showing 1 changed file with 20 additions and 1 deletion.
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

0 comments on commit c33a429

Please sign in to comment.