|
2 | 2 | # Permissions are hereby granted under the terms of the MIT License: |
3 | 3 | # https://opensource.org/licenses/MIT. |
4 | 4 |
|
5 | | -from typing import Iterable, Any |
6 | 5 | import collections.abc |
| 6 | +from typing import Iterable, Any |
7 | 7 |
|
8 | 8 | import numpy as np |
9 | 9 | import xarray as xr |
10 | 10 | import zarr.attrs |
11 | 11 | import zarr.convenience |
12 | 12 |
|
13 | 13 | from .config import ConfigLike |
14 | | -from .config import exclude_from_config |
15 | | -from .config import normalize_config |
16 | | -from .config import validate_config |
17 | 14 | from .config import eval_dyn_config_attrs |
| 15 | +from .config import exclude_from_config |
18 | 16 | from .config import get_dyn_config_attrs_env |
19 | 17 | from .config import has_dyn_config_attrs |
| 18 | +from .config import normalize_config |
| 19 | +from .config import validate_config |
20 | 20 | from .context import Context |
21 | | -from .fsutil.transaction import Transaction |
| 21 | +from .fsutil import FileObj |
22 | 22 | from .fsutil.transaction import RollbackCallback |
23 | | -from .log import logger |
| 23 | +from .fsutil.transaction import Transaction |
24 | 24 | from .log import configure_logging |
| 25 | +from .log import logger |
25 | 26 | from .profiler import Profiler |
26 | 27 | from .rollbackstore import RollbackStore |
27 | 28 | from .slice import SliceObj |
@@ -53,6 +54,12 @@ def __init__(self, config: ConfigLike = None, **kwargs): |
53 | 54 | configure_logging(config.get("logging")) |
54 | 55 | self._profiler = Profiler(config.get("profiling")) |
55 | 56 | self._config = config |
| 57 | + if config.get("force_new"): |
| 58 | + logger.warning( |
| 59 | + f"Setting 'force_new' is enabled. This will " |
| 60 | + f" permanently delete existing targets (no rollback)." |
| 61 | + ) |
| 62 | + delete_target_permanently(config) |
56 | 63 |
|
57 | 64 | def process_slices(self, slices: Iterable[SliceObj]): |
58 | 65 | """Process the given `slices`. |
@@ -119,6 +126,26 @@ def process_slice(self, slice_obj: SliceObj, slice_index: int = 0): |
119 | 126 | update_target_from_slice(ctx, slice_dataset, rollback_callback) |
120 | 127 |
|
121 | 128 |
|
| 129 | +def delete_target_permanently(config: dict[str, Any]): |
| 130 | + # TODO: I'm not happy with the config being a dict here, because it |
| 131 | + # implies and hence duplicates definition of default values. |
| 132 | + # Make Processor constructor turn config dict into config object, |
| 133 | + # Pass config object to Context and publish via ctx.config property. |
| 134 | + dry_run = config.get("dry_run", False) |
| 135 | + target_uri = config.get("target_dir") |
| 136 | + target_storage_options = config.get("target_storage_options") |
| 137 | + target_dir = FileObj(target_uri, storage_options=target_storage_options) |
| 138 | + if target_dir.exists(): |
| 139 | + logger.warning(f"Permanently deleting {target_dir}") |
| 140 | + if not dry_run: |
| 141 | + target_dir.delete(recursive=True) |
| 142 | + target_lock = Transaction.get_lock_file(target_dir) |
| 143 | + if target_lock.exists(): |
| 144 | + logger.warning(f"Permanently deleting {target_lock}") |
| 145 | + if not dry_run: |
| 146 | + target_lock.delete() |
| 147 | + |
| 148 | + |
122 | 149 | def create_target_from_slice( |
123 | 150 | ctx: Context, slice_ds: xr.Dataset, rollback_cb: RollbackCallback |
124 | 151 | ): |
|
0 commit comments