| 173 | |
| 174 | |
| 175 | def move_cache(old_cache_dir: Optional[str] = None, new_cache_dir: Optional[str] = None) -> None: |
| 176 | if new_cache_dir is None: |
| 177 | new_cache_dir = DIFFUSERS_CACHE |
| 178 | if old_cache_dir is None: |
| 179 | old_cache_dir = old_diffusers_cache |
| 180 | |
| 181 | old_cache_dir = Path(old_cache_dir).expanduser() |
| 182 | new_cache_dir = Path(new_cache_dir).expanduser() |
| 183 | for old_blob_path in old_cache_dir.glob("**/blobs/*"): |
| 184 | if old_blob_path.is_file() and not old_blob_path.is_symlink(): |
| 185 | new_blob_path = new_cache_dir / old_blob_path.relative_to(old_cache_dir) |
| 186 | new_blob_path.parent.mkdir(parents=True, exist_ok=True) |
| 187 | os.replace(old_blob_path, new_blob_path) |
| 188 | try: |
| 189 | os.symlink(new_blob_path, old_blob_path) |
| 190 | except OSError: |
| 191 | logger.warning( |
| 192 | "Could not create symlink between old cache and new cache. If you use an older version of diffusers again, files will be re-downloaded." |
| 193 | ) |
| 194 | # At this point, old_cache_dir contains symlinks to the new cache (it can still be used). |
| 195 | |
| 196 | |
| 197 | cache_version_file = os.path.join(DIFFUSERS_CACHE, "version_diffusers_cache.txt") |