Spaces:
Sleeping
Sleeping
Sync from kink_cli (Docker Space)
Browse files
backend/user_snapshot_hub.py
CHANGED
|
@@ -13,6 +13,7 @@ from __future__ import annotations
|
|
| 13 |
|
| 14 |
import logging
|
| 15 |
import os
|
|
|
|
| 16 |
from pathlib import Path
|
| 17 |
|
| 18 |
logger = logging.getLogger(__name__)
|
|
@@ -82,7 +83,10 @@ def pull_user_snapshot(target_path: Path) -> bool:
|
|
| 82 |
except (EntryNotFoundError, RepositoryNotFoundError):
|
| 83 |
logger.info("snapshot %s/%s not present on Hub yet (cold start)", snapshot_repo(), snapshot_filename())
|
| 84 |
return False
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
| 86 |
return True
|
| 87 |
|
| 88 |
|
|
|
|
| 13 |
|
| 14 |
import logging
|
| 15 |
import os
|
| 16 |
+
import shutil
|
| 17 |
from pathlib import Path
|
| 18 |
|
| 19 |
logger = logging.getLogger(__name__)
|
|
|
|
| 83 |
except (EntryNotFoundError, RepositoryNotFoundError):
|
| 84 |
logger.info("snapshot %s/%s not present on Hub yet (cold start)", snapshot_repo(), snapshot_filename())
|
| 85 |
return False
|
| 86 |
+
# hf_hub_download returns a path that is a symlink into the Hub blob cache with a
|
| 87 |
+
# *relative* target. ``Path.replace`` would move the symlink and break it at the
|
| 88 |
+
# new location — copy the resolved blob instead so target_path is a real file.
|
| 89 |
+
shutil.copyfile(Path(downloaded).resolve(), target_path)
|
| 90 |
return True
|
| 91 |
|
| 92 |
|
deploy/hf/seed/hf_bundled_store.db-shm
ADDED
|
Binary file (32.8 kB). View file
|
|
|
deploy/hf/seed/hf_bundled_store.db-wal
ADDED
|
File without changes
|
tests/test_user_snapshot.py
CHANGED
|
@@ -125,3 +125,34 @@ def test_restore_is_upsert_not_destructive(store_path: Path, tmp_path: Path) ->
|
|
| 125 |
assert counts.get("user", 0) >= 1
|
| 126 |
assert _table_count(store_path, "user") == 1
|
| 127 |
assert _table_count(store_path, "playpreference") == 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
assert counts.get("user", 0) >= 1
|
| 126 |
assert _table_count(store_path, "user") == 1
|
| 127 |
assert _table_count(store_path, "playpreference") == 1
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def test_pull_user_snapshot_resolves_relative_symlink(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
| 131 |
+
"""hf_hub_download returns a *relative* symlink into the blob cache. Path.replace() would
|
| 132 |
+
move the symlink, breaking it at the new location and silently failing the boot-time
|
| 133 |
+
restore. pull_user_snapshot must materialise a real file via the resolved blob.
|
| 134 |
+
"""
|
| 135 |
+
from unittest.mock import patch
|
| 136 |
+
from backend import user_snapshot_hub
|
| 137 |
+
|
| 138 |
+
# Build a fake Hub cache layout with a relative symlink, mirroring hf_hub_download's output.
|
| 139 |
+
cache_root = tmp_path / "hub_cache"
|
| 140 |
+
blob = cache_root / "blobs" / "abc123"
|
| 141 |
+
blob.parent.mkdir(parents=True)
|
| 142 |
+
blob.write_bytes(b"snapshot-bytes")
|
| 143 |
+
snap_dir = cache_root / "snapshots" / "rev"
|
| 144 |
+
snap_dir.mkdir(parents=True)
|
| 145 |
+
relative_symlink = snap_dir / "user_state.db"
|
| 146 |
+
relative_symlink.symlink_to(Path("../../blobs/abc123"))
|
| 147 |
+
assert relative_symlink.is_file(), "symlink resolves at original location"
|
| 148 |
+
|
| 149 |
+
target = tmp_path / "out" / "snap.db"
|
| 150 |
+
|
| 151 |
+
monkeypatch.setenv("HF_TOKEN", "tok")
|
| 152 |
+
monkeypatch.setenv("KINK_USER_SNAPSHOT_REPO", "owner/repo")
|
| 153 |
+
with patch("huggingface_hub.hf_hub_download", return_value=str(relative_symlink)):
|
| 154 |
+
ok = user_snapshot_hub.pull_user_snapshot(target)
|
| 155 |
+
|
| 156 |
+
assert ok is True
|
| 157 |
+
assert target.is_file(), "target must exist as a real file (not a broken symlink)"
|
| 158 |
+
assert target.read_bytes() == b"snapshot-bytes"
|