Repository navigation
Expand file tree
/
Copy pathpackage_verification.py
More file actions
230 lines (223 loc) · 9.95 KB
/
Copy pathpackage_verification.py
File metadata and controls
230 lines (223 loc) · 9.95 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
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
"""Verify live package artifacts against manifest and registry receipts."""
from __future__ import annotations
from pathlib import Path
from .. import paths
from ..environments import Package
from .artifacts import verify_artifact_file
from .checkouts import (
_checkout_integrity_issues,
checkout_file_matches,
checkout_patch_expectation,
)
from .integrity import hub_materialization_issues
from .locations import managed_checkout_path
from .models import ProvisioningError
from .products import built_product_candidates, validated_built_product
from .teardown import _source_revision_report
from .toolchain import Toolchain
def verify_packages(
toolchain: Toolchain,
document: dict,
package_catalog: dict[str, Package],
built_runtime_probes: dict[str, bool],
invalid_environment_roots: set[str],
) -> tuple[list[dict], list[dict]]:
verified: list[dict] = []
failed: list[dict] = []
for identifier, entry in sorted(document["packages"].items()):
if entry.get("state") != "ready":
failed.append(
{
"package": identifier,
"code": "package_not_ready",
"detail": f"state is {entry.get('state')!r}",
"fix": f"audio packages pull --repair {identifier}",
}
)
continue
package = package_catalog.get(identifier)
if package is None:
failed.append(
{
"package": identifier,
"code": "package_unknown",
"detail": "ready registry entry is not present in the installed manifest",
"fix": f"Inspect {paths.registry_path()} and remove the stale entry",
}
)
continue
# The environment-root verdict owns every path below it. Once that root is
# redirected, do not inspect a checkout or launch a product reached through it;
# the environment failure above is the complete, safe diagnosis.
if package.environment in invalid_environment_roots:
continue
record: dict = {"package": identifier}
materialized = entry.get("materialized", {})
if not isinstance(materialized, dict):
failed.append(
{
"package": identifier,
"code": "package_integrity_failed",
"detail": "materialized receipt is not an object",
"fix": f"audio packages pull --repair {identifier}",
}
)
continue
# Git blob identity is reported explicitly, separately from URL SHA-256 verdicts.
if package.source["type"] in {"url", "git-blob"}:
try:
_path, verdict, _provenance = verify_artifact_file(
package, materialized.get("path")
)
except ProvisioningError as exc:
failed.append(exc.as_dict())
continue
record.update(verdict)
elif package.source["type"] in {
"huggingface",
"huggingface_multi",
}:
issues = hub_materialization_issues(package, materialized)
if issues:
failed.append(
{
"package": identifier,
"code": "package_integrity_failed",
"detail": "; ".join(issues),
"fix": f"audio packages pull --repair {identifier}",
}
)
continue
# The receipt is not the pin. It is mutable local history and may predate a
# manifest correction; only the manifest revisions can be republished as the
# revisions this verification actually required above.
record.update(_source_revision_report(package))
elif package.source["type"] == "git+build":
checkout_value = materialized.get("path")
checkout, location_issue = managed_checkout_path(package, checkout_value)
product = str(package.source["product"])
issues: list[str] = []
if location_issue is not None:
issues.append(location_issue)
elif checkout is None or not checkout.is_dir():
issues.append(f"built checkout is not a directory: {checkout}")
else:
try:
state = toolchain.inspect_checkout(checkout)
except ValueError as exc:
issues.append(str(exc))
else:
if state.head != package.source["commit"]:
issues.append(
f"checkout HEAD is {state.head!r}, expected exact commit "
f"{package.source['commit']!r}"
)
try:
expected_patches, expected_names, expected_digests = (
checkout_patch_expectation(package)
)
except (OSError, ValueError) as exc:
issues.append(f"shipped checkout patch cannot be read: {exc}")
expected_patches, expected_names, expected_digests = (), (), {}
if set(state.modified) != set(expected_names):
issues.append(
f"built checkout tracked changes are "
f"{list(state.modified)!r}, expected "
f"{sorted(expected_names)!r}"
)
applied = materialized.get("patches_applied", [])
if applied != list(expected_patches):
issues.append(
f"recorded patches are {applied!r}, expected exactly "
f"{list(expected_patches)!r}"
)
recorded_digests = materialized.get("patched_file_digests", {})
if recorded_digests != expected_digests:
issues.append("recorded patched-file digests differ from manifest")
changed = [
name
for name, digest in expected_digests.items()
if not checkout_file_matches(checkout, name, digest, toolchain.file_digest)
]
if changed:
issues.append(f"live patched-file hashes changed for {sorted(changed)!r}")
candidates = built_product_candidates(checkout, product)
if len(candidates) != 1:
issues.append(
f"expected one executable built product {product!r}, found "
f"{len(candidates)}"
)
else:
_executable, product_issue = validated_built_product(
checkout, product, materialized
)
if product_issue is not None:
issues.append(product_issue)
if issues:
failed.append(
{
"package": identifier,
"code": "package_integrity_failed",
"detail": "; ".join(issues),
"fix": f"audio packages pull --repair {identifier}",
}
)
continue
if package.environment in built_runtime_probes:
product_runs = built_runtime_probes[package.environment]
else:
try:
product_runs = toolchain.built_product_runs(candidates[0])
except OSError:
product_runs = False
if not product_runs:
failed.append(
{
"package": identifier,
"code": "package_build_unusable",
"detail": f"built product {product!r} does not run",
"fix": f"audio packages pull --repair {identifier}",
}
)
continue
record["product_runs"] = True
record["product_digest"] = "ok"
record["patches_applied"] = list(expected_patches)
else:
locations = [
Path(p)
for p in (
[materialized["path"]]
if materialized.get("path")
else list((materialized.get("paths") or {}).values())
)
]
gone = [str(location) for location in locations if not location.exists()]
if gone:
# The shared-cache consequence: another root's purge, or a manual cache
# clear, can take weights out from under a root that still calls them
# ready. Better an exit 3 with a fix than a stack that fails mid-run.
failed.append(
{
"package": identifier,
"code": "package_integrity_failed",
"detail": f"materialized path(s) no longer exist: {', '.join(gone)}",
"fix": f"audio packages pull --repair {identifier}",
}
)
continue
if package.checkout is not None:
issues = _checkout_integrity_issues(package, materialized, toolchain)
if issues:
failed.append(
{
"package": identifier,
"code": "package_integrity_failed",
"detail": "; ".join(issues),
"fix": f"audio packages pull --repair {identifier}",
}
)
continue
record["patches_applied"] = materialized.get("patches_applied", [])
verified.append(record)
return verified, failed