Dan Jacques | 2bcefd2 | 2015-05-18 14:57:00 -0700 | [diff] [blame] | 1 | # Copyright 2015 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Show the builder layout for CrOS waterfalls.""" |
| 6 | |
| 7 | from __future__ import print_function |
| 8 | |
| 9 | import json |
| 10 | import sys |
| 11 | |
Don Garrett | b44956b | 2015-06-05 17:28:06 -0700 | [diff] [blame] | 12 | from chromite.cbuildbot import config_lib |
Dan Jacques | 2bcefd2 | 2015-05-18 14:57:00 -0700 | [diff] [blame] | 13 | from chromite.lib import commandline |
| 14 | |
| 15 | |
| 16 | def _FormatText(data, out): |
| 17 | """Formatter function for text output.""" |
| 18 | output = lambda *a: print(*a, file=out) |
| 19 | |
| 20 | for waterfall in sorted(data.iterkeys()): |
| 21 | layout = data[waterfall] |
| 22 | if not layout: |
| 23 | continue |
| 24 | |
| 25 | output('== %s ==' % (waterfall,)) |
| 26 | for board in sorted(layout.iterkeys()): |
| 27 | board_layout = layout[board] |
| 28 | children = board_layout.get('children', ()) |
| 29 | if not children: |
| 30 | output('%(name)s' % board_layout) |
| 31 | else: |
| 32 | output('[%(name)s]' % board_layout) |
| 33 | for child in sorted(board_layout.get('children', ())): |
| 34 | output(' %s' % (child,)) |
| 35 | output() |
| 36 | |
| 37 | |
| 38 | def _FormatJson(data, out): |
| 39 | """Formatter function for JSON output.""" |
| 40 | json.dump(data, out, sort_keys=True) |
| 41 | |
| 42 | |
| 43 | _FORMATTERS = { |
| 44 | 'text': _FormatText, |
| 45 | 'json': _FormatJson, |
| 46 | } |
| 47 | |
| 48 | |
| 49 | def _ParseArguments(argv): |
| 50 | parser = commandline.ArgumentParser(description=__doc__) |
| 51 | |
| 52 | parser.add_argument('--format', default='text', |
| 53 | choices=sorted(_FORMATTERS.iterkeys()), |
| 54 | help='Choose output format.') |
| 55 | opts = parser.parse_args(argv) |
| 56 | opts.format = _FORMATTERS[opts.format] |
| 57 | opts.Freeze() |
| 58 | return opts |
| 59 | |
| 60 | |
| 61 | def main(argv): |
| 62 | opts = _ParseArguments(argv) |
| 63 | |
Don Garrett | 737cadd | 2015-10-22 17:43:47 -0700 | [diff] [blame^] | 64 | site_config = config_lib.GetConfig() |
Don Garrett | b44956b | 2015-06-05 17:28:06 -0700 | [diff] [blame] | 65 | |
Dan Jacques | 2bcefd2 | 2015-05-18 14:57:00 -0700 | [diff] [blame] | 66 | layout = {} |
Don Garrett | b44956b | 2015-06-05 17:28:06 -0700 | [diff] [blame] | 67 | for config_name, config in site_config.iteritems(): |
Dan Jacques | 2bcefd2 | 2015-05-18 14:57:00 -0700 | [diff] [blame] | 68 | active_waterfall = config['active_waterfall'] |
| 69 | if not active_waterfall: |
| 70 | continue |
| 71 | |
| 72 | waterfall_layout = layout.setdefault(active_waterfall, {}) |
| 73 | board_layout = waterfall_layout[config_name] = { |
| 74 | 'name': config_name, |
| 75 | } |
| 76 | |
| 77 | children = config['child_configs'] |
| 78 | if children: |
| 79 | board_layout['children'] = [c['name'] for c in children] |
| 80 | opts.format(layout, sys.stdout) |