blob: c9908de14640e94138827ca2101d174b262ae155 [file] [log] [blame]
Alex Kleinf4dc4f52018-12-05 13:55:12 -07001# 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 Kleind815ca62020-01-10 12:21:30 -07005"""The Build API entry point."""
Alex Kleinf4dc4f52018-12-05 13:55:12 -07006
Alex Klein5bcb4d22019-03-21 13:51:54 -06007import os
Alex Kleind815ca62020-01-10 12:21:30 -07008
Alex Klein69339cc2019-07-22 14:08:35 -06009from chromite.api import api_config as api_config_lib
Alex Klein2008aee2019-08-20 16:25:27 -060010from chromite.api import controller
Alex Kleine191ed62020-02-27 15:59:55 -070011from chromite.api import message_util
Alex Klein146d4772019-06-20 13:48:25 -060012from chromite.api import router as router_lib
Alex Kleind815ca62020-01-10 12:21:30 -070013from chromite.api.gen.chromite.api import build_api_config_pb2
Alex Kleinf4dc4f52018-12-05 13:55:12 -070014from chromite.lib import commandline
Alex Klein2bfacb22019-02-04 11:42:17 -070015from chromite.lib import cros_build_lib
Michael Mortensen3e86c1e2019-11-21 15:51:54 -070016from chromite.lib import cros_logging as logging
17from chromite.lib import tee
Alex Klein00b1f1e2019-02-08 13:53:42 -070018from chromite.utils import matching
Alex Kleinf4dc4f52018-12-05 13:55:12 -070019
Mike Frysinger898265b2020-02-10 23:49:12 -050020
Alex Kleinf4dc4f52018-12-05 13:55:12 -070021def GetParser():
Alex Klein00b1f1e2019-02-08 13:53:42 -070022 """Build the argument parser."""
Alex Kleinf4dc4f52018-12-05 13:55:12 -070023 parser = commandline.ArgumentParser(description=__doc__)
24
Alex Kleind815ca62020-01-10 12:21:30 -070025 parser.add_argument(
Alex Klein2008aee2019-08-20 16:25:27 -060026 'service_method',
Alex Klein2008aee2019-08-20 16:25:27 -060027 help='The "chromite.api.Service/Method" that is being called.')
Alex Kleine191ed62020-02-27 15:59:55 -070028 # Input arguments.
29 input_args = parser.add_mutually_exclusive_group(required=True)
30 input_args.add_argument(
31 '--input-binary',
32 type='path',
33 help='Path to the protobuf binary serialization of the input message.')
34 input_args.add_argument(
Alex Klein2008aee2019-08-20 16:25:27 -060035 '--input-json',
36 type='path',
Alex Kleinf4dc4f52018-12-05 13:55:12 -070037 help='Path to the JSON serialized input argument protobuf message.')
Alex Kleine191ed62020-02-27 15:59:55 -070038 # Output options.
39 parser.add_argument(
40 '--output-binary',
41 type='path',
42 help='The path to which the protobuf binary serialization of the '
43 'response message should be written.')
Alex Kleind815ca62020-01-10 12:21:30 -070044 parser.add_argument(
Alex Klein2008aee2019-08-20 16:25:27 -060045 '--output-json',
46 type='path',
Alex Kleine191ed62020-02-27 15:59:55 -070047 help='The path to which the JSON serialization of the response message '
48 'should be written.')
49 # Config options.
50 config_args = parser.add_mutually_exclusive_group()
51 config_args.add_argument(
52 '--config-binary',
53 type='path',
54 help='The path to the protobuf binary serialization of the Build API '
55 'call configs.')
56 config_args.add_argument(
Alex Kleind815ca62020-01-10 12:21:30 -070057 '--config-json',
58 type='path',
Alex Kleine191ed62020-02-27 15:59:55 -070059 help='The path to the JSON encoded Build API call configs.')
Alex Kleind815ca62020-01-10 12:21:30 -070060 # TODO(crbug.com/1040978): Remove after usages removed.
61 parser.add_argument(
Michael Mortensen3e86c1e2019-11-21 15:51:54 -070062 '--tee-log',
63 type='path',
64 help='The path to which stdout and stderr should be teed to.')
Alex Klein2008aee2019-08-20 16:25:27 -060065
Alex Kleinf4dc4f52018-12-05 13:55:12 -070066 return parser
67
68
Alex Klein00b1f1e2019-02-08 13:53:42 -070069def _ParseArgs(argv, router):
Alex Kleinf4dc4f52018-12-05 13:55:12 -070070 """Parse and validate arguments."""
71 parser = GetParser()
Alex Klein7cc434f2019-12-17 14:58:57 -070072 opts, unknown = parser.parse_known_args(
73 argv, namespace=commandline.ArgumentNamespace())
74 parser.DoPostParseSetup(opts, unknown)
75
76 if unknown:
77 logging.warning('Unknown args ignored: %s', ' '.join(unknown))
Alex Kleinf4dc4f52018-12-05 13:55:12 -070078
Alex Klein00b1f1e2019-02-08 13:53:42 -070079 methods = router.ListMethods()
George Engelbrechtd3de8df2019-09-04 18:15:05 -060080
Alex Klein2008aee2019-08-20 16:25:27 -060081 # Positional service_method argument validation.
George Engelbrechtd3de8df2019-09-04 18:15:05 -060082 parts = opts.service_method.split('/')
83 if len(parts) != 2:
Alex Kleind815ca62020-01-10 12:21:30 -070084 parser.error('Invalid service/method specification format. It should be '
85 'something like chromite.api.SdkService/Create.')
George Engelbrechtd3de8df2019-09-04 18:15:05 -060086
Alex Klein00b1f1e2019-02-08 13:53:42 -070087 if opts.service_method not in methods:
Alex Klein00aa8072019-04-15 16:36:00 -060088 # Unknown method, try to match against known methods and make a suggestion.
89 # This is just for developer sanity, e.g. misspellings when testing.
Alex Klein2008aee2019-08-20 16:25:27 -060090 matched = matching.GetMostLikelyMatchedObject(
91 methods, opts.service_method, matched_score_threshold=0.6)
Alex Klein00b1f1e2019-02-08 13:53:42 -070092 error = 'Unrecognized service name.'
93 if matched:
94 error += '\nDid you mean: \n%s' % '\n'.join(matched)
95 parser.error(error)
96
Alex Kleinf4dc4f52018-12-05 13:55:12 -070097 opts.service = parts[0]
98 opts.method = parts[1]
99
Alex Kleine191ed62020-02-27 15:59:55 -0700100 # Input and output validation.
101 if not opts.output_binary and not opts.output_json:
102 parser.error('At least one output file must be specified.')
Alex Klein2008aee2019-08-20 16:25:27 -0600103
Alex Kleine191ed62020-02-27 15:59:55 -0700104 if not os.path.exists(opts.input_binary or opts.input_json):
Alex Klein5bcb4d22019-03-21 13:51:54 -0600105 parser.error('Input file does not exist.')
106
Alex Kleind815ca62020-01-10 12:21:30 -0700107 config_msg = build_api_config_pb2.BuildApiConfig()
108 if opts.config_json:
Alex Kleine191ed62020-02-27 15:59:55 -0700109 handler = message_util.get_message_handler(opts.config_json,
110 message_util.FORMAT_JSON)
111 else:
112 handler = message_util.get_message_handler(opts.config_binary,
113 message_util.FORMAT_BINARY)
114
115 if opts.config_json or opts.config_binary:
116 # We have been given a config, so read it.
Alex Kleind815ca62020-01-10 12:21:30 -0700117 try:
Alex Kleine191ed62020-02-27 15:59:55 -0700118 handler.read_into(config_msg)
119 except message_util.Error as e:
120 parser.error(str(e))
Alex Kleind815ca62020-01-10 12:21:30 -0700121
122 opts.config = api_config_lib.build_config_from_proto(config_msg)
Alex Kleine191ed62020-02-27 15:59:55 -0700123 opts.config_handler = handler
Alex Klein69339cc2019-07-22 14:08:35 -0600124
Alex Kleinf4dc4f52018-12-05 13:55:12 -0700125 opts.Freeze()
126 return opts
127
128
Alex Kleine191ed62020-02-27 15:59:55 -0700129def _get_io_handlers(opts):
130 """Build the input and output handlers."""
131 if opts.input_binary:
132 input_handler = message_util.get_message_handler(opts.input_binary,
133 message_util.FORMAT_BINARY)
134 else:
135 input_handler = message_util.get_message_handler(opts.input_json,
136 message_util.FORMAT_JSON)
137
138 output_handlers = []
139 if opts.output_binary:
140 handler = message_util.get_message_handler(opts.output_binary,
141 message_util.FORMAT_BINARY)
142 output_handlers.append(handler)
143 if opts.output_json:
144 handler = message_util.get_message_handler(opts.output_json,
145 message_util.FORMAT_JSON)
146 output_handlers.append(handler)
147
148 return input_handler, output_handlers
149
150
Alex Kleinf4dc4f52018-12-05 13:55:12 -0700151def main(argv):
Michael Mortensen3e86c1e2019-11-21 15:51:54 -0700152 with cros_build_lib.ContextManagerStack() as stack:
Michael Mortensen3e86c1e2019-11-21 15:51:54 -0700153 router = router_lib.GetRouter()
154 opts = _ParseArgs(argv, router)
Alex Klein00b1f1e2019-02-08 13:53:42 -0700155
Michael Mortensen3e86c1e2019-11-21 15:51:54 -0700156 if opts.tee_log:
157 stack.Add(tee.Tee, opts.tee_log)
158 logging.info('Teeing stdout and stderr to %s', opts.tee_log)
Alex Kleind815ca62020-01-10 12:21:30 -0700159 if opts.config.log_path:
160 stack.Add(tee.Tee, opts.config.log_path)
161 logging.info('Teeing stdout and stderr to %s', opts.config.log_path)
Michael Mortensena0515d92020-01-02 11:39:34 -0700162 tee_log_env_value = os.environ.get('BUILD_API_TEE_LOG_FILE')
163 if tee_log_env_value:
164 stack.Add(tee.Tee, tee_log_env_value)
165 logging.info('Teeing stdout and stderr to env path %s', tee_log_env_value)
Alex Klein2008aee2019-08-20 16:25:27 -0600166
Alex Kleind815ca62020-01-10 12:21:30 -0700167 if opts.config.mock_invalid:
Michael Mortensen3e86c1e2019-11-21 15:51:54 -0700168 # --mock-invalid handling. We print error messages, but no output is ever
169 # set for validation errors, so we can handle it by just giving back the
170 # correct return code here.
171 return controller.RETURN_CODE_INVALID_INPUT
172
Alex Kleine191ed62020-02-27 15:59:55 -0700173 input_handler, output_handlers = _get_io_handlers(opts)
174
Michael Mortensen3e86c1e2019-11-21 15:51:54 -0700175 try:
Alex Kleine191ed62020-02-27 15:59:55 -0700176 return router.Route(opts.service, opts.method, opts.config, input_handler,
177 output_handlers, opts.config_handler)
Michael Mortensen3e86c1e2019-11-21 15:51:54 -0700178 except router_lib.Error as e:
179 # Handle router_lib.Error derivatives nicely, but let anything else bubble
180 # up.
181 cros_build_lib.Die(e)