Skip to content

Commit

Permalink
Allow circular dependencies between bblocks
Browse files Browse the repository at this point in the history
  • Loading branch information
avillar committed Feb 19, 2025
1 parent 4628215 commit d55f19e
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions ogc/bblocks/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,14 @@ def __init__(self,
cycles = list(nx.simple_cycles(dep_graph))
if cycles:
cycles_str = '\n - '.join(' -> '.join(reversed(c)) + ' -> ' + c[-1] for c in cycles)
raise BuildingBlockError(f"Circular dependencies found: \n - {cycles_str}")
print("=== WARNING!! ===", file=sys.stderr)
print(f"Circular dependencies found: \n - {cycles_str}", file=sys.stderr)
print("Circular dependency support is experimental", file=sys.stderr)
print("=== WARNING!! ===", file=sys.stderr)
while cycles:
for cycle in cycles:
dep_graph.remove_edge(cycle[0], cycle[1])
cycles = list(nx.simple_cycles(dep_graph))
self.bblocks: dict[str, BuildingBlock] = {b: self.bblocks[b]
for b in nx.topological_sort(dep_graph)
if b in self.bblocks}
Expand Down Expand Up @@ -558,7 +565,7 @@ def _resolve_bblock_deps(self, bblock: BuildingBlock) -> set[str]:
return dependencies

@lru_cache
def find_dependencies(self, identifier: str) -> list[dict | BuildingBlock]:
def find_dependencies(self, identifier: str, seen: tuple[str] = None) -> list[dict | BuildingBlock]:
if identifier in self.bblocks:
bblock = self.bblocks[identifier]
metadata = bblock.metadata
Expand All @@ -569,8 +576,10 @@ def find_dependencies(self, identifier: str) -> list[dict | BuildingBlock]:
return []

dependencies = [bblock or metadata]
seen = (identifier,) if not seen else seen + (identifier,)
for d in metadata.get('dependsOn', ()):
dependencies.extend(self.find_dependencies(d))
if d not in seen:
dependencies.extend(self.find_dependencies(d, seen=seen))

return dependencies

Expand Down

0 comments on commit d55f19e

Please sign in to comment.