DSAextract.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #! /usr/bin/python
  2. #this is a script to extract given named nodes from a dot file, with
  3. #the associated edges. An edge is kept iff for edge x -> y
  4. # x and y are both nodes specified to be kept.
  5. #known issues: if a line contains '->' and is not an edge line
  6. #problems will occur. If node labels do not begin with
  7. #Node this also will not work. Since this is designed to work
  8. #on DSA dot output and not general dot files this is ok.
  9. #If you want to use this on other files rename the node labels
  10. #to Node[.*] with a script or something. This also relies on
  11. #the length of a node name being 13 characters (as it is in all
  12. #DSA dot output files)
  13. #Note that the name of the node can be any substring of the actual
  14. #name in the dot file. Thus if you say specify COLLAPSED
  15. #as a parameter this script will pull out all COLLAPSED
  16. #nodes in the file
  17. #Specifying escape characters in the name like \n also will not work,
  18. #as Python
  19. #will make it \\n, I'm not really sure how to fix this
  20. #currently the script prints the names it is searching for
  21. #to STDOUT, so you can check to see if they are what you intend
  22. import re
  23. import string
  24. import sys
  25. if len(sys.argv) < 3:
  26. print 'usage is ./DSAextract <dot_file_to_modify> \
  27. <output_file> [list of nodes to extract]'
  28. #open the input file
  29. input = open(sys.argv[1], 'r')
  30. #construct a set of node names
  31. node_name_set = set()
  32. for name in sys.argv[3:]:
  33. node_name_set |= set([name])
  34. #construct a list of compiled regular expressions from the
  35. #node_name_set
  36. regexp_list = []
  37. for name in node_name_set:
  38. regexp_list.append(re.compile(name))
  39. #used to see what kind of line we are on
  40. nodeexp = re.compile('Node')
  41. #used to check to see if the current line is an edge line
  42. arrowexp = re.compile('->')
  43. node_set = set()
  44. #read the file one line at a time
  45. buffer = input.readline()
  46. while buffer != '':
  47. #filter out the unnecessary checks on all the edge lines
  48. if not arrowexp.search(buffer):
  49. #check to see if this is a node we are looking for
  50. for regexp in regexp_list:
  51. #if this name is for the current node, add the dot variable name
  52. #for the node (it will be Node(hex number)) to our set of nodes
  53. if regexp.search(buffer):
  54. node_set |= set([re.split('\s+',buffer,2)[1]])
  55. break
  56. buffer = input.readline()
  57. #test code
  58. #print '\n'
  59. print node_name_set
  60. #print node_set
  61. #open the output file
  62. output = open(sys.argv[2], 'w')
  63. #start the second pass over the file
  64. input = open(sys.argv[1], 'r')
  65. buffer = input.readline()
  66. while buffer != '':
  67. #there are three types of lines we are looking for
  68. #1) node lines, 2) edge lines 3) support lines (like page size, etc)
  69. #is this an edge line?
  70. #note that this is no completely robust, if a none edge line
  71. #for some reason contains -> it will be missidentified
  72. #hand edit the file if this happens
  73. if arrowexp.search(buffer):
  74. #check to make sure that both nodes are in the node list
  75. #if they are print this to output
  76. nodes = arrowexp.split(buffer)
  77. nodes[0] = string.strip(nodes[0])
  78. nodes[1] = string.strip(nodes[1])
  79. if nodes[0][:13] in node_set and \
  80. nodes[1][:13] in node_set:
  81. output.write(buffer)
  82. elif nodeexp.search(buffer): #this is a node line
  83. node = re.split('\s+', buffer,2)[1]
  84. if node in node_set:
  85. output.write(buffer)
  86. else: #this is a support line
  87. output.write(buffer)
  88. buffer = input.readline()