-
Notifications
You must be signed in to change notification settings - Fork 145
/
manifest.py
490 lines (420 loc) · 17.7 KB
/
manifest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
"""Compile a Meltano manifest."""
from __future__ import annotations
import json
import re
import subprocess
import typing as t
from collections import defaultdict
from contextlib import suppress
from functools import cached_property, reduce
from operator import getitem
from pathlib import Path
from tempfile import NamedTemporaryFile
import flatten_dict # type: ignore
import structlog
import yaml
from meltano import __file__ as package_root_path
from meltano.core.manifest.jsonschema import meltano_config_env_locations
from meltano.core.plugin.settings_service import PluginSettingsService
from meltano.core.plugin_lock_service import PluginLock
from meltano.core.utils import (
EnvVarMissingBehavior,
MergeStrategy,
deep_merge,
default_deep_merge_strategies,
expand_env_vars,
get_no_color_flag,
)
if t.TYPE_CHECKING:
import sys
from collections.abc import Iterable
from meltano.core.plugin.base import PluginType
from meltano.core.plugin.project_plugin import ProjectPlugin
from meltano.core.project import Project
if sys.version_info >= (3, 10):
from typing import TypeAlias
else:
from typing_extensions import TypeAlias
# NOTE: We do not use `Project(...).meltano.canonical` for 3 reasons:
# - It will make it difficult to refactor the rest of the Meltano core to be
# based on the manifest if the manifest itself depends on the rest of Meltano
# core.
# - The canonical representation has changes to the file that we don't want,
# e.g. turning strings into datetime objects.
# - The canonical representation uses Ruamel, which is known to have poor
# performance in some cases. No need to eat that cost here, since we don't
# care about comment preservation in the manifest.
logger = structlog.getLogger(__name__)
JSON_LOCATION_PATTERN = re.compile(r"\.|(\[\])")
MANIFEST_SCHEMA_PATH = (
Path(package_root_path).parent / "schemas" / "meltano.schema.json"
)
Trie: TypeAlias = t.Dict[str, "Trie"]
PluginsByType: TypeAlias = t.Mapping[str, t.List[t.Mapping[str, t.Any]]]
PluginsByNameByType: TypeAlias = t.Mapping[str, t.Mapping[str, t.Mapping[str, t.Any]]]
# Ruamel doesn't have this problem where YAML tags like timestamps are
# automatically converted to Python objects that aren't JSON-serializable, but
# it's not suitable for use here because it doesn't load the YAML file as plain
# Python data structures like dictionaries and lists. An alternative to using
# `YamlLimitedSafeLoader` would be to load the YAML files with Ruamel, and then
# copying the data out at depth into regular dictionaries and lists.
class YamlLimitedSafeLoader(type):
"""Meta YAML loader that skips the resolution of the specified YAML tags."""
def __new__(
cls,
name,
bases,
namespace,
do_not_resolve: Iterable[str],
) -> YamlLimitedSafeLoader:
"""Generate a new instance of this metaclass.
Args:
name: The name of the new class.
bases: The base classes of the new class. The first base class will
always be `yaml.SafeLoader`. These will come afterwards.
namespace: The namespace of the new class.
do_not_resolve: The YAML tags whose resolution should be skipped
during parsing. They will be left as raw strings.
Returns:
The new class object, which is a subclass of `yaml.SafeLoader`
which skips the resolution of the specified YAML tags.
"""
do_not_resolve = set(do_not_resolve)
implicit_resolvers = {
key: [(tag, regex) for tag, regex in mappings if tag not in do_not_resolve]
for key, mappings in yaml.SafeLoader.yaml_implicit_resolvers.items()
}
return super().__new__(
cls,
name,
tuple({yaml.SafeLoader, *bases}),
{**namespace, "yaml_implicit_resolvers": implicit_resolvers},
)
class YamlNoTimestampSafeLoader(
yaml.SafeLoader,
metaclass=YamlLimitedSafeLoader,
do_not_resolve={"tag:yaml.org,2002:timestamp"},
):
"""A safe YAML loader that leaves timestamps as strings."""
class Manifest: # noqa: WPS214
"""A complete unambiguous static representation of a Meltano project."""
def __init__(self, project: Project, path: Path, check_schema: bool) -> None:
"""Initialize the manifest.
This class does not write a manifest file. It merely generates the data
that would go into one.
The contents of the manifest can be access using the `data` attribute.
This data is generated when it is first accessed. After that, a cached
result is used.
Args:
project: The Meltano project which this manifest describes.
path: The path the Manifest will be saved to - only used for
jsonschema validation failure error messages.
check_schema: Whether the project files and generated manifest
files should be validated against the Meltano schema.
"""
self.project = project
self._project_root_str = str(self.project.root.resolve())
self._meltano_file = self.project.meltanofile.read_text()
self.path = path
self.check_schema = check_schema
with open(MANIFEST_SCHEMA_PATH) as manifest_schema_file:
manifest_schema = json.load(manifest_schema_file)
self._env_locations = meltano_config_env_locations(manifest_schema)
@staticmethod
def _validate_against_manifest_schema(
instance_name: str,
instance_path: Path,
instance_data: dict[str, t.Any],
) -> None:
with NamedTemporaryFile(suffix=".json") as schema_instance_file:
schema_instance_file.write(json.dumps(instance_data).encode())
schema_instance_file.flush()
proc = subprocess.run(
(
"check-jsonschema",
"--color",
"never" if get_no_color_flag() else "always",
"--schemafile",
str(MANIFEST_SCHEMA_PATH),
schema_instance_file.name,
),
text=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
if proc.returncode:
jsonschema_checker_message = proc.stdout.strip().replace(
schema_instance_file.name,
str(instance_path),
)
logger.warning(
f"Failed to validate {instance_name} against Meltano manifest " # noqa: G004
f"schema ({MANIFEST_SCHEMA_PATH}):\n{jsonschema_checker_message}",
)
@cached_property
def _project_files(self) -> dict[str, t.Any]:
project_files = flatten_dict.unflatten(
deep_merge(
yaml.load(
self._meltano_file,
t.cast( # noqa: S506
t.Type[yaml.SafeLoader],
YamlNoTimestampSafeLoader,
),
),
*(
yaml.load(
x.read_text(),
t.cast( # noqa: S506
t.Type[yaml.SafeLoader],
YamlNoTimestampSafeLoader,
),
)
for x in self.project.project_files.include_paths
),
),
"dot",
)
if self.check_schema:
self._validate_against_manifest_schema(
"project files",
self.project.meltanofile,
project_files,
)
return project_files
def _merge_plugin_lockfiles(
self,
plugins: dict[PluginType, list[ProjectPlugin]],
manifest: dict[str, t.Any],
) -> None:
locked_plugins = t.cast(
t.Dict[str, t.List[t.Mapping[str, t.Any]]],
{
plugin_type.value: [
PluginLock(self.project, plugin).load(create=True, loader=json.load)
for plugin in plugins
]
for (plugin_type, plugins) in plugins.items()
},
)
# Merge the locked plugins with the root-level plugins from `meltano.yml`:
manifest["plugins"] = deep_merge( # noqa: WPS204
_plugins_by_name_by_type(manifest["plugins"]),
_plugins_by_name_by_type(locked_plugins),
)
def sanitize_env_vars(self, env: t.Mapping[str, str]) -> dict[str, str]:
"""Sanitize environment variables.
Sanitization is perfomed by:
- Replacing the project root as an absolute path in values with
'${MELTANO_PROJECT_ROOT}'
Args:
env: A mapping of environment variables.
Returns:
The given environment variables with some removed or edited as
necessary.
"""
return {
k: v.replace(self._project_root_str, "${MELTANO_PROJECT_ROOT}")
for k, v in env.items()
}
def env_aware_merge_mappings(
self,
data: t.MutableMapping[str, t.Any],
key: str,
value: t.Any,
_: tuple[t.Any, ...] | None = None,
) -> None:
"""Merge behavior for `deep_merge` that expands env vars when the key is "env".
Args:
data: The dictionary being merged into.
key: The key for the dictionary being merged into.
value: The value being merged into the dictionary.
_: Merge strategies - unused argument.
Returns:
`NotImplemented` if the key is not "env"; `None` otherwise.
"""
if key != "env":
return NotImplemented # type: ignore
data[key] = self.sanitize_env_vars(
{
**expand_env_vars(
data[key],
value,
if_missing=EnvVarMissingBehavior.ignore,
),
**expand_env_vars(
value,
data[key],
if_missing=EnvVarMissingBehavior.ignore,
),
},
)
def _merge_env_vars(
self,
plugins: dict[PluginType, list[ProjectPlugin]],
manifest: dict[str, t.Any],
) -> None:
# Merge env vars derived from project settings:
self.env_aware_merge_mappings(manifest, "env", self.project.settings.as_env())
# Ensure the environment-level plugin config is mergable:
environment = next(iter(manifest["environments"]))
environment_plugins = environment.setdefault("config", {}).setdefault(
"plugins",
{},
)
environment["config"]["plugins"] = _plugins_by_name_by_type(environment_plugins)
merge_strategies = (
MergeStrategy(dict, self.env_aware_merge_mappings),
*default_deep_merge_strategies,
)
# Env precedence hierarchy:
# - environment plugin env # highest
# - root plugin env
# - environment env
# - root env # Manifest only cares about this level and higher
# - schedule env
# - job env
# - .env file
# - terminal env # lowest
# NOTE: The schedule, job, .env, and terminal env are all injected into
# `os.environ`. Meltano should only ever read the schedule and
# job env fields within `meltano.yml` or a manifest file in order
# to inject it into `os.environ`.
environment = next(iter(manifest["environments"]))
# Merge 'environment level plugin env' into 'root level plugin env':
manifest["plugins"] = deep_merge(
manifest["plugins"],
environment.get("config", {}).get("plugins", {}),
strategies=merge_strategies,
)
# Merge env vars derived from plugin settings:
for plugin_type in plugins:
for plugin in plugins[plugin_type]:
self.env_aware_merge_mappings(
manifest["plugins"][plugin_type][plugin.name],
"env",
PluginSettingsService(project=self.project, plugin=plugin).as_env(),
)
# Merge 'environment level env' into 'root level env':
self.env_aware_merge_mappings(manifest, "env", environment["env"])
@cached_property
def data(self) -> dict[str, t.Any]:
"""Generate the manifest data.
The data is generated when this property is first accessed, then cached
in the instance for fast subsequent accesses. This cached result is
mutable, because Python lacks a `frozendict` class in its standard
library. Please do not alter it in-place.
Returns:
The manifest data.
"""
# The manifest begins as the project files, i.e. `meltano.yml` + included files
manifest = self._project_files
# Remove all environments other than the one this manifest is for:
if self.project.environment is None:
# We use a mock environment here because the rest of the code
# expects there to be exactly one environment. It is omitted from
# the final output, and during processing it only affects things if
# it has content, which this mock does not.
manifest["environments"] = [{"name": "mock"}] # noqa: WPS204
else:
manifest["environments"] = [
x
for x in manifest["environments"]
if x["name"] == self.project.environment.name
]
apply_scaffold(manifest, self._env_locations)
plugins = self.project.plugins.plugins_by_type()
# Remove the 'mappings' category of plugins, since those are created
# dynamically at run-time, and shouldn't be represented directly in
# `meltano.yml` or in manifest files. For more details, refer to:
# https://gitlab.com/meltano/meltano/-/merge_requests/2481#note_832478775
with suppress(KeyError):
del plugins["mappings"]
# NOTE: `self._merge_plugin_lockfiles` restructures the plugins into a
# map from plugin types to maps of plugin IDs to their values.
# This structure is easier to work with, but is reset to the
# proper structure below, before the manifest is returned.
self._merge_plugin_lockfiles(plugins, manifest)
self._merge_env_vars(plugins, manifest)
# Make each plugin type a list again:
manifest["plugins"] = {
plugin_type: list(plugins.values())
for plugin_type, plugins in manifest["plugins"].items()
}
# XXX: Meltano shouldn't manipulate annotations, yet here we do
# because the alternative is to completely lose the environment-level
# annotations.
environment = next(iter(manifest["environments"]))
if "annotations" in environment:
manifest["annotations"] = deep_merge(
manifest.get("annotations", {}),
environment["annotations"],
)
# Everything from the selected environment has been merged into the
# higher levels, so it can be deleted to avoid any ambiguity about
# which fields within the manifest should be read.
with suppress(KeyError):
del manifest["environments"]
# The include paths have already been resolved, and so are removed to
# avoid ambiguity. If this information is of interest, it could be
# re-added into an annotation.
with suppress(KeyError):
del manifest["include_paths"]
if self.check_schema:
self._validate_against_manifest_schema(
"newly compiled manifest",
self.path,
manifest,
)
return manifest
def _plugins_by_name_by_type(
plugins_by_type: PluginsByType,
) -> PluginsByNameByType:
return {
plugin_type: {plugin["name"]: plugin for plugin in plugins}
for plugin_type, plugins in plugins_by_type.items()
}
def _locations_trie(paths: Iterable[Iterable[str]]) -> Trie:
def defaultdict_defaultdict():
return defaultdict(defaultdict_defaultdict)
root = defaultdict_defaultdict()
for path in paths:
reduce(getitem, path, root)
return json.loads(json.dumps(root))
def _apply_scaffold(
trie: Trie,
manifest_component: dict[str, t.Any] | list[t.Any],
) -> None:
for key, sub_trie in trie.items():
if key == "[]":
if not isinstance(manifest_component, list):
raise TypeError(
"Expected list during manifest scaffolding, "
f"got {type(manifest_component)}",
)
for element in manifest_component:
_apply_scaffold(sub_trie, element)
elif isinstance(manifest_component, dict):
_apply_scaffold(
sub_trie,
manifest_component.setdefault(key, [] if "[]" in sub_trie else {}),
)
else:
raise TypeError(
"Expected dict during manifest scaffolding, "
f"got {type(manifest_component)}",
)
def apply_scaffold(manifest: dict[str, t.Any], locations: Iterable[str]) -> None:
"""Update the provided manifest dict with dicts/lists at the provided locations.
Args:
manifest: The manifest dictionary to update in-place.
locations: The locations (as jq filters) which will be added to the the
manifest as necessary.
"""
_apply_scaffold(
_locations_trie(
(x for x in JSON_LOCATION_PATTERN.split(location) if x)
for location in locations
),
manifest,
)