Repository navigation
Expand file tree
/
Copy pathcatalog.py
More file actions
71 lines (58 loc) · 2.1 KB
/
Copy pathcatalog.py
File metadata and controls
71 lines (58 loc) · 2.1 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
"""builds/catalog.json — bounded per-lambda build history committed to source repo."""
from __future__ import annotations
import json
from dataclasses import asdict, dataclass, field
from pathlib import Path
SCHEMA_VERSION = 1
MAX_HISTORY = 10
@dataclass
class CatalogEntry:
sha256: str
source_commit: str
runtime: str
arch: str
region: str
builder_version: str
base_image_digest: str
built_at: str # ISO-8601 UTC, e.g. "2026-05-27T09:30:00Z"
@dataclass
class LambdaCatalog:
current: str
history: list[CatalogEntry] = field(default_factory=list)
@dataclass
class Catalog:
lambdas: dict[str, LambdaCatalog]
def record(self, logical_name: str, entry: CatalogEntry) -> None:
lc = self.lambdas.get(logical_name)
if lc is None:
self.lambdas[logical_name] = LambdaCatalog(current=entry.sha256, history=[entry])
return
if lc.current == entry.sha256:
return
lc.history.insert(0, entry)
lc.current = entry.sha256
lc.history = lc.history[:MAX_HISTORY]
def save(self, path: Path) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
payload = {
"schema_version": SCHEMA_VERSION,
"lambdas": {
name: {
"current": lc.current,
"history": [asdict(e) for e in lc.history],
}
for name, lc in self.lambdas.items()
},
}
path.write_text(json.dumps(payload, indent=2, sort_keys=True) + "\n")
def load_catalog(path: Path) -> Catalog:
if not path.exists():
return Catalog(lambdas={})
raw = json.loads(path.read_text())
if raw.get("schema_version") != SCHEMA_VERSION:
raise ValueError(f"{path}: unsupported schema_version {raw.get('schema_version')!r}")
lambdas: dict[str, LambdaCatalog] = {}
for name, lc in raw.get("lambdas", {}).items():
history = [CatalogEntry(**e) for e in lc.get("history", [])]
lambdas[name] = LambdaCatalog(current=lc["current"], history=history)
return Catalog(lambdas=lambdas)