Skip to content

Commit cce60b9

Browse files
committed
Do not raise if chmod fails (#2429)
* Do not raise if chmod fails * just to test * test * comments
1 parent 071503f commit cce60b9

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/huggingface_hub/file_download.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1951,6 +1951,11 @@ def _chmod_and_move(src: Path, dst: Path) -> None:
19511951
tmp_file.touch()
19521952
cache_dir_mode = Path(tmp_file).stat().st_mode
19531953
os.chmod(str(src), stat.S_IMODE(cache_dir_mode))
1954+
except OSError as e:
1955+
logger.warning(
1956+
f"Could not set the permissions on the file '{src}'. "
1957+
f"Error: {e}.\nContinuing without setting permissions."
1958+
)
19541959
finally:
19551960
try:
19561961
tmp_file.unlink()
@@ -1959,7 +1964,21 @@ def _chmod_and_move(src: Path, dst: Path) -> None:
19591964
# See https://github.com/huggingface/huggingface_hub/issues/2359
19601965
pass
19611966

1962-
shutil.move(str(src), str(dst))
1967+
shutil.move(str(src), str(dst), copy_function=_copy_no_matter_what)
1968+
1969+
1970+
def _copy_no_matter_what(src: str, dst: str) -> None:
1971+
"""Copy file from src to dst.
1972+
1973+
If `shutil.copy2` fails, fallback to `shutil.copyfile`.
1974+
"""
1975+
try:
1976+
# Copy file with metadata and permission
1977+
# Can fail e.g. if dst is an S3 mount
1978+
shutil.copy2(src, dst)
1979+
except OSError:
1980+
# Copy only file content
1981+
shutil.copyfile(src, dst)
19631982

19641983

19651984
def _get_pointer_path(storage_folder: str, revision: str, relative_filename: str) -> str:

0 commit comments

Comments
 (0)