Skip to content

Instantly share code, notes, and snippets.

@kanru
Last active August 29, 2015 13:57
Show Gist options
  • Save kanru/9388306 to your computer and use it in GitHub Desktop.
Save kanru/9388306 to your computer and use it in GitHub Desktop.
Output IPDL graph using graphviz dot format
#!/usr/bin/python
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import ipdl
import sys
import os
def files_match(suffixes, topdir, excludes):
includedirs = set()
files = []
for dirpath, dirnames, filenames in os.walk(topdir):
skip = False
for prefix in excludes:
if dirpath.startswith(prefix):
skip = True
break
if skip:
continue
for name in filenames:
for suffix in suffixes:
if name.endswith(suffix):
includedirs.add(dirpath)
files.append(os.path.join(dirpath, name))
return files, list(includedirs)
def print_ast_graph(ast):
edges = set()
if ast.protocol:
edges.add(" {0};".format(ast.protocol.name))
for manager in ast.protocol.managers:
edges.add('{{{0} -> {1}}};'.format(
manager.name, ast.protocol.name))
for method in ast.protocol.messageDecls:
for param in method.inParams + method.outParams:
if str(param.typespec).startswith('P'):
if str(param.typespec)[1].isupper():
edges.add('{{edge[label="{2}",decorate,constraint=false] {0} -> {1}}};'.format(param.typespec, ast.protocol.name, method.name))
for edge in edges:
print(edge)
def print_graph_header():
print('digraph ipdlgraph {')
print(' size="11.6929,8.2677!";')
print(' ratio="fill";')
print(' margin=0;')
print(' splines=true;')
print(' node[fontname="monospace"]')
def print_graph_footer():
print('}')
def ipdlgraph(srcdir, excludes):
files, includedirs = files_match([".ipdl", ".ipdlh"], srcdir, excludes)
ast_array = []
for src in files:
ast_array.append(ipdl.parse(open(src).read(), src, includedirs))
print_graph_header()
for ast in ast_array:
print_ast_graph(ast)
print_graph_footer()
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Usage: ipdlgraph.py SOURCE_DIR")
exit(0)
ipdlgraph(sys.argv[1], sys.argv[2:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment