| 203 | |
| 204 | |
| 205 | def move_cache(old_cache_dir: Optional[str] = None, new_cache_dir: Optional[str] = None) -> None: |
| 206 | if new_cache_dir is None: |
| 207 | new_cache_dir = HF_HUB_CACHE |
| 208 | if old_cache_dir is None: |
| 209 | old_cache_dir = old_diffusers_cache |
| 210 | |
| 211 | old_cache_dir = Path(old_cache_dir).expanduser() |
| 212 | new_cache_dir = Path(new_cache_dir).expanduser() |
| 213 | for old_blob_path in old_cache_dir.glob("**/blobs/*"): |
| 214 | if old_blob_path.is_file() and not old_blob_path.is_symlink(): |
| 215 | new_blob_path = new_cache_dir / old_blob_path.relative_to(old_cache_dir) |
| 216 | new_blob_path.parent.mkdir(parents=True, exist_ok=True) |
| 217 | os.replace(old_blob_path, new_blob_path) |
| 218 | try: |
| 219 | os.symlink(new_blob_path, old_blob_path) |
| 220 | except OSError: |
| 221 | logger.warning( |
| 222 | "Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded." |
| 223 | ) |
| 224 | # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). |
| 225 | |
| 226 | |
| 227 | cache_version_file = os.path.join(HF_HUB_CACHE, "version_diffusers_cache.txt") |