DSAextract.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/usr/bin/env 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. from __future__ import print_function
  23. import re
  24. import string
  25. import sys
  26. if len(sys.argv) < 3:
  27. print('usage is ./DSAextract <dot_file_to_modify> \
  28. <output_file> [list of nodes to extract]')
  29. #open the input file
  30. input = open(sys.argv[1], 'r')
  31. #construct a set of node names
  32. node_name_set = set()
  33. for name in sys.argv[3:]:
  34. node_name_set |= set([name])
  35. #construct a list of compiled regular expressions from the
  36. #node_name_set
  37. regexp_list = []
  38. for name in node_name_set:
  39. regexp_list.append(re.compile(name))
  40. #used to see what kind of line we are on
  41. nodeexp = re.compile('Node')
  42. #used to check to see if the current line is an edge line
  43. arrowexp = re.compile('->')
  44. node_set = set()
  45. #read the file one line at a time
  46. buffer = input.readline()
  47. while buffer != '':
  48. #filter out the unnecessary checks on all the edge lines
  49. if not arrowexp.search(buffer):
  50. #check to see if this is a node we are looking for
  51. for regexp in regexp_list:
  52. #if this name is for the current node, add the dot variable name
  53. #for the node (it will be Node(hex number)) to our set of nodes
  54. if regexp.search(buffer):
  55. node_set |= set([re.split('\s+',buffer,2)[1]])
  56. break
  57. buffer = input.readline()
  58. #test code
  59. #print '\n'
  60. print(node_name_set)
  61. #print node_set
  62. #open the output file
  63. output = open(sys.argv[2], 'w')
  64. #start the second pass over the file
  65. input = open(sys.argv[1], 'r')
  66. buffer = input.readline()
  67. while buffer != '':
  68. #there are three types of lines we are looking for
  69. #1) node lines, 2) edge lines 3) support lines (like page size, etc)
  70. #is this an edge line?
  71. #note that this is no completely robust, if a none edge line
  72. #for some reason contains -> it will be missidentified
  73. #hand edit the file if this happens
  74. if arrowexp.search(buffer):
  75. #check to make sure that both nodes are in the node list
  76. #if they are print this to output
  77. nodes = arrowexp.split(buffer)
  78. nodes[0] = string.strip(nodes[0])
  79. nodes[1] = string.strip(nodes[1])
  80. if nodes[0][:13] in node_set and \
  81. nodes[1][:13] in node_set:
  82. output.write(buffer)
  83. elif nodeexp.search(buffer): #this is a node line
  84. node = re.split('\s+', buffer,2)[1]
  85. if node in node_set:
  86. output.write(buffer)
  87. else: #this is a support line
  88. output.write(buffer)
  89. buffer = input.readline()