blob: a065bb84cbe5c53fdc8805dc02b6caf20add8f5b [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
16"""Runs selected gRPC test/build tasks."""
17
siddharthshukla0589e532016-07-07 16:08:01 +020018from __future__ import print_function
19
Jan Tattermuschbe538a12016-01-28 14:58:15 -080020import argparse
Jan Tattermuschbe538a12016-01-28 14:58:15 -080021import multiprocessing
22import sys
23
Jan Tattermusch5c79a312016-12-20 11:02:50 +010024import artifacts.artifact_targets as artifact_targets
25import artifacts.distribtest_targets as distribtest_targets
26import artifacts.package_targets as package_targets
27import python_utils.jobset as jobset
Jan Tattermusch47c75212017-04-21 13:27:42 +020028import python_utils.report_utils as report_utils
Jan Tattermuschbe538a12016-01-28 14:58:15 -080029
30_TARGETS = []
31_TARGETS += artifact_targets.targets()
Jan Tattermusch38b519a2016-01-27 18:32:42 -080032_TARGETS += distribtest_targets.targets()
Jan Tattermuschbe538a12016-01-28 14:58:15 -080033_TARGETS += package_targets.targets()
34
35def _create_build_map():
36 """Maps task names and labels to list of tasks to be built."""
37 target_build_map = dict([(target.name, [target])
38 for target in _TARGETS])
39 if len(_TARGETS) > len(target_build_map.keys()):
40 raise Exception('Target names need to be unique')
41
42 label_build_map = {}
43 label_build_map['all'] = [t for t in _TARGETS] # to build all targets
44 for target in _TARGETS:
45 for label in target.labels:
46 if label in label_build_map:
47 label_build_map[label].append(target)
48 else:
49 label_build_map[label] = [target]
50
51 if set(target_build_map.keys()).intersection(label_build_map.keys()):
52 raise Exception('Target names need to be distinct from label names')
53 return dict( target_build_map.items() + label_build_map.items())
54
55
56_BUILD_MAP = _create_build_map()
57
58argp = argparse.ArgumentParser(description='Runs build/test targets.')
59argp.add_argument('-b', '--build',
60 choices=sorted(_BUILD_MAP.keys()),
61 nargs='+',
62 default=['all'],
63 help='Target name or target label to build.')
64argp.add_argument('-f', '--filter',
65 choices=sorted(_BUILD_MAP.keys()),
66 nargs='+',
67 default=[],
68 help='Filter targets to build with AND semantics.')
69argp.add_argument('-j', '--jobs', default=multiprocessing.cpu_count(), type=int)
70argp.add_argument('-t', '--travis',
71 default=False,
72 action='store_const',
73 const=True)
74
75args = argp.parse_args()
76
77# Figure out which targets to build
78targets = []
79for label in args.build:
80 targets += _BUILD_MAP[label]
81
82# Among targets selected by -b, filter out those that don't match the filter
83targets = [t for t in targets if all(f in t.labels for f in args.filter)]
84targets = sorted(set(targets))
85
86# Execute pre-build phase
87prebuild_jobs = []
88for target in targets:
89 prebuild_jobs += target.pre_build_jobspecs()
90if prebuild_jobs:
91 num_failures, _ = jobset.run(
92 prebuild_jobs, newline_on_success=True, maxjobs=args.jobs)
93 if num_failures != 0:
94 jobset.message('FAILED', 'Pre-build phase failed.', do_newline=True)
95 sys.exit(1)
96
97build_jobs = []
98for target in targets:
99 build_jobs.append(target.build_jobspec())
100if not build_jobs:
siddharthshukla0589e532016-07-07 16:08:01 +0200101 print('Nothing to build.')
Jan Tattermuschbe538a12016-01-28 14:58:15 -0800102 sys.exit(1)
103
104jobset.message('START', 'Building targets.', do_newline=True)
Jan Tattermusch47c75212017-04-21 13:27:42 +0200105num_failures, resultset = jobset.run(
Jan Tattermuschbe538a12016-01-28 14:58:15 -0800106 build_jobs, newline_on_success=True, maxjobs=args.jobs)
Jan Tattermusch47c75212017-04-21 13:27:42 +0200107report_utils.render_junit_xml_report(resultset, 'report_taskrunner_sponge_log.xml',
108 suite_name='tasks')
Jan Tattermuschbe538a12016-01-28 14:58:15 -0800109if num_failures == 0:
110 jobset.message('SUCCESS', 'All targets built successfully.',
111 do_newline=True)
112else:
113 jobset.message('FAILED', 'Failed to build targets.',
114 do_newline=True)
115 sys.exit(1)