17e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#! /usr/bin/python 27e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith 37e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#this is a script to extract given named nodes from a dot file, with 47e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#the associated edges. An edge is kept iff for edge x -> y 57e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith# x and y are both nodes specified to be kept. 67e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith 77e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#known issues: if a line contains '->' and is not an edge line 87e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#problems will occur. If node labels do not begin with 97e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#Node this also will not work. Since this is designed to work 107e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#on DSA dot output and not general dot files this is ok. 117e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#If you want to use this on other files rename the node labels 127e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#to Node[.*] with a script or something. This also relies on 137e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#the length of a node name being 13 characters (as it is in all 147e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#DSA dot output files) 157e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith 167e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#Note that the name of the node can be any substring of the actual 177e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#name in the dot file. Thus if you say specify COLLAPSED 187e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#as a parameter this script will pull out all COLLAPSED 197e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#nodes in the file 207e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith 217e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#Specifying escape characters in the name like \n also will not work, 227e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#as Python 237e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#will make it \\n, I'm not really sure how to fix this 247e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith 257e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#currently the script prints the names it is searching for 267e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#to STDOUT, so you can check to see if they are what you intend 277e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith 287e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredithimport re 297e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredithimport string 307e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredithimport sys 317e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith 327e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith 337e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredithif len(sys.argv) < 3: 347e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith print 'usage is ./DSAextract <dot_file_to_modify> \ 357e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith <output_file> [list of nodes to extract]' 367e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith 377e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#open the input file 387e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredithinput = open(sys.argv[1], 'r') 397e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith 407e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#construct a set of node names 417e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredithnode_name_set = set() 427e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredithfor name in sys.argv[3:]: 437e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith node_name_set |= set([name]) 447e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith 457e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#construct a list of compiled regular expressions from the 467e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#node_name_set 477e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredithregexp_list = [] 487e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredithfor name in node_name_set: 497e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith regexp_list.append(re.compile(name)) 507e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith 517e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#used to see what kind of line we are on 527e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredithnodeexp = re.compile('Node') 537e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#used to check to see if the current line is an edge line 547e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Mereditharrowexp = re.compile('->') 557e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith 567e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredithnode_set = set() 577e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith 587e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#read the file one line at a time 597e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredithbuffer = input.readline() 607e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredithwhile buffer != '': 617a2bdde0a0eebcd2125055e0eacaca040f0b766cChris Lattner #filter out the unnecessary checks on all the edge lines 627e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith if not arrowexp.search(buffer): 637e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith #check to see if this is a node we are looking for 647e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith for regexp in regexp_list: 657e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith #if this name is for the current node, add the dot variable name 667e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith #for the node (it will be Node(hex number)) to our set of nodes 677e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith if regexp.search(buffer): 687e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith node_set |= set([re.split('\s+',buffer,2)[1]]) 69dae87b65365a8a3dae8e41c2e836572388ec84baPatrick Meredith break 707e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith buffer = input.readline() 717e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith 727e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith 737e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#test code 747e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#print '\n' 757e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith 767e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredithprint node_name_set 777e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith 787e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#print node_set 797e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith 807e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith 817e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#open the output file 827e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredithoutput = open(sys.argv[2], 'w') 837e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith#start the second pass over the file 847e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredithinput = open(sys.argv[1], 'r') 857e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith 867e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredithbuffer = input.readline() 877e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredithwhile buffer != '': 887e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith #there are three types of lines we are looking for 897e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith #1) node lines, 2) edge lines 3) support lines (like page size, etc) 907e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith 917e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith #is this an edge line? 927e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith #note that this is no completely robust, if a none edge line 937e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith #for some reason contains -> it will be missidentified 947e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith #hand edit the file if this happens 957e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith if arrowexp.search(buffer): 967e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith #check to make sure that both nodes are in the node list 977e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith #if they are print this to output 987e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith nodes = arrowexp.split(buffer) 997e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith nodes[0] = string.strip(nodes[0]) 1007e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith nodes[1] = string.strip(nodes[1]) 1017e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith if nodes[0][:13] in node_set and \ 1027e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith nodes[1][:13] in node_set: 1037e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith output.write(buffer) 1047e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith elif nodeexp.search(buffer): #this is a node line 1057e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith node = re.split('\s+', buffer,2)[1] 1067e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith if node in node_set: 1077e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith output.write(buffer) 1087e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith else: #this is a support line 1097e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith output.write(buffer) 1107e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith buffer = input.readline() 1117e1cf1c8f456a82de9f68c1727a94e1202a927a0Patrick Meredith 112