summaryrefslogtreecommitdiff
path: root/scripts/cros_show_waterfall_layout.py
blob: 9bad367c613f5fe086b08816002b00646c6d4437 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Copyright 2015 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Show the builder layout for CrOS waterfalls."""

from __future__ import print_function

import json
import sys

from chromite.cbuildbot import config_lib
from chromite.lib import commandline


def _FormatText(data, out):
  """Formatter function for text output."""
  output = lambda *a: print(*a, file=out)

  for waterfall in sorted(data.iterkeys()):
    layout = data[waterfall]
    if not layout:
      continue

    output('== %s ==' % (waterfall,))
    for board in sorted(layout.iterkeys()):
      board_layout = layout[board]
      children = board_layout.get('children', ())
      if not children:
        output('%(name)s' % board_layout)
      else:
        output('[%(name)s]' % board_layout)
      for child in sorted(board_layout.get('children', ())):
        output('  %s' % (child,))
    output()


def _FormatJson(data, out):
  """Formatter function for JSON output."""
  json.dump(data, out, sort_keys=True)


_FORMATTERS = {
    'text': _FormatText,
    'json': _FormatJson,
}


def _ParseArguments(argv):
  parser = commandline.ArgumentParser(description=__doc__)

  parser.add_argument('--format', default='text',
                      choices=sorted(_FORMATTERS.iterkeys()),
                      help='Choose output format.')
  opts = parser.parse_args(argv)
  opts.format = _FORMATTERS[opts.format]
  opts.Freeze()
  return opts


def main(argv):
  opts = _ParseArguments(argv)

  site_config = config_lib.GetConfig()

  layout = {}
  for config_name, config in site_config.iteritems():
    active_waterfall = config['active_waterfall']
    if not active_waterfall:
      continue

    waterfall_layout = layout.setdefault(active_waterfall, {})
    board_layout = waterfall_layout[config_name] = {
        'name': config_name,
    }

    children = config['child_configs']
    if children:
      board_layout['children'] = [c['name'] for c in children]
  opts.format(layout, sys.stdout)