blob: 01b16c1f19cd86c9362daa89a3e3bb2d2120627c [file] [log] [blame]
Alex Kleinf4dc4f52018-12-05 13:55:12 -07001# -*- 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 Kleind815ca62020-01-10 12:21:30 -07006"""The Build API entry point."""
Alex Kleinf4dc4f52018-12-05 13:55:12 -07007
8from __future__ import print_function
9
Alex Klein5bcb4d22019-03-21 13:51:54 -060010import os
Alex Kleind815ca62020-01-10 12:21:30 -070011
12from google.protobuf import json_format
Alex Kleinf4dc4f52018-12-05 13:55:12 -070013
Alex Klein69339cc2019-07-22 14:08:35 -060014from chromite.api import api_config as api_config_lib
Alex Klein2008aee2019-08-20 16:25:27 -060015from chromite.api import controller
Alex Klein146d4772019-06-20 13:48:25 -060016from chromite.api import router as router_lib
Alex Kleind815ca62020-01-10 12:21:30 -070017from chromite.api.gen.chromite.api import build_api_config_pb2
Alex Kleinf4dc4f52018-12-05 13:55:12 -070018from chromite.lib import commandline
Alex Klein2bfacb22019-02-04 11:42:17 -070019from chromite.lib import cros_build_lib
Michael Mortensen3e86c1e2019-11-21 15:51:54 -070020from chromite.lib import cros_logging as logging
Alex Kleind815ca62020-01-10 12:21:30 -070021from chromite.lib import osutils
Michael Mortensen3e86c1e2019-11-21 15:51:54 -070022from chromite.lib import tee
Alex Klein00b1f1e2019-02-08 13:53:42 -070023from chromite.utils import matching
Alex Kleinf4dc4f52018-12-05 13:55:12 -070024
25
Alex Kleinf4dc4f52018-12-05 13:55:12 -070026def GetParser():
Alex Klein00b1f1e2019-02-08 13:53:42 -070027 """Build the argument parser."""
Alex Kleinf4dc4f52018-12-05 13:55:12 -070028 parser = commandline.ArgumentParser(description=__doc__)
29
Alex Kleind815ca62020-01-10 12:21:30 -070030 parser.add_argument(
Alex Klein2008aee2019-08-20 16:25:27 -060031 'service_method',
Alex Klein2008aee2019-08-20 16:25:27 -060032 help='The "chromite.api.Service/Method" that is being called.')
Alex Kleind815ca62020-01-10 12:21:30 -070033 parser.add_argument(
Alex Klein2008aee2019-08-20 16:25:27 -060034 '--input-json',
35 type='path',
Alex Kleinf4dc4f52018-12-05 13:55:12 -070036 help='Path to the JSON serialized input argument protobuf message.')
Alex Kleind815ca62020-01-10 12:21:30 -070037 parser.add_argument(
Alex Klein2008aee2019-08-20 16:25:27 -060038 '--output-json',
39 type='path',
Alex Kleinf4dc4f52018-12-05 13:55:12 -070040 help='The path to which the result protobuf message should be written.')
Alex Kleind815ca62020-01-10 12:21:30 -070041 parser.add_argument(
42 '--config-json',
43 type='path',
44 help='The path to the Build API call configs.')
45 # TODO(crbug.com/1040978): Remove after usages removed.
46 parser.add_argument(
Michael Mortensen3e86c1e2019-11-21 15:51:54 -070047 '--tee-log',
48 type='path',
49 help='The path to which stdout and stderr should be teed to.')
Alex Klein2008aee2019-08-20 16:25:27 -060050
Alex Kleinf4dc4f52018-12-05 13:55:12 -070051 return parser
52
53
Alex Klein00b1f1e2019-02-08 13:53:42 -070054def _ParseArgs(argv, router):
Alex Kleinf4dc4f52018-12-05 13:55:12 -070055 """Parse and validate arguments."""
56 parser = GetParser()
Alex Klein7cc434f2019-12-17 14:58:57 -070057 opts, unknown = parser.parse_known_args(
58 argv, namespace=commandline.ArgumentNamespace())
59 parser.DoPostParseSetup(opts, unknown)
60
61 if unknown:
62 logging.warning('Unknown args ignored: %s', ' '.join(unknown))
Alex Kleinf4dc4f52018-12-05 13:55:12 -070063
Alex Klein00b1f1e2019-02-08 13:53:42 -070064 methods = router.ListMethods()
George Engelbrechtd3de8df2019-09-04 18:15:05 -060065
Alex Klein2008aee2019-08-20 16:25:27 -060066 # Positional service_method argument validation.
George Engelbrechtd3de8df2019-09-04 18:15:05 -060067 parts = opts.service_method.split('/')
68 if len(parts) != 2:
Alex Kleind815ca62020-01-10 12:21:30 -070069 parser.error('Invalid service/method specification format. It should be '
70 'something like chromite.api.SdkService/Create.')
George Engelbrechtd3de8df2019-09-04 18:15:05 -060071
Alex Klein00b1f1e2019-02-08 13:53:42 -070072 if opts.service_method not in methods:
Alex Klein00aa8072019-04-15 16:36:00 -060073 # Unknown method, try to match against known methods and make a suggestion.
74 # This is just for developer sanity, e.g. misspellings when testing.
Alex Klein2008aee2019-08-20 16:25:27 -060075 matched = matching.GetMostLikelyMatchedObject(
76 methods, opts.service_method, matched_score_threshold=0.6)
Alex Klein00b1f1e2019-02-08 13:53:42 -070077 error = 'Unrecognized service name.'
78 if matched:
79 error += '\nDid you mean: \n%s' % '\n'.join(matched)
80 parser.error(error)
81
Alex Kleinf4dc4f52018-12-05 13:55:12 -070082 opts.service = parts[0]
83 opts.method = parts[1]
84
Alex Klein2008aee2019-08-20 16:25:27 -060085 # --input-json and --output-json validation.
86 if not opts.input_json or not opts.output_json:
87 parser.error('--input-json and --output-json are both required.')
88
Alex Klein5bcb4d22019-03-21 13:51:54 -060089 if not os.path.exists(opts.input_json):
90 parser.error('Input file does not exist.')
91
Alex Kleind815ca62020-01-10 12:21:30 -070092 config_msg = build_api_config_pb2.BuildApiConfig()
93 if opts.config_json:
94 try:
95 json_format.Parse(osutils.ReadFile(opts.config_json), config_msg,
96 ignore_unknown_fields=True)
97 except IOError as e:
98 parser.error(e)
99
100 opts.config = api_config_lib.build_config_from_proto(config_msg)
Alex Klein69339cc2019-07-22 14:08:35 -0600101
Alex Kleinf4dc4f52018-12-05 13:55:12 -0700102 opts.Freeze()
103 return opts
104
105
Alex Kleinf4dc4f52018-12-05 13:55:12 -0700106def main(argv):
Michael Mortensen3e86c1e2019-11-21 15:51:54 -0700107 with cros_build_lib.ContextManagerStack() as stack:
Alex Kleinf4dc4f52018-12-05 13:55:12 -0700108
Michael Mortensen3e86c1e2019-11-21 15:51:54 -0700109 router = router_lib.GetRouter()
110 opts = _ParseArgs(argv, router)
Alex Klein00b1f1e2019-02-08 13:53:42 -0700111
Michael Mortensen3e86c1e2019-11-21 15:51:54 -0700112 if opts.tee_log:
113 stack.Add(tee.Tee, opts.tee_log)
114 logging.info('Teeing stdout and stderr to %s', opts.tee_log)
Alex Kleind815ca62020-01-10 12:21:30 -0700115 if opts.config.log_path:
116 stack.Add(tee.Tee, opts.config.log_path)
117 logging.info('Teeing stdout and stderr to %s', opts.config.log_path)
Michael Mortensena0515d92020-01-02 11:39:34 -0700118 tee_log_env_value = os.environ.get('BUILD_API_TEE_LOG_FILE')
119 if tee_log_env_value:
120 stack.Add(tee.Tee, tee_log_env_value)
121 logging.info('Teeing stdout and stderr to env path %s', tee_log_env_value)
Alex Klein2008aee2019-08-20 16:25:27 -0600122
Alex Kleind815ca62020-01-10 12:21:30 -0700123 if opts.config.mock_invalid:
Michael Mortensen3e86c1e2019-11-21 15:51:54 -0700124 # --mock-invalid handling. We print error messages, but no output is ever
125 # set for validation errors, so we can handle it by just giving back the
126 # correct return code here.
127 return controller.RETURN_CODE_INVALID_INPUT
128
129 try:
130 return router.Route(opts.service, opts.method, opts.input_json,
131 opts.output_json, opts.config)
132 except router_lib.Error as e:
133 # Handle router_lib.Error derivatives nicely, but let anything else bubble
134 # up.
135 cros_build_lib.Die(e)