Siddharth Shukla | 8e64d90 | 2017-03-12 19:50:18 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 2 | # Copyright 2016 gRPC authors. |
Jan Tattermusch | be538a1 | 2016-01-28 14:58:15 -0800 | [diff] [blame] | 3 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 4 | # 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 Tattermusch | be538a1 | 2016-01-28 14:58:15 -0800 | [diff] [blame] | 7 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
Jan Tattermusch | be538a1 | 2016-01-28 14:58:15 -0800 | [diff] [blame] | 9 | # |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 10 | # 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 Tattermusch | be538a1 | 2016-01-28 14:58:15 -0800 | [diff] [blame] | 15 | |
| 16 | """Runs selected gRPC test/build tasks.""" |
| 17 | |
siddharthshukla | 0589e53 | 2016-07-07 16:08:01 +0200 | [diff] [blame] | 18 | from __future__ import print_function |
| 19 | |
Jan Tattermusch | be538a1 | 2016-01-28 14:58:15 -0800 | [diff] [blame] | 20 | import argparse |
Jan Tattermusch | be538a1 | 2016-01-28 14:58:15 -0800 | [diff] [blame] | 21 | import multiprocessing |
| 22 | import sys |
| 23 | |
Jan Tattermusch | 5c79a31 | 2016-12-20 11:02:50 +0100 | [diff] [blame] | 24 | import artifacts.artifact_targets as artifact_targets |
| 25 | import artifacts.distribtest_targets as distribtest_targets |
| 26 | import artifacts.package_targets as package_targets |
| 27 | import python_utils.jobset as jobset |
Jan Tattermusch | 47c7521 | 2017-04-21 13:27:42 +0200 | [diff] [blame] | 28 | import python_utils.report_utils as report_utils |
Jan Tattermusch | be538a1 | 2016-01-28 14:58:15 -0800 | [diff] [blame] | 29 | |
| 30 | _TARGETS = [] |
| 31 | _TARGETS += artifact_targets.targets() |
Jan Tattermusch | 38b519a | 2016-01-27 18:32:42 -0800 | [diff] [blame] | 32 | _TARGETS += distribtest_targets.targets() |
Jan Tattermusch | be538a1 | 2016-01-28 14:58:15 -0800 | [diff] [blame] | 33 | _TARGETS += package_targets.targets() |
| 34 | |
| 35 | def _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 | |
| 58 | argp = argparse.ArgumentParser(description='Runs build/test targets.') |
| 59 | argp.add_argument('-b', '--build', |
| 60 | choices=sorted(_BUILD_MAP.keys()), |
| 61 | nargs='+', |
| 62 | default=['all'], |
| 63 | help='Target name or target label to build.') |
| 64 | argp.add_argument('-f', '--filter', |
| 65 | choices=sorted(_BUILD_MAP.keys()), |
| 66 | nargs='+', |
| 67 | default=[], |
| 68 | help='Filter targets to build with AND semantics.') |
| 69 | argp.add_argument('-j', '--jobs', default=multiprocessing.cpu_count(), type=int) |
| 70 | argp.add_argument('-t', '--travis', |
| 71 | default=False, |
| 72 | action='store_const', |
| 73 | const=True) |
| 74 | |
| 75 | args = argp.parse_args() |
| 76 | |
| 77 | # Figure out which targets to build |
| 78 | targets = [] |
| 79 | for 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 |
| 83 | targets = [t for t in targets if all(f in t.labels for f in args.filter)] |
| 84 | targets = sorted(set(targets)) |
| 85 | |
| 86 | # Execute pre-build phase |
| 87 | prebuild_jobs = [] |
| 88 | for target in targets: |
| 89 | prebuild_jobs += target.pre_build_jobspecs() |
| 90 | if 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 | |
| 97 | build_jobs = [] |
| 98 | for target in targets: |
| 99 | build_jobs.append(target.build_jobspec()) |
| 100 | if not build_jobs: |
siddharthshukla | 0589e53 | 2016-07-07 16:08:01 +0200 | [diff] [blame] | 101 | print('Nothing to build.') |
Jan Tattermusch | be538a1 | 2016-01-28 14:58:15 -0800 | [diff] [blame] | 102 | sys.exit(1) |
| 103 | |
| 104 | jobset.message('START', 'Building targets.', do_newline=True) |
Jan Tattermusch | 47c7521 | 2017-04-21 13:27:42 +0200 | [diff] [blame] | 105 | num_failures, resultset = jobset.run( |
Jan Tattermusch | be538a1 | 2016-01-28 14:58:15 -0800 | [diff] [blame] | 106 | build_jobs, newline_on_success=True, maxjobs=args.jobs) |
Jan Tattermusch | 47c7521 | 2017-04-21 13:27:42 +0200 | [diff] [blame] | 107 | report_utils.render_junit_xml_report(resultset, 'report_taskrunner_sponge_log.xml', |
| 108 | suite_name='tasks') |
Jan Tattermusch | be538a1 | 2016-01-28 14:58:15 -0800 | [diff] [blame] | 109 | if num_failures == 0: |
| 110 | jobset.message('SUCCESS', 'All targets built successfully.', |
| 111 | do_newline=True) |
| 112 | else: |
| 113 | jobset.message('FAILED', 'Failed to build targets.', |
| 114 | do_newline=True) |
| 115 | sys.exit(1) |