Skip to content

Instantly share code, notes, and snippets.

@mattgillooly
Created February 23, 2015 21:33
Show Gist options
  • Select an option

  • Save mattgillooly/f056dfa230a37794f3a7 to your computer and use it in GitHub Desktop.

Select an option

Save mattgillooly/f056dfa230a37794f3a7 to your computer and use it in GitHub Desktop.
require 'graphviz'
class Step < Struct.new(:inputs, :outputs)
def each_edge(&block)
inputs.product(outputs).each(&block)
end
end
unless TARGET_FILENAME = ARGV[0]
fail "missing TARGET_FILENAME"
end
steps = []
current_step = nil
# Parse output from `drake --print`
$stdin.each_line do |line|
line_type, path = line.split
case line_type
when 'S'
steps << current_step if current_step
current_step = Step.new([], [])
when 'I'
current_step.inputs << path
when 'O'
current_step.outputs << path
else
$stderr.puts "Unknown line type: #{line_type.inspect}"
end
end
steps << current_step
# Create a new graph
g = GraphViz.new(:G, :type => :digraph)
# Create an edge between each input and output of each step
steps.each do |step|
step.each_edge do |input, output|
g.add_edges(input, output)
end
end
# Generate output image
g.output(:png => TARGET_FILENAME)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment