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