views-datafactory 1.9.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- datafactory_adapters/ARCHITECTURE.md +54 -0
- datafactory_adapters/__init__.py +38 -0
- datafactory_adapters/_conservation.py +95 -0
- datafactory_adapters/_reconciliation.py +92 -0
- datafactory_adapters/_validation.py +39 -0
- datafactory_adapters/feature_frame.py +25 -0
- datafactory_adapters/grid_from_feature_frame.py +89 -0
- datafactory_adapters/grid_to_country_month.py +169 -0
- datafactory_adapters/grid_to_dataframe.py +176 -0
- datafactory_compilation/ARCHITECTURE.md +63 -0
- datafactory_compilation/__init__.py +39 -0
- datafactory_compilation/aggregation.py +64 -0
- datafactory_compilation/compilation_config.py +97 -0
- datafactory_compilation/conservation.py +36 -0
- datafactory_compilation/grid_compilation.py +352 -0
- datafactory_compilation/output.py +85 -0
- datafactory_compilation/preflight.py +61 -0
- datafactory_compilation/pregridded_compilation.py +291 -0
- datafactory_consolidation/ARCHITECTURE.md +58 -0
- datafactory_consolidation/__init__.py +28 -0
- datafactory_consolidation/consolidation_result.py +27 -0
- datafactory_consolidation/consolidators/__init__.py +32 -0
- datafactory_consolidation/consolidators/acled.py +462 -0
- datafactory_consolidation/consolidators/ucdp.py +524 -0
- datafactory_consolidation/event_store.py +67 -0
- datafactory_consolidation/tagging.py +46 -0
- datafactory_harvester/ARCHITECTURE.md +103 -0
- datafactory_harvester/__init__.py +37 -0
- datafactory_harvester/event_validation.py +372 -0
- datafactory_harvester/harvest_runner.py +72 -0
- datafactory_harvester/pipeline_runner.py +125 -0
- datafactory_harvester/snapshot_storage.py +117 -0
- datafactory_harvester/sources/__init__.py +36 -0
- datafactory_harvester/sources/_ucdp_common.py +34 -0
- datafactory_harvester/sources/acled.py +743 -0
- datafactory_harvester/sources/gaul_admin.py +567 -0
- datafactory_harvester/sources/ghsbuilts.py +271 -0
- datafactory_harvester/sources/ghspop.py +267 -0
- datafactory_harvester/sources/priogrid_static.py +359 -0
- datafactory_harvester/sources/shdi.py +625 -0
- datafactory_harvester/sources/ucdp_annual.py +616 -0
- datafactory_harvester/sources/ucdp_candidate.py +419 -0
- datafactory_harvester/sources/ucdp_dot9.py +407 -0
- datafactory_harvester/sources/vdem.py +350 -0
- datafactory_harvester/validation.py +74 -0
- datafactory_http/__init__.py +10 -0
- datafactory_http/retry.py +102 -0
- datafactory_priogrid/ARCHITECTURE.md +62 -0
- datafactory_priogrid/__init__.py +51 -0
- datafactory_priogrid/cell_generator.py +137 -0
- datafactory_priogrid/grid_config.py +107 -0
- datafactory_priogrid/land_mask.py +62 -0
- datafactory_priogrid/parity_validation.py +176 -0
- datafactory_priogrid/shapefile_harvester.py +174 -0
- datafactory_priogrid/shapefile_reader.py +125 -0
- datafactory_priogrid/spatiotemporal.py +66 -0
- datafactory_priogrid/temporal_config.py +78 -0
- datafactory_priogrid/temporal_generator.py +65 -0
- datafactory_provenance/ARCHITECTURE.md +61 -0
- datafactory_provenance/__init__.py +81 -0
- datafactory_provenance/constants.py +3 -0
- datafactory_provenance/digests_and_ledgers.py +370 -0
- datafactory_provenance/health.py +310 -0
- datafactory_provenance/pipeline_lock.py +200 -0
- datafactory_provenance/registry.py +92 -0
- datafactory_provenance/skip.py +212 -0
- datafactory_provenance/source_registry.py +466 -0
- datafactory_query/ARCHITECTURE.md +60 -0
- datafactory_query/__init__.py +36 -0
- datafactory_query/africa_me_gaul_pgids.json +1 -0
- datafactory_query/africa_me_legacy_pgids.json +1 -0
- datafactory_query/backends_npy.py +90 -0
- datafactory_query/backends_zarr.py +259 -0
- datafactory_query/coverage.py +64 -0
- datafactory_query/dataset.py +263 -0
- datafactory_query/defaults.py +101 -0
- datafactory_query/land_gaul_pgids.json +1 -0
- datafactory_query/land_pgids.json +1 -0
- datafactory_query/output_format.py +52 -0
- datafactory_query/regions.py +293 -0
- datafactory_query/temporal.py +115 -0
- datafactory_viewpoint/ARCHITECTURE.md +68 -0
- datafactory_viewpoint/__init__.py +33 -0
- datafactory_viewpoint/builders/__init__.py +32 -0
- datafactory_viewpoint/builders/acled_v1.py +213 -0
- datafactory_viewpoint/builders/ghsbuilts_v1.py +351 -0
- datafactory_viewpoint/builders/ghspop_v1.py +471 -0
- datafactory_viewpoint/builders/shdi_v1.py +389 -0
- datafactory_viewpoint/builders/ucdp_v1.py +437 -0
- datafactory_viewpoint/builders/vdem_v1.py +419 -0
- datafactory_viewpoint/profiles.py +169 -0
- datafactory_viewpoint/raster_io.py +49 -0
- datafactory_viewpoint/spatial_distribution.py +203 -0
- datafactory_viewpoint/spatial_weights.py +207 -0
- datafactory_viewpoint/survivorship.py +108 -0
- datafactory_viewpoint/temporal.py +111 -0
- datafactory_viewpoint/temporal_distribution.py +295 -0
- datafactory_viewpoint/viewpoint_config.py +124 -0
- datafactory_viewpoint/viewpoint_result.py +33 -0
- views_datafactory-1.9.0.dist-info/METADATA +528 -0
- views_datafactory-1.9.0.dist-info/RECORD +103 -0
- views_datafactory-1.9.0.dist-info/WHEEL +4 -0
- views_datafactory-1.9.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"""Pipeline-level writer lock — shared with refresh_pipeline.sh (C-316).
|
|
2
|
+
|
|
3
|
+
refresh_pipeline.sh holds an exclusive flock on
|
|
4
|
+
``/var/lock/views-pipeline.lock`` for the duration of a run
|
|
5
|
+
(C-285/C-147). Standalone writer scripts (assemble, export,
|
|
6
|
+
run_*_pipeline, consumer bridge) must honor the same lock so a
|
|
7
|
+
manual invocation cannot collide with the monthly cron — the
|
|
8
|
+
2026-07-21 incident: a manual export read a half-rewritten
|
|
9
|
+
grid.npy mid-cron-assembly and survived only because the digest
|
|
10
|
+
gate ABORTed (C-316).
|
|
11
|
+
|
|
12
|
+
Semantics are refuse-fast, not queue: a writer script started
|
|
13
|
+
while the pipeline runs should tell the operator to wait, not
|
|
14
|
+
silently block for hours. Crash safety is kernel-level (flock
|
|
15
|
+
dies with its holder — see file_lock's rationale); there is no
|
|
16
|
+
staleness heuristic to misfire.
|
|
17
|
+
|
|
18
|
+
Usage in a writer script::
|
|
19
|
+
|
|
20
|
+
from datafactory_provenance import pipeline_lock
|
|
21
|
+
|
|
22
|
+
with pipeline_lock(force=args.force_no_lock):
|
|
23
|
+
... # write shared data directories
|
|
24
|
+
|
|
25
|
+
``force=True`` skips acquisition entirely — a deliberate operator
|
|
26
|
+
escape hatch (logged loudly) for recovery scenarios.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
from __future__ import annotations
|
|
30
|
+
|
|
31
|
+
import fcntl
|
|
32
|
+
import logging
|
|
33
|
+
import os
|
|
34
|
+
from collections.abc import Iterator
|
|
35
|
+
from contextlib import contextmanager
|
|
36
|
+
from pathlib import Path
|
|
37
|
+
|
|
38
|
+
__all__ = [
|
|
39
|
+
"PIPELINE_LOCK_PATH",
|
|
40
|
+
"PipelineLockHeldError",
|
|
41
|
+
"hold_pipeline_lock",
|
|
42
|
+
"pipeline_lock",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
logger = logging.getLogger(__name__)
|
|
46
|
+
|
|
47
|
+
# Must match LOCK_FILE in scripts/refresh_pipeline.sh — the whole
|
|
48
|
+
# point is that bash and Python contend on the SAME file.
|
|
49
|
+
PIPELINE_LOCK_PATH = Path("/var/lock/views-pipeline.lock")
|
|
50
|
+
|
|
51
|
+
# Set by refresh_pipeline.sh after IT acquires the flock: child
|
|
52
|
+
# scripts it invokes are cooperating parts of the same run and
|
|
53
|
+
# must not contend with their own parent.
|
|
54
|
+
_LOCK_HELD_ENV = "VIEWS_PIPELINE_LOCK_HELD"
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def _parent_holds_lock() -> bool:
|
|
58
|
+
return os.environ.get(_LOCK_HELD_ENV) == "1"
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class PipelineLockHeldError(RuntimeError):
|
|
62
|
+
"""Raised when another pipeline process holds the writer lock."""
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
@contextmanager
|
|
66
|
+
def pipeline_lock(
|
|
67
|
+
lock_path: Path = PIPELINE_LOCK_PATH,
|
|
68
|
+
*,
|
|
69
|
+
force: bool = False,
|
|
70
|
+
) -> Iterator[None]:
|
|
71
|
+
"""Hold the pipeline writer lock, or refuse immediately.
|
|
72
|
+
|
|
73
|
+
Args:
|
|
74
|
+
lock_path: Lock file shared with refresh_pipeline.sh.
|
|
75
|
+
force: Skip locking entirely (operator escape hatch;
|
|
76
|
+
logged as a warning).
|
|
77
|
+
|
|
78
|
+
Raises:
|
|
79
|
+
PipelineLockHeldError: If the lock is held by another
|
|
80
|
+
process (typically the monthly cron pipeline).
|
|
81
|
+
"""
|
|
82
|
+
if force:
|
|
83
|
+
logger.warning(
|
|
84
|
+
"Pipeline lock BYPASSED (--force-no-lock) — caller "
|
|
85
|
+
"accepts collision risk with a running pipeline",
|
|
86
|
+
)
|
|
87
|
+
yield
|
|
88
|
+
return
|
|
89
|
+
|
|
90
|
+
if _parent_holds_lock():
|
|
91
|
+
logger.debug(
|
|
92
|
+
"Pipeline lock already held by parent run "
|
|
93
|
+
"(%s=1) — proceeding as part of the run",
|
|
94
|
+
_LOCK_HELD_ENV,
|
|
95
|
+
)
|
|
96
|
+
yield
|
|
97
|
+
return
|
|
98
|
+
|
|
99
|
+
lock_path.parent.mkdir(parents=True, exist_ok=True)
|
|
100
|
+
with open(lock_path, "a+") as lock_file:
|
|
101
|
+
try:
|
|
102
|
+
fcntl.flock(lock_file, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
103
|
+
except BlockingIOError:
|
|
104
|
+
holder = ""
|
|
105
|
+
try:
|
|
106
|
+
lock_file.seek(0)
|
|
107
|
+
holder = lock_file.read(32).strip()
|
|
108
|
+
except OSError:
|
|
109
|
+
pass
|
|
110
|
+
msg = (
|
|
111
|
+
f"Pipeline lock {lock_path} is held"
|
|
112
|
+
f"{f' (holder pid {holder})' if holder else ''} — "
|
|
113
|
+
f"a pipeline run is in progress. Wait for it to "
|
|
114
|
+
f"finish (watch the status page), or re-run with "
|
|
115
|
+
f"--force-no-lock if you accept the collision "
|
|
116
|
+
f"risk (see C-316)."
|
|
117
|
+
)
|
|
118
|
+
raise PipelineLockHeldError(msg) from None
|
|
119
|
+
try:
|
|
120
|
+
lock_file.seek(0)
|
|
121
|
+
lock_file.truncate()
|
|
122
|
+
lock_file.write(str(os.getpid()))
|
|
123
|
+
lock_file.flush()
|
|
124
|
+
yield
|
|
125
|
+
finally:
|
|
126
|
+
fcntl.flock(lock_file, fcntl.LOCK_UN)
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
# Held-for-process-lifetime locks, keyed by resolved path (keeps
|
|
130
|
+
# fds referenced AND makes hold_pipeline_lock idempotent within a
|
|
131
|
+
# process — re-acquiring a lock you already hold is a no-op, e.g.
|
|
132
|
+
# a script's main() invoked twice in one interpreter).
|
|
133
|
+
_held_locks: dict[str, object] = {}
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def hold_pipeline_lock(
|
|
137
|
+
lock_path: Path = PIPELINE_LOCK_PATH,
|
|
138
|
+
*,
|
|
139
|
+
force: bool = False,
|
|
140
|
+
) -> None:
|
|
141
|
+
"""Acquire the pipeline writer lock for the process lifetime.
|
|
142
|
+
|
|
143
|
+
The one-liner for writer scripts' main(): acquire-or-refuse,
|
|
144
|
+
then hold until the process exits (the kernel releases the
|
|
145
|
+
flock when the fd closes at exit — no cleanup needed, and a
|
|
146
|
+
crash releases it just the same).
|
|
147
|
+
|
|
148
|
+
Raises:
|
|
149
|
+
PipelineLockHeldError: If another process holds the lock.
|
|
150
|
+
"""
|
|
151
|
+
if force:
|
|
152
|
+
logger.warning(
|
|
153
|
+
"Pipeline lock BYPASSED (--force-no-lock) — caller "
|
|
154
|
+
"accepts collision risk with a running pipeline",
|
|
155
|
+
)
|
|
156
|
+
return
|
|
157
|
+
|
|
158
|
+
if _parent_holds_lock():
|
|
159
|
+
logger.debug(
|
|
160
|
+
"Pipeline lock already held by parent run "
|
|
161
|
+
"(%s=1) — proceeding as part of the run",
|
|
162
|
+
_LOCK_HELD_ENV,
|
|
163
|
+
)
|
|
164
|
+
return
|
|
165
|
+
|
|
166
|
+
key = str(lock_path.resolve())
|
|
167
|
+
if key in _held_locks:
|
|
168
|
+
logger.debug(
|
|
169
|
+
"Pipeline lock %s already held by this process — "
|
|
170
|
+
"no-op",
|
|
171
|
+
lock_path,
|
|
172
|
+
)
|
|
173
|
+
return
|
|
174
|
+
|
|
175
|
+
lock_path.parent.mkdir(parents=True, exist_ok=True)
|
|
176
|
+
lock_file = open(lock_path, "a+") # noqa: SIM115 — held for process lifetime
|
|
177
|
+
try:
|
|
178
|
+
fcntl.flock(lock_file, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
179
|
+
except BlockingIOError:
|
|
180
|
+
holder = ""
|
|
181
|
+
try:
|
|
182
|
+
lock_file.seek(0)
|
|
183
|
+
holder = lock_file.read(32).strip()
|
|
184
|
+
except OSError:
|
|
185
|
+
pass
|
|
186
|
+
lock_file.close()
|
|
187
|
+
msg = (
|
|
188
|
+
f"Pipeline lock {lock_path} is held"
|
|
189
|
+
f"{f' (holder pid {holder})' if holder else ''} — "
|
|
190
|
+
f"a pipeline run is in progress. Wait for it to "
|
|
191
|
+
f"finish (watch the status page), or re-run with "
|
|
192
|
+
f"--force-no-lock if you accept the collision risk "
|
|
193
|
+
f"(see C-316)."
|
|
194
|
+
)
|
|
195
|
+
raise PipelineLockHeldError(msg) from None
|
|
196
|
+
lock_file.seek(0)
|
|
197
|
+
lock_file.truncate()
|
|
198
|
+
lock_file.write(str(os.getpid()))
|
|
199
|
+
lock_file.flush()
|
|
200
|
+
_held_locks[key] = lock_file
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"""Generic function registry for pluggable components.
|
|
2
|
+
|
|
3
|
+
Replaces 6 identical dict-based registries (C-80) with a single
|
|
4
|
+
reusable class. Supports both explicit registration and decorator
|
|
5
|
+
registration. See ADR-020 for the concern that motivated this.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import logging
|
|
11
|
+
from collections.abc import Callable
|
|
12
|
+
from typing import Generic, TypeVar
|
|
13
|
+
|
|
14
|
+
T = TypeVar("T", bound=Callable[..., object])
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Registry(Generic[T]):
|
|
18
|
+
"""Named registry of callable objects.
|
|
19
|
+
|
|
20
|
+
Provides registration (explicit or decorator), lookup, listing,
|
|
21
|
+
and test-isolation clearing. Used by harvester sources,
|
|
22
|
+
consolidators, viewpoint builders, and strategy registries.
|
|
23
|
+
|
|
24
|
+
Example (explicit registration)::
|
|
25
|
+
|
|
26
|
+
_registry = Registry[Callable[..., Any]]("data source")
|
|
27
|
+
_registry.register("ucdp_annual", fetch_ucdp_annual)
|
|
28
|
+
fn = _registry.get("ucdp_annual")
|
|
29
|
+
|
|
30
|
+
Example (decorator registration)::
|
|
31
|
+
|
|
32
|
+
_registry = Registry[Callable[[list[dict]], float]]("strategy")
|
|
33
|
+
|
|
34
|
+
@_registry.decorator("count")
|
|
35
|
+
def count(events: list[dict]) -> float:
|
|
36
|
+
return float(len(events))
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
def __init__(self, kind: str) -> None:
|
|
40
|
+
self._kind = kind
|
|
41
|
+
self._entries: dict[str, T] = {}
|
|
42
|
+
self._logger = logging.getLogger(
|
|
43
|
+
f"{__name__}.{kind.replace(' ', '_')}"
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
def register(self, name: str, fn: T) -> None:
|
|
47
|
+
"""Register a callable by name."""
|
|
48
|
+
self._entries[name] = fn
|
|
49
|
+
self._logger.info(
|
|
50
|
+
"Registered %s: %s", self._kind, name
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
def decorator(self, name: str) -> Callable[[T], T]:
|
|
54
|
+
"""Decorator factory for registration."""
|
|
55
|
+
def wrapper(fn: T) -> T:
|
|
56
|
+
self.register(name, fn)
|
|
57
|
+
return fn
|
|
58
|
+
return wrapper
|
|
59
|
+
|
|
60
|
+
def get(self, name: str) -> T:
|
|
61
|
+
"""Look up a registered callable by name.
|
|
62
|
+
|
|
63
|
+
Raises:
|
|
64
|
+
KeyError: If the name is not registered. Message
|
|
65
|
+
lists available entries.
|
|
66
|
+
"""
|
|
67
|
+
if name not in self._entries:
|
|
68
|
+
available = sorted(self._entries.keys())
|
|
69
|
+
err_msg = (
|
|
70
|
+
f"Unknown {self._kind} '{name}'. "
|
|
71
|
+
f"Available: {available or 'none registered'}"
|
|
72
|
+
)
|
|
73
|
+
self._logger.error(err_msg)
|
|
74
|
+
raise KeyError(err_msg)
|
|
75
|
+
return self._entries[name]
|
|
76
|
+
|
|
77
|
+
def list_entries(self) -> list[str]:
|
|
78
|
+
"""Return sorted list of registered names."""
|
|
79
|
+
return sorted(self._entries.keys())
|
|
80
|
+
|
|
81
|
+
def clear(self) -> None:
|
|
82
|
+
"""Remove all entries. For test isolation only."""
|
|
83
|
+
self._entries.clear()
|
|
84
|
+
|
|
85
|
+
@property
|
|
86
|
+
def entries(self) -> dict[str, T]:
|
|
87
|
+
"""Direct access to the underlying dict.
|
|
88
|
+
|
|
89
|
+
Exposed for backward-compatible test fixtures that
|
|
90
|
+
manipulate the dict directly.
|
|
91
|
+
"""
|
|
92
|
+
return self._entries
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"""Content-addressed skip decisions for assembly and export.
|
|
2
|
+
|
|
3
|
+
Encapsulates the logic for deciding whether a pipeline step can be
|
|
4
|
+
skipped because its inputs haven't changed since the last successful
|
|
5
|
+
run. Each function reads the previous provenance record, compares
|
|
6
|
+
digests, and returns a SkipVerdict that callers use for control flow.
|
|
7
|
+
|
|
8
|
+
No outbound imports to other datafactory_* packages.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import json
|
|
14
|
+
from dataclasses import dataclass
|
|
15
|
+
from pathlib import Path
|
|
16
|
+
|
|
17
|
+
from datafactory_provenance.digests_and_ledgers import compute_file_digest
|
|
18
|
+
|
|
19
|
+
__all__ = [
|
|
20
|
+
"SkipVerdict",
|
|
21
|
+
"check_assembly_skip",
|
|
22
|
+
"check_export_skip",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass(frozen=True)
|
|
27
|
+
class SkipVerdict:
|
|
28
|
+
"""Result of a content-addressed skip check."""
|
|
29
|
+
|
|
30
|
+
should_skip: bool
|
|
31
|
+
reason: str
|
|
32
|
+
changed_keys: tuple[str, ...]
|
|
33
|
+
missing_keys: tuple[str, ...]
|
|
34
|
+
output_valid: bool
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def check_assembly_skip(
|
|
38
|
+
current_digests: dict[str, str],
|
|
39
|
+
provenance_path: Path,
|
|
40
|
+
output_path: Path,
|
|
41
|
+
*,
|
|
42
|
+
sources_key: str = "sources",
|
|
43
|
+
output_digest_key: str = "output_digest",
|
|
44
|
+
) -> SkipVerdict:
|
|
45
|
+
"""Decide whether assembly can be skipped.
|
|
46
|
+
|
|
47
|
+
Compares *current_digests* (caller-computed input digests) against
|
|
48
|
+
the previous run's provenance record and verifies that the output
|
|
49
|
+
file's content still matches the recorded output digest.
|
|
50
|
+
|
|
51
|
+
Args:
|
|
52
|
+
current_digests: Map of digest keys to hex digest strings
|
|
53
|
+
for the current inputs (e.g. ``{"ucdp_digest": "ab12..."}``).
|
|
54
|
+
provenance_path: Path to the previous run's provenance.json.
|
|
55
|
+
output_path: Path to the output file (e.g. grid.npy) whose
|
|
56
|
+
integrity is verified on the skip path.
|
|
57
|
+
sources_key: Key in provenance JSON holding the sources dict.
|
|
58
|
+
output_digest_key: Key in provenance JSON holding the output digest.
|
|
59
|
+
|
|
60
|
+
Returns:
|
|
61
|
+
A SkipVerdict describing whether to skip and why.
|
|
62
|
+
"""
|
|
63
|
+
if not provenance_path.exists() or not output_path.exists():
|
|
64
|
+
return SkipVerdict(
|
|
65
|
+
should_skip=False,
|
|
66
|
+
reason="no previous run (provenance or output missing)",
|
|
67
|
+
changed_keys=(),
|
|
68
|
+
missing_keys=(),
|
|
69
|
+
output_valid=False,
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
prov = json.loads(provenance_path.read_text())
|
|
73
|
+
prev_sources = prov.get(sources_key, {})
|
|
74
|
+
|
|
75
|
+
prev_digest_keys = {
|
|
76
|
+
k for k in prev_sources
|
|
77
|
+
if k.endswith("_digest") and prev_sources[k] is not None
|
|
78
|
+
}
|
|
79
|
+
current_keys = set(current_digests.keys())
|
|
80
|
+
|
|
81
|
+
missing = tuple(sorted(prev_digest_keys - current_keys))
|
|
82
|
+
added = tuple(sorted(current_keys - prev_digest_keys))
|
|
83
|
+
|
|
84
|
+
if missing or added:
|
|
85
|
+
return SkipVerdict(
|
|
86
|
+
should_skip=False,
|
|
87
|
+
reason=(
|
|
88
|
+
f"source set changed: "
|
|
89
|
+
f"missing={missing}, added={added}"
|
|
90
|
+
),
|
|
91
|
+
changed_keys=added,
|
|
92
|
+
missing_keys=missing,
|
|
93
|
+
output_valid=True,
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
changed = tuple(sorted(
|
|
97
|
+
k for k, v in current_digests.items()
|
|
98
|
+
if prev_sources.get(k) != v
|
|
99
|
+
))
|
|
100
|
+
|
|
101
|
+
if changed:
|
|
102
|
+
return SkipVerdict(
|
|
103
|
+
should_skip=False,
|
|
104
|
+
reason=f"input digests differ: {changed}",
|
|
105
|
+
changed_keys=changed,
|
|
106
|
+
missing_keys=(),
|
|
107
|
+
output_valid=True,
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
recorded_output = prov.get(output_digest_key)
|
|
111
|
+
actual_output = compute_file_digest(output_path)
|
|
112
|
+
output_valid = recorded_output is not None and actual_output == recorded_output
|
|
113
|
+
|
|
114
|
+
if not output_valid:
|
|
115
|
+
return SkipVerdict(
|
|
116
|
+
should_skip=False,
|
|
117
|
+
reason=(
|
|
118
|
+
f"output integrity check failed: "
|
|
119
|
+
f"recorded={recorded_output}, actual={actual_output}"
|
|
120
|
+
),
|
|
121
|
+
changed_keys=(),
|
|
122
|
+
missing_keys=(),
|
|
123
|
+
output_valid=False,
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
return SkipVerdict(
|
|
127
|
+
should_skip=True,
|
|
128
|
+
reason="all input digests match and output is intact",
|
|
129
|
+
changed_keys=(),
|
|
130
|
+
missing_keys=(),
|
|
131
|
+
output_valid=True,
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def check_export_skip(
|
|
136
|
+
provenance_path: Path,
|
|
137
|
+
export_path: Path,
|
|
138
|
+
digest_attr: str = "source_digest",
|
|
139
|
+
*,
|
|
140
|
+
output_digest_key: str = "output_digest",
|
|
141
|
+
) -> SkipVerdict:
|
|
142
|
+
"""Decide whether an export can be skipped.
|
|
143
|
+
|
|
144
|
+
Compares the assembly output digest recorded in provenance against
|
|
145
|
+
the digest stored in the export artifact's metadata.
|
|
146
|
+
|
|
147
|
+
Args:
|
|
148
|
+
provenance_path: Path to provenance.json from assembly.
|
|
149
|
+
export_path: Path to the export artifact (e.g. zarr store
|
|
150
|
+
directory). Must contain a ``.zattrs`` file with the
|
|
151
|
+
stored digest.
|
|
152
|
+
digest_attr: Key in ``.zattrs`` holding the stored digest.
|
|
153
|
+
output_digest_key: Key in provenance JSON holding the
|
|
154
|
+
assembly output digest.
|
|
155
|
+
|
|
156
|
+
Returns:
|
|
157
|
+
A SkipVerdict describing whether to skip and why.
|
|
158
|
+
"""
|
|
159
|
+
zattrs_path = export_path / ".zattrs"
|
|
160
|
+
|
|
161
|
+
if not provenance_path.exists() or not export_path.exists():
|
|
162
|
+
return SkipVerdict(
|
|
163
|
+
should_skip=False,
|
|
164
|
+
reason="no previous run (provenance or export missing)",
|
|
165
|
+
changed_keys=(),
|
|
166
|
+
missing_keys=(),
|
|
167
|
+
output_valid=False,
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
if not zattrs_path.exists():
|
|
171
|
+
return SkipVerdict(
|
|
172
|
+
should_skip=False,
|
|
173
|
+
reason="export has no .zattrs metadata",
|
|
174
|
+
changed_keys=(),
|
|
175
|
+
missing_keys=(),
|
|
176
|
+
output_valid=False,
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
prov = json.loads(provenance_path.read_text())
|
|
180
|
+
assembly_digest = prov.get(output_digest_key)
|
|
181
|
+
|
|
182
|
+
attrs = json.loads(zattrs_path.read_text())
|
|
183
|
+
export_digest = attrs.get(digest_attr)
|
|
184
|
+
|
|
185
|
+
if not assembly_digest or not export_digest:
|
|
186
|
+
return SkipVerdict(
|
|
187
|
+
should_skip=False,
|
|
188
|
+
reason="digest not recorded in provenance or export",
|
|
189
|
+
changed_keys=(),
|
|
190
|
+
missing_keys=(),
|
|
191
|
+
output_valid=False,
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
if assembly_digest != export_digest:
|
|
195
|
+
return SkipVerdict(
|
|
196
|
+
should_skip=False,
|
|
197
|
+
reason=(
|
|
198
|
+
f"digest mismatch: "
|
|
199
|
+
f"assembly={assembly_digest}, export={export_digest}"
|
|
200
|
+
),
|
|
201
|
+
changed_keys=(),
|
|
202
|
+
missing_keys=(),
|
|
203
|
+
output_valid=False,
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
return SkipVerdict(
|
|
207
|
+
should_skip=True,
|
|
208
|
+
reason="export digest matches assembly output",
|
|
209
|
+
changed_keys=(),
|
|
210
|
+
missing_keys=(),
|
|
211
|
+
output_valid=True,
|
|
212
|
+
)
|