extract_vplan.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env python
  2. # This script extracts the VPlan digraphs from the vectoriser debug messages
  3. # and saves them in individual dot files (one for each plan). Optionally, and
  4. # providing 'dot' is installed, it can also render the dot into a PNG file.
  5. from __future__ import print_function
  6. import sys
  7. import re
  8. import argparse
  9. import shutil
  10. import subprocess
  11. parser = argparse.ArgumentParser()
  12. parser.add_argument('--png', action='store_true')
  13. args = parser.parse_args()
  14. dot = shutil.which('dot')
  15. if args.png and not dot:
  16. raise RuntimeError("Can't export to PNG without 'dot' in the system")
  17. pattern = re.compile(r"(digraph VPlan {.*?\n})",re.DOTALL)
  18. matches = re.findall(pattern, sys.stdin.read())
  19. for vplan in matches:
  20. m = re.search("graph \[.+(VF=.+,UF.+), ", vplan)
  21. if not m:
  22. raise ValueError("Can't get the right VPlan name")
  23. name = re.sub('[^a-zA-Z0-9]', '', m.group(1))
  24. if args.png:
  25. filename = 'VPlan' + name + '.png'
  26. print("Exporting " + name + " to PNG via dot: " + filename)
  27. p = subprocess.Popen([dot, '-Tpng', '-o', filename],
  28. encoding='utf-8',
  29. stdin=subprocess.PIPE,
  30. stdout=subprocess.PIPE,
  31. stderr=subprocess.PIPE)
  32. out, err = p.communicate(input=vplan)
  33. if err:
  34. raise RuntimeError("Error running dot: " + err)
  35. else:
  36. filename = 'VPlan' + name + '.dot'
  37. print("Exporting " + name + " to DOT: " + filename)
  38. with open(filename, 'w') as out:
  39. out.write(vplan)