def OutputGraphViz(scenario, final_tasks, output):
  """Outputs the build dependency graph covered by this scenario.

  Args:
    scenario: The generated scenario.
    final_tasks: The final tasks used to generate the scenario.
    output: A file-like output stream to receive the dot file.

  Graph interpretations:
    - Final tasks (the one that where directly appended) are box shaped.
    - Non final tasks are ellipse shaped.
    - Frozen tasks have a blue shape.
  """
  task_execution_ids = {t: i for i, t in enumerate(scenario)}
  tasks_node_ids = dict()

  def GetTaskNodeId(task):
    if task in tasks_node_ids:
      return tasks_node_ids[task]
    node_id = len(tasks_node_ids)
    node_label = task.name
    node_color = 'blue'
    node_shape = 'ellipse'
    if task in task_execution_ids:
      node_color = 'black'
      node_label = str(task_execution_ids[task]) + ': ' + node_label
    if task in final_tasks:
      node_shape = 'box'
    output.write('  n{} [label="{}", color={}, shape={}];\n'.format(
        node_id, node_label, node_color, node_shape))
    tasks_node_ids[task] = node_id
    return node_id

  output.write('digraph graphname {\n')
  for task in scenario:
    task_node_id = GetTaskNodeId(task)
    for dep in task._dependencies:
      dep_node_id = GetTaskNodeId(dep)
      output.write('  n{} -> n{};\n'.format(dep_node_id, task_node_id))
  output.write('}\n')