#!/usr/bin/python # direct inclusion of the README file include(README.dot2term) # J Templon NIKHEF 2010. import getopt, sys, os import re def usage(): print "Usage: dot2term [-r root_template ] file.dot" print "Converts from dot format to udg format readable by uDrawGraph" print "option -r means to display only that part of the include tree stemming from the"+\ " specified template" # note : in order to avoid having to 'install' this program, I have # included the relevant pieces of treedefs.py and dvtree.py in this file. Don't edit here, # edit the originals. # BEGIN code included via m4 macro processor include(treedefs.py) include(dvtree.py) # END code included via m4 macro processor dprefix = " " def main(): try: opts, args = getopt.getopt(sys.argv[1:], "hr:", ["help","root="]) except getopt.GetoptError: usage() sys.exit(2) root = None # default to printing entire graph for o, a in opts: if o in ("-h", "--help"): usage() sys.exit() if o in ("-r", "--root"): root = a if args[0][-4:] == '.dot' : outfile=args[0][:-4]+'.udg' else: outfile = args[0] + '.udg' dotlines = open(args[0]).readlines() if dotlines[0].find('digraph') < 0 : print "Expected digraph as first line, found", dotlines[0] print "Bailing out" sys.exit(1) li = dotlines[0].find('"') + 1 ri = dotlines[0].rfind('"') nodename=dotlines[0][li:ri] shape='ellipse' tnode = Node(nodename,shape) Ltreenode.append(tnode) Dnode[tnode.name] = tnode for dl in dotlines[1:]: l = dl[:-1].strip() # get rid of trailing newline and any preceding whitespace if l.find('node') == 0: li=l.find('shape=')+6 ri=l.rfind(']') shape=l[li:ri] elif l.find(' -> ') > 0: # edge definition fields=l.split('"') # nodes are between quotes, so split on that par = fields[1] child = fields[3] if par not in Dnode.keys(): tnode = Node(par, shape) Dnode[tnode.name] = tnode pn = tnode else: pn = Dnode[par] if not pn.shape : pn.shape = shape if child not in Dnode.keys(): tnode = Node(child) Dnode[tnode.name] = tnode cn = tnode else: cn = Dnode[child] if not pn.children: pn.children = [] pn.children.append(cn) elif l.find('}') == 0: break else: print 'found some incomprehensible line', l sys.exit(1) dvFmt = Formathandler(dvnode, dvref, dvb4child, dvb4next, dvafterchild, dvafterchildren, filename=outfile) init_traverse() # initialize data structures for tree traversal if not root: n = Ltreenode[0] # the root of the entire graph else: if root not in Dnode.keys(): print 'template ' + root + ' not found in call graph, bailing out' sys.exit(2) else: n = Dnode[root] dvFmt.of.write('[\n') # open the term representation fmt_applyunique(n, dvFmt) # traverse the tree, outputting daVinci file dvFmt.of.write(']\n') # close the term representation main()