Nathaniel Manista | cbf21da | 2016-02-02 22:17:44 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python2.7 |
Jan Tattermusch | be538a1 | 2016-01-28 14:58:15 -0800 | [diff] [blame] | 2 | # Copyright 2016, Google Inc. |
| 3 | # All rights reserved. |
| 4 | # |
| 5 | # Redistribution and use in source and binary forms, with or without |
| 6 | # modification, are permitted provided that the following conditions are |
| 7 | # met: |
| 8 | # |
| 9 | # * Redistributions of source code must retain the above copyright |
| 10 | # notice, this list of conditions and the following disclaimer. |
| 11 | # * Redistributions in binary form must reproduce the above |
| 12 | # copyright notice, this list of conditions and the following disclaimer |
| 13 | # in the documentation and/or other materials provided with the |
| 14 | # distribution. |
| 15 | # * Neither the name of Google Inc. nor the names of its |
| 16 | # contributors may be used to endorse or promote products derived from |
| 17 | # this software without specific prior written permission. |
| 18 | # |
| 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | """Runs selected gRPC test/build tasks.""" |
| 32 | |
siddharthshukla | 0589e53 | 2016-07-07 16:08:01 +0200 | [diff] [blame] | 33 | from __future__ import print_function |
| 34 | |
Jan Tattermusch | be538a1 | 2016-01-28 14:58:15 -0800 | [diff] [blame] | 35 | import argparse |
Jan Tattermusch | be538a1 | 2016-01-28 14:58:15 -0800 | [diff] [blame] | 36 | import multiprocessing |
| 37 | import sys |
| 38 | |
Jan Tattermusch | 5c79a31 | 2016-12-20 11:02:50 +0100 | [diff] [blame] | 39 | import artifacts.artifact_targets as artifact_targets |
| 40 | import artifacts.distribtest_targets as distribtest_targets |
| 41 | import artifacts.package_targets as package_targets |
| 42 | import python_utils.jobset as jobset |
Jan Tattermusch | be538a1 | 2016-01-28 14:58:15 -0800 | [diff] [blame] | 43 | |
| 44 | _TARGETS = [] |
| 45 | _TARGETS += artifact_targets.targets() |
Jan Tattermusch | 38b519a | 2016-01-27 18:32:42 -0800 | [diff] [blame] | 46 | _TARGETS += distribtest_targets.targets() |
Jan Tattermusch | be538a1 | 2016-01-28 14:58:15 -0800 | [diff] [blame] | 47 | _TARGETS += package_targets.targets() |
| 48 | |
| 49 | def _create_build_map(): |
| 50 | """Maps task names and labels to list of tasks to be built.""" |
| 51 | target_build_map = dict([(target.name, [target]) |
| 52 | for target in _TARGETS]) |
| 53 | if len(_TARGETS) > len(target_build_map.keys()): |
| 54 | raise Exception('Target names need to be unique') |
| 55 | |
| 56 | label_build_map = {} |
| 57 | label_build_map['all'] = [t for t in _TARGETS] # to build all targets |
| 58 | for target in _TARGETS: |
| 59 | for label in target.labels: |
| 60 | if label in label_build_map: |
| 61 | label_build_map[label].append(target) |
| 62 | else: |
| 63 | label_build_map[label] = [target] |
| 64 | |
| 65 | if set(target_build_map.keys()).intersection(label_build_map.keys()): |
| 66 | raise Exception('Target names need to be distinct from label names') |
| 67 | return dict( target_build_map.items() + label_build_map.items()) |
| 68 | |
| 69 | |
| 70 | _BUILD_MAP = _create_build_map() |
| 71 | |
| 72 | argp = argparse.ArgumentParser(description='Runs build/test targets.') |
| 73 | argp.add_argument('-b', '--build', |
| 74 | choices=sorted(_BUILD_MAP.keys()), |
| 75 | nargs='+', |
| 76 | default=['all'], |
| 77 | help='Target name or target label to build.') |
| 78 | argp.add_argument('-f', '--filter', |
| 79 | choices=sorted(_BUILD_MAP.keys()), |
| 80 | nargs='+', |
| 81 | default=[], |
| 82 | help='Filter targets to build with AND semantics.') |
| 83 | argp.add_argument('-j', '--jobs', default=multiprocessing.cpu_count(), type=int) |
| 84 | argp.add_argument('-t', '--travis', |
| 85 | default=False, |
| 86 | action='store_const', |
| 87 | const=True) |
| 88 | |
| 89 | args = argp.parse_args() |
| 90 | |
| 91 | # Figure out which targets to build |
| 92 | targets = [] |
| 93 | for label in args.build: |
| 94 | targets += _BUILD_MAP[label] |
| 95 | |
| 96 | # Among targets selected by -b, filter out those that don't match the filter |
| 97 | targets = [t for t in targets if all(f in t.labels for f in args.filter)] |
| 98 | targets = sorted(set(targets)) |
| 99 | |
| 100 | # Execute pre-build phase |
| 101 | prebuild_jobs = [] |
| 102 | for target in targets: |
| 103 | prebuild_jobs += target.pre_build_jobspecs() |
| 104 | if prebuild_jobs: |
| 105 | num_failures, _ = jobset.run( |
| 106 | prebuild_jobs, newline_on_success=True, maxjobs=args.jobs) |
| 107 | if num_failures != 0: |
| 108 | jobset.message('FAILED', 'Pre-build phase failed.', do_newline=True) |
| 109 | sys.exit(1) |
| 110 | |
| 111 | build_jobs = [] |
| 112 | for target in targets: |
| 113 | build_jobs.append(target.build_jobspec()) |
| 114 | if not build_jobs: |
siddharthshukla | 0589e53 | 2016-07-07 16:08:01 +0200 | [diff] [blame] | 115 | print('Nothing to build.') |
Jan Tattermusch | be538a1 | 2016-01-28 14:58:15 -0800 | [diff] [blame] | 116 | sys.exit(1) |
| 117 | |
| 118 | jobset.message('START', 'Building targets.', do_newline=True) |
| 119 | num_failures, _ = jobset.run( |
| 120 | build_jobs, newline_on_success=True, maxjobs=args.jobs) |
| 121 | if num_failures == 0: |
| 122 | jobset.message('SUCCESS', 'All targets built successfully.', |
| 123 | do_newline=True) |
| 124 | else: |
| 125 | jobset.message('FAILED', 'Failed to build targets.', |
| 126 | do_newline=True) |
| 127 | sys.exit(1) |