continuo-python-runtime 0.12.0__py3-none-any.whl → 0.14.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 +180 -43
- continuo_python_runtime/content_check.py +4 -4
- continuo_python_runtime/contract/kinds.py +37 -29
- continuo_python_runtime/contract/loader.py +238 -439
- continuo_python_runtime/contract/model.py +12 -6
- continuo_python_runtime/contract/ontology_check.py +89 -0
- continuo_python_runtime/contract/pack.py +299 -0
- continuo_python_runtime/contract/paths.py +1 -1
- continuo_python_runtime/contract/read_tables.py +85 -0
- continuo_python_runtime/harness.py +14 -5
- continuo_python_runtime/hashing.py +23 -40
- continuo_python_runtime/packages.py +56 -0
- {continuo_python_runtime-0.12.0.dist-info → continuo_python_runtime-0.14.0.dist-info}/METADATA +274 -119
- {continuo_python_runtime-0.12.0.dist-info → continuo_python_runtime-0.14.0.dist-info}/RECORD +18 -16
- continuo_python_runtime/contract/merge.py +0 -178
- continuo_python_runtime/contract/secret_ref.py +0 -31
- {continuo_python_runtime-0.12.0.dist-info → continuo_python_runtime-0.14.0.dist-info}/WHEEL +0 -0
- {continuo_python_runtime-0.12.0.dist-info → continuo_python_runtime-0.14.0.dist-info}/entry_points.txt +0 -0
- {continuo_python_runtime-0.12.0.dist-info → continuo_python_runtime-0.14.0.dist-info}/licenses/LICENSE +0 -0
- {continuo_python_runtime-0.12.0.dist-info → continuo_python_runtime-0.14.0.dist-info}/licenses/NOTICE +0 -0
continuo_python_runtime/cli.py
CHANGED
|
@@ -1,26 +1,37 @@
|
|
|
1
|
-
"""CLI for continuo-runtime: validate,
|
|
1
|
+
"""CLI for continuo-runtime: validate, pack, hash, packages, lint, run and validation-op."""
|
|
2
2
|
|
|
3
3
|
import argparse
|
|
4
|
+
import json
|
|
4
5
|
import logging
|
|
5
6
|
import os
|
|
6
7
|
import sys
|
|
7
8
|
from importlib import metadata
|
|
8
9
|
from pathlib import Path
|
|
9
10
|
|
|
10
|
-
from continuo_python_runtime.contract.loader import load_contract_dir
|
|
11
|
-
from continuo_python_runtime.contract.
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
from continuo_python_runtime.contract.loader import load_contract_dir, load_contract_docs
|
|
12
|
+
from continuo_python_runtime.contract.ontology_check import check_service
|
|
13
|
+
from continuo_python_runtime.contract.pack import (
|
|
14
|
+
PACKAGE_SOURCE_USAGE,
|
|
15
|
+
build_bundle,
|
|
16
|
+
image_packages_from,
|
|
17
|
+
write_bundle,
|
|
14
18
|
)
|
|
15
19
|
from continuo_python_runtime.errors import HarnessError
|
|
20
|
+
from continuo_python_runtime.hash_inputs import gather_hash_inputs
|
|
21
|
+
from continuo_python_runtime.hashing import hash_parts
|
|
16
22
|
from continuo_python_runtime.lint import lint_paths
|
|
23
|
+
from continuo_python_runtime.packages import installed_packages
|
|
17
24
|
from continuo_python_runtime import harness
|
|
18
25
|
|
|
19
26
|
logger = logging.getLogger(__name__)
|
|
20
27
|
|
|
21
28
|
|
|
22
29
|
def main(argv: list[str] | None = None) -> int:
|
|
23
|
-
"""
|
|
30
|
+
"""Run the subcommand ``argv`` names and return its exit code.
|
|
31
|
+
|
|
32
|
+
A usage error exits 2 through argparse. A :class:`HarnessError` or an
|
|
33
|
+
``OSError`` raised by a subcommand is logged and returns 1.
|
|
34
|
+
"""
|
|
24
35
|
# Configure logging once
|
|
25
36
|
logging.basicConfig(
|
|
26
37
|
stream=sys.stderr,
|
|
@@ -47,16 +58,44 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
47
58
|
)
|
|
48
59
|
validate_parser.add_argument("contract_dir", help="Path to contract directory")
|
|
49
60
|
validate_parser.add_argument("--dialect", default=None, help=dialect_help)
|
|
61
|
+
validate_parser.add_argument(
|
|
62
|
+
"--service",
|
|
63
|
+
default=None,
|
|
64
|
+
help="The service the contracts belong to: a contract whose `service` property "
|
|
65
|
+
"names another service is refused",
|
|
66
|
+
)
|
|
67
|
+
validate_parser.add_argument(
|
|
68
|
+
"--ontology",
|
|
69
|
+
default=None,
|
|
70
|
+
help="The company's ontology file (needs --service): check every contract "
|
|
71
|
+
"against it, as a release does",
|
|
72
|
+
)
|
|
50
73
|
|
|
51
|
-
#
|
|
52
|
-
|
|
53
|
-
"
|
|
74
|
+
# pack subcommand
|
|
75
|
+
pack_parser = subparsers.add_parser(
|
|
76
|
+
"pack",
|
|
77
|
+
help="Pack the contracts into the bundle (odcs-bundle.json) a release carries",
|
|
78
|
+
description=(
|
|
79
|
+
"Check every contract of the folder, lint every script it runs and write "
|
|
80
|
+
"the bundle. " + PACKAGE_SOURCE_USAGE + "."
|
|
81
|
+
),
|
|
54
82
|
)
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
83
|
+
pack_parser.add_argument("contract_dir", help="Path to contract directory")
|
|
84
|
+
pack_parser.add_argument("--service", required=True, help="Service name")
|
|
85
|
+
pack_parser.add_argument("--repo-root", required=True, help="Repository root path")
|
|
86
|
+
pack_parser.add_argument(
|
|
87
|
+
"--image",
|
|
88
|
+
default=None,
|
|
89
|
+
help="Image the service runs; its package list is read with "
|
|
90
|
+
"`docker run --entrypoint continuo-runtime <image> packages`",
|
|
91
|
+
)
|
|
92
|
+
pack_parser.add_argument(
|
|
93
|
+
"--image-packages",
|
|
94
|
+
default=None,
|
|
95
|
+
help="File holding the package list of that image, one JSON object of name to version",
|
|
96
|
+
)
|
|
97
|
+
pack_parser.add_argument("--out", required=True, help="Output file path")
|
|
98
|
+
pack_parser.add_argument("--dialect", default=None, help=dialect_help)
|
|
60
99
|
|
|
61
100
|
# hash subcommand
|
|
62
101
|
hash_parser = subparsers.add_parser(
|
|
@@ -68,6 +107,13 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
68
107
|
)
|
|
69
108
|
hash_parser.add_argument("--dialect", default=None, help=dialect_help)
|
|
70
109
|
|
|
110
|
+
# packages subcommand
|
|
111
|
+
subparsers.add_parser(
|
|
112
|
+
"packages",
|
|
113
|
+
help="Print the installed packages of this environment as one JSON object "
|
|
114
|
+
"({normalised name: version})",
|
|
115
|
+
)
|
|
116
|
+
|
|
71
117
|
# lint subcommand
|
|
72
118
|
lint_parser = subparsers.add_parser(
|
|
73
119
|
"lint", help="Lint Python scripts for forbidden imports and SQL literals"
|
|
@@ -76,7 +122,7 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
76
122
|
|
|
77
123
|
# run subcommand
|
|
78
124
|
subparsers.add_parser(
|
|
79
|
-
"run", help="
|
|
125
|
+
"run", help="Run the node the environment names (container entrypoint)"
|
|
80
126
|
)
|
|
81
127
|
|
|
82
128
|
# validation-op subcommand
|
|
@@ -87,16 +133,33 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
87
133
|
)
|
|
88
134
|
|
|
89
135
|
args = parser.parse_args(argv)
|
|
136
|
+
if args.command == "validate" and args.ontology is not None and args.service is None:
|
|
137
|
+
validate_parser.error("--ontology needs --service")
|
|
138
|
+
if args.command == "pack" and (args.image is None) == (args.image_packages is None):
|
|
139
|
+
pack_parser.error(PACKAGE_SOURCE_USAGE)
|
|
90
140
|
|
|
91
141
|
try:
|
|
92
142
|
if args.command == "validate":
|
|
93
|
-
return cmd_validate(
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
args.
|
|
143
|
+
return cmd_validate(
|
|
144
|
+
args.contract_dir,
|
|
145
|
+
args.dialect,
|
|
146
|
+
service=args.service,
|
|
147
|
+
ontology=args.ontology,
|
|
148
|
+
)
|
|
149
|
+
elif args.command == "pack":
|
|
150
|
+
return cmd_pack(
|
|
151
|
+
args.contract_dir,
|
|
152
|
+
args.service,
|
|
153
|
+
args.repo_root,
|
|
154
|
+
args.out,
|
|
155
|
+
image=args.image,
|
|
156
|
+
image_packages=args.image_packages,
|
|
157
|
+
dialect=args.dialect,
|
|
97
158
|
)
|
|
98
159
|
elif args.command == "hash":
|
|
99
160
|
return cmd_hash(args.contract_dir, args.repo_root, args.dialect)
|
|
161
|
+
elif args.command == "packages":
|
|
162
|
+
return cmd_packages()
|
|
100
163
|
elif args.command == "lint":
|
|
101
164
|
return cmd_lint(args.path)
|
|
102
165
|
elif args.command == "run":
|
|
@@ -115,42 +178,114 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
115
178
|
return 0
|
|
116
179
|
|
|
117
180
|
|
|
118
|
-
|
|
119
|
-
"""Validate contracts in a directory."""
|
|
120
|
-
load_contract_dir(Path(contract_dir), dialect=dialect)
|
|
121
|
-
return 0
|
|
181
|
+
_FINDING_LEVELS = {"error": logging.ERROR, "warning": logging.WARNING, "info": logging.INFO}
|
|
122
182
|
|
|
123
183
|
|
|
124
|
-
def
|
|
184
|
+
def cmd_validate(
|
|
185
|
+
contract_dir: str,
|
|
186
|
+
dialect: str | None = None,
|
|
187
|
+
*,
|
|
188
|
+
service: str | None = None,
|
|
189
|
+
ontology: str | None = None,
|
|
190
|
+
) -> int:
|
|
191
|
+
"""Validate contracts in a directory.
|
|
192
|
+
|
|
193
|
+
With no ``service``, every contract is checked as a contract of any service
|
|
194
|
+
and nothing is logged. With a ``service``, a contract whose ``service``
|
|
195
|
+
property names another one is refused too, as ``pack`` refuses it.
|
|
196
|
+
|
|
197
|
+
With an ``ontology`` file (which needs the ``service``), each contract is
|
|
198
|
+
then checked against it by the per-service rules of continuo-contracts.
|
|
199
|
+
Each finding is logged at its level, ERROR for an error, WARNING for a
|
|
200
|
+
warning and INFO for information, as ``<severity> [<rule>] <subject>:
|
|
201
|
+
<message>``, in the order the library returns them, followed by one INFO
|
|
202
|
+
line counting the three. Any error makes the exit code 1. An ontology file
|
|
203
|
+
that cannot be read, is not an ontology or is refused by the library's
|
|
204
|
+
``ontology`` rule is one ERROR line and exit 1, before any finding. Nothing
|
|
205
|
+
goes to stdout.
|
|
206
|
+
"""
|
|
207
|
+
if service is None:
|
|
208
|
+
load_contract_dir(Path(contract_dir), dialect=dialect)
|
|
209
|
+
return 0
|
|
210
|
+
loaded = load_contract_docs(Path(contract_dir), dialect=dialect, service=service)
|
|
211
|
+
if ontology is None:
|
|
212
|
+
return 0
|
|
213
|
+
|
|
214
|
+
findings = check_service(
|
|
215
|
+
[doc for _, doc in loaded], service=service, ontology_path=Path(ontology)
|
|
216
|
+
)
|
|
217
|
+
for finding in findings:
|
|
218
|
+
logger.log(
|
|
219
|
+
_FINDING_LEVELS[finding.severity],
|
|
220
|
+
"%s [%s] %s: %s",
|
|
221
|
+
finding.severity,
|
|
222
|
+
finding.rule,
|
|
223
|
+
finding.subject,
|
|
224
|
+
finding.message,
|
|
225
|
+
)
|
|
226
|
+
counts = {severity: 0 for severity in _FINDING_LEVELS}
|
|
227
|
+
for finding in findings:
|
|
228
|
+
counts[finding.severity] += 1
|
|
229
|
+
logger.info(
|
|
230
|
+
"%d error(s), %d warning(s), %d info: %s",
|
|
231
|
+
counts["error"],
|
|
232
|
+
counts["warning"],
|
|
233
|
+
counts["info"],
|
|
234
|
+
service,
|
|
235
|
+
)
|
|
236
|
+
return 1 if counts["error"] else 0
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def cmd_pack(
|
|
125
240
|
contract_dir: str,
|
|
126
241
|
service: str,
|
|
127
242
|
repo_root: str,
|
|
128
243
|
out: str,
|
|
244
|
+
*,
|
|
245
|
+
image: str | None,
|
|
246
|
+
image_packages: str | None,
|
|
129
247
|
dialect: str | None = None,
|
|
130
248
|
) -> int:
|
|
131
|
-
"""
|
|
132
|
-
|
|
133
|
-
|
|
249
|
+
"""Pack the contracts into the bundle file ``out``.
|
|
250
|
+
|
|
251
|
+
The bundle records the package list of ``image``, or the one ``image_packages``
|
|
252
|
+
holds, never the packages of the environment this command runs in. Nothing is
|
|
253
|
+
written to ``out`` unless every check passed.
|
|
254
|
+
"""
|
|
255
|
+
packages = image_packages_from(
|
|
256
|
+
image=image, image_packages=None if image_packages is None else Path(image_packages)
|
|
257
|
+
)
|
|
258
|
+
doc = build_bundle(Path(contract_dir), Path(repo_root), service, packages, dialect=dialect)
|
|
259
|
+
write_bundle(doc, Path(out))
|
|
134
260
|
return 0
|
|
135
261
|
|
|
136
262
|
|
|
137
263
|
def cmd_hash(contract_dir: str, repo_root: str, dialect: str | None = None) -> int:
|
|
138
|
-
"""Print relation
|
|
264
|
+
"""Print ``<relation>\\t<content_hash>`` for each node, one line each, sorted by relation.
|
|
139
265
|
|
|
140
|
-
|
|
141
|
-
|
|
266
|
+
Each node is the one a contract in ``contract_dir`` declares, hashed from the
|
|
267
|
+
files of ``repo_root`` it names. Every node is hashed before the first line
|
|
268
|
+
is printed, so a node that cannot be hashed leaves stdout empty.
|
|
142
269
|
"""
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
270
|
+
root = Path(repo_root)
|
|
271
|
+
hashes = {}
|
|
272
|
+
for node in load_contract_dir(Path(contract_dir), dialect=dialect):
|
|
273
|
+
inputs = gather_hash_inputs(node, root)
|
|
274
|
+
hashes[node.relation] = hash_parts(node, inputs.primary, inputs.units)["content_hash"]
|
|
147
275
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
print(f"{relation}\t{hash_value}")
|
|
276
|
+
for relation, content_hash in sorted(hashes.items()):
|
|
277
|
+
print(f"{relation}\t{content_hash}")
|
|
278
|
+
|
|
279
|
+
return 0
|
|
153
280
|
|
|
281
|
+
|
|
282
|
+
def cmd_packages() -> int:
|
|
283
|
+
"""Print every installed package as one JSON object, ``{normalised name: version}``, and a newline.
|
|
284
|
+
|
|
285
|
+
The object is the only thing on stdout: a caller that runs this command in an image
|
|
286
|
+
parses it as the image's package list.
|
|
287
|
+
"""
|
|
288
|
+
print(json.dumps(installed_packages(), sort_keys=True))
|
|
154
289
|
return 0
|
|
155
290
|
|
|
156
291
|
|
|
@@ -168,10 +303,12 @@ def cmd_lint(paths: list[str]) -> int:
|
|
|
168
303
|
|
|
169
304
|
|
|
170
305
|
def cmd_run() -> int:
|
|
171
|
-
"""
|
|
306
|
+
"""Run the node ``os.environ`` names and return its exit code.
|
|
172
307
|
|
|
173
|
-
This is the container entrypoint
|
|
174
|
-
|
|
175
|
-
|
|
308
|
+
This is the container entrypoint. ``harness.run_node`` reads ``NODE_ID``,
|
|
309
|
+
``TABLE_NAME`` and ``TARGET_SCHEMA``, and, when set, ``CONTRACT_DIR`` (the
|
|
310
|
+
folder of the image's ``*.odcs.yaml`` contracts) and ``APP_ROOT`` (the
|
|
311
|
+
repository root a script path is resolved against), runs the node and
|
|
312
|
+
prints exactly one sentinel result block.
|
|
176
313
|
"""
|
|
177
314
|
return harness.run_node(os.environ)
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
continuo sets ``CONTENT_HASH`` to the ``content_hash`` the release promoted for
|
|
4
4
|
the node. The check recomputes the node's hash from the files in the image with
|
|
5
|
-
the helpers ``
|
|
6
|
-
two can never disagree, and refuses to run a node whose
|
|
7
|
-
different one: the image holds other code, imports, assets or
|
|
8
|
-
release.
|
|
5
|
+
the helpers ``continuo-runtime hash`` uses (``gather_hash_inputs`` and
|
|
6
|
+
``hash_parts``), so the two can never disagree, and refuses to run a node whose
|
|
7
|
+
image reproduces a different one: the image holds other code, imports, assets or
|
|
8
|
+
contract than the release.
|
|
9
9
|
"""
|
|
10
10
|
|
|
11
11
|
import logging
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
"""Per-kind
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
"""Per-kind reads rules: how the runtime checks the reads of a node of each kind.
|
|
2
|
+
|
|
3
|
+
continuo-contracts decides which fields a node of each kind must, may and may
|
|
4
|
+
not carry. The runtime keeps one rule per kind for itself, the check of its
|
|
5
|
+
reads: a python-node's reads each parse as one read query under the
|
|
6
|
+
repository's dialect and name the schema of every table they read, a
|
|
7
|
+
python-csv node's uri is one its readers fetch, and a python-api node reads
|
|
8
|
+
nothing. Adding a kind means adding one entry to ``RULES``; nothing that loads
|
|
9
|
+
a contract branches on the kind's name.
|
|
7
10
|
"""
|
|
8
11
|
|
|
9
12
|
from __future__ import annotations
|
|
@@ -13,9 +16,10 @@ from dataclasses import dataclass
|
|
|
13
16
|
from typing import Any
|
|
14
17
|
|
|
15
18
|
from continuo_engine_contract.sql import ensure_single_read # type: ignore[import-untyped]
|
|
16
|
-
from sqlglot.errors import TokenError
|
|
19
|
+
from sqlglot.errors import ParseError, TokenError
|
|
17
20
|
|
|
18
|
-
from continuo_python_runtime.contract.model import
|
|
21
|
+
from continuo_python_runtime.contract.model import KINDS
|
|
22
|
+
from continuo_python_runtime.contract.read_tables import unqualified_tables
|
|
19
23
|
from continuo_python_runtime.csv_source import parse_csv_uri
|
|
20
24
|
from continuo_python_runtime.errors import ContractError
|
|
21
25
|
|
|
@@ -30,7 +34,11 @@ class ReadsRule(ABC):
|
|
|
30
34
|
|
|
31
35
|
|
|
32
36
|
class SqlReads(ReadsRule):
|
|
33
|
-
"""A non-empty mapping of read name -> single SELECT.
|
|
37
|
+
"""A non-empty mapping of read name -> single SELECT.
|
|
38
|
+
|
|
39
|
+
When the reads are checked, every table a query names must name its schema
|
|
40
|
+
(a reference to a CTE in scope excepted).
|
|
41
|
+
"""
|
|
34
42
|
|
|
35
43
|
def validate(
|
|
36
44
|
self, reads: Any, label: str, *, dialect: str | None, check_reads: bool
|
|
@@ -69,6 +77,22 @@ class SqlReads(ReadsRule):
|
|
|
69
77
|
raise ContractError(
|
|
70
78
|
f"{label}: 'reads.{name}' must be a single read query ({exc})"
|
|
71
79
|
) from exc
|
|
80
|
+
try:
|
|
81
|
+
unqualified = unqualified_tables(sql, dialect=dialect)
|
|
82
|
+
except ParseError as exc:
|
|
83
|
+
# ensure_single_read keeps the statements sqlglot parsed and
|
|
84
|
+
# accepts a query that starts with an empty statement (`; select
|
|
85
|
+
# ...`); sqlglot's parse_one, which finds the tables here and in
|
|
86
|
+
# topology-controller, refuses it. The read is refused as the
|
|
87
|
+
# release would refuse it.
|
|
88
|
+
raise ContractError(
|
|
89
|
+
f"{label}: 'reads.{name}' must be a single read query ({exc})"
|
|
90
|
+
) from exc
|
|
91
|
+
if unqualified:
|
|
92
|
+
raise ContractError(
|
|
93
|
+
f"{label}: read {name!r} names table {unqualified[0]!r} without its schema; "
|
|
94
|
+
f"write <schema>.{unqualified[0]}"
|
|
95
|
+
)
|
|
72
96
|
return dict(reads)
|
|
73
97
|
|
|
74
98
|
|
|
@@ -104,31 +128,15 @@ class NoReads(ReadsRule):
|
|
|
104
128
|
|
|
105
129
|
@dataclass(frozen=True)
|
|
106
130
|
class KindRules:
|
|
107
|
-
"""
|
|
131
|
+
"""The rules the runtime keeps for a node of one kind."""
|
|
108
132
|
|
|
109
|
-
script_required: bool
|
|
110
133
|
reads: ReadsRule
|
|
111
|
-
secret_allowed: bool = False
|
|
112
|
-
# The write modes a node of this kind may declare. A new kind gets replace
|
|
113
|
-
# only unless its entry opts into more.
|
|
114
|
-
mode_types: frozenset[str] = frozenset({DEFAULT_MODE_TYPE})
|
|
115
|
-
# Whether a node of this kind may list the repository files its script reads.
|
|
116
|
-
assets_allowed: bool = False
|
|
117
134
|
|
|
118
135
|
|
|
119
136
|
RULES: dict[str, KindRules] = {
|
|
120
|
-
"python-node": KindRules(
|
|
121
|
-
|
|
122
|
-
),
|
|
123
|
-
# A csv drop is a snapshot of its source: replace only (spec D2).
|
|
124
|
-
"python-csv": KindRules(script_required=False, reads=CsvRead()),
|
|
125
|
-
"python-api": KindRules(
|
|
126
|
-
script_required=True,
|
|
127
|
-
reads=NoReads(),
|
|
128
|
-
secret_allowed=True,
|
|
129
|
-
mode_types=MODE_TYPES,
|
|
130
|
-
assets_allowed=True,
|
|
131
|
-
),
|
|
137
|
+
"python-node": KindRules(reads=SqlReads()),
|
|
138
|
+
"python-csv": KindRules(reads=CsvRead()),
|
|
139
|
+
"python-api": KindRules(reads=NoReads()),
|
|
132
140
|
}
|
|
133
141
|
|
|
134
142
|
|