|
1 | 1 | import os |
2 | 2 | import pathlib |
3 | 3 | from copy import deepcopy |
| 4 | +from tempfile import TemporaryDirectory |
4 | 5 | from typing import Dict, Optional, Sequence, Union |
5 | 6 | from zipfile import ZIP_DEFLATED, ZipFile |
6 | 7 |
|
|
10 | 11 | from bioimageio.core.resource_io.nodes import ResourceDescription |
11 | 12 | from bioimageio.spec import load_raw_resource_description |
12 | 13 | from bioimageio.spec.shared import raw_nodes |
13 | | -from bioimageio.spec.shared.common import BIOIMAGEIO_CACHE_PATH, get_class_name_from_type |
| 14 | +from bioimageio.spec.shared.common import ( |
| 15 | + BIOIMAGEIO_CACHE_PATH, |
| 16 | + BIOIMAGEIO_USE_CACHE, |
| 17 | + get_class_name_from_type, |
| 18 | + no_cache_tmp_list, |
| 19 | +) |
14 | 20 | from bioimageio.spec.shared.raw_nodes import ResourceDescription as RawResourceDescription |
15 | 21 | from . import nodes |
16 | 22 | from .utils import resolve_raw_resource_description, resolve_source |
@@ -134,21 +140,26 @@ def _get_package_base_name(raw_rd: RawResourceDescription, weights_priority_orde |
134 | 140 |
|
135 | 141 |
|
136 | 142 | def _get_tmp_package_path(raw_rd: RawResourceDescription, weights_priority_order: Optional[Sequence[str]]): |
137 | | - package_file_name = _get_package_base_name(raw_rd, weights_priority_order) |
138 | | - |
139 | | - cache_folder = BIOIMAGEIO_CACHE_PATH / "packages" |
140 | | - cache_folder.mkdir(exist_ok=True, parents=True) |
141 | | - package_path = (cache_folder / package_file_name).with_suffix(".zip") |
142 | | - max_cached_packages_with_same_name = 100 |
143 | | - for p in range(max_cached_packages_with_same_name): |
144 | | - if package_path.exists(): |
145 | | - package_path = (cache_folder / f"{package_file_name}p{p}").with_suffix(".zip") |
| 143 | + if BIOIMAGEIO_USE_CACHE: |
| 144 | + package_file_name = _get_package_base_name(raw_rd, weights_priority_order) |
| 145 | + cache_folder = BIOIMAGEIO_CACHE_PATH / "packages" |
| 146 | + cache_folder.mkdir(exist_ok=True, parents=True) |
| 147 | + |
| 148 | + package_path = (cache_folder / package_file_name).with_suffix(".zip") |
| 149 | + max_cached_packages_with_same_name = 100 |
| 150 | + for p in range(max_cached_packages_with_same_name): |
| 151 | + if package_path.exists(): |
| 152 | + package_path = (cache_folder / f"{package_file_name}p{p}").with_suffix(".zip") |
| 153 | + else: |
| 154 | + break |
146 | 155 | else: |
147 | | - break |
| 156 | + raise FileExistsError( |
| 157 | + f"Already caching {max_cached_packages_with_same_name} versions of {cache_folder / package_file_name}!" |
| 158 | + ) |
148 | 159 | else: |
149 | | - raise FileExistsError( |
150 | | - f"Already caching {max_cached_packages_with_same_name} versions of {cache_folder / package_file_name}!" |
151 | | - ) |
| 160 | + tmp_dir = TemporaryDirectory() |
| 161 | + no_cache_tmp_list.append(tmp_dir) |
| 162 | + package_path = pathlib.Path(tmp_dir.name) / "file" |
152 | 163 |
|
153 | 164 | return package_path |
154 | 165 |
|
|
0 commit comments