continuo-python-runtime 0.14.0__py3-none-any.whl → 0.16.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.
- continuo_python_runtime/cli.py +23 -23
- continuo_python_runtime/contract/loader.py +11 -11
- continuo_python_runtime/contract/ontology_check.py +14 -14
- continuo_python_runtime/contract/pack.py +9 -9
- continuo_python_runtime/packages.py +1 -1
- continuo_python_runtime/validation/dependents_check.py +256 -0
- continuo_python_runtime/validation/drop_dependents.py +170 -0
- continuo_python_runtime/validation/probe_views.py +99 -0
- continuo_python_runtime/validation/runner.py +31 -23
- continuo_python_runtime/validation/warehouse_read.py +111 -0
- {continuo_python_runtime-0.14.0.dist-info → continuo_python_runtime-0.16.0.dist-info}/METADATA +104 -58
- {continuo_python_runtime-0.14.0.dist-info → continuo_python_runtime-0.16.0.dist-info}/RECORD +16 -13
- continuo_python_runtime/validation/preflight.py +0 -410
- {continuo_python_runtime-0.14.0.dist-info → continuo_python_runtime-0.16.0.dist-info}/WHEEL +0 -0
- {continuo_python_runtime-0.14.0.dist-info → continuo_python_runtime-0.16.0.dist-info}/entry_points.txt +0 -0
- {continuo_python_runtime-0.14.0.dist-info → continuo_python_runtime-0.16.0.dist-info}/licenses/LICENSE +0 -0
- {continuo_python_runtime-0.14.0.dist-info → continuo_python_runtime-0.16.0.dist-info}/licenses/NOTICE +0 -0
continuo_python_runtime/cli.py
CHANGED
|
@@ -9,7 +9,7 @@ from importlib import metadata
|
|
|
9
9
|
from pathlib import Path
|
|
10
10
|
|
|
11
11
|
from continuo_python_runtime.contract.loader import load_contract_dir, load_contract_docs
|
|
12
|
-
from continuo_python_runtime.contract.ontology_check import
|
|
12
|
+
from continuo_python_runtime.contract.ontology_check import check_project
|
|
13
13
|
from continuo_python_runtime.contract.pack import (
|
|
14
14
|
PACKAGE_SOURCE_USAGE,
|
|
15
15
|
build_bundle,
|
|
@@ -59,15 +59,15 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
59
59
|
validate_parser.add_argument("contract_dir", help="Path to contract directory")
|
|
60
60
|
validate_parser.add_argument("--dialect", default=None, help=dialect_help)
|
|
61
61
|
validate_parser.add_argument(
|
|
62
|
-
"--
|
|
62
|
+
"--project",
|
|
63
63
|
default=None,
|
|
64
|
-
help="The
|
|
65
|
-
"names another
|
|
64
|
+
help="The project the contracts belong to: a contract whose `project` property "
|
|
65
|
+
"names another project is refused",
|
|
66
66
|
)
|
|
67
67
|
validate_parser.add_argument(
|
|
68
68
|
"--ontology",
|
|
69
69
|
default=None,
|
|
70
|
-
help="The company's ontology file (needs --
|
|
70
|
+
help="The company's ontology file (needs --project): check every contract "
|
|
71
71
|
"against it, as a release does",
|
|
72
72
|
)
|
|
73
73
|
|
|
@@ -81,12 +81,12 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
81
81
|
),
|
|
82
82
|
)
|
|
83
83
|
pack_parser.add_argument("contract_dir", help="Path to contract directory")
|
|
84
|
-
pack_parser.add_argument("--
|
|
84
|
+
pack_parser.add_argument("--project", required=True, help="Project name")
|
|
85
85
|
pack_parser.add_argument("--repo-root", required=True, help="Repository root path")
|
|
86
86
|
pack_parser.add_argument(
|
|
87
87
|
"--image",
|
|
88
88
|
default=None,
|
|
89
|
-
help="Image the
|
|
89
|
+
help="Image the project runs; its package list is read with "
|
|
90
90
|
"`docker run --entrypoint continuo-runtime <image> packages`",
|
|
91
91
|
)
|
|
92
92
|
pack_parser.add_argument(
|
|
@@ -133,8 +133,8 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
133
133
|
)
|
|
134
134
|
|
|
135
135
|
args = parser.parse_args(argv)
|
|
136
|
-
if args.command == "validate" and args.ontology is not None and args.
|
|
137
|
-
validate_parser.error("--ontology needs --
|
|
136
|
+
if args.command == "validate" and args.ontology is not None and args.project is None:
|
|
137
|
+
validate_parser.error("--ontology needs --project")
|
|
138
138
|
if args.command == "pack" and (args.image is None) == (args.image_packages is None):
|
|
139
139
|
pack_parser.error(PACKAGE_SOURCE_USAGE)
|
|
140
140
|
|
|
@@ -143,13 +143,13 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
143
143
|
return cmd_validate(
|
|
144
144
|
args.contract_dir,
|
|
145
145
|
args.dialect,
|
|
146
|
-
|
|
146
|
+
project=args.project,
|
|
147
147
|
ontology=args.ontology,
|
|
148
148
|
)
|
|
149
149
|
elif args.command == "pack":
|
|
150
150
|
return cmd_pack(
|
|
151
151
|
args.contract_dir,
|
|
152
|
-
args.
|
|
152
|
+
args.project,
|
|
153
153
|
args.repo_root,
|
|
154
154
|
args.out,
|
|
155
155
|
image=args.image,
|
|
@@ -185,17 +185,17 @@ def cmd_validate(
|
|
|
185
185
|
contract_dir: str,
|
|
186
186
|
dialect: str | None = None,
|
|
187
187
|
*,
|
|
188
|
-
|
|
188
|
+
project: str | None = None,
|
|
189
189
|
ontology: str | None = None,
|
|
190
190
|
) -> int:
|
|
191
191
|
"""Validate contracts in a directory.
|
|
192
192
|
|
|
193
|
-
With no ``
|
|
194
|
-
and nothing is logged. With a ``
|
|
193
|
+
With no ``project``, every contract is checked as a contract of any project
|
|
194
|
+
and nothing is logged. With a ``project``, a contract whose ``project``
|
|
195
195
|
property names another one is refused too, as ``pack`` refuses it.
|
|
196
196
|
|
|
197
|
-
With an ``ontology`` file (which needs the ``
|
|
198
|
-
then checked against it by the per-
|
|
197
|
+
With an ``ontology`` file (which needs the ``project``), each contract is
|
|
198
|
+
then checked against it by the per-project rules of continuo-contracts.
|
|
199
199
|
Each finding is logged at its level, ERROR for an error, WARNING for a
|
|
200
200
|
warning and INFO for information, as ``<severity> [<rule>] <subject>:
|
|
201
201
|
<message>``, in the order the library returns them, followed by one INFO
|
|
@@ -204,15 +204,15 @@ def cmd_validate(
|
|
|
204
204
|
``ontology`` rule is one ERROR line and exit 1, before any finding. Nothing
|
|
205
205
|
goes to stdout.
|
|
206
206
|
"""
|
|
207
|
-
if
|
|
207
|
+
if project is None:
|
|
208
208
|
load_contract_dir(Path(contract_dir), dialect=dialect)
|
|
209
209
|
return 0
|
|
210
|
-
loaded = load_contract_docs(Path(contract_dir), dialect=dialect,
|
|
210
|
+
loaded = load_contract_docs(Path(contract_dir), dialect=dialect, project=project)
|
|
211
211
|
if ontology is None:
|
|
212
212
|
return 0
|
|
213
213
|
|
|
214
|
-
findings =
|
|
215
|
-
[doc for _, doc in loaded],
|
|
214
|
+
findings = check_project(
|
|
215
|
+
[doc for _, doc in loaded], project=project, ontology_path=Path(ontology)
|
|
216
216
|
)
|
|
217
217
|
for finding in findings:
|
|
218
218
|
logger.log(
|
|
@@ -231,14 +231,14 @@ def cmd_validate(
|
|
|
231
231
|
counts["error"],
|
|
232
232
|
counts["warning"],
|
|
233
233
|
counts["info"],
|
|
234
|
-
|
|
234
|
+
project,
|
|
235
235
|
)
|
|
236
236
|
return 1 if counts["error"] else 0
|
|
237
237
|
|
|
238
238
|
|
|
239
239
|
def cmd_pack(
|
|
240
240
|
contract_dir: str,
|
|
241
|
-
|
|
241
|
+
project: str,
|
|
242
242
|
repo_root: str,
|
|
243
243
|
out: str,
|
|
244
244
|
*,
|
|
@@ -255,7 +255,7 @@ def cmd_pack(
|
|
|
255
255
|
packages = image_packages_from(
|
|
256
256
|
image=image, image_packages=None if image_packages is None else Path(image_packages)
|
|
257
257
|
)
|
|
258
|
-
doc = build_bundle(Path(contract_dir), Path(repo_root),
|
|
258
|
+
doc = build_bundle(Path(contract_dir), Path(repo_root), project, packages, dialect=dialect)
|
|
259
259
|
write_bundle(doc, Path(out))
|
|
260
260
|
return 0
|
|
261
261
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
A contracts folder holds one ODCS v3.2.0 contract per node, in a file named
|
|
4
4
|
``<table>.odcs.yaml``, or ``<schema>.<table>.odcs.yaml`` when two nodes of one
|
|
5
|
-
|
|
5
|
+
project share a table name. continuo-contracts reads each file strictly, checks
|
|
6
6
|
it and maps it to its ``PythonNode``; the runtime then applies the rules it
|
|
7
7
|
keeps for itself and builds its :class:`~continuo_python_runtime.contract.model
|
|
8
8
|
.Node`. Every refusal is a :class:`ContractError` that names the file.
|
|
@@ -134,14 +134,14 @@ def node_from_contract(
|
|
|
134
134
|
*,
|
|
135
135
|
dialect: str | None,
|
|
136
136
|
check_reads: bool,
|
|
137
|
-
|
|
137
|
+
project: str | None = None,
|
|
138
138
|
) -> Node:
|
|
139
139
|
"""Check the ODCS contract ``doc``, read from ``file``, and build its :class:`Node`.
|
|
140
140
|
|
|
141
141
|
continuo-contracts checks the contract first
|
|
142
142
|
(``validate_python_contract``, of which only the base name of ``file``
|
|
143
|
-
counts for the file-name rule). With a ``
|
|
144
|
-
contract whose authored ``
|
|
143
|
+
counts for the file-name rule). With a ``project`` it also refuses a
|
|
144
|
+
contract whose authored ``project`` property names another project; with
|
|
145
145
|
none, that property is not compared. When it finds issues they become one
|
|
146
146
|
:class:`ContractError` with one :func:`issue_lines` line per issue, and
|
|
147
147
|
nothing else runs.
|
|
@@ -162,7 +162,7 @@ def node_from_contract(
|
|
|
162
162
|
``required``), ``extra_columns``, ``config``, ``secret_ref``,
|
|
163
163
|
``mode_type``, the key when the node upserts and the assets.
|
|
164
164
|
"""
|
|
165
|
-
issues = validate_python_contract(doc, path=str(file),
|
|
165
|
+
issues = validate_python_contract(doc, path=str(file), project=project)
|
|
166
166
|
if issues:
|
|
167
167
|
raise ContractError("\n".join(issue_lines(file, issues)))
|
|
168
168
|
python = node_from_odcs(doc)
|
|
@@ -232,7 +232,7 @@ def load_contract_dir(
|
|
|
232
232
|
) -> list[Node]:
|
|
233
233
|
"""Load and check every ``*.odcs.yaml`` contract in ``path``, in file-name order.
|
|
234
234
|
|
|
235
|
-
The nodes, as :func:`load_contract_docs` loads them with no
|
|
235
|
+
The nodes, as :func:`load_contract_docs` loads them with no project. Every
|
|
236
236
|
refusal and every rule are described there.
|
|
237
237
|
"""
|
|
238
238
|
return [node for node, _ in load_contract_docs(path, dialect=dialect, check_reads=check_reads)]
|
|
@@ -243,14 +243,14 @@ def load_contract_docs(
|
|
|
243
243
|
*,
|
|
244
244
|
dialect: str | None = None,
|
|
245
245
|
check_reads: bool = True,
|
|
246
|
-
|
|
246
|
+
project: str | None = None,
|
|
247
247
|
) -> list[tuple[Node, Mapping[str, Any]]]:
|
|
248
248
|
"""Load and check every ``*.odcs.yaml`` contract in ``path``, in file-name order.
|
|
249
249
|
|
|
250
250
|
Each node comes with the document it was built from, the one read from its
|
|
251
251
|
file and checked, so a caller that needs the documents does not read the
|
|
252
|
-
files again. ``
|
|
253
|
-
to: continuo-contracts then refuses a contract whose authored ``
|
|
252
|
+
files again. ``project``, when given, is the project the contracts belong
|
|
253
|
+
to: continuo-contracts then refuses a contract whose authored ``project``
|
|
254
254
|
property names another one, as :func:`node_from_contract` says.
|
|
255
255
|
|
|
256
256
|
Any other file whose name ends in ``.yml`` or ``.yaml``, in any case
|
|
@@ -285,7 +285,7 @@ def load_contract_docs(
|
|
|
285
285
|
|
|
286
286
|
Raises ``ContractError`` for an unknown ``dialect``, a file in another
|
|
287
287
|
format, a folder with no contract, a contract that is refused (one that
|
|
288
|
-
names another
|
|
288
|
+
names another project than ``project``, when given, included), and two
|
|
289
289
|
files that declare the same ``<schema>.<table>``, compared in lower case.
|
|
290
290
|
"""
|
|
291
291
|
if dialect is not None:
|
|
@@ -316,7 +316,7 @@ def load_contract_docs(
|
|
|
316
316
|
for file in files:
|
|
317
317
|
doc = parse_contract(read_contract_text(file), file)
|
|
318
318
|
node = node_from_contract(
|
|
319
|
-
doc, file, dialect=dialect, check_reads=check_reads,
|
|
319
|
+
doc, file, dialect=dialect, check_reads=check_reads, project=project
|
|
320
320
|
)
|
|
321
321
|
# Relations are compared in lower case, as continuo compares them: a
|
|
322
322
|
# warehouse folds the case of an unquoted name, so two contracts whose
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
"""Check a Python
|
|
1
|
+
"""Check a Python project's contracts against the company's ontology file.
|
|
2
2
|
|
|
3
|
-
``continuo-runtime validate --
|
|
4
|
-
per-
|
|
3
|
+
``continuo-runtime validate --project <name> --ontology <file>`` runs the
|
|
4
|
+
per-project rules of continuo-contracts over the contracts of one project:
|
|
5
5
|
each contract is stored as a release stores it, filled with what the
|
|
6
6
|
ontology knows and checked, so a team sees in its own CI what a release says
|
|
7
7
|
about its bindings. The rules read contracts against an ontology file that the
|
|
@@ -20,7 +20,7 @@ from continuo_contracts import ontology
|
|
|
20
20
|
from continuo_contracts.contract import normalise_python
|
|
21
21
|
from continuo_contracts.errors import ContractsError, OntologyError
|
|
22
22
|
from continuo_contracts.ontology_check import (
|
|
23
|
-
|
|
23
|
+
PER_PROJECT_RULES,
|
|
24
24
|
Finding,
|
|
25
25
|
check,
|
|
26
26
|
ontology_findings,
|
|
@@ -36,7 +36,7 @@ def _read_ontology(ontology_path: Path) -> ontology.Ontology:
|
|
|
36
36
|
that cannot be read (missing, a folder, no permission), one that is not
|
|
37
37
|
UTF-8 text and one the library does not accept as an ontology, whose
|
|
38
38
|
reason is the library's own message. The ``ontology`` rule, which can
|
|
39
|
-
refuse a file that loads, is run by :func:`
|
|
39
|
+
refuse a file that loads, is run by :func:`check_project`.
|
|
40
40
|
"""
|
|
41
41
|
try:
|
|
42
42
|
text = ontology_path.read_bytes().decode("utf-8")
|
|
@@ -50,27 +50,27 @@ def _read_ontology(ontology_path: Path) -> ontology.Ontology:
|
|
|
50
50
|
raise ContractError(f"{ontology_path}: {exc}") from None
|
|
51
51
|
|
|
52
52
|
|
|
53
|
-
def
|
|
54
|
-
docs: Sequence[Mapping[str, Any]], *,
|
|
53
|
+
def check_project(
|
|
54
|
+
docs: Sequence[Mapping[str, Any]], *, project: str, ontology_path: Path
|
|
55
55
|
) -> list[Finding]:
|
|
56
|
-
"""Return the findings of the per-
|
|
56
|
+
"""Return the findings of the per-project rules over ``docs``, the contracts of ``project``.
|
|
57
57
|
|
|
58
58
|
``docs`` are contract documents that continuo-contracts already accepted as
|
|
59
59
|
a Python contract; none is changed. The ontology file is read first, and
|
|
60
60
|
the library's ``ontology`` rule runs over it alone. Each document then goes
|
|
61
|
-
through ``normalise_python`` as the contract of ``
|
|
61
|
+
through ``normalise_python`` as the contract of ``project`` that reads
|
|
62
62
|
nothing the ontology tracks, then through ``ontology.resolve``, and the
|
|
63
63
|
rules ``contract``, ``coverage``, ``quality`` and ``privacy`` run over all
|
|
64
64
|
of them. The findings come as the library returns them, sorted. A
|
|
65
|
-
``
|
|
65
|
+
``project`` the ontology does not declare gets the library's one
|
|
66
66
|
information finding and one warning for each contract that writes a
|
|
67
67
|
binding.
|
|
68
68
|
|
|
69
69
|
Raises ``ContractError`` when ``ontology_path`` cannot be read or is not an
|
|
70
70
|
ontology (see :func:`_read_ontology`), when the ``ontology`` rule finds an
|
|
71
|
-
error in the file, whatever ``
|
|
71
|
+
error in the file, whatever ``project`` is (the message is ``<file>: the
|
|
72
72
|
ontology file has <n> error(s): run continuo-contracts ontology check
|
|
73
|
-
<file>``, and no contract is read against the file), and when ``
|
|
73
|
+
<file>``, and no contract is read against the file), and when ``project``
|
|
74
74
|
is blank. Findings of the ``ontology`` rule that are not errors do not stop
|
|
75
75
|
the check.
|
|
76
76
|
"""
|
|
@@ -82,8 +82,8 @@ def check_service(
|
|
|
82
82
|
f"run continuo-contracts ontology check {ontology_path}"
|
|
83
83
|
)
|
|
84
84
|
try:
|
|
85
|
-
stored = [normalise_python(doc,
|
|
85
|
+
stored = [normalise_python(doc, project=project, reads=[]) for doc in docs]
|
|
86
86
|
except ContractsError as exc:
|
|
87
87
|
raise ContractError(str(exc)) from None
|
|
88
88
|
resolved = [ontology.resolve(contract, loaded) for contract in stored]
|
|
89
|
-
return check(loaded, resolved, rules=
|
|
89
|
+
return check(loaded, resolved, rules=PER_PROJECT_RULES)
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
"""``continuo-runtime pack``: the bundle of a
|
|
1
|
+
"""``continuo-runtime pack``: the bundle of a project's ODCS contracts.
|
|
2
2
|
|
|
3
|
-
A
|
|
3
|
+
A project's CI packs the contracts of its ``contracts/`` folder into one
|
|
4
4
|
``odcs-bundle.json`` and uploads it with the release. The bundle is UTF-8 JSON,
|
|
5
5
|
written by :func:`write_bundle` as ``json.dumps(doc, sort_keys=True, indent=2,
|
|
6
6
|
ensure_ascii=False)`` and a newline, and holds::
|
|
7
7
|
|
|
8
8
|
{"bundle_version": 1,
|
|
9
|
-
"
|
|
9
|
+
"project": <project>,
|
|
10
10
|
"producer": {"continuo_python_runtime": <version>, "continuo_contracts": <version>},
|
|
11
11
|
"image_packages": {<name>: <version>},
|
|
12
12
|
"nodes": [{"path": <repo path of the contract file>,
|
|
@@ -18,7 +18,7 @@ ensure_ascii=False)`` and a newline, and holds::
|
|
|
18
18
|
are the two parts :func:`~continuo_python_runtime.hashing.hash_parts` computes from
|
|
19
19
|
the files of the repository; the third part, the config hash, is computed from the
|
|
20
20
|
contract text by whoever reads the bundle. ``image_packages`` is the package list of
|
|
21
|
-
the image the
|
|
21
|
+
the image the project runs, read from that image, never from the environment ``pack``
|
|
22
22
|
runs in.
|
|
23
23
|
"""
|
|
24
24
|
|
|
@@ -51,7 +51,7 @@ from continuo_python_runtime.lint import lint_source
|
|
|
51
51
|
BUNDLE_VERSION = 1
|
|
52
52
|
|
|
53
53
|
PACKAGE_SOURCE_USAGE = (
|
|
54
|
-
"pack records the package list of the image the
|
|
54
|
+
"pack records the package list of the image the project runs: give exactly one of "
|
|
55
55
|
"--image <ref> (read from the image) or --image-packages <file> "
|
|
56
56
|
"(a JSON object of package name to version)"
|
|
57
57
|
)
|
|
@@ -155,7 +155,7 @@ def _repo_path(file: Path, repo_root: Path) -> str:
|
|
|
155
155
|
def build_bundle(
|
|
156
156
|
contract_dir: Path,
|
|
157
157
|
repo_root: Path,
|
|
158
|
-
|
|
158
|
+
project: str,
|
|
159
159
|
image_packages: Mapping[str, str],
|
|
160
160
|
*,
|
|
161
161
|
dialect: str | None = None,
|
|
@@ -165,7 +165,7 @@ def build_bundle(
|
|
|
165
165
|
The contracts are loaded as :func:`~continuo_python_runtime.contract.loader
|
|
166
166
|
.load_contract_dir` loads them, every read checked under ``dialect`` (``None``
|
|
167
167
|
is sqlglot's dialect-neutral parser). Each contract is then checked once more
|
|
168
|
-
against ``
|
|
168
|
+
against ``project`` by continuo-contracts, as the release does: one
|
|
169
169
|
``ContractError`` lists every issue of every file, one line each, naming the
|
|
170
170
|
file by its path below ``repo_root``.
|
|
171
171
|
|
|
@@ -183,7 +183,7 @@ def build_bundle(
|
|
|
183
183
|
files = [(node, _repo_path(node.path, repo_root), read_contract_text(node.path)) for node in nodes]
|
|
184
184
|
issues: list[str] = []
|
|
185
185
|
for _, path, text in files:
|
|
186
|
-
found = validate_python_contract(parse_contract(text, path), path=path,
|
|
186
|
+
found = validate_python_contract(parse_contract(text, path), path=path, project=project)
|
|
187
187
|
issues.extend(issue_lines(path, found))
|
|
188
188
|
if issues:
|
|
189
189
|
raise ContractError("\n".join(issues))
|
|
@@ -205,7 +205,7 @@ def build_bundle(
|
|
|
205
205
|
|
|
206
206
|
return {
|
|
207
207
|
"bundle_version": BUNDLE_VERSION,
|
|
208
|
-
"
|
|
208
|
+
"project": project,
|
|
209
209
|
"producer": {
|
|
210
210
|
"continuo_python_runtime": metadata.version("continuo-python-runtime"),
|
|
211
211
|
"continuo_contracts": metadata.version("continuo-contracts"),
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
"""The ``dependents_check`` validation op.
|
|
2
|
+
|
|
3
|
+
``dependents_check`` asks the warehouse which objects a reload of a seed's
|
|
4
|
+
production table would destroy or leave reading a missing table, and compares
|
|
5
|
+
them with the objects the operator has allowed or acknowledged. ``check`` stops
|
|
6
|
+
a reload that would hit an object outside that set; ``report`` prints the same
|
|
7
|
+
conclusion and never stops (exit 0). The block and the exit codes follow
|
|
8
|
+
:mod:`~continuo_python_runtime.validation.warehouse_read`.
|
|
9
|
+
|
|
10
|
+
The decision is the pure function :func:`decide`; the rest of the module reads
|
|
11
|
+
the inputs, drives the adapter and prints.
|
|
12
|
+
"""
|
|
13
|
+
import logging
|
|
14
|
+
from collections.abc import Mapping
|
|
15
|
+
from dataclasses import dataclass
|
|
16
|
+
from enum import Enum
|
|
17
|
+
from typing import Any, cast
|
|
18
|
+
|
|
19
|
+
from continuo_engine_contract.port import ( # type: ignore[import-untyped]
|
|
20
|
+
DependentsIntrospection,
|
|
21
|
+
discover_adapter,
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
from continuo_python_runtime.validation.warehouse_read import (
|
|
25
|
+
Discover,
|
|
26
|
+
InputError,
|
|
27
|
+
close_quietly,
|
|
28
|
+
declared_drop_semantics,
|
|
29
|
+
discover_and_check_env,
|
|
30
|
+
emit,
|
|
31
|
+
first_line,
|
|
32
|
+
json_list,
|
|
33
|
+
seed_node_id,
|
|
34
|
+
seed_relation,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
logger = logging.getLogger("validation_dependents_check")
|
|
38
|
+
|
|
39
|
+
# The adapter's ``drop_semantics`` mapped to the engine family the release plan declares.
|
|
40
|
+
_FAMILY_OF_DROP_SEMANTICS = {"cascade": "schema_bound", "late_binding": "late_binding"}
|
|
41
|
+
_MODES = ("check", "report")
|
|
42
|
+
_FAMILIES = ("schema_bound", "late_binding")
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class Situation(Enum):
|
|
46
|
+
"""How far ``dependents_check`` got, which is what decides its verdict."""
|
|
47
|
+
|
|
48
|
+
BAD_INPUT = "bad_input"
|
|
49
|
+
ENGINE_MISMATCH = "engine_mismatch"
|
|
50
|
+
NO_CAPABILITY = "no_capability"
|
|
51
|
+
UNREACHABLE = "unreachable"
|
|
52
|
+
LISTING_FAILED = "listing_failed"
|
|
53
|
+
LISTED = "listed"
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass(frozen=True)
|
|
57
|
+
class Verdict:
|
|
58
|
+
"""What ``dependents_check`` concludes: the block's status, reason, message and the exit code."""
|
|
59
|
+
|
|
60
|
+
status: str
|
|
61
|
+
reason: str
|
|
62
|
+
exit_code: int
|
|
63
|
+
checked: bool
|
|
64
|
+
completeness: str
|
|
65
|
+
message: str
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def decide(
|
|
69
|
+
mode: str,
|
|
70
|
+
situation: Situation,
|
|
71
|
+
*,
|
|
72
|
+
detail: str = "",
|
|
73
|
+
completeness: str = "none",
|
|
74
|
+
has_unowned: bool = False,
|
|
75
|
+
) -> Verdict:
|
|
76
|
+
"""The decision table of ``dependents_check``.
|
|
77
|
+
|
|
78
|
+
*detail* is the text that explains the situation, *completeness* the
|
|
79
|
+
adapter's own and *has_unowned* whether a listed object is outside the
|
|
80
|
+
allowed set. The table is the same on every engine family: a reload stops
|
|
81
|
+
on an object continuo does not own whether the engine's drop destroys it
|
|
82
|
+
(``schema_bound``) or leaves it reading a re-created table
|
|
83
|
+
(``late_binding``). A situation that a ``check`` stops on gives ``error``
|
|
84
|
+
and exit 1; ``report`` mode gives the same ``reason`` on a ``success`` block
|
|
85
|
+
and exit 0. A bad input is exit 2 in both modes. A ``reason`` prefixes the
|
|
86
|
+
message.
|
|
87
|
+
"""
|
|
88
|
+
if situation is Situation.BAD_INPUT:
|
|
89
|
+
return Verdict("error", "", 2, False, "none", detail)
|
|
90
|
+
reason, stops = _conclusion(situation, has_unowned)
|
|
91
|
+
failed = stops and mode == "check"
|
|
92
|
+
return Verdict(
|
|
93
|
+
status="error" if failed else "success",
|
|
94
|
+
reason=reason,
|
|
95
|
+
exit_code=1 if failed else 0,
|
|
96
|
+
checked=situation is Situation.LISTED,
|
|
97
|
+
completeness=completeness if situation is Situation.LISTED else "none",
|
|
98
|
+
message=f"{reason}: {detail}" if reason else detail,
|
|
99
|
+
)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def _conclusion(situation: Situation, has_unowned: bool) -> tuple[str, bool]:
|
|
103
|
+
"""The ``reason`` a situation carries and whether a ``check`` stops on it."""
|
|
104
|
+
if situation is Situation.ENGINE_MISMATCH:
|
|
105
|
+
return "engine_mismatch", True
|
|
106
|
+
if situation is Situation.UNREACHABLE:
|
|
107
|
+
return "warehouse_unreachable", True
|
|
108
|
+
if situation is Situation.NO_CAPABILITY:
|
|
109
|
+
return "unowned_dependents", True
|
|
110
|
+
if situation is Situation.LISTING_FAILED:
|
|
111
|
+
return "", True
|
|
112
|
+
if has_unowned:
|
|
113
|
+
return "unowned_dependents", True
|
|
114
|
+
return "", False
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
@dataclass(frozen=True)
|
|
120
|
+
class _SeedInputs:
|
|
121
|
+
mode: str
|
|
122
|
+
family: str
|
|
123
|
+
schema: str
|
|
124
|
+
table: str
|
|
125
|
+
allowed: frozenset[str]
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _seed_inputs(env: Mapping[str, str]) -> _SeedInputs:
|
|
129
|
+
mode = env.get("DEPENDENTS_CHECK_MODE", "")
|
|
130
|
+
if mode not in _MODES:
|
|
131
|
+
raise InputError(f"DEPENDENTS_CHECK_MODE must be one of {', '.join(_MODES)}, got {mode!r}")
|
|
132
|
+
family = env.get("DEPENDENTS_CHECK_ENGINE_FAMILY", "")
|
|
133
|
+
if family not in _FAMILIES:
|
|
134
|
+
raise InputError(
|
|
135
|
+
f"DEPENDENTS_CHECK_ENGINE_FAMILY must be one of {', '.join(_FAMILIES)}, got {family!r}"
|
|
136
|
+
)
|
|
137
|
+
schema, table = seed_relation(env)
|
|
138
|
+
allowed = json_list(env, "DEPENDENTS_CHECK_ALLOWED", required=False)
|
|
139
|
+
acknowledged = json_list(env, "DEPENDENTS_CHECK_ACKNOWLEDGED", required=False)
|
|
140
|
+
return _SeedInputs(
|
|
141
|
+
mode=mode,
|
|
142
|
+
family=family,
|
|
143
|
+
schema=schema,
|
|
144
|
+
table=table,
|
|
145
|
+
allowed=frozenset(entry.strip().lower() for entry in [*allowed, *acknowledged]),
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def _seed_fields(
|
|
150
|
+
env: Mapping[str, str], drop_semantics: str, verdict: Verdict, dependents: list[dict[str, Any]]
|
|
151
|
+
) -> dict[str, Any]:
|
|
152
|
+
return {
|
|
153
|
+
"op": "dependents_check",
|
|
154
|
+
"mode": env.get("DEPENDENTS_CHECK_MODE", ""),
|
|
155
|
+
"checked": verdict.checked,
|
|
156
|
+
"engine_family": env.get("DEPENDENTS_CHECK_ENGINE_FAMILY", ""),
|
|
157
|
+
"drop_semantics": drop_semantics,
|
|
158
|
+
"completeness": verdict.completeness,
|
|
159
|
+
"dependents": dependents,
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _finish_seed(
|
|
164
|
+
env: Mapping[str, str], drop_semantics: str, verdict: Verdict,
|
|
165
|
+
dependents: list[dict[str, Any]] | None = None,
|
|
166
|
+
) -> int:
|
|
167
|
+
node_id = seed_node_id(env)
|
|
168
|
+
if verdict.exit_code == 2:
|
|
169
|
+
logger.error("dependents_check %s: %s", node_id, verdict.message)
|
|
170
|
+
elif verdict.reason or verdict.exit_code:
|
|
171
|
+
logger.warning("dependents_check %s: %s", node_id, verdict.message)
|
|
172
|
+
emit(
|
|
173
|
+
verdict.status, verdict.message, node_id, verdict.reason,
|
|
174
|
+
_seed_fields(env, drop_semantics, verdict, dependents or []),
|
|
175
|
+
)
|
|
176
|
+
return verdict.exit_code
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def run_dependents_check(
|
|
180
|
+
env: Mapping[str, str], discover: Discover = discover_adapter
|
|
181
|
+
) -> int:
|
|
182
|
+
"""Run ``dependents_check`` against *env*; print its one block and return the exit code.
|
|
183
|
+
|
|
184
|
+
Reads the op's inputs and the adapter's ``required_env()`` from *env* (adapters
|
|
185
|
+
connect from ``os.environ``, so the runner passes that). An unexpected error
|
|
186
|
+
still prints one ``error`` block and returns 2.
|
|
187
|
+
"""
|
|
188
|
+
try:
|
|
189
|
+
return _dependents_check(env, discover)
|
|
190
|
+
except Exception as exc:
|
|
191
|
+
logger.exception("dependents_check %s failed unexpectedly", seed_node_id(env))
|
|
192
|
+
verdict = Verdict("error", "", 2, False, "none", f"unexpected error: {first_line(exc)}")
|
|
193
|
+
return _finish_seed(env, "", verdict)
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def _dependents_check(env: Mapping[str, str], discover: Discover) -> int:
|
|
197
|
+
try:
|
|
198
|
+
inputs = _seed_inputs(env)
|
|
199
|
+
engine, adapter_cls = discover_and_check_env(env, discover)
|
|
200
|
+
except InputError as exc:
|
|
201
|
+
return _finish_seed(env, "", decide("", Situation.BAD_INPUT, detail=str(exc)))
|
|
202
|
+
|
|
203
|
+
drop_semantics = declared_drop_semantics(adapter_cls)
|
|
204
|
+
adapter_family = _FAMILY_OF_DROP_SEMANTICS.get(drop_semantics)
|
|
205
|
+
|
|
206
|
+
def finish(
|
|
207
|
+
situation: Situation, detail: str = "", *, completeness: str = "none",
|
|
208
|
+
has_unowned: bool = False, dependents: list[dict[str, Any]] | None = None,
|
|
209
|
+
) -> int:
|
|
210
|
+
verdict = decide(
|
|
211
|
+
inputs.mode, situation,
|
|
212
|
+
detail=detail, completeness=completeness, has_unowned=has_unowned,
|
|
213
|
+
)
|
|
214
|
+
return _finish_seed(env, drop_semantics, verdict, dependents)
|
|
215
|
+
|
|
216
|
+
if adapter_family != inputs.family:
|
|
217
|
+
return finish(
|
|
218
|
+
Situation.ENGINE_MISMATCH,
|
|
219
|
+
f"engine {engine!r} is {adapter_family or 'of no known family'}, "
|
|
220
|
+
f"but the release expects {inputs.family}",
|
|
221
|
+
)
|
|
222
|
+
if not issubclass(adapter_cls, DependentsIntrospection):
|
|
223
|
+
return finish(Situation.NO_CAPABILITY, f"engine {engine!r} cannot list the objects that read a table")
|
|
224
|
+
|
|
225
|
+
try:
|
|
226
|
+
adapter = adapter_cls.from_env()
|
|
227
|
+
except Exception as exc:
|
|
228
|
+
return finish(Situation.UNREACHABLE, first_line(exc))
|
|
229
|
+
try:
|
|
230
|
+
try:
|
|
231
|
+
listing = cast(DependentsIntrospection, adapter).dependents(inputs.schema, inputs.table)
|
|
232
|
+
except Exception as exc:
|
|
233
|
+
return finish(
|
|
234
|
+
Situation.LISTING_FAILED,
|
|
235
|
+
f"listing the objects that read {inputs.schema}.{inputs.table} failed: {first_line(exc)}",
|
|
236
|
+
)
|
|
237
|
+
dependents = [
|
|
238
|
+
{
|
|
239
|
+
"schema": found.schema, "name": found.name, "kind": found.kind, "owner": found.owner,
|
|
240
|
+
"depth": found.depth,
|
|
241
|
+
"allowed": f"{found.schema}.{found.name}".lower() in inputs.allowed,
|
|
242
|
+
}
|
|
243
|
+
for found in sorted(listing.objects, key=lambda d: (d.depth, d.schema, d.name))
|
|
244
|
+
]
|
|
245
|
+
finally:
|
|
246
|
+
close_quietly(adapter)
|
|
247
|
+
outside = [d for d in dependents if not d["allowed"]]
|
|
248
|
+
effect = "would drop" if inputs.family == "schema_bound" else "may break"
|
|
249
|
+
detail = f"the reload {effect} objects continuo does not own: " + ", ".join(
|
|
250
|
+
f"{d['schema']}.{d['name']} ({d['kind']}" + (f", owner {d['owner']})" if d["owner"] else ")")
|
|
251
|
+
for d in outside
|
|
252
|
+
)
|
|
253
|
+
return finish(
|
|
254
|
+
Situation.LISTED, detail if outside else "",
|
|
255
|
+
completeness=listing.completeness, has_unowned=bool(outside), dependents=dependents,
|
|
256
|
+
)
|