Repository navigation
Expand file tree
/
Copy pathcheck_generated_models.py
More file actions
153 lines (121 loc) · 4.9 KB
/
Copy pathcheck_generated_models.py
File metadata and controls
153 lines (121 loc) · 4.9 KB
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
# Copyright 2026 Collate
# Licensed under the Collate Community License, Version 1.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# https://github.com/open-metadata/OpenMetadata/blob/main/ingestion/LICENSE
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Check whether generated ingestion models match their generation inputs."""
from __future__ import annotations
import argparse
import hashlib
import json
import sys
from pathlib import Path
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Iterable
ROOT = Path(__file__).resolve().parents[1]
GENERATED_ROOT = ROOT / "ingestion/src/metadata/generated"
MANIFEST = GENERATED_ROOT / ".generation-manifest.json"
MANIFEST_VERSION = 2
INPUT_ROOTS = (
ROOT / "openmetadata-spec/src/main/resources/json/schema",
ROOT / "openmetadata-spec/src/main/resources/odcs",
ROOT / "openmetadata-spec/src/main/antlr4",
)
INPUT_FILES = (
ROOT / "Makefile",
ROOT / "ingestion/pyproject.toml",
ROOT / "ingestion/uv.lock",
ROOT / "ingestion/noxfile.py",
ROOT / "ingestion/Makefile",
ROOT / "scripts/datamodel_generation.py",
ROOT / "openmetadata-spec/scripts/prepare_model_schemas.py",
ROOT / "scripts/generate_ingestion_models.py",
Path(__file__).resolve(),
)
def sha256(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as file_:
for chunk in iter(lambda: file_.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def relative(path: Path) -> str:
return path.relative_to(ROOT).as_posix()
def input_paths() -> list[Path]:
paths = set(INPUT_FILES)
for root in INPUT_ROOTS:
paths.update(path for path in root.rglob("*") if path.is_file())
return sorted(paths)
def generated_paths() -> list[Path]:
if not GENERATED_ROOT.exists():
return []
return sorted(
path
for path in GENERATED_ROOT.rglob("*")
if path.is_file() and path != MANIFEST and "__pycache__" not in path.parts and path.suffix != ".pyc"
)
def hashes(paths: Iterable[Path]) -> dict[str, str]:
return {relative(path): sha256(path) for path in paths}
def current_manifest() -> dict[str, object]:
return {
"version": MANIFEST_VERSION,
"inputs": hashes(input_paths()),
"outputs": hashes(generated_paths()),
}
def changed_paths(expected: dict[str, str], actual: dict[str, str]) -> tuple[list[str], list[str], list[str]]:
expected_paths = set(expected)
actual_paths = set(actual)
added = sorted(actual_paths - expected_paths)
removed = sorted(expected_paths - actual_paths)
changed = sorted(path for path in expected_paths & actual_paths if expected[path] != actual[path])
return added, removed, changed
def report_difference(label: str, expected: dict[str, str], actual: dict[str, str]) -> bool:
added, removed, changed = changed_paths(expected, actual)
if not any((added, removed, changed)):
return False
sys.stdout.write(f"{label} changed:" + "\n")
for category, paths in (
("added", added),
("removed", removed),
("modified", changed),
):
for path in paths:
sys.stdout.write(f" {category}: {path}" + "\n")
return True
def check() -> int:
if not MANIFEST.exists():
sys.stdout.write(f"Generated-model manifest is missing: {MANIFEST}" + "\n")
return 1
expected = json.loads(MANIFEST.read_text(encoding="utf-8"))
actual = current_manifest()
stale = expected.get("version") != MANIFEST_VERSION
stale |= report_difference("Generation inputs", expected.get("inputs", {}), actual["inputs"])
stale |= report_difference("Generated outputs", expected.get("outputs", {}), actual["outputs"])
if stale:
return 1
sys.stdout.write("Generated models are up to date." + "\n")
return 0
def write() -> int:
if not generated_paths():
sys.stdout.write("Cannot write generated-model manifest: generated files are missing." + "\n")
return 1
MANIFEST.write_text(
json.dumps(current_manifest(), indent=2, sort_keys=True) + "\n",
encoding="utf-8",
)
sys.stdout.write(f"Wrote generated-model manifest: {MANIFEST}" + "\n")
return 0
def main() -> int:
parser = argparse.ArgumentParser()
action = parser.add_mutually_exclusive_group(required=True)
action.add_argument("--check", action="store_true")
action.add_argument("--write", action="store_true")
args = parser.parse_args()
return check() if args.check else write()
if __name__ == "__main__":
raise SystemExit(main())