blob: 81a1fc9a003f4b00cc3b6d412ec5f90d330f98aa [file] [log] [blame]
# -*- coding: utf-8 -*-
# Copyright 2017 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.
"""Find suite ids corresponding to builds.."""
from __future__ import print_function
import json
import re
import sys
from chromite.cbuildbot import topology
from chromite.cli.cros import cros_cidbcreds
from chromite.lib import cidb
from chromite.lib import commandline
from chromite.lib import cros_logging as logging
from chromite.lib import milo
SUITE_RE = re.compile(
def GetParser():
"""Creates the argparse parser."""
parser = commandline.ArgumentParser(description=__doc__)
parser.add_argument('--service_acct_json', type=str, action='store',
help='Path to service account credentials JSON file.')
parser.add_argument('build_ids', type=str, nargs='*', action='store',
help='Build ids to report on.')
parser.add_argument('--cred_dir', type=str, action='store',
help='Database credentials directory with certificates '
'and other connection information. Obtain your '
'credentials at go/cros-cidb-admin .')
parser.add_argument('--build_config', type=str, action='store',
help='Build config to report on.')
parser.add_argument('--num_builds', type=int, action='store', default=1,
help='Number of master builds to gather.')
parser.add_argument('--milo_host', type=str, action='store',
help='URL of MILO host.')
parser.add_argument('--output', '-o', type=str, action='store',
help='Filename to write to.')
parser.add_argument('--json', action='store_true',
help='Output as JSON.')
parser.add_argument('--allow_empty', action='store_true',
help='Include builds with no suites.')
parser.add_argument('--no_suites', action='store_true',
help='Do not include list of suites.')
return parser
def GetSuites(milo_client, waterfall, builder_name, build_number):
"""Gets a list of suites ids for a given build from Milo.
milo_client: MiloClient object.
waterfall: Buildbot waterfall.
builder_name: Buildbot builder name.
build_number: Buidlbot build number.
A set of suite ids.
buildinfo = milo_client.BuildInfoGetBuildbot(waterfall, builder_name,
suite_ids = set()
for step in buildinfo['steps']:
for link in buildinfo['steps'][step].get('otherLinks', []):
if link.get('label') == 'Link to suite':
url = link.get('url')
m =
if m:
logging.error('Unable to parse suite link for %s: %s' %
(buildinfo['steps'][step]['name'], url))
return suite_ids
def MakeBuildEntry(db, milo_client, build_id,
build_status=None, no_suites=False):
"""Generates an entry for a single build.
db: CIDB DB connection.
milo_client: MiloClient object.
build_id: The CIDB build ID.
build_status: CIDB build status dictionary.
no_suites: Boolean indiciating if suites do not need to be included.
Dictionary for the build.
if build_status is None:
build_status = db.GetBuildStatus(build_id)
waterfall = build_status['waterfall']
builder_name = build_status['builder_name']
build_number = build_status['build_number']
build_entry = {
'build_id': build_status['id'],
'waterfall': waterfall,
'builder_name': builder_name,
'build_number': build_number,
if not no_suites:
build_entry['suite_ids'] = list(GetSuites(milo_client, waterfall,
builder_name, build_number))
return build_entry
def StringifyBuildEntry(entry):
"""Pretty print a build entry.
entry: a build entry from MakeBuildEntry.
A printable string.
return '%s %s %d: %s' % (entry['build_id'], entry['builder_name'],
' '.join(entry.get('suite_ids', [])))
def main(argv):
# Parse command line arguments.
parser = GetParser()
options = parser.parse_args(argv)
# Set up clients.
credentials = options.cred_dir or cros_cidbcreds.CheckAndGetCIDBCreds()
db = cidb.CIDBConnection(credentials)
milo_client = milo.MiloClient(options.service_acct_json,
builds = []
# Add explicitly requested builds.
if options.build_ids:
for build_id in options.build_ids:
builds.append(MakeBuildEntry(db, milo_client, build_id,
# Search for builds by build config.
if options.build_config:
masters = db.GetBuildHistory(options.build_config, options.num_builds,
for master in masters:
builds.append(MakeBuildEntry(db, milo_client, master['id'], master,
statuses = db.GetSlaveStatuses(master['id'])
for slave in statuses:
builds.append(MakeBuildEntry(db, milo_client, slave['id'], slave,
if not options.allow_empty and not options.no_suites:
builds = [b for b in builds if len(b.get('suite_ids', []))]
# Output results.
with open(options.output, 'w') if options.output else sys.stdout as f:
if options.json:
output = {
'builds': builds,
json.dump(output, f)
for b in builds: