Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | # Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 6 | """The Build API entry point.""" |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 7 | |
| 8 | from __future__ import print_function |
| 9 | |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 10 | import os |
Mike Frysinger | 898265b | 2020-02-10 23:49:12 -0500 | [diff] [blame] | 11 | import sys |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 12 | |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 13 | from chromite.api import api_config as api_config_lib |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 14 | from chromite.api import controller |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame^] | 15 | from chromite.api import message_util |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 16 | from chromite.api import router as router_lib |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 17 | from chromite.api.gen.chromite.api import build_api_config_pb2 |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 18 | from chromite.lib import commandline |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 19 | from chromite.lib import cros_build_lib |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 20 | from chromite.lib import cros_logging as logging |
| 21 | from chromite.lib import tee |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 22 | from chromite.utils import matching |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 23 | |
Mike Frysinger | 898265b | 2020-02-10 23:49:12 -0500 | [diff] [blame] | 24 | assert sys.version_info >= (3, 6), 'This module requires Python 3.6+' |
| 25 | |
| 26 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 27 | def GetParser(): |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 28 | """Build the argument parser.""" |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 29 | parser = commandline.ArgumentParser(description=__doc__) |
| 30 | |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 31 | parser.add_argument( |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 32 | 'service_method', |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 33 | help='The "chromite.api.Service/Method" that is being called.') |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame^] | 34 | # Input arguments. |
| 35 | input_args = parser.add_mutually_exclusive_group(required=True) |
| 36 | input_args.add_argument( |
| 37 | '--input-binary', |
| 38 | type='path', |
| 39 | help='Path to the protobuf binary serialization of the input message.') |
| 40 | input_args.add_argument( |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 41 | '--input-json', |
| 42 | type='path', |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 43 | help='Path to the JSON serialized input argument protobuf message.') |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame^] | 44 | # Output options. |
| 45 | parser.add_argument( |
| 46 | '--output-binary', |
| 47 | type='path', |
| 48 | help='The path to which the protobuf binary serialization of the ' |
| 49 | 'response message should be written.') |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 50 | parser.add_argument( |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 51 | '--output-json', |
| 52 | type='path', |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame^] | 53 | help='The path to which the JSON serialization of the response message ' |
| 54 | 'should be written.') |
| 55 | # Config options. |
| 56 | config_args = parser.add_mutually_exclusive_group() |
| 57 | config_args.add_argument( |
| 58 | '--config-binary', |
| 59 | type='path', |
| 60 | help='The path to the protobuf binary serialization of the Build API ' |
| 61 | 'call configs.') |
| 62 | config_args.add_argument( |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 63 | '--config-json', |
| 64 | type='path', |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame^] | 65 | help='The path to the JSON encoded Build API call configs.') |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 66 | # TODO(crbug.com/1040978): Remove after usages removed. |
| 67 | parser.add_argument( |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 68 | '--tee-log', |
| 69 | type='path', |
| 70 | help='The path to which stdout and stderr should be teed to.') |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 71 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 72 | return parser |
| 73 | |
| 74 | |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 75 | def _ParseArgs(argv, router): |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 76 | """Parse and validate arguments.""" |
| 77 | parser = GetParser() |
Alex Klein | 7cc434f | 2019-12-17 14:58:57 -0700 | [diff] [blame] | 78 | opts, unknown = parser.parse_known_args( |
| 79 | argv, namespace=commandline.ArgumentNamespace()) |
| 80 | parser.DoPostParseSetup(opts, unknown) |
| 81 | |
| 82 | if unknown: |
| 83 | logging.warning('Unknown args ignored: %s', ' '.join(unknown)) |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 84 | |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 85 | methods = router.ListMethods() |
George Engelbrecht | d3de8df | 2019-09-04 18:15:05 -0600 | [diff] [blame] | 86 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 87 | # Positional service_method argument validation. |
George Engelbrecht | d3de8df | 2019-09-04 18:15:05 -0600 | [diff] [blame] | 88 | parts = opts.service_method.split('/') |
| 89 | if len(parts) != 2: |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 90 | parser.error('Invalid service/method specification format. It should be ' |
| 91 | 'something like chromite.api.SdkService/Create.') |
George Engelbrecht | d3de8df | 2019-09-04 18:15:05 -0600 | [diff] [blame] | 92 | |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 93 | if opts.service_method not in methods: |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 94 | # Unknown method, try to match against known methods and make a suggestion. |
| 95 | # This is just for developer sanity, e.g. misspellings when testing. |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 96 | matched = matching.GetMostLikelyMatchedObject( |
| 97 | methods, opts.service_method, matched_score_threshold=0.6) |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 98 | error = 'Unrecognized service name.' |
| 99 | if matched: |
| 100 | error += '\nDid you mean: \n%s' % '\n'.join(matched) |
| 101 | parser.error(error) |
| 102 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 103 | opts.service = parts[0] |
| 104 | opts.method = parts[1] |
| 105 | |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame^] | 106 | # Input and output validation. |
| 107 | if not opts.output_binary and not opts.output_json: |
| 108 | parser.error('At least one output file must be specified.') |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 109 | |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame^] | 110 | if not os.path.exists(opts.input_binary or opts.input_json): |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 111 | parser.error('Input file does not exist.') |
| 112 | |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 113 | config_msg = build_api_config_pb2.BuildApiConfig() |
| 114 | if opts.config_json: |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame^] | 115 | handler = message_util.get_message_handler(opts.config_json, |
| 116 | message_util.FORMAT_JSON) |
| 117 | else: |
| 118 | handler = message_util.get_message_handler(opts.config_binary, |
| 119 | message_util.FORMAT_BINARY) |
| 120 | |
| 121 | if opts.config_json or opts.config_binary: |
| 122 | # We have been given a config, so read it. |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 123 | try: |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame^] | 124 | handler.read_into(config_msg) |
| 125 | except message_util.Error as e: |
| 126 | parser.error(str(e)) |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 127 | |
| 128 | opts.config = api_config_lib.build_config_from_proto(config_msg) |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame^] | 129 | opts.config_handler = handler |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 130 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 131 | opts.Freeze() |
| 132 | return opts |
| 133 | |
| 134 | |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame^] | 135 | def _get_io_handlers(opts): |
| 136 | """Build the input and output handlers.""" |
| 137 | if opts.input_binary: |
| 138 | input_handler = message_util.get_message_handler(opts.input_binary, |
| 139 | message_util.FORMAT_BINARY) |
| 140 | else: |
| 141 | input_handler = message_util.get_message_handler(opts.input_json, |
| 142 | message_util.FORMAT_JSON) |
| 143 | |
| 144 | output_handlers = [] |
| 145 | if opts.output_binary: |
| 146 | handler = message_util.get_message_handler(opts.output_binary, |
| 147 | message_util.FORMAT_BINARY) |
| 148 | output_handlers.append(handler) |
| 149 | if opts.output_json: |
| 150 | handler = message_util.get_message_handler(opts.output_json, |
| 151 | message_util.FORMAT_JSON) |
| 152 | output_handlers.append(handler) |
| 153 | |
| 154 | return input_handler, output_handlers |
| 155 | |
| 156 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 157 | def main(argv): |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 158 | with cros_build_lib.ContextManagerStack() as stack: |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 159 | router = router_lib.GetRouter() |
| 160 | opts = _ParseArgs(argv, router) |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 161 | |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 162 | if opts.tee_log: |
| 163 | stack.Add(tee.Tee, opts.tee_log) |
| 164 | logging.info('Teeing stdout and stderr to %s', opts.tee_log) |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 165 | if opts.config.log_path: |
| 166 | stack.Add(tee.Tee, opts.config.log_path) |
| 167 | logging.info('Teeing stdout and stderr to %s', opts.config.log_path) |
Michael Mortensen | a0515d9 | 2020-01-02 11:39:34 -0700 | [diff] [blame] | 168 | tee_log_env_value = os.environ.get('BUILD_API_TEE_LOG_FILE') |
| 169 | if tee_log_env_value: |
| 170 | stack.Add(tee.Tee, tee_log_env_value) |
| 171 | logging.info('Teeing stdout and stderr to env path %s', tee_log_env_value) |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 172 | |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 173 | if opts.config.mock_invalid: |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 174 | # --mock-invalid handling. We print error messages, but no output is ever |
| 175 | # set for validation errors, so we can handle it by just giving back the |
| 176 | # correct return code here. |
| 177 | return controller.RETURN_CODE_INVALID_INPUT |
| 178 | |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame^] | 179 | input_handler, output_handlers = _get_io_handlers(opts) |
| 180 | |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 181 | try: |
Alex Klein | e191ed6 | 2020-02-27 15:59:55 -0700 | [diff] [blame^] | 182 | return router.Route(opts.service, opts.method, opts.config, input_handler, |
| 183 | output_handlers, opts.config_handler) |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 184 | except router_lib.Error as e: |
| 185 | # Handle router_lib.Error derivatives nicely, but let anything else bubble |
| 186 | # up. |
| 187 | cros_build_lib.Die(e) |