[BuildLibrary] eliminate BuiltLibrary and BuiltOverlayLibrary
This commit is contained in:
parent
1f4185107b
commit
717c31abf4
6 changed files with 95 additions and 191 deletions
|
|
@ -69,9 +69,6 @@ Tree: TypeAlias = MutableMapping[str, 'Pattern']
|
|||
dangling_mode_t: TypeAlias = Literal['error', 'ignore', 'include']
|
||||
""" How helpers should handle refs whose targets are not present in the library. """
|
||||
|
||||
emitted_via_t: TypeAlias = Literal['declaration', 'helper_write', 'tree_merge', 'source_import']
|
||||
""" Build-provenance origin tags for emitted cells. """
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CellProvenance:
|
||||
|
|
@ -83,29 +80,22 @@ class CellProvenance:
|
|||
chosen.
|
||||
|
||||
Attributes:
|
||||
final_name: Name exposed by the completed library.
|
||||
requested_name: First name requested for this cell during the build.
|
||||
kind: Whether the cell came from a declaration, helper emission, or an
|
||||
imported source library.
|
||||
owner_declared_name: Declared cell responsible for this output cell, if
|
||||
any. Imported source cells leave this as `None`.
|
||||
emitted_via: High-level path by which the cell entered the output.
|
||||
build_chain: Declared-cell dependency chain that was active when the
|
||||
cell was emitted.
|
||||
renamed_from: Original requested name when the final name differs.
|
||||
source_name: Original on-source name for imported cells.
|
||||
source_metadata: Optional source-library metadata copied through from
|
||||
lazy GDS readers.
|
||||
"""
|
||||
final_name: str
|
||||
requested_name: str
|
||||
kind: Literal['declared', 'helper', 'source']
|
||||
owner_declared_name: str | None
|
||||
emitted_via: emitted_via_t
|
||||
build_chain: tuple[str, ...]
|
||||
renamed_from: str | None = None
|
||||
source_name: str | None = None
|
||||
source_metadata: dict[str, Any] | None = None
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
|
|
@ -121,15 +111,11 @@ class BuildReport:
|
|||
requested_roots: Roots explicitly requested for the run. A full
|
||||
`build()` uses all declared cells.
|
||||
provenance: Mapping from final output name to provenance metadata.
|
||||
owned_cells: Mapping from declared cell name to all final output cell
|
||||
names it owns, including helper cells emitted while that declared
|
||||
cell was building.
|
||||
dependency_graph: Declared-cell dependency graph discovered through
|
||||
library-mediated reads and explicit recipe hints.
|
||||
"""
|
||||
requested_roots: tuple[str, ...]
|
||||
provenance: Mapping[str, CellProvenance]
|
||||
owned_cells: Mapping[str, tuple[str, ...]]
|
||||
dependency_graph: Mapping[str, frozenset[str]]
|
||||
|
||||
|
||||
|
|
@ -1467,25 +1453,6 @@ class Library(ILibrary):
|
|||
return tree, pat
|
||||
|
||||
|
||||
class BuiltLibrary(Library):
|
||||
"""
|
||||
Eager library returned by `BuildLibrary.build(output='library')`.
|
||||
|
||||
This is a normal materialized `Library` with one additional attribute,
|
||||
`build_report`, which records how the library was assembled from
|
||||
declarations, helper emissions, and imported source-backed cells.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
mapping: MutableMapping[str, 'Pattern'] | None = None,
|
||||
*,
|
||||
build_report: BuildReport | None = None,
|
||||
) -> None:
|
||||
super().__init__(mapping=mapping)
|
||||
self.build_report = build_report
|
||||
|
||||
|
||||
class _CellFactory:
|
||||
"""
|
||||
Adapter that turns a plain pattern factory into a deferred recipe factory.
|
||||
|
|
@ -1516,18 +1483,6 @@ class _BuildRecipe:
|
|||
return self
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class _PatternDeclaration:
|
||||
""" Declared cell backed by an already-built `Pattern`. """
|
||||
pattern: 'Pattern'
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class _RecipeDeclaration:
|
||||
""" Declared cell backed by a deferred recipe. """
|
||||
recipe: _BuildRecipe
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class _SourceDeclaration:
|
||||
"""
|
||||
|
|
@ -1596,16 +1551,15 @@ class BuildLibrary(ILibrary):
|
|||
The builder itself is not a normal readable library during authoring.
|
||||
Instead, `validate()` and `build()` create a temporary build-session library
|
||||
that recipes can read from and write helper cells into while dependencies
|
||||
are resolved. `build()` then freezes the builder on success and returns a
|
||||
normal library-like object carrying a `build_report`.
|
||||
are resolved. `build()` then freezes the builder on success and returns the
|
||||
built library plus a `BuildReport`.
|
||||
"""
|
||||
|
||||
def __init__(self, *, check_on_register: bool = False) -> None:
|
||||
self.check_on_register = check_on_register
|
||||
self.cells = BuildCellsView(self)
|
||||
self.last_build_report: BuildReport | None = None
|
||||
self._frozen = False
|
||||
self._declarations: dict[str, _PatternDeclaration | _RecipeDeclaration] = {}
|
||||
self._declarations: dict[str, 'Pattern | _BuildRecipe'] = {}
|
||||
self._sources: list[_SourceDeclaration] = []
|
||||
self._names: set[str] = set()
|
||||
self._order: list[str] = []
|
||||
|
|
@ -1664,13 +1618,12 @@ class BuildLibrary(ILibrary):
|
|||
if key in self._names:
|
||||
raise LibraryError(f'"{key}" already exists in the builder. Overwriting is not allowed!')
|
||||
|
||||
declaration: _PatternDeclaration | _RecipeDeclaration
|
||||
if isinstance(value, _BuildRecipe):
|
||||
declaration = _RecipeDeclaration(value)
|
||||
declaration = value
|
||||
else:
|
||||
if callable(value):
|
||||
raise TypeError('BuildLibrary recipes must be wrapped with cell(fn)(...) or @cell.')
|
||||
declaration = _PatternDeclaration(value)
|
||||
declaration = value
|
||||
|
||||
self._declarations[key] = declaration
|
||||
self._names.add(key)
|
||||
|
|
@ -1813,6 +1766,8 @@ class BuildLibrary(ILibrary):
|
|||
Mapping of `{source_name: visible_name}` for imported names that
|
||||
were renamed while being added.
|
||||
"""
|
||||
if self._active_session() is not None:
|
||||
raise BuildError('BuildLibrary.add_source() is only available while authoring, not during validate() or build().')
|
||||
self._assert_editable()
|
||||
|
||||
view = source if isinstance(source, ILibraryView) else LibraryView(source)
|
||||
|
|
@ -1862,8 +1817,7 @@ class BuildLibrary(ILibrary):
|
|||
execution path used by `build()`. Any generated library is discarded
|
||||
after validation completes.
|
||||
"""
|
||||
report, _output = self._run_build(names=names, output='overlay', allow_dangling=allow_dangling, persist_output=False)
|
||||
self.last_build_report = report
|
||||
_session, report = self._run_build(names=names, allow_dangling=allow_dangling)
|
||||
return report
|
||||
|
||||
def build(
|
||||
|
|
@ -1871,9 +1825,9 @@ class BuildLibrary(ILibrary):
|
|||
*,
|
||||
output: Literal['overlay', 'library'] = 'overlay',
|
||||
allow_dangling: bool = False,
|
||||
) -> 'BuiltLibrary | ILibrary':
|
||||
) -> tuple[ILibrary, BuildReport]:
|
||||
"""
|
||||
Materialize declarations and return a usable output library.
|
||||
Materialize declarations and return a usable output library plus report.
|
||||
|
||||
Args:
|
||||
output: `'overlay'` preserves imported source-backed cells where
|
||||
|
|
@ -1882,20 +1836,23 @@ class BuildLibrary(ILibrary):
|
|||
allow_dangling: If `False`, fail the build when the completed
|
||||
library still contains dangling references.
|
||||
"""
|
||||
if output not in ('overlay', 'library'):
|
||||
raise ValueError(f'Unknown build output mode: {output!r}')
|
||||
self._assert_editable()
|
||||
report, built_output = self._run_build(names=None, output=output, allow_dangling=allow_dangling, persist_output=True)
|
||||
session, report = self._run_build(names=None, allow_dangling=allow_dangling)
|
||||
if output == 'library':
|
||||
built_output = session.to_library()
|
||||
else:
|
||||
built_output = session.to_overlay()
|
||||
self._frozen = True
|
||||
self.last_build_report = report
|
||||
return built_output
|
||||
return built_output, report
|
||||
|
||||
def _run_build(
|
||||
self,
|
||||
*,
|
||||
names: Sequence[str] | None,
|
||||
output: Literal['overlay', 'library'],
|
||||
allow_dangling: bool,
|
||||
persist_output: bool,
|
||||
) -> tuple[BuildReport, BuiltLibrary | ILibrary | None]:
|
||||
) -> tuple['_BuildSessionLibrary', BuildReport]:
|
||||
roots = tuple(dict.fromkeys(names if names is not None else self._declarations.keys()))
|
||||
unknown = [name for name in roots if name not in self._names]
|
||||
if unknown:
|
||||
|
|
@ -1909,19 +1866,11 @@ class BuildLibrary(ILibrary):
|
|||
session.materialize_many(roots)
|
||||
if not allow_dangling:
|
||||
session.child_graph(dangling='error')
|
||||
if output == 'library':
|
||||
built_output = session.to_library() if persist_output else None
|
||||
elif persist_output:
|
||||
built_output = session.to_overlay()
|
||||
else:
|
||||
built_output = None
|
||||
finally:
|
||||
_ACTIVE_BUILD_SESSIONS.reset(token)
|
||||
|
||||
report = session.build_report(roots)
|
||||
if built_output is not None:
|
||||
built_output.build_report = report
|
||||
return report, built_output
|
||||
return session, report
|
||||
|
||||
|
||||
class _BuildSessionLibrary(ILibrary):
|
||||
|
|
@ -1935,22 +1884,19 @@ class _BuildSessionLibrary(ILibrary):
|
|||
"""
|
||||
|
||||
def __init__(self, builder: BuildLibrary) -> None:
|
||||
from .file.gdsii_lazy_core import BuiltOverlayLibrary, _SourceEntry, _SourceLayer # noqa: PLC0415
|
||||
from .file.gdsii_lazy_core import OverlayLibrary, _SourceEntry, _SourceLayer # noqa: PLC0415
|
||||
|
||||
self._builder = builder
|
||||
self._overlay = BuiltOverlayLibrary()
|
||||
self._overlay = OverlayLibrary()
|
||||
self._source_entry_type = _SourceEntry
|
||||
self._source_layer_type = _SourceLayer
|
||||
self._states: dict[str, Literal['unbuilt', 'building', 'built']] = {
|
||||
name: 'unbuilt' for name in builder._declarations
|
||||
}
|
||||
self._declared_stack: list[str] = []
|
||||
self._emission_stack: list[str] = []
|
||||
self._emission_via_stack: list[emitted_via_t] = []
|
||||
self._names = set(builder._names)
|
||||
self._order = list(builder._order)
|
||||
self._provenance: dict[str, CellProvenance] = {}
|
||||
self._owned_cells: defaultdict[str, list[str]] = defaultdict(list)
|
||||
self._dependency_graph: defaultdict[str, set[str]] = defaultdict(set)
|
||||
self._install_sources()
|
||||
|
||||
|
|
@ -1962,11 +1908,9 @@ class _BuildSessionLibrary(ILibrary):
|
|||
visible_to_source = dict(spec.visible_to_source),
|
||||
child_graph = {name: set(children) for name, children in spec.child_graph.items()},
|
||||
order = list(spec.order),
|
||||
)
|
||||
)
|
||||
layer_index = len(self._overlay._layers)
|
||||
self._overlay._layers.append(layer)
|
||||
source_info = getattr(spec.library, 'library_info', None)
|
||||
source_meta = dict(source_info) if isinstance(source_info, dict) else None
|
||||
|
||||
for source_name, visible_name in spec.source_to_visible.items():
|
||||
self._overlay._entries[visible_name] = self._source_entry_type(
|
||||
|
|
@ -1976,15 +1920,12 @@ class _BuildSessionLibrary(ILibrary):
|
|||
if visible_name not in self._overlay._order:
|
||||
self._overlay._order.append(visible_name)
|
||||
self._provenance[visible_name] = CellProvenance(
|
||||
final_name = visible_name,
|
||||
requested_name = source_name,
|
||||
kind = 'source',
|
||||
owner_declared_name = None,
|
||||
emitted_via = 'source_import',
|
||||
build_chain = (),
|
||||
renamed_from = source_name if visible_name != source_name else None,
|
||||
source_name = source_name,
|
||||
source_metadata = source_meta,
|
||||
)
|
||||
|
||||
def __iter__(self) -> Iterator[str]:
|
||||
|
|
@ -2020,14 +1961,6 @@ class _BuildSessionLibrary(ILibrary):
|
|||
if provenance is not None and provenance.kind == 'source':
|
||||
raise BuildError(f'Cannot {operation} imported source cell "{key}" during an active build session.')
|
||||
|
||||
def _remove_owned_cell(self, owner: str | None, name: str) -> None:
|
||||
if owner is None or owner not in self._owned_cells:
|
||||
return
|
||||
cells = self._owned_cells[owner]
|
||||
self._owned_cells[owner] = [cell for cell in cells if cell != name]
|
||||
if not self._owned_cells[owner]:
|
||||
del self._owned_cells[owner]
|
||||
|
||||
def rename(
|
||||
self,
|
||||
old_name: str,
|
||||
|
|
@ -2056,16 +1989,8 @@ class _BuildSessionLibrary(ILibrary):
|
|||
requested_name = provenance.requested_name
|
||||
self._provenance[new_name] = replace(
|
||||
provenance,
|
||||
final_name = new_name,
|
||||
renamed_from = requested_name if new_name != requested_name else None,
|
||||
)
|
||||
|
||||
owner = provenance.owner_declared_name
|
||||
if owner is not None and owner in self._owned_cells:
|
||||
self._owned_cells[owner] = [
|
||||
new_name if cell_name == old_name else cell_name
|
||||
for cell_name in self._owned_cells[owner]
|
||||
]
|
||||
return self
|
||||
|
||||
def __getitem__(self, key: str) -> 'Pattern':
|
||||
|
|
@ -2090,28 +2015,19 @@ class _BuildSessionLibrary(ILibrary):
|
|||
self._touch_name(key)
|
||||
|
||||
kind: Literal['declared', 'helper']
|
||||
via = self._emission_via_stack[-1] if self._emission_via_stack else 'helper_write'
|
||||
if current is not None and key == current:
|
||||
kind = 'declared'
|
||||
via = 'declaration'
|
||||
else:
|
||||
kind = 'helper'
|
||||
if not self._emission_via_stack:
|
||||
via = 'helper_write'
|
||||
|
||||
self._emission_stack.append(key)
|
||||
try:
|
||||
self._record_provenance(
|
||||
final_name = key,
|
||||
requested_name = key,
|
||||
kind = kind,
|
||||
owner_declared_name = current if kind == 'helper' else key,
|
||||
emitted_via = via,
|
||||
build_chain = tuple(self._declared_stack),
|
||||
renamed_from = None,
|
||||
)
|
||||
finally:
|
||||
self._emission_stack.pop()
|
||||
self._record_provenance(
|
||||
name = key,
|
||||
requested_name = key,
|
||||
kind = kind,
|
||||
owner_declared_name = current if kind == 'helper' else key,
|
||||
build_chain = tuple(self._declared_stack),
|
||||
renamed_from = None,
|
||||
)
|
||||
|
||||
def __delitem__(self, key: str) -> None:
|
||||
if key not in self._overlay:
|
||||
|
|
@ -2120,15 +2036,12 @@ class _BuildSessionLibrary(ILibrary):
|
|||
raise KeyError(key)
|
||||
|
||||
self._guard_mutable_output_name(key, operation='delete')
|
||||
provenance = self._provenance.get(key)
|
||||
if key in self._overlay:
|
||||
del self._overlay[key]
|
||||
self._names.discard(key)
|
||||
if key in self._order:
|
||||
self._order.remove(key)
|
||||
self._provenance.pop(key, None)
|
||||
if provenance is not None:
|
||||
self._remove_owned_cell(provenance.owner_declared_name, key)
|
||||
|
||||
def _merge(self, key_self: str, other: Mapping[str, 'Pattern'], key_other: str) -> None:
|
||||
self[key_self] = copy.deepcopy(other[key_other])
|
||||
|
|
@ -2139,11 +2052,7 @@ class _BuildSessionLibrary(ILibrary):
|
|||
rename_theirs: Callable[['ILibraryView', str], str] = _rename_patterns,
|
||||
mutate_other: bool = False,
|
||||
) -> dict[str, str]:
|
||||
self._emission_via_stack.append('tree_merge')
|
||||
try:
|
||||
rename_map = super().add(other, rename_theirs=rename_theirs, mutate_other=mutate_other)
|
||||
finally:
|
||||
self._emission_via_stack.pop()
|
||||
rename_map = super().add(other, rename_theirs=rename_theirs, mutate_other=mutate_other)
|
||||
|
||||
current = self._current_declared()
|
||||
for old_name, new_name in rename_map.items():
|
||||
|
|
@ -2159,32 +2068,24 @@ class _BuildSessionLibrary(ILibrary):
|
|||
def _record_provenance(
|
||||
self,
|
||||
*,
|
||||
final_name: str,
|
||||
name: str,
|
||||
requested_name: str,
|
||||
kind: Literal['declared', 'helper'],
|
||||
owner_declared_name: str | None,
|
||||
emitted_via: emitted_via_t,
|
||||
build_chain: tuple[str, ...],
|
||||
renamed_from: str | None,
|
||||
) -> None:
|
||||
self._provenance[final_name] = CellProvenance(
|
||||
final_name = final_name,
|
||||
self._provenance[name] = CellProvenance(
|
||||
requested_name = requested_name,
|
||||
kind = kind,
|
||||
owner_declared_name = owner_declared_name,
|
||||
emitted_via = emitted_via,
|
||||
build_chain = build_chain,
|
||||
renamed_from = renamed_from,
|
||||
)
|
||||
if owner_declared_name is not None and final_name not in self._owned_cells[owner_declared_name]:
|
||||
self._owned_cells[owner_declared_name].append(final_name)
|
||||
|
||||
def _wrap_error(self, name: str, exc: Exception) -> BuildError:
|
||||
helper = self._emission_stack[-1] if self._emission_stack else None
|
||||
chain = tuple(self._declared_stack)
|
||||
msg = [f'Failed while building declared cell "{name}"']
|
||||
if helper is not None and helper != name:
|
||||
msg.append(f'while materializing helper/output "{helper}"')
|
||||
if chain:
|
||||
msg.append(f'Dependency chain: {" -> ".join(chain)}')
|
||||
msg.append(f'Cause: {exc}')
|
||||
|
|
@ -2213,14 +2114,14 @@ class _BuildSessionLibrary(ILibrary):
|
|||
self._states[name] = 'building'
|
||||
self._declared_stack.append(name)
|
||||
try:
|
||||
if isinstance(declaration, _PatternDeclaration):
|
||||
pattern = declaration.pattern.deepcopy()
|
||||
else:
|
||||
for dep in declaration.recipe.explicit_dependencies:
|
||||
if isinstance(declaration, _BuildRecipe):
|
||||
for dep in declaration.explicit_dependencies:
|
||||
self._ensure_named(dep)
|
||||
pattern = declaration.recipe.func(*declaration.recipe.args, **declaration.recipe.kwargs)
|
||||
pattern = declaration.func(*declaration.args, **declaration.kwargs)
|
||||
if not isinstance(pattern, Pattern):
|
||||
raise BuildError(f'Recipe for "{name}" returned {type(pattern).__name__}, expected Pattern')
|
||||
else:
|
||||
pattern = declaration.deepcopy()
|
||||
|
||||
if name in self._overlay:
|
||||
if self._overlay[name] is not pattern:
|
||||
|
|
@ -2261,23 +2162,18 @@ class _BuildSessionLibrary(ILibrary):
|
|||
for name in self._builder._declarations
|
||||
if name in self._dependency_graph or name in requested_roots
|
||||
}
|
||||
owned_cells = {
|
||||
name: tuple(cells)
|
||||
for name, cells in self._owned_cells.items()
|
||||
}
|
||||
return BuildReport(
|
||||
requested_roots = tuple(dict.fromkeys(requested_roots)),
|
||||
provenance = dict(self._provenance),
|
||||
owned_cells = owned_cells,
|
||||
dependency_graph = dependency_graph,
|
||||
)
|
||||
|
||||
def to_overlay(self) -> ILibrary:
|
||||
return self._overlay
|
||||
|
||||
def to_library(self) -> BuiltLibrary:
|
||||
def to_library(self) -> Library:
|
||||
mapping = {name: self._overlay[name] for name in self._overlay.source_order()}
|
||||
return BuiltLibrary(mapping)
|
||||
return Library(mapping)
|
||||
|
||||
|
||||
class LazyLibrary(ILibrary):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue