blob: 794db6e1be0e854c53f2116da18be815e2f22235 [file] [log] [blame]
Siddharth Shukla8e64d902017-03-12 19:50:18 +01001#!/usr/bin/env python
Jan Tattermusch7897ae92017-06-07 22:57:36 +02002# Copyright 2016 gRPC authors.
Jan Tattermuschbe538a12016-01-28 14:58:15 -08003#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02004# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
Jan Tattermuschbe538a12016-01-28 14:58:15 -08007#
Jan Tattermusch7897ae92017-06-07 22:57:36 +02008# http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermuschbe538a12016-01-28 14:58:15 -08009#
Jan Tattermusch7897ae92017-06-07 22:57:36 +020010# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
Jan Tattermuschbe538a12016-01-28 14:58:15 -080015"""Runs selected gRPC test/build tasks."""
16
siddharthshukla0589e532016-07-07 16:08:01 +020017from __future__ import print_function
18
Jan Tattermuschbe538a12016-01-28 14:58:15 -080019import argparse
Jan Tattermuschbe538a12016-01-28 14:58:15 -080020import multiprocessing
21import sys
22
Jan Tattermusch5c79a312016-12-20 11:02:50 +010023import artifacts.artifact_targets as artifact_targets
24import artifacts.distribtest_targets as distribtest_targets
25import artifacts.package_targets as package_targets
26import python_utils.jobset as jobset
Jan Tattermusch47c75212017-04-21 13:27:42 +020027import python_utils.report_utils as report_utils
Jan Tattermuschbe538a12016-01-28 14:58:15 -080028
29_TARGETS = []
30_TARGETS += artifact_targets.targets()
Jan Tattermusch38b519a2016-01-27 18:32:42 -080031_TARGETS += distribtest_targets.targets()
Jan Tattermuschbe538a12016-01-28 14:58:15 -080032_TARGETS += package_targets.targets()
33
ncteisen888093c2017-12-11 18:00:40 -080034
Jan Tattermuschbe538a12016-01-28 14:58:15 -080035def _create_build_map():
ncteisen888093c2017-12-11 18:00:40 -080036 """Maps task names and labels to list of tasks to be built."""
37 target_build_map = dict([(target.name, [target]) for target in _TARGETS])
38 if len(_TARGETS) > len(target_build_map.keys()):
39 raise Exception('Target names need to be unique')
Jan Tattermuschbe538a12016-01-28 14:58:15 -080040
ncteisen888093c2017-12-11 18:00:40 -080041 label_build_map = {}
42 label_build_map['all'] = [t for t in _TARGETS] # to build all targets
43 for target in _TARGETS:
44 for label in target.labels:
45 if label in label_build_map:
46 label_build_map[label].append(target)
47 else:
48 label_build_map[label] = [target]
Jan Tattermuschbe538a12016-01-28 14:58:15 -080049
ncteisen888093c2017-12-11 18:00:40 -080050 if set(target_build_map.keys()).intersection(label_build_map.keys()):
51 raise Exception('Target names need to be distinct from label names')
52 return dict(target_build_map.items() + label_build_map.items())
Jan Tattermuschbe538a12016-01-28 14:58:15 -080053
54
55_BUILD_MAP = _create_build_map()
56
57argp = argparse.ArgumentParser(description='Runs build/test targets.')
ncteisen888093c2017-12-11 18:00:40 -080058argp.add_argument(
59 '-b',
60 '--build',
61 choices=sorted(_BUILD_MAP.keys()),
62 nargs='+',
63 default=['all'],
64 help='Target name or target label to build.')
65argp.add_argument(
66 '-f',
67 '--filter',
68 choices=sorted(_BUILD_MAP.keys()),
69 nargs='+',
70 default=[],
71 help='Filter targets to build with AND semantics.')
Jan Tattermuschbe538a12016-01-28 14:58:15 -080072argp.add_argument('-j', '--jobs', default=multiprocessing.cpu_count(), type=int)
ncteisen888093c2017-12-11 18:00:40 -080073argp.add_argument(
74 '-t', '--travis', default=False, action='store_const', const=True)
Jan Tattermuschbe538a12016-01-28 14:58:15 -080075
76args = argp.parse_args()
77
78# Figure out which targets to build
79targets = []
80for label in args.build:
ncteisen888093c2017-12-11 18:00:40 -080081 targets += _BUILD_MAP[label]
Jan Tattermuschbe538a12016-01-28 14:58:15 -080082
83# Among targets selected by -b, filter out those that don't match the filter
84targets = [t for t in targets if all(f in t.labels for f in args.filter)]
85targets = sorted(set(targets))
86
87# Execute pre-build phase
88prebuild_jobs = []
89for target in targets:
ncteisen888093c2017-12-11 18:00:40 -080090 prebuild_jobs += target.pre_build_jobspecs()
Jan Tattermuschbe538a12016-01-28 14:58:15 -080091if prebuild_jobs:
ncteisen888093c2017-12-11 18:00:40 -080092 num_failures, _ = jobset.run(
93 prebuild_jobs, newline_on_success=True, maxjobs=args.jobs)
94 if num_failures != 0:
95 jobset.message('FAILED', 'Pre-build phase failed.', do_newline=True)
96 sys.exit(1)
Jan Tattermuschbe538a12016-01-28 14:58:15 -080097
98build_jobs = []
99for target in targets:
ncteisen888093c2017-12-11 18:00:40 -0800100 build_jobs.append(target.build_jobspec())
Jan Tattermuschbe538a12016-01-28 14:58:15 -0800101if not build_jobs:
ncteisen888093c2017-12-11 18:00:40 -0800102 print('Nothing to build.')
103 sys.exit(1)
Jan Tattermuschbe538a12016-01-28 14:58:15 -0800104
105jobset.message('START', 'Building targets.', do_newline=True)
Jan Tattermusch47c75212017-04-21 13:27:42 +0200106num_failures, resultset = jobset.run(
Jan Tattermuschbe538a12016-01-28 14:58:15 -0800107 build_jobs, newline_on_success=True, maxjobs=args.jobs)
ncteisen888093c2017-12-11 18:00:40 -0800108report_utils.render_junit_xml_report(
109 resultset, 'report_taskrunner_sponge_log.xml', suite_name='tasks')
Jan Tattermuschbe538a12016-01-28 14:58:15 -0800110if num_failures == 0:
ncteisen888093c2017-12-11 18:00:40 -0800111 jobset.message(
112 'SUCCESS', 'All targets built successfully.', do_newline=True)
Jan Tattermuschbe538a12016-01-28 14:58:15 -0800113else:
ncteisen888093c2017-12-11 18:00:40 -0800114 jobset.message('FAILED', 'Failed to build targets.', do_newline=True)
115 sys.exit(1)