ssot-pack-contracts 0.2.18__tar.gz
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.
- ssot_pack_contracts-0.2.18/PKG-INFO +29 -0
- ssot_pack_contracts-0.2.18/README.md +5 -0
- ssot_pack_contracts-0.2.18/pyproject.toml +42 -0
- ssot_pack_contracts-0.2.18/setup.cfg +4 -0
- ssot_pack_contracts-0.2.18/src/ssot_pack_contracts/__init__.py +54 -0
- ssot_pack_contracts-0.2.18/src/ssot_pack_contracts/api.py +454 -0
- ssot_pack_contracts-0.2.18/src/ssot_pack_contracts/py.typed +1 -0
- ssot_pack_contracts-0.2.18/src/ssot_pack_contracts.egg-info/PKG-INFO +29 -0
- ssot_pack_contracts-0.2.18/src/ssot_pack_contracts.egg-info/SOURCES.txt +11 -0
- ssot_pack_contracts-0.2.18/src/ssot_pack_contracts.egg-info/dependency_links.txt +1 -0
- ssot_pack_contracts-0.2.18/src/ssot_pack_contracts.egg-info/requires.txt +3 -0
- ssot_pack_contracts-0.2.18/src/ssot_pack_contracts.egg-info/top_level.txt +1 -0
- ssot_pack_contracts-0.2.18/tests/test_pack_contracts.py +208 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ssot-pack-contracts
|
|
3
|
+
Version: 0.2.18
|
|
4
|
+
Summary: Shared governance-pack metadata, manifest, and packaged document contracts for SSOT.
|
|
5
|
+
Author-email: Jacob Stewart <jacob@swarmauri.com>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/groupsum/ssot-registry/tree/main/pkgs/ssot-pack-contracts
|
|
8
|
+
Project-URL: Repository, https://github.com/groupsum/ssot-registry/tree/main/pkgs/ssot-pack-contracts
|
|
9
|
+
Project-URL: Issues, https://github.com/groupsum/ssot-registry/issues
|
|
10
|
+
Keywords: ssot,governance,packs,contracts,metadata,manifest
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
|
+
Requires-Python: <3.15,>=3.10
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
Requires-Dist: tomli>=2.0.1; python_version < "3.11"
|
|
24
|
+
|
|
25
|
+
# ssot-pack-contracts
|
|
26
|
+
|
|
27
|
+
Shared Python contracts for installable SSOT governance packs.
|
|
28
|
+
|
|
29
|
+
The package validates pack metadata, document manifests, and packaged document resources without depending on `ssot-core`, `ssot-cli`, or any repository mutation layer.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=69", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "ssot-pack-contracts"
|
|
7
|
+
version = "0.2.18"
|
|
8
|
+
description = "Shared governance-pack metadata, manifest, and packaged document contracts for SSOT."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10,<3.15"
|
|
11
|
+
license = "Apache-2.0"
|
|
12
|
+
authors = [{ name = "Jacob Stewart", email = "jacob@swarmauri.com" }]
|
|
13
|
+
dependencies = [
|
|
14
|
+
"tomli>=2.0.1; python_version < '3.11'",
|
|
15
|
+
]
|
|
16
|
+
keywords = ["ssot", "governance", "packs", "contracts", "metadata", "manifest"]
|
|
17
|
+
classifiers = [
|
|
18
|
+
"Development Status :: 3 - Alpha",
|
|
19
|
+
"Intended Audience :: Developers",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
22
|
+
"Programming Language :: Python :: 3.10",
|
|
23
|
+
"Programming Language :: Python :: 3.11",
|
|
24
|
+
"Programming Language :: Python :: 3.12",
|
|
25
|
+
"Programming Language :: Python :: 3.13",
|
|
26
|
+
"Programming Language :: Python :: 3.14",
|
|
27
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
Homepage = "https://github.com/groupsum/ssot-registry/tree/main/pkgs/ssot-pack-contracts"
|
|
32
|
+
Repository = "https://github.com/groupsum/ssot-registry/tree/main/pkgs/ssot-pack-contracts"
|
|
33
|
+
Issues = "https://github.com/groupsum/ssot-registry/issues"
|
|
34
|
+
|
|
35
|
+
[tool.setuptools]
|
|
36
|
+
package-dir = {"" = "src"}
|
|
37
|
+
|
|
38
|
+
[tool.setuptools.packages.find]
|
|
39
|
+
where = ["src"]
|
|
40
|
+
|
|
41
|
+
[tool.setuptools.package-data]
|
|
42
|
+
"ssot_pack_contracts" = ["py.typed"]
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from importlib.metadata import PackageNotFoundError, version as package_version
|
|
4
|
+
|
|
5
|
+
from .api import (
|
|
6
|
+
InvalidPackManifestError,
|
|
7
|
+
InvalidPackMetadataError,
|
|
8
|
+
PackContractError,
|
|
9
|
+
PackDocumentEntry,
|
|
10
|
+
PackMetadata,
|
|
11
|
+
UnsupportedDocumentKindError,
|
|
12
|
+
bind_pack_contract,
|
|
13
|
+
list_packaged_document_ids,
|
|
14
|
+
get_packaged_document_entry,
|
|
15
|
+
load_document_manifest,
|
|
16
|
+
load_pack_manifest,
|
|
17
|
+
load_pack_metadata,
|
|
18
|
+
load_pack_schema_version,
|
|
19
|
+
normalize_document_kind,
|
|
20
|
+
read_packaged_document_bytes,
|
|
21
|
+
read_packaged_document_text,
|
|
22
|
+
validate_pack_document_entry,
|
|
23
|
+
validate_pack_metadata,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
__ssot_package_name__ = "ssot-pack-contracts"
|
|
27
|
+
|
|
28
|
+
try:
|
|
29
|
+
__version__ = package_version(__ssot_package_name__)
|
|
30
|
+
except PackageNotFoundError:
|
|
31
|
+
__version__ = "0.0.0"
|
|
32
|
+
|
|
33
|
+
__all__ = [
|
|
34
|
+
"__version__",
|
|
35
|
+
"__ssot_package_name__",
|
|
36
|
+
"PackMetadata",
|
|
37
|
+
"PackDocumentEntry",
|
|
38
|
+
"PackContractError",
|
|
39
|
+
"UnsupportedDocumentKindError",
|
|
40
|
+
"InvalidPackMetadataError",
|
|
41
|
+
"InvalidPackManifestError",
|
|
42
|
+
"bind_pack_contract",
|
|
43
|
+
"load_pack_metadata",
|
|
44
|
+
"load_pack_schema_version",
|
|
45
|
+
"load_pack_manifest",
|
|
46
|
+
"load_document_manifest",
|
|
47
|
+
"read_packaged_document_bytes",
|
|
48
|
+
"read_packaged_document_text",
|
|
49
|
+
"validate_pack_metadata",
|
|
50
|
+
"validate_pack_document_entry",
|
|
51
|
+
"normalize_document_kind",
|
|
52
|
+
"list_packaged_document_ids",
|
|
53
|
+
"get_packaged_document_entry",
|
|
54
|
+
]
|
|
@@ -0,0 +1,454 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import hashlib
|
|
4
|
+
import importlib
|
|
5
|
+
import json
|
|
6
|
+
import re
|
|
7
|
+
from dataclasses import dataclass
|
|
8
|
+
from importlib.metadata import PackageNotFoundError
|
|
9
|
+
from importlib.metadata import metadata as distribution_metadata
|
|
10
|
+
from importlib.metadata import packages_distributions
|
|
11
|
+
from importlib.metadata import version as distribution_version
|
|
12
|
+
from importlib import resources
|
|
13
|
+
from types import ModuleType
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
METADATA_RESOURCE = "metadata.json"
|
|
18
|
+
SUPPORTED_DOCUMENT_KINDS = {"adr": "adr", "adrs": "adr", "spec": "spec", "specs": "spec"}
|
|
19
|
+
SHA256_PATTERN = re.compile(r"^[a-f0-9]{64}$")
|
|
20
|
+
PACK_ID_PATTERN = re.compile(r"^pack:[a-z0-9][a-z0-9._-]*$")
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class PackContractError(ValueError):
|
|
24
|
+
"""Base exception for governance-pack contract failures."""
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class UnsupportedDocumentKindError(PackContractError):
|
|
28
|
+
"""Raised when a caller requests a document kind outside the pack contract."""
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class InvalidPackMetadataError(PackContractError):
|
|
32
|
+
"""Raised when packaged governance-pack metadata is missing or invalid."""
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class InvalidPackManifestError(PackContractError):
|
|
36
|
+
"""Raised when a packaged document manifest or resource is invalid."""
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@dataclass(frozen=True)
|
|
40
|
+
class PackMetadata:
|
|
41
|
+
schema_version: str
|
|
42
|
+
ssot_package_name: str
|
|
43
|
+
pypi_package_name: str
|
|
44
|
+
origin: dict[str, Any]
|
|
45
|
+
compatibility: dict[str, Any]
|
|
46
|
+
trust: dict[str, Any]
|
|
47
|
+
documents: dict[str, Any]
|
|
48
|
+
version: str
|
|
49
|
+
|
|
50
|
+
def as_dict(self) -> dict[str, Any]:
|
|
51
|
+
return {
|
|
52
|
+
"schema_version": self.schema_version,
|
|
53
|
+
"ssot_package_name": self.ssot_package_name,
|
|
54
|
+
"pypi_package_name": self.pypi_package_name,
|
|
55
|
+
"origin": dict(self.origin),
|
|
56
|
+
"compatibility": dict(self.compatibility),
|
|
57
|
+
"trust": dict(self.trust),
|
|
58
|
+
"documents": dict(self.documents),
|
|
59
|
+
"version": self.version,
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@dataclass(frozen=True)
|
|
64
|
+
class PackDocumentEntry:
|
|
65
|
+
id: str
|
|
66
|
+
number: int
|
|
67
|
+
slug: str
|
|
68
|
+
title: str
|
|
69
|
+
filename: str
|
|
70
|
+
target_path: str
|
|
71
|
+
sha256: str
|
|
72
|
+
origin: str
|
|
73
|
+
reservation_owner: str
|
|
74
|
+
immutable: bool
|
|
75
|
+
minimum_schema_version: str
|
|
76
|
+
introduced_in: str
|
|
77
|
+
status: str
|
|
78
|
+
supersedes: list[str]
|
|
79
|
+
superseded_by: list[str]
|
|
80
|
+
status_notes: list[dict[str, Any]]
|
|
81
|
+
kind: str | None = None
|
|
82
|
+
adr_ids: list[str] | None = None
|
|
83
|
+
|
|
84
|
+
def as_dict(self) -> dict[str, Any]:
|
|
85
|
+
payload: dict[str, Any] = {
|
|
86
|
+
"id": self.id,
|
|
87
|
+
"number": self.number,
|
|
88
|
+
"slug": self.slug,
|
|
89
|
+
"title": self.title,
|
|
90
|
+
"filename": self.filename,
|
|
91
|
+
"target_path": self.target_path,
|
|
92
|
+
"sha256": self.sha256,
|
|
93
|
+
"origin": self.origin,
|
|
94
|
+
"reservation_owner": self.reservation_owner,
|
|
95
|
+
"immutable": self.immutable,
|
|
96
|
+
"minimum_schema_version": self.minimum_schema_version,
|
|
97
|
+
"introduced_in": self.introduced_in,
|
|
98
|
+
"status": self.status,
|
|
99
|
+
"supersedes": list(self.supersedes),
|
|
100
|
+
"superseded_by": list(self.superseded_by),
|
|
101
|
+
"status_notes": [dict(row) for row in self.status_notes],
|
|
102
|
+
}
|
|
103
|
+
if self.kind is not None:
|
|
104
|
+
payload["kind"] = self.kind
|
|
105
|
+
if self.adr_ids is not None:
|
|
106
|
+
payload["adr_ids"] = list(self.adr_ids)
|
|
107
|
+
return payload
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _module(package: str | ModuleType | None) -> ModuleType:
|
|
111
|
+
if package is None:
|
|
112
|
+
raise InvalidPackMetadataError("A governance pack package name or module is required")
|
|
113
|
+
if isinstance(package, ModuleType):
|
|
114
|
+
return package
|
|
115
|
+
return importlib.import_module(package)
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _resource_text(module: ModuleType, resource_name: str) -> str:
|
|
119
|
+
resource = resources.files(module).joinpath(resource_name)
|
|
120
|
+
if not resource.is_file():
|
|
121
|
+
raise InvalidPackMetadataError(f"Governance pack {module.__name__} is missing packaged {resource_name}")
|
|
122
|
+
return resource.read_text(encoding="utf-8")
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _normalized_distribution_name(value: str) -> str:
|
|
126
|
+
return re.sub(r"[-_.]+", "-", value).lower()
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def _distribution_identity_for_module(module: ModuleType, *, expected_name: str | None = None) -> tuple[str, str]:
|
|
130
|
+
top_level = module.__name__.split(".", 1)[0]
|
|
131
|
+
dist_names = packages_distributions().get(top_level, [])
|
|
132
|
+
if not dist_names:
|
|
133
|
+
raise InvalidPackMetadataError(f"No installed distribution found for import package {top_level}")
|
|
134
|
+
|
|
135
|
+
candidates: list[tuple[str, str]] = []
|
|
136
|
+
for dist_name in dist_names:
|
|
137
|
+
try:
|
|
138
|
+
resolved_name = distribution_metadata(dist_name)["Name"]
|
|
139
|
+
resolved_version = distribution_version(dist_name)
|
|
140
|
+
except (KeyError, PackageNotFoundError) as exc:
|
|
141
|
+
raise InvalidPackMetadataError(f"Distribution metadata for {dist_name} is not readable") from exc
|
|
142
|
+
candidates.append((resolved_name, resolved_version))
|
|
143
|
+
|
|
144
|
+
if expected_name is not None:
|
|
145
|
+
normalized_expected = _normalized_distribution_name(expected_name)
|
|
146
|
+
matches = [candidate for candidate in candidates if _normalized_distribution_name(candidate[0]) == normalized_expected]
|
|
147
|
+
if len(matches) == 1:
|
|
148
|
+
return matches[0]
|
|
149
|
+
if len(matches) > 1:
|
|
150
|
+
unique_matches = sorted(set(matches))
|
|
151
|
+
if len(unique_matches) == 1:
|
|
152
|
+
return unique_matches[0]
|
|
153
|
+
raise InvalidPackMetadataError(f"Multiple installed distributions match expected package name {expected_name}")
|
|
154
|
+
|
|
155
|
+
if len(candidates) == 1:
|
|
156
|
+
return candidates[0]
|
|
157
|
+
unique_candidates = sorted(set(candidates))
|
|
158
|
+
if len(unique_candidates) == 1:
|
|
159
|
+
return unique_candidates[0]
|
|
160
|
+
names = ", ".join(name for name, _ in candidates)
|
|
161
|
+
raise InvalidPackMetadataError(f"Import package {top_level} maps to multiple installed distributions: {names}")
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def _json_object(text: str, *, label: str) -> dict[str, Any]:
|
|
165
|
+
try:
|
|
166
|
+
payload = json.loads(text)
|
|
167
|
+
except json.JSONDecodeError as exc:
|
|
168
|
+
raise InvalidPackMetadataError(f"{label} must be valid JSON: {exc.msg}") from exc
|
|
169
|
+
if not isinstance(payload, dict):
|
|
170
|
+
raise InvalidPackMetadataError(f"{label} must decode to a JSON object")
|
|
171
|
+
return payload
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def normalize_document_kind(kind: str) -> str:
|
|
175
|
+
try:
|
|
176
|
+
return SUPPORTED_DOCUMENT_KINDS[kind]
|
|
177
|
+
except KeyError as exc:
|
|
178
|
+
raise UnsupportedDocumentKindError(f"Unsupported governance-pack document kind: {kind}") from exc
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def validate_pack_metadata(
|
|
182
|
+
metadata: dict[str, Any],
|
|
183
|
+
*,
|
|
184
|
+
import_name: str | None = None,
|
|
185
|
+
pypi_package_name: str | None = None,
|
|
186
|
+
version: str | None = None,
|
|
187
|
+
) -> PackMetadata:
|
|
188
|
+
schema_version = metadata.get("schema_version")
|
|
189
|
+
if not isinstance(schema_version, str) or not schema_version.strip():
|
|
190
|
+
raise InvalidPackMetadataError("metadata.schema_version is required")
|
|
191
|
+
ssot_package_name = metadata.get("ssot_package_name")
|
|
192
|
+
if not isinstance(ssot_package_name, str) or not ssot_package_name.strip():
|
|
193
|
+
raise InvalidPackMetadataError("metadata.ssot_package_name is required")
|
|
194
|
+
origin = metadata.get("origin")
|
|
195
|
+
compatibility = metadata.get("compatibility")
|
|
196
|
+
trust = metadata.get("trust")
|
|
197
|
+
documents = metadata.get("documents")
|
|
198
|
+
if not isinstance(origin, dict):
|
|
199
|
+
raise InvalidPackMetadataError("metadata.origin must be an object")
|
|
200
|
+
if not isinstance(compatibility, dict):
|
|
201
|
+
raise InvalidPackMetadataError("metadata.compatibility must be an object")
|
|
202
|
+
if not isinstance(trust, dict):
|
|
203
|
+
raise InvalidPackMetadataError("metadata.trust must be an object")
|
|
204
|
+
if not isinstance(documents, dict):
|
|
205
|
+
raise InvalidPackMetadataError("metadata.documents must be an object")
|
|
206
|
+
origin_id = origin.get("id")
|
|
207
|
+
if not isinstance(origin_id, str) or PACK_ID_PATTERN.match(origin_id) is None:
|
|
208
|
+
raise InvalidPackMetadataError("metadata.origin.id must be a normalized pack:* identifier")
|
|
209
|
+
package_name = origin.get("package_name")
|
|
210
|
+
if not isinstance(package_name, str) or not package_name.strip():
|
|
211
|
+
raise InvalidPackMetadataError("metadata.origin.package_name is required")
|
|
212
|
+
if package_name != ssot_package_name:
|
|
213
|
+
raise InvalidPackMetadataError("metadata.origin.package_name must match metadata.ssot_package_name")
|
|
214
|
+
metadata_import_name = origin.get("import_name")
|
|
215
|
+
if not isinstance(metadata_import_name, str) or not metadata_import_name.strip():
|
|
216
|
+
raise InvalidPackMetadataError("metadata.origin.import_name is required")
|
|
217
|
+
if import_name is not None and metadata_import_name != import_name:
|
|
218
|
+
raise InvalidPackMetadataError(f"metadata.origin.import_name must be {import_name}, got {metadata_import_name}")
|
|
219
|
+
if origin.get("kind") != "governance-pack":
|
|
220
|
+
raise InvalidPackMetadataError("metadata.origin.kind must be governance-pack")
|
|
221
|
+
if trust.get("origin") != "extension-pack":
|
|
222
|
+
raise InvalidPackMetadataError("metadata.trust.origin must be extension-pack")
|
|
223
|
+
if "trusted_by_default" not in trust or not isinstance(trust.get("trusted_by_default"), bool):
|
|
224
|
+
raise InvalidPackMetadataError("metadata.trust.trusted_by_default must be a boolean")
|
|
225
|
+
reservation_owner = trust.get("reservation_owner")
|
|
226
|
+
if not isinstance(reservation_owner, str) or not reservation_owner.startswith("extension-pack:"):
|
|
227
|
+
raise InvalidPackMetadataError("metadata.trust.reservation_owner must start with extension-pack:")
|
|
228
|
+
for key in ("python", "ssot_registry_schema", "ssot_pack_contract"):
|
|
229
|
+
if not isinstance(compatibility.get(key), str) or not compatibility[key].strip():
|
|
230
|
+
raise InvalidPackMetadataError(f"metadata.compatibility.{key} is required")
|
|
231
|
+
for raw_kind, value in documents.items():
|
|
232
|
+
kind = normalize_document_kind(str(raw_kind))
|
|
233
|
+
if kind != raw_kind:
|
|
234
|
+
raise InvalidPackMetadataError(f"metadata.documents must use normalized kind key {kind}, got {raw_kind}")
|
|
235
|
+
if not isinstance(value, dict) or not isinstance(value.get("manifest_path"), str) or not value["manifest_path"].strip():
|
|
236
|
+
raise InvalidPackMetadataError(f"metadata.documents.{kind}.manifest_path is required")
|
|
237
|
+
return PackMetadata(
|
|
238
|
+
schema_version=schema_version,
|
|
239
|
+
ssot_package_name=ssot_package_name,
|
|
240
|
+
pypi_package_name=pypi_package_name or ssot_package_name,
|
|
241
|
+
origin=dict(origin),
|
|
242
|
+
compatibility=dict(compatibility),
|
|
243
|
+
trust=dict(trust),
|
|
244
|
+
documents=dict(documents),
|
|
245
|
+
version=version or "",
|
|
246
|
+
)
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def load_pack_metadata(package: str | ModuleType) -> dict[str, Any]:
|
|
250
|
+
module = _module(package)
|
|
251
|
+
metadata = _json_object(_resource_text(module, METADATA_RESOURCE), label=METADATA_RESOURCE)
|
|
252
|
+
ssot_package_name = metadata.get("ssot_package_name")
|
|
253
|
+
pypi_package_name, version = _distribution_identity_for_module(
|
|
254
|
+
module,
|
|
255
|
+
expected_name=ssot_package_name if isinstance(ssot_package_name, str) else None,
|
|
256
|
+
)
|
|
257
|
+
validated = validate_pack_metadata(
|
|
258
|
+
metadata,
|
|
259
|
+
import_name=module.__name__,
|
|
260
|
+
pypi_package_name=pypi_package_name,
|
|
261
|
+
version=version,
|
|
262
|
+
)
|
|
263
|
+
return validated.as_dict()
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
def load_pack_schema_version(package: str | ModuleType) -> str:
|
|
267
|
+
return load_pack_metadata(package)["schema_version"]
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def _manifest_path(metadata: dict[str, Any], kind: str) -> str:
|
|
271
|
+
documents = metadata.get("documents")
|
|
272
|
+
if not isinstance(documents, dict) or kind not in documents:
|
|
273
|
+
raise InvalidPackManifestError(f"Governance pack does not declare a {kind} document manifest")
|
|
274
|
+
declaration = documents[kind]
|
|
275
|
+
if not isinstance(declaration, dict) or not isinstance(declaration.get("manifest_path"), str):
|
|
276
|
+
raise InvalidPackManifestError(f"Governance pack {kind} manifest_path is invalid")
|
|
277
|
+
return declaration["manifest_path"]
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def validate_pack_document_entry(entry: dict[str, Any], *, kind: str) -> PackDocumentEntry:
|
|
281
|
+
normalized_kind = normalize_document_kind(kind)
|
|
282
|
+
required = [
|
|
283
|
+
"id",
|
|
284
|
+
"number",
|
|
285
|
+
"slug",
|
|
286
|
+
"title",
|
|
287
|
+
"filename",
|
|
288
|
+
"target_path",
|
|
289
|
+
"sha256",
|
|
290
|
+
"origin",
|
|
291
|
+
"reservation_owner",
|
|
292
|
+
"immutable",
|
|
293
|
+
"minimum_schema_version",
|
|
294
|
+
"introduced_in",
|
|
295
|
+
"status",
|
|
296
|
+
"supersedes",
|
|
297
|
+
"superseded_by",
|
|
298
|
+
"status_notes",
|
|
299
|
+
]
|
|
300
|
+
missing = [field for field in required if field not in entry]
|
|
301
|
+
if missing:
|
|
302
|
+
raise InvalidPackManifestError(f"{normalized_kind} manifest entry missing required fields: {', '.join(missing)}")
|
|
303
|
+
if normalized_kind == "spec" and "kind" not in entry:
|
|
304
|
+
raise InvalidPackManifestError("spec manifest entry missing required field: kind")
|
|
305
|
+
if not isinstance(entry["id"], str) or not entry["id"].startswith("adr:" if normalized_kind == "adr" else "spc:"):
|
|
306
|
+
raise InvalidPackManifestError(f"{normalized_kind} manifest entry id has the wrong prefix")
|
|
307
|
+
if not isinstance(entry["number"], int):
|
|
308
|
+
raise InvalidPackManifestError(f"{entry.get('id', normalized_kind)} number must be an integer")
|
|
309
|
+
if not isinstance(entry["sha256"], str) or SHA256_PATTERN.match(entry["sha256"]) is None:
|
|
310
|
+
raise InvalidPackManifestError(f"{entry.get('id', normalized_kind)} sha256 must be lowercase hex")
|
|
311
|
+
for field in ("slug", "title", "filename", "target_path", "origin", "reservation_owner", "minimum_schema_version", "introduced_in", "status"):
|
|
312
|
+
if not isinstance(entry[field], str) or not entry[field].strip():
|
|
313
|
+
raise InvalidPackManifestError(f"{entry.get('id', normalized_kind)} {field} must be a non-empty string")
|
|
314
|
+
if not isinstance(entry["immutable"], bool):
|
|
315
|
+
raise InvalidPackManifestError(f"{entry['id']} immutable must be a boolean")
|
|
316
|
+
for field in ("supersedes", "superseded_by", "status_notes"):
|
|
317
|
+
if not isinstance(entry[field], list):
|
|
318
|
+
raise InvalidPackManifestError(f"{entry['id']} {field} must be a list")
|
|
319
|
+
return PackDocumentEntry(
|
|
320
|
+
id=entry["id"],
|
|
321
|
+
number=entry["number"],
|
|
322
|
+
slug=entry["slug"],
|
|
323
|
+
title=entry["title"],
|
|
324
|
+
filename=entry["filename"],
|
|
325
|
+
target_path=entry["target_path"],
|
|
326
|
+
sha256=entry["sha256"],
|
|
327
|
+
origin=entry["origin"],
|
|
328
|
+
reservation_owner=entry["reservation_owner"],
|
|
329
|
+
immutable=entry["immutable"],
|
|
330
|
+
minimum_schema_version=entry["minimum_schema_version"],
|
|
331
|
+
introduced_in=entry["introduced_in"],
|
|
332
|
+
status=entry["status"],
|
|
333
|
+
supersedes=list(entry["supersedes"]),
|
|
334
|
+
superseded_by=list(entry["superseded_by"]),
|
|
335
|
+
status_notes=list(entry["status_notes"]),
|
|
336
|
+
kind=entry.get("kind") if normalized_kind == "spec" else None,
|
|
337
|
+
adr_ids=list(entry.get("adr_ids", [])) if normalized_kind == "spec" else None,
|
|
338
|
+
)
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
def load_document_manifest(package: str | ModuleType, kind: str) -> list[dict[str, Any]]:
|
|
342
|
+
module = _module(package)
|
|
343
|
+
normalized_kind = normalize_document_kind(kind)
|
|
344
|
+
metadata = load_pack_metadata(module)
|
|
345
|
+
manifest_path = _manifest_path(metadata, normalized_kind)
|
|
346
|
+
resource = resources.files(module).joinpath(manifest_path)
|
|
347
|
+
if not resource.is_file():
|
|
348
|
+
raise InvalidPackManifestError(f"Governance pack {module.__name__} is missing manifest {manifest_path}")
|
|
349
|
+
try:
|
|
350
|
+
manifest = json.loads(resource.read_text(encoding="utf-8"))
|
|
351
|
+
except json.JSONDecodeError as exc:
|
|
352
|
+
raise InvalidPackManifestError(f"{manifest_path} must be valid JSON: {exc.msg}") from exc
|
|
353
|
+
if not isinstance(manifest, list):
|
|
354
|
+
raise InvalidPackManifestError(f"{manifest_path} must decode to a JSON array")
|
|
355
|
+
return [validate_pack_document_entry(row, kind=normalized_kind).as_dict() for row in manifest]
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
def load_pack_manifest(package: str | ModuleType) -> dict[str, Any]:
|
|
359
|
+
module = _module(package)
|
|
360
|
+
metadata = load_pack_metadata(module)
|
|
361
|
+
manifests: dict[str, list[dict[str, Any]]] = {}
|
|
362
|
+
for kind in sorted(metadata["documents"]):
|
|
363
|
+
manifests[kind] = load_document_manifest(module, kind)
|
|
364
|
+
return {"metadata": metadata, "documents": manifests}
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
def _entry_for_filename(package: str | ModuleType, kind: str, filename: str) -> dict[str, Any]:
|
|
368
|
+
normalized_kind = normalize_document_kind(kind)
|
|
369
|
+
for entry in load_document_manifest(package, normalized_kind):
|
|
370
|
+
if entry["filename"] == filename:
|
|
371
|
+
return entry
|
|
372
|
+
raise InvalidPackManifestError(f"{normalized_kind} manifest does not declare document file {filename}")
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
def read_packaged_document_bytes(package: str | ModuleType, kind: str, filename: str) -> bytes:
|
|
376
|
+
module = _module(package)
|
|
377
|
+
normalized_kind = normalize_document_kind(kind)
|
|
378
|
+
entry = _entry_for_filename(module, normalized_kind, filename)
|
|
379
|
+
manifest_path = _manifest_path(load_pack_metadata(module), normalized_kind)
|
|
380
|
+
resource = resources.files(module).joinpath(manifest_path).parent.joinpath(filename)
|
|
381
|
+
if not resource.is_file():
|
|
382
|
+
raise InvalidPackManifestError(f"Governance pack {module.__name__} is missing document resource {filename}")
|
|
383
|
+
payload = resource.read_bytes()
|
|
384
|
+
actual = hashlib.sha256(payload).hexdigest()
|
|
385
|
+
if actual != entry["sha256"]:
|
|
386
|
+
raise InvalidPackManifestError(f"{entry['id']} sha256 mismatch: expected {entry['sha256']}, got {actual}")
|
|
387
|
+
return payload
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
def read_packaged_document_text(package: str | ModuleType, kind: str, filename: str) -> str:
|
|
391
|
+
return read_packaged_document_bytes(package, kind, filename).decode("utf-8-sig")
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
def list_packaged_document_ids(package: str | ModuleType, kind: str | None = None) -> list[str]:
|
|
395
|
+
if kind is not None:
|
|
396
|
+
return [entry["id"] for entry in load_document_manifest(package, kind)]
|
|
397
|
+
manifest = load_pack_manifest(package)
|
|
398
|
+
ids: list[str] = []
|
|
399
|
+
for entries in manifest["documents"].values():
|
|
400
|
+
ids.extend(entry["id"] for entry in entries)
|
|
401
|
+
return sorted(ids)
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
def get_packaged_document_entry(package: str | ModuleType, document_id: str) -> dict[str, Any]:
|
|
405
|
+
for kind in ("adr", "spec"):
|
|
406
|
+
for entry in load_document_manifest(package, kind):
|
|
407
|
+
if entry["id"] == document_id:
|
|
408
|
+
return entry
|
|
409
|
+
raise InvalidPackManifestError(f"Unknown packaged document id: {document_id}")
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
def bind_pack_contract(package: str | ModuleType) -> dict[str, Any]:
|
|
413
|
+
module = _module(package)
|
|
414
|
+
metadata = load_pack_metadata(module)
|
|
415
|
+
|
|
416
|
+
def bound_load_pack_metadata() -> dict[str, Any]:
|
|
417
|
+
return load_pack_metadata(module)
|
|
418
|
+
|
|
419
|
+
def bound_load_pack_schema_version() -> str:
|
|
420
|
+
return load_pack_schema_version(module)
|
|
421
|
+
|
|
422
|
+
def bound_load_pack_manifest() -> dict[str, Any]:
|
|
423
|
+
return load_pack_manifest(module)
|
|
424
|
+
|
|
425
|
+
def bound_load_document_manifest(kind: str) -> list[dict[str, Any]]:
|
|
426
|
+
return load_document_manifest(module, kind)
|
|
427
|
+
|
|
428
|
+
def bound_read_packaged_document_bytes(kind: str, filename: str) -> bytes:
|
|
429
|
+
return read_packaged_document_bytes(module, kind, filename)
|
|
430
|
+
|
|
431
|
+
def bound_read_packaged_document_text(kind: str, filename: str) -> str:
|
|
432
|
+
return read_packaged_document_text(module, kind, filename)
|
|
433
|
+
|
|
434
|
+
def bound_list_packaged_document_ids(kind: str | None = None) -> list[str]:
|
|
435
|
+
return list_packaged_document_ids(module, kind)
|
|
436
|
+
|
|
437
|
+
def bound_get_packaged_document_entry(document_id: str) -> dict[str, Any]:
|
|
438
|
+
return get_packaged_document_entry(module, document_id)
|
|
439
|
+
|
|
440
|
+
exported = {
|
|
441
|
+
"__version__": metadata["version"],
|
|
442
|
+
"__ssot_package_name__": metadata["ssot_package_name"],
|
|
443
|
+
"__pypi_package_name__": metadata["pypi_package_name"],
|
|
444
|
+
"load_pack_metadata": bound_load_pack_metadata,
|
|
445
|
+
"load_pack_schema_version": bound_load_pack_schema_version,
|
|
446
|
+
"load_pack_manifest": bound_load_pack_manifest,
|
|
447
|
+
"load_document_manifest": bound_load_document_manifest,
|
|
448
|
+
"read_packaged_document_bytes": bound_read_packaged_document_bytes,
|
|
449
|
+
"read_packaged_document_text": bound_read_packaged_document_text,
|
|
450
|
+
"list_packaged_document_ids": bound_list_packaged_document_ids,
|
|
451
|
+
"get_packaged_document_entry": bound_get_packaged_document_entry,
|
|
452
|
+
}
|
|
453
|
+
exported["__all__"] = sorted(exported.keys())
|
|
454
|
+
return exported
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ssot-pack-contracts
|
|
3
|
+
Version: 0.2.18
|
|
4
|
+
Summary: Shared governance-pack metadata, manifest, and packaged document contracts for SSOT.
|
|
5
|
+
Author-email: Jacob Stewart <jacob@swarmauri.com>
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Homepage, https://github.com/groupsum/ssot-registry/tree/main/pkgs/ssot-pack-contracts
|
|
8
|
+
Project-URL: Repository, https://github.com/groupsum/ssot-registry/tree/main/pkgs/ssot-pack-contracts
|
|
9
|
+
Project-URL: Issues, https://github.com/groupsum/ssot-registry/issues
|
|
10
|
+
Keywords: ssot,governance,packs,contracts,metadata,manifest
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
20
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
21
|
+
Requires-Python: <3.15,>=3.10
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
Requires-Dist: tomli>=2.0.1; python_version < "3.11"
|
|
24
|
+
|
|
25
|
+
# ssot-pack-contracts
|
|
26
|
+
|
|
27
|
+
Shared Python contracts for installable SSOT governance packs.
|
|
28
|
+
|
|
29
|
+
The package validates pack metadata, document manifests, and packaged document resources without depending on `ssot-core`, `ssot-cli`, or any repository mutation layer.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
src/ssot_pack_contracts/__init__.py
|
|
4
|
+
src/ssot_pack_contracts/api.py
|
|
5
|
+
src/ssot_pack_contracts/py.typed
|
|
6
|
+
src/ssot_pack_contracts.egg-info/PKG-INFO
|
|
7
|
+
src/ssot_pack_contracts.egg-info/SOURCES.txt
|
|
8
|
+
src/ssot_pack_contracts.egg-info/dependency_links.txt
|
|
9
|
+
src/ssot_pack_contracts.egg-info/requires.txt
|
|
10
|
+
src/ssot_pack_contracts.egg-info/top_level.txt
|
|
11
|
+
tests/test_pack_contracts.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ssot_pack_contracts
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import hashlib
|
|
4
|
+
import importlib
|
|
5
|
+
import json
|
|
6
|
+
import sys
|
|
7
|
+
import unittest
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from ssot_pack_contracts import (
|
|
11
|
+
InvalidPackManifestError,
|
|
12
|
+
InvalidPackMetadataError,
|
|
13
|
+
UnsupportedDocumentKindError,
|
|
14
|
+
bind_pack_contract,
|
|
15
|
+
get_packaged_document_entry,
|
|
16
|
+
list_packaged_document_ids,
|
|
17
|
+
load_document_manifest,
|
|
18
|
+
load_pack_manifest,
|
|
19
|
+
load_pack_metadata,
|
|
20
|
+
load_pack_schema_version,
|
|
21
|
+
normalize_document_kind,
|
|
22
|
+
read_packaged_document_text,
|
|
23
|
+
)
|
|
24
|
+
from tests.helpers import workspace_tempdir
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _write_dist_info(root: Path, *, dist_name: str, version: str, top_level: str) -> None:
|
|
28
|
+
dist_info = root / f"{dist_name.replace('-', '_')}-{version}.dist-info"
|
|
29
|
+
dist_info.mkdir()
|
|
30
|
+
(dist_info / "METADATA").write_text(
|
|
31
|
+
f"Metadata-Version: 2.1\nName: {dist_name}\nVersion: {version}\n",
|
|
32
|
+
encoding="utf-8",
|
|
33
|
+
)
|
|
34
|
+
(dist_info / "top_level.txt").write_text(f"{top_level}\n", encoding="utf-8")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def _write_pack(root: Path, package_name: str = "example_governance_pack", dist_name: str = "example-governance-pack") -> str:
|
|
38
|
+
package_root = root / package_name
|
|
39
|
+
docs_root = package_root / "templates" / "adr"
|
|
40
|
+
docs_root.mkdir(parents=True)
|
|
41
|
+
(package_root / "__init__.py").write_text("", encoding="utf-8")
|
|
42
|
+
_write_dist_info(root, dist_name=dist_name, version="1.2.3", top_level=package_name)
|
|
43
|
+
document = (
|
|
44
|
+
'schema_version: "0.4.0"\n'
|
|
45
|
+
'kind: "adr"\n'
|
|
46
|
+
'id: "adr:5000"\n'
|
|
47
|
+
"number: 5000\n"
|
|
48
|
+
'slug: "example-pack-adr"\n'
|
|
49
|
+
'title: "Example Pack ADR"\n'
|
|
50
|
+
'status: "draft"\n'
|
|
51
|
+
'origin: "extension-pack"\n'
|
|
52
|
+
"decision_date: null\n"
|
|
53
|
+
"tags: []\n"
|
|
54
|
+
'summary: "Example Pack ADR"\n'
|
|
55
|
+
"supersedes: []\n"
|
|
56
|
+
"superseded_by: []\n"
|
|
57
|
+
"status_notes: []\n"
|
|
58
|
+
"references: []\n"
|
|
59
|
+
"body: |-\n"
|
|
60
|
+
" Example packaged ADR.\n"
|
|
61
|
+
)
|
|
62
|
+
document_path = docs_root / "ADR-5000-example-pack-adr.yaml"
|
|
63
|
+
document_path.write_text(document, encoding="utf-8", newline="\n")
|
|
64
|
+
sha256 = hashlib.sha256(document_path.read_bytes()).hexdigest()
|
|
65
|
+
(docs_root / "manifest.json").write_text(
|
|
66
|
+
json.dumps(
|
|
67
|
+
[
|
|
68
|
+
{
|
|
69
|
+
"id": "adr:5000",
|
|
70
|
+
"number": 5000,
|
|
71
|
+
"slug": "example-pack-adr",
|
|
72
|
+
"title": "Example Pack ADR",
|
|
73
|
+
"filename": "ADR-5000-example-pack-adr.yaml",
|
|
74
|
+
"target_path": ".ssot/adr/ADR-5000-example-pack-adr.yaml",
|
|
75
|
+
"sha256": sha256,
|
|
76
|
+
"origin": "extension-pack",
|
|
77
|
+
"reservation_owner": "extension-pack:example-governance-pack",
|
|
78
|
+
"immutable": True,
|
|
79
|
+
"minimum_schema_version": "0.4.0",
|
|
80
|
+
"introduced_in": "1.2.3",
|
|
81
|
+
"status": "draft",
|
|
82
|
+
"supersedes": [],
|
|
83
|
+
"superseded_by": [],
|
|
84
|
+
"status_notes": [],
|
|
85
|
+
}
|
|
86
|
+
]
|
|
87
|
+
),
|
|
88
|
+
encoding="utf-8",
|
|
89
|
+
)
|
|
90
|
+
(package_root / "metadata.json").write_text(
|
|
91
|
+
json.dumps(
|
|
92
|
+
{
|
|
93
|
+
"schema_version": "1.0.0",
|
|
94
|
+
"ssot_package_name": dist_name,
|
|
95
|
+
"origin": {
|
|
96
|
+
"id": "pack:example-governance",
|
|
97
|
+
"package_name": dist_name,
|
|
98
|
+
"import_name": package_name,
|
|
99
|
+
"kind": "governance-pack",
|
|
100
|
+
"title": "Example Governance Pack",
|
|
101
|
+
"description": "Installable SSOT governance pack.",
|
|
102
|
+
},
|
|
103
|
+
"compatibility": {
|
|
104
|
+
"python": ">=3.10,<3.15",
|
|
105
|
+
"ssot_registry_schema": ">=0.4.0",
|
|
106
|
+
"ssot_pack_contract": ">=1.0.0",
|
|
107
|
+
},
|
|
108
|
+
"trust": {
|
|
109
|
+
"trusted_by_default": True,
|
|
110
|
+
"origin": "extension-pack",
|
|
111
|
+
"reservation_owner": "extension-pack:example-governance-pack",
|
|
112
|
+
},
|
|
113
|
+
"documents": {"adr": {"manifest_path": "templates/adr/manifest.json"}},
|
|
114
|
+
}
|
|
115
|
+
),
|
|
116
|
+
encoding="utf-8",
|
|
117
|
+
)
|
|
118
|
+
return package_name
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
class PackContractTests(unittest.TestCase):
|
|
122
|
+
def test_pack_metadata_manifest_and_document_loading(self) -> None:
|
|
123
|
+
temp_dir = workspace_tempdir()
|
|
124
|
+
self.addCleanup(temp_dir.cleanup)
|
|
125
|
+
root = Path(temp_dir.name)
|
|
126
|
+
package_name = _write_pack(root, "example_governance_pack_a")
|
|
127
|
+
sys.path.insert(0, str(root))
|
|
128
|
+
self.addCleanup(sys.path.remove, str(root))
|
|
129
|
+
importlib.invalidate_caches()
|
|
130
|
+
|
|
131
|
+
metadata = load_pack_metadata(package_name)
|
|
132
|
+
|
|
133
|
+
self.assertEqual(metadata["schema_version"], "1.0.0")
|
|
134
|
+
self.assertEqual(metadata["ssot_package_name"], "example-governance-pack")
|
|
135
|
+
self.assertEqual(metadata["pypi_package_name"], "example-governance-pack")
|
|
136
|
+
self.assertEqual(metadata["origin"]["package_name"], "example-governance-pack")
|
|
137
|
+
self.assertEqual(metadata["version"], "1.2.3")
|
|
138
|
+
self.assertEqual(load_pack_schema_version(package_name), "1.0.0")
|
|
139
|
+
self.assertEqual(normalize_document_kind("adrs"), "adr")
|
|
140
|
+
manifest = load_document_manifest(package_name, "adr")
|
|
141
|
+
self.assertEqual(manifest[0]["id"], "adr:5000")
|
|
142
|
+
self.assertEqual(load_pack_manifest(package_name)["documents"]["adr"][0]["id"], "adr:5000")
|
|
143
|
+
self.assertEqual(list_packaged_document_ids(package_name), ["adr:5000"])
|
|
144
|
+
self.assertEqual(get_packaged_document_entry(package_name, "adr:5000")["filename"], "ADR-5000-example-pack-adr.yaml")
|
|
145
|
+
self.assertIn("Example packaged ADR.", read_packaged_document_text(package_name, "adr", "ADR-5000-example-pack-adr.yaml"))
|
|
146
|
+
|
|
147
|
+
def test_bound_pack_contract_exports_zero_argument_pack_api(self) -> None:
|
|
148
|
+
temp_dir = workspace_tempdir()
|
|
149
|
+
self.addCleanup(temp_dir.cleanup)
|
|
150
|
+
root = Path(temp_dir.name)
|
|
151
|
+
package_name = _write_pack(root, "example_governance_pack_exports")
|
|
152
|
+
sys.path.insert(0, str(root))
|
|
153
|
+
self.addCleanup(sys.path.remove, str(root))
|
|
154
|
+
importlib.invalidate_caches()
|
|
155
|
+
|
|
156
|
+
exports = bind_pack_contract(package_name)
|
|
157
|
+
|
|
158
|
+
self.assertEqual(exports["__version__"], "1.2.3")
|
|
159
|
+
self.assertEqual(exports["__ssot_package_name__"], "example-governance-pack")
|
|
160
|
+
self.assertEqual(exports["__pypi_package_name__"], "example-governance-pack")
|
|
161
|
+
self.assertIn("load_pack_metadata", exports["__all__"])
|
|
162
|
+
self.assertEqual(exports["load_pack_metadata"]()["ssot_package_name"], "example-governance-pack")
|
|
163
|
+
self.assertEqual(exports["load_pack_schema_version"](), "1.0.0")
|
|
164
|
+
self.assertEqual(exports["load_document_manifest"]("adr")[0]["id"], "adr:5000")
|
|
165
|
+
self.assertEqual(exports["list_packaged_document_ids"](), ["adr:5000"])
|
|
166
|
+
self.assertEqual(exports["get_packaged_document_entry"]("adr:5000")["filename"], "ADR-5000-example-pack-adr.yaml")
|
|
167
|
+
|
|
168
|
+
def test_invalid_kind_and_hash_fail_closed(self) -> None:
|
|
169
|
+
temp_dir = workspace_tempdir()
|
|
170
|
+
self.addCleanup(temp_dir.cleanup)
|
|
171
|
+
root = Path(temp_dir.name)
|
|
172
|
+
package_name = _write_pack(root, "example_governance_pack_b")
|
|
173
|
+
sys.path.insert(0, str(root))
|
|
174
|
+
self.addCleanup(sys.path.remove, str(root))
|
|
175
|
+
importlib.invalidate_caches()
|
|
176
|
+
|
|
177
|
+
with self.assertRaises(UnsupportedDocumentKindError):
|
|
178
|
+
normalize_document_kind("claim")
|
|
179
|
+
|
|
180
|
+
manifest_path = root / package_name / "templates" / "adr" / "manifest.json"
|
|
181
|
+
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
|
|
182
|
+
manifest[0]["sha256"] = "0" * 64
|
|
183
|
+
manifest_path.write_text(json.dumps(manifest), encoding="utf-8")
|
|
184
|
+
|
|
185
|
+
with self.assertRaises(InvalidPackManifestError):
|
|
186
|
+
read_packaged_document_text(package_name, "adr", "ADR-5000-example-pack-adr.yaml")
|
|
187
|
+
|
|
188
|
+
def test_ambiguous_import_package_distribution_mapping_fails_closed(self) -> None:
|
|
189
|
+
temp_dir = workspace_tempdir()
|
|
190
|
+
self.addCleanup(temp_dir.cleanup)
|
|
191
|
+
root = Path(temp_dir.name)
|
|
192
|
+
package_name = _write_pack(root, "example_governance_pack_ambiguous", dist_name="expected-governance-pack")
|
|
193
|
+
_write_dist_info(root, dist_name="other-governance-pack", version="9.9.9", top_level=package_name)
|
|
194
|
+
metadata_path = root / package_name / "metadata.json"
|
|
195
|
+
payload = json.loads(metadata_path.read_text(encoding="utf-8"))
|
|
196
|
+
payload["ssot_package_name"] = "missing-governance-pack"
|
|
197
|
+
payload["origin"]["package_name"] = "missing-governance-pack"
|
|
198
|
+
metadata_path.write_text(json.dumps(payload), encoding="utf-8")
|
|
199
|
+
sys.path.insert(0, str(root))
|
|
200
|
+
self.addCleanup(sys.path.remove, str(root))
|
|
201
|
+
importlib.invalidate_caches()
|
|
202
|
+
|
|
203
|
+
with self.assertRaises(InvalidPackMetadataError):
|
|
204
|
+
load_pack_metadata(package_name)
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
if __name__ == "__main__":
|
|
208
|
+
unittest.main()
|