blob: ac0055d2e798ad0e57b0298d8eb25c0206fc03f1 [file] [log] [blame]
Renato Golin7c15b632018-10-16 09:37:52 +00001#!/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
7import sys
8import re
9import argparse
10import shutil
11import subprocess
12
13parser = argparse.ArgumentParser()
14parser.add_argument('--png', action='store_true')
15args = parser.parse_args()
16
17dot = shutil.which('dot')
18if args.png and not dot:
19 raise RuntimeError("Can't export to PNG without 'dot' in the system")
20
21pattern = re.compile(r"(digraph VPlan {.*?\n})",re.DOTALL)
22matches = re.findall(pattern, sys.stdin.read())
23
24for vplan in matches:
25 m = re.search("graph \[.+(VF=.+,UF.+), ", vplan)
26 if not m:
27 raise ValueError("Can't get the right VPlan name")
28 name = re.sub('[^a-zA-Z0-9]', '', m.group(1))
29
30 if args.png:
31 filename = 'VPlan' + name + '.png'
32 print("Exporting " + name + " to PNG via dot: " + filename)
33 p = subprocess.Popen([dot, '-Tpng', '-o', filename],
34 encoding='utf-8',
35 stdin=subprocess.PIPE,
36 stdout=subprocess.PIPE,
37 stderr=subprocess.PIPE)
38 out, err = p.communicate(input=vplan)
39 if err:
40 raise RuntimeError("Error running dot: " + err)
41
42 else:
43 filename = 'VPlan' + name + '.dot'
44 print("Exporting " + name + " to DOT: " + filename)
45 with open(filename, 'w') as out:
46 out.write(vplan)