Added graphviz visualization.

This commit is contained in:
Sleepy Monax 2023-02-06 12:36:23 +01:00
parent 8ee6e9625d
commit 59e4bc424e
5 changed files with 47 additions and 1 deletions

View file

@ -24,6 +24,13 @@ class Args:
return result
return default
def tryConsumeOpt(self, key: str) -> Value | None:
if key in self.opts:
result = self.opts[key]
del self.opts[key]
return result
return None
def consumeArg(self) -> str | None:
if len(self.args) == 0:
return None

View file

@ -1,7 +1,8 @@
from typing import Callable, cast
from osdk.args import Args
from osdk import context, shell, const, vt100, builder
from osdk.context import contextFor
from osdk import context, shell, const, vt100, builder, graph
Callback = Callable[[Args], None]
@ -136,6 +137,18 @@ def versionCmd(args: Args):
cmds += [Cmd("v", "version", "Show current version", versionCmd)]
def graphCmd(args: Args):
targetSpec = cast(str, args.consumeOpt(
"target", "host-" + shell.uname().machine))
context = contextFor(targetSpec, {})
graph.view(context)
cmds += [Cmd("g", "graph", "Show dependency graph", graphCmd)]
def usage():
print(f"Usage: {const.ARGV0} <command> [args...]")

View file

@ -79,6 +79,9 @@ class Context:
def cdefs(self) -> list[str]:
return self.target.cdefs()
def builddir(self) -> str:
return self.target.builddir()
def loadAllTargets() -> list[TargetManifest]:
files = shell.find(const.TARGETS_DIR, ["*.json"])

22
osdk/graph.py Normal file
View file

@ -0,0 +1,22 @@
from graphviz import Digraph
from osdk.context import Context
def view(context: Context):
g = Digraph(context.target.id, filename='graph.gv')
g.attr('node', shape='ellipse')
for instance in context.instances:
if not instance.isLib():
continue
g.node(instance.manifest.id, f"<<B>{instance.manifest.id}</B><BR/>{instance.manifest.decription}>",
shape="plaintext", style="filled")
for req in instance.manifest.requires:
g.edge(req, instance.manifest.id)
for req in instance.manifest.provides:
g.edge(instance.manifest.id, req)
g.view(filename=f"{context.builddir()}/graph.gv")

View file

@ -12,6 +12,7 @@ setup(
packages=["osdk"],
install_requires=[
"requests",
"graphviz"
],
entry_points={
"console_scripts": [