Use temp download dir and robust rmtree handler to avoid permission errors on cleanup
Browse files- download_model.py +32 -5
download_model.py
CHANGED
|
@@ -2,6 +2,8 @@
|
|
| 2 |
|
| 3 |
import os
|
| 4 |
import shutil
|
|
|
|
|
|
|
| 5 |
from pathlib import Path
|
| 6 |
|
| 7 |
from huggingface_hub import HfApi, snapshot_download
|
|
@@ -64,16 +66,38 @@ def main() -> None:
|
|
| 64 |
if cached.exists():
|
| 65 |
print(f"using cached model: {cached}")
|
| 66 |
return
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
# allow user to request a specific file in the repo (useful when repo has no .gguf)
|
| 70 |
model_file_override = os.environ.get("MODEL_FILE", "").strip()
|
| 71 |
|
| 72 |
# previous cache check moved above
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
api = HfApi(token=hf_token)
|
| 79 |
print(f"listing model files: {model_name}", flush=True)
|
|
@@ -139,7 +163,10 @@ def main() -> None:
|
|
| 139 |
repo_marker.unlink()
|
| 140 |
repo_marker.write_text(f"{model_name}|{selected_basename}", encoding="utf-8")
|
| 141 |
|
| 142 |
-
|
|
|
|
|
|
|
|
|
|
| 143 |
print(f"ready: {target_model}")
|
| 144 |
|
| 145 |
|
|
|
|
| 2 |
|
| 3 |
import os
|
| 4 |
import shutil
|
| 5 |
+
import tempfile
|
| 6 |
+
import stat
|
| 7 |
from pathlib import Path
|
| 8 |
|
| 9 |
from huggingface_hub import HfApi, snapshot_download
|
|
|
|
| 66 |
if cached.exists():
|
| 67 |
print(f"using cached model: {cached}")
|
| 68 |
return
|
| 69 |
+
# Use a temporary directory (under /tmp) for downloads to avoid permission issues
|
| 70 |
+
download_dir = Path(tempfile.mkdtemp(prefix="hf-download-"))
|
| 71 |
|
| 72 |
# allow user to request a specific file in the repo (useful when repo has no .gguf)
|
| 73 |
model_file_override = os.environ.get("MODEL_FILE", "").strip()
|
| 74 |
|
| 75 |
# previous cache check moved above
|
| 76 |
|
| 77 |
+
# Ensure any stale temp dir is removed with a permissive onerror handler
|
| 78 |
+
def _rmtree_onerror(func, path, exc_info):
|
| 79 |
+
# try to make the file writable then retry
|
| 80 |
+
try:
|
| 81 |
+
os.chmod(path, stat.S_IWUSR | stat.S_IRUSR)
|
| 82 |
+
except Exception:
|
| 83 |
+
pass
|
| 84 |
+
try:
|
| 85 |
+
func(path)
|
| 86 |
+
except Exception:
|
| 87 |
+
try:
|
| 88 |
+
if os.path.isdir(path):
|
| 89 |
+
for root, dirs, files in os.walk(path):
|
| 90 |
+
for name in files:
|
| 91 |
+
fp = os.path.join(root, name)
|
| 92 |
+
try:
|
| 93 |
+
os.chmod(fp, stat.S_IWUSR | stat.S_IRUSR)
|
| 94 |
+
except Exception:
|
| 95 |
+
pass
|
| 96 |
+
func(path)
|
| 97 |
+
else:
|
| 98 |
+
os.remove(path)
|
| 99 |
+
except Exception as e:
|
| 100 |
+
print(f"warning: failed to remove {path}: {e}", flush=True)
|
| 101 |
|
| 102 |
api = HfApi(token=hf_token)
|
| 103 |
print(f"listing model files: {model_name}", flush=True)
|
|
|
|
| 163 |
repo_marker.unlink()
|
| 164 |
repo_marker.write_text(f"{model_name}|{selected_basename}", encoding="utf-8")
|
| 165 |
|
| 166 |
+
try:
|
| 167 |
+
shutil.rmtree(download_dir, onerror=_rmtree_onerror)
|
| 168 |
+
except Exception as e:
|
| 169 |
+
print(f"warning: unable to fully remove temp download dir {download_dir}: {e}", flush=True)
|
| 170 |
print(f"ready: {target_model}")
|
| 171 |
|
| 172 |
|