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