Skip to content
This repository was archived by the owner on Dec 19, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/how-to-guides.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ wit inspect --dot | dot -Tsvg > graph.svg

This SVG file can be directly viewed in most web browsers.

There is also a `--simple` option for `--dot` mode, which constructs a simpler
version of the graph.


== Restore a previous workspace

Expand Down
58 changes: 57 additions & 1 deletion lib/wit/inspect.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
from .common import print_errors
from .witlogger import getLogger
from .workspace import WorkSpace

log = getLogger()

Expand All @@ -18,7 +19,10 @@ def inspect_tree(ws, args):
_print_generic_tree(x)

if args.dot:
_print_dot_tree(ws, packages)
if args.simple:
_print_simple_dot_tree(ws, packages)
else:
_print_dot_tree(ws, packages)

print_errors(errors)

Expand Down Expand Up @@ -91,6 +95,58 @@ def print_dep(pkg, dep):
log.output('}')


def _print_simple_dot_tree(ws, packages_dict):
# This algorithm is based on the normal one except we identify nodes based
# on just the package name and not the revision.
packages = list(packages_dict.values())

def sanitize(s):
return s.replace(r"-", "_")

nodes = set()
for pkg in packages:
nodes.add(sanitize(pkg.name))

drawn_connections = set()

def draw_connection(from_id, to_id):
if from_id == to_id:
return
pair = (from_id, to_id)
if pair not in drawn_connections:
drawn_connections.add(pair)

def print_dep(pkg, dep):
if isinstance(pkg, WorkSpace):
pkg_name = "root"
else:
pkg_name = sanitize(pkg.name)
dep_name = sanitize(dep.name)
dep.load(packages_dict, ws.repo_paths, ws.root, False)
if dep.package.repo is None:
log.error("Cannot generate graph with missing repo '{}'".format(dep.name))
sys.exit(1)
draw_connection(pkg_name, dep_name)

for dep in ws.manifest.dependencies:
print_dep(ws, dep)

for pkg in packages:
for dep in pkg.get_dependencies():
print_dep(pkg, dep)

log.output('digraph dependencies {')
log.output('root [label="[root]"]')

for node in sorted(nodes):
log.output('{} [label="{}"]'.format(node, node))

for from_id, to_id in sorted(drawn_connections):
log.output("{} -> {}".format(from_id, to_id))

log.output('}')


def _print_generic_tree(data):
tag = data.pop('')
print(tag)
Expand Down
2 changes: 1 addition & 1 deletion lib/wit/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def main() -> None:
if args.dot or args.tree:
inspect_tree(ws, args)
else:
log.error('`wit inspect` must be run with a flag')
log.error('`wit inspect` must be run with --dot or --tree')
print(parser.parse_args('inspect -h'.split()))
sys.exit(1)
except WitUserError as e:
Expand Down
7 changes: 7 additions & 0 deletions lib/wit/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@ def err(msg):
inspect_group = inspect_parser.add_mutually_exclusive_group()
inspect_group.add_argument('--tree', action="store_true")
inspect_group.add_argument('--dot', action="store_true")
# Common to all
inspect_parser.add_argument(
'--simple',
action='store_true',
help='Print simple view. When used with --dot, this will collapse all versions of a dependency'
' into a single node.'
)

# ********** foreach subparser **********
foreach_parser = subparsers.add_parser(
Expand Down