Index: utils/extract_vplan.py =================================================================== --- /dev/null +++ utils/extract_vplan.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python + +# This script extracts the VPlan digraphs from the vectoriser debug messages +# and saves them in individual dot files (one for each plan). Optionally, and +# providing 'dot' is installed, it can also render the dot into a PNG file. + +import sys +import re +import argparse +import shutil +import subprocess + +parser = argparse.ArgumentParser() +parser.add_argument('--png', action='store_true') +args = parser.parse_args() + +dot = shutil.which('dot') +if args.png and not dot: + raise RuntimeError("Can't export to PNG without 'dot' in the system") + +VPlans = list() +Names = list() +CurVPlan = '' +for line in sys.stdin: + if not CurVPlan and re.match("digraph VPlan {", line): + CurVPlan = line + elif CurVPlan: + if re.match("}", line): + CurVPlan += line + VPlans.append(CurVPlan) + CurVPlan = '' + else: + m = re.search("(VF.*,UF.*), ", line) + if m: + name = re.sub('[^a-zA-Z0-9]', '', m.group(1)) + Names.append(name) + print("Found VPlan for " + name) + CurVPlan += line + +if len(VPlans) != len(Names): + raise RuntimeError("Incongruent number of VPlans and respective names") + +for i in range(len(VPlans)): + if args.png: + filename = 'VPlan' + Names[i] + '.png' + print("Exporting " + Names[i] + " to PNG via dot: " + filename) + p = subprocess.Popen([dot, '-Tpng', '-o', filename], encoding='utf-8', + stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + out, err = p.communicate(input=VPlans[i]) + if err: + raise RuntimeError("Error running dot: " + err) + + else: + filename = 'VPlan' + Names[i] + '.dot' + print("No visualisation requested, saving to file: " + filename) + out = open(filename, 'w') + out.write(VPlans[i]) + out.close()