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