17c15b630SRenato Golin#!/usr/bin/env python 27c15b630SRenato Golin 37c15b630SRenato Golin# This script extracts the VPlan digraphs from the vectoriser debug messages 47c15b630SRenato Golin# and saves them in individual dot files (one for each plan). Optionally, and 57c15b630SRenato Golin# providing 'dot' is installed, it can also render the dot into a PNG file. 67c15b630SRenato Golin 74a27478aSSerge Gueltonfrom __future__ import print_function 84a27478aSSerge Guelton 97c15b630SRenato Golinimport sys 107c15b630SRenato Golinimport re 117c15b630SRenato Golinimport argparse 127c15b630SRenato Golinimport shutil 137c15b630SRenato Golinimport subprocess 147c15b630SRenato Golin 157c15b630SRenato Golinparser = argparse.ArgumentParser() 16*b71edfaaSTobias Hietaparser.add_argument("--png", action="store_true") 177c15b630SRenato Golinargs = parser.parse_args() 187c15b630SRenato Golin 19*b71edfaaSTobias Hietadot = shutil.which("dot") 207c15b630SRenato Golinif args.png and not dot: 217c15b630SRenato Golin raise RuntimeError("Can't export to PNG without 'dot' in the system") 227c15b630SRenato Golin 237c15b630SRenato Golinpattern = re.compile(r"(digraph VPlan {.*?\n})", re.DOTALL) 247c15b630SRenato Golinmatches = re.findall(pattern, sys.stdin.read()) 257c15b630SRenato Golin 267c15b630SRenato Golinfor vplan in matches: 27c7899395SMauri Mustonen m = re.search("graph \[.+(VF=.+,UF.+)", vplan) 287c15b630SRenato Golin if not m: 297c15b630SRenato Golin raise ValueError("Can't get the right VPlan name") 30*b71edfaaSTobias Hieta name = re.sub("[^a-zA-Z0-9]", "", m.group(1)) 317c15b630SRenato Golin 327c15b630SRenato Golin if args.png: 33*b71edfaaSTobias Hieta filename = "VPlan" + name + ".png" 347c15b630SRenato Golin print("Exporting " + name + " to PNG via dot: " + filename) 35*b71edfaaSTobias Hieta p = subprocess.Popen( 36*b71edfaaSTobias Hieta [dot, "-Tpng", "-o", filename], 37*b71edfaaSTobias Hieta encoding="utf-8", 387c15b630SRenato Golin stdin=subprocess.PIPE, 397c15b630SRenato Golin stdout=subprocess.PIPE, 40*b71edfaaSTobias Hieta stderr=subprocess.PIPE, 41*b71edfaaSTobias Hieta ) 427c15b630SRenato Golin out, err = p.communicate(input=vplan) 437c15b630SRenato Golin if err: 447c15b630SRenato Golin raise RuntimeError("Error running dot: " + err) 457c15b630SRenato Golin 467c15b630SRenato Golin else: 47*b71edfaaSTobias Hieta filename = "VPlan" + name + ".dot" 487c15b630SRenato Golin print("Exporting " + name + " to DOT: " + filename) 49*b71edfaaSTobias Hieta with open(filename, "w") as out: 507c15b630SRenato Golin out.write(vplan) 51