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 |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame^] | 11 | |
| 12 | from google.protobuf import json_format |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 13 | |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 14 | from chromite.api import api_config as api_config_lib |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 15 | from chromite.api import controller |
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 |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame^] | 21 | from chromite.lib import osutils |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 22 | from chromite.lib import tee |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 23 | from chromite.utils import matching |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 24 | |
| 25 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 26 | def GetParser(): |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 27 | """Build the argument parser.""" |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 28 | parser = commandline.ArgumentParser(description=__doc__) |
| 29 | |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame^] | 30 | parser.add_argument( |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 31 | 'service_method', |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 32 | help='The "chromite.api.Service/Method" that is being called.') |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame^] | 33 | parser.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 | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame^] | 37 | parser.add_argument( |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 38 | '--output-json', |
| 39 | type='path', |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 40 | help='The path to which the result protobuf message should be written.') |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame^] | 41 | 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 Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 47 | '--tee-log', |
| 48 | type='path', |
| 49 | help='The path to which stdout and stderr should be teed to.') |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 50 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 51 | return parser |
| 52 | |
| 53 | |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 54 | def _ParseArgs(argv, router): |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 55 | """Parse and validate arguments.""" |
| 56 | parser = GetParser() |
Alex Klein | 7cc434f | 2019-12-17 14:58:57 -0700 | [diff] [blame] | 57 | 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 Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 63 | |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 64 | methods = router.ListMethods() |
George Engelbrecht | d3de8df | 2019-09-04 18:15:05 -0600 | [diff] [blame] | 65 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 66 | # Positional service_method argument validation. |
George Engelbrecht | d3de8df | 2019-09-04 18:15:05 -0600 | [diff] [blame] | 67 | parts = opts.service_method.split('/') |
| 68 | if len(parts) != 2: |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame^] | 69 | parser.error('Invalid service/method specification format. It should be ' |
| 70 | 'something like chromite.api.SdkService/Create.') |
George Engelbrecht | d3de8df | 2019-09-04 18:15:05 -0600 | [diff] [blame] | 71 | |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 72 | if opts.service_method not in methods: |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 73 | # 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 Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 75 | matched = matching.GetMostLikelyMatchedObject( |
| 76 | methods, opts.service_method, matched_score_threshold=0.6) |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 77 | error = 'Unrecognized service name.' |
| 78 | if matched: |
| 79 | error += '\nDid you mean: \n%s' % '\n'.join(matched) |
| 80 | parser.error(error) |
| 81 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 82 | opts.service = parts[0] |
| 83 | opts.method = parts[1] |
| 84 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 85 | # --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 Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 89 | if not os.path.exists(opts.input_json): |
| 90 | parser.error('Input file does not exist.') |
| 91 | |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame^] | 92 | 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 Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 101 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 102 | opts.Freeze() |
| 103 | return opts |
| 104 | |
| 105 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 106 | def main(argv): |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 107 | with cros_build_lib.ContextManagerStack() as stack: |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 108 | |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 109 | router = router_lib.GetRouter() |
| 110 | opts = _ParseArgs(argv, router) |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 111 | |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 112 | 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 Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame^] | 115 | 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 Mortensen | a0515d9 | 2020-01-02 11:39:34 -0700 | [diff] [blame] | 118 | 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 Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 122 | |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame^] | 123 | if opts.config.mock_invalid: |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 124 | # --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) |