import os class Node: aliasIndex = 0 def __init__(self, name): global aliasIndex self.name=name self.children=[] self.alias = 'a' + repr(Node.aliasIndex) Node.aliasIndex = Node.aliasIndex + 1 def addChild(self, child): self.children.append(child) def dumpNode(self): components = self.name.split(":") print self.alias + ' [shape = record, label="' + components[0] + '\\n' + components[1] + '\\n' + components[3] + '"];' for child in self.children: child.dumpNode() components = child.name.split(":") if (len(components) > 3): print self.alias + ' -> ' + child.alias + '[style=solid, arrowhead=vee, label="' + components[4] + '"];' else: print self.alias + ' -> ' + child.alias + '[style=solid, arrowhead=vee];' def getLevel(chunks): for i in range(len(chunks)): if (chunks[i].find(":") >1): return i f=os.popen("mvn dependency:tree") foundStart=0 foundEnd=0 root=Node("root") parent=root currentLevel=1 # 0-based, 0=root, 1=root's children, 2=root's grandchildren, etc. stack=[] for i in f.readlines(): if (i.startswith("[INFO] [dependency:tree {execution: default-cli}]")): foundStart=1 elif (foundStart == 1 and i.startswith("[INFO] ------------------------------------------------------------------------")): foundEnd = 1 elif (foundStart == 1 and foundEnd == 0): chunks = i.split() level=getLevel(chunks) if (level > currentLevel): stack.append(parent) parent=parent.children[-1] currentLevel = level elif (level < currentLevel): parent = stack.pop() currentLevel = level parent.addChild(Node(chunks[level])) print 'digraph sample {' print 'nodesep=1.0' print 'ranksep=1.0' print 'rankdir=TB' print 'bgcolor=white' print 'concentrate=true' print 'node [style="filled", fillcolor=lightyellow, color=black, shape=box];' root.children[0].dumpNode() print '}'