-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreference_packs.py
More file actions
302 lines (241 loc) · 10.3 KB
/
Copy pathreference_packs.py
File metadata and controls
302 lines (241 loc) · 10.3 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
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
#!/usr/bin/env python3
"""Utilities for loading and validating reference pack configuration."""
from __future__ import annotations
import argparse
import base64
import json
import os
import re
import sys
from dataclasses import asdict, dataclass
from pathlib import Path
from typing import Any
DEFAULT_CONFIG_RELPATH = ".github/reference_packs.json"
PACK_NAME_PATTERN = re.compile(r"^[A-Za-z0-9._-]+$")
class ReferencePackConfigError(ValueError):
"""Raised when reference pack configuration is invalid."""
@dataclass(frozen=True)
class ReferencePack:
"""A single reference pack configuration entry."""
name: str
repo: str
ref: str
paths: list[str]
@dataclass(frozen=True)
class ReferencePackSnapshot:
"""Loaded reference pack state for a workspace."""
exists: bool
config_path: Path
config_text: str | None
packs: list[ReferencePack]
@dataclass(frozen=True)
class ReferencePackCheckoutPlan:
"""Workflow-ready checkout plan derived from validated pack config."""
name: str
repo: str
ref: str
paths: list[str]
checkout_path: str
def reference_pack_config_path(workspace: Path | str = ".") -> Path:
"""Return the absolute path to the reference pack config file."""
return Path(workspace).resolve() / DEFAULT_CONFIG_RELPATH
def reference_pack_config_exists(workspace: Path | str = ".") -> bool:
"""Return whether `.github/reference_packs.json` exists in the workspace."""
return reference_pack_config_path(workspace).is_file()
def read_reference_pack_config_text(workspace: Path | str = ".") -> tuple[Path, str | None]:
"""Read `.github/reference_packs.json` from a workspace when it exists."""
config_path = reference_pack_config_path(workspace)
if not config_path.is_file():
return config_path, None
try:
return config_path, config_path.read_text(encoding="utf-8")
except UnicodeDecodeError as exc:
raise ReferencePackConfigError(
f"Malformed text in {config_path}: file must be valid UTF-8"
) from exc
def _require_nonempty_string(value: Any, field_name: str) -> str:
if not isinstance(value, str) or not value.strip():
raise ReferencePackConfigError(f"{field_name} must be a non-empty string")
return value.strip()
def _validate_repo(repo: str) -> str:
if "/" not in repo or repo.startswith("/") or repo.endswith("/"):
raise ReferencePackConfigError("repo must use owner/name format")
return repo
def _validate_paths(raw_paths: Any) -> list[str]:
if not isinstance(raw_paths, list) or not raw_paths:
raise ReferencePackConfigError("paths must be a non-empty array of strings")
validated: list[str] = []
for entry in raw_paths:
path = _require_nonempty_string(entry, "paths[]")
if path.startswith("/"):
raise ReferencePackConfigError("paths[] must be relative, not absolute")
if ".." in path.split("/"):
raise ReferencePackConfigError("paths[] must not traverse parent directories")
validated.append(path)
return validated
def _build_pack(name: str, payload: Any) -> ReferencePack:
normalized_name = _require_nonempty_string(name, "pack name")
if not PACK_NAME_PATTERN.fullmatch(normalized_name):
raise ReferencePackConfigError(
"pack name may only contain letters, numbers, '.', '_', and '-'"
)
if not isinstance(payload, dict):
raise ReferencePackConfigError(f"pack '{normalized_name}' must be a JSON object")
repo = _validate_repo(_require_nonempty_string(payload.get("repo"), "repo"))
ref = _require_nonempty_string(payload.get("ref"), "ref")
paths = _validate_paths(payload.get("paths"))
return ReferencePack(name=normalized_name, repo=repo, ref=ref, paths=paths)
def parse_reference_packs(payload: Any) -> list[ReferencePack]:
"""Parse reference packs from decoded JSON payload.
Accepted formats:
- Mapping format: {"pack_name": {"repo": ..., "ref": ..., "paths": [...]}}
- List format: {"packs": [{"name": ..., "repo": ..., "ref": ..., "paths": [...]}]}
"""
if not isinstance(payload, dict):
raise ReferencePackConfigError("reference_packs.json must contain a JSON object")
if "packs" in payload:
extra_keys = sorted(str(key) for key in payload if key != "packs")
if extra_keys:
extras = ", ".join(extra_keys)
raise ReferencePackConfigError(
f"when using 'packs' format, no additional top-level keys are allowed: {extras}"
)
packs_node = payload["packs"]
if not isinstance(packs_node, list):
raise ReferencePackConfigError("packs must be an array")
packs: list[ReferencePack] = []
for index, entry in enumerate(packs_node):
if not isinstance(entry, dict):
raise ReferencePackConfigError(f"packs[{index}] must be an object")
name = _require_nonempty_string(entry.get("name"), f"packs[{index}].name")
packs.append(_build_pack(name, entry))
else:
packs = [_build_pack(name, item) for name, item in payload.items()]
seen_names: set[str] = set()
for pack in packs:
if pack.name in seen_names:
raise ReferencePackConfigError(f"duplicate pack name: {pack.name}")
seen_names.add(pack.name)
return packs
def parse_reference_pack_config_text(config_text: str, config_path: Path) -> list[ReferencePack]:
"""Parse and validate reference packs from raw JSON text."""
try:
payload = json.loads(config_text)
except json.JSONDecodeError as exc:
raise ReferencePackConfigError(
f"Malformed JSON in {config_path}: line {exc.lineno} column {exc.colno}: {exc.msg}"
) from exc
try:
return parse_reference_packs(payload)
except ReferencePackConfigError as exc:
raise ReferencePackConfigError(f"Invalid config in {config_path}: {exc}") from exc
def load_reference_packs(workspace: Path | str = ".") -> ReferencePackSnapshot:
"""Load and validate reference pack configuration from a workspace."""
config_path, config_text = read_reference_pack_config_text(workspace)
if config_text is None:
return ReferencePackSnapshot(
exists=False, config_path=config_path, config_text=None, packs=[]
)
packs = parse_reference_pack_config_text(config_text, config_path)
return ReferencePackSnapshot(
exists=True, config_path=config_path, config_text=config_text, packs=packs
)
def build_checkout_plan(packs: list[ReferencePack]) -> list[ReferencePackCheckoutPlan]:
"""Build a stable, workflow-ready checkout plan for reference packs."""
return [
ReferencePackCheckoutPlan(
name=pack.name,
repo=pack.repo,
ref=pack.ref,
paths=pack.paths,
checkout_path=f".reference/{pack.name}",
)
for pack in packs
]
def _snapshot_to_dict(snapshot: ReferencePackSnapshot) -> dict[str, Any]:
checkout_plan = build_checkout_plan(snapshot.packs)
return {
"exists": snapshot.exists,
"config_path": str(snapshot.config_path),
"config_text": snapshot.config_text,
"packs": [asdict(pack) for pack in snapshot.packs],
"checkout_plan": [asdict(plan_entry) for plan_entry in checkout_plan],
}
def _github_output_block(key: str, value: str) -> list[str]:
delimiter = f"__{key.upper()}_EOF__"
while delimiter in value:
delimiter = f"_{delimiter}"
return [f"{key}<<{delimiter}", value, delimiter]
def _build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="Read and validate .github/reference_packs.json")
parser.add_argument(
"--workspace",
default=".",
help="Workspace root that may contain .github/reference_packs.json",
)
parser.add_argument(
"--format",
choices=["json", "github-output", "self-check"],
default="json",
help="Output format",
)
return parser
def main(argv: list[str] | None = None) -> int:
args = _build_parser().parse_args(argv)
try:
snapshot = load_reference_packs(args.workspace)
except ReferencePackConfigError as exc:
print(f"Reference packs config error: {exc}", file=sys.stderr)
return 2
payload = _snapshot_to_dict(snapshot)
if args.format == "json":
print(json.dumps(payload, separators=(",", ":")))
return 0
if args.format == "self-check":
if not snapshot.exists:
print(
f"Reference packs self-check: skipped ({snapshot.config_path} not found).",
file=sys.stderr,
)
return 0
print(
"Reference packs self-check: OK "
f"({len(snapshot.packs)} pack(s) validated from {snapshot.config_path}).",
file=sys.stderr,
)
return 0
github_output_raw = os.environ.get("GITHUB_OUTPUT")
if not github_output_raw:
print("Reference packs config error: GITHUB_OUTPUT is not set", file=sys.stderr)
return 2
github_output = Path(github_output_raw)
canonical_payload_json = json.dumps(payload, separators=(",", ":"))
checkout_plan_json = json.dumps(
[asdict(plan_entry) for plan_entry in build_checkout_plan(snapshot.packs)],
separators=(",", ":"),
)
config_text_b64 = (
base64.b64encode((snapshot.config_text or "").encode("utf-8")).decode("ascii")
if snapshot.exists
else ""
)
lines = [
f"reference_packs_exists={'true' if snapshot.exists else 'false'}",
f"reference_packs_path={snapshot.config_path}",
f"reference_packs_count={len(snapshot.packs)}",
f"reference_packs_config_text_b64={config_text_b64}",
]
lines.extend(
_github_output_block(
"reference_packs_json",
json.dumps([asdict(pack) for pack in snapshot.packs], separators=(",", ":")),
)
)
lines.extend(_github_output_block("reference_packs_payload_json", canonical_payload_json))
lines.extend(_github_output_block("reference_packs_checkout_plan_json", checkout_plan_json))
lines.extend(_github_output_block("reference_packs_config_text", snapshot.config_text or ""))
with github_output.open("a", encoding="utf-8") as handle:
handle.write("\n".join(lines) + "\n")
return 0
if __name__ == "__main__":
raise SystemExit(main())