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 |
Mike Frysinger | 898265b | 2020-02-10 23:49:12 -0500 | [diff] [blame^] | 11 | import sys |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 12 | |
| 13 | from google.protobuf import json_format |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 14 | |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 15 | from chromite.api import api_config as api_config_lib |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 16 | from chromite.api import controller |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 17 | from chromite.api import router as router_lib |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 18 | from chromite.api.gen.chromite.api import build_api_config_pb2 |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 19 | from chromite.lib import commandline |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 20 | from chromite.lib import cros_build_lib |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 21 | from chromite.lib import cros_logging as logging |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 22 | from chromite.lib import osutils |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 23 | from chromite.lib import tee |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 24 | from chromite.utils import matching |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 25 | |
| 26 | |
Mike Frysinger | 898265b | 2020-02-10 23:49:12 -0500 | [diff] [blame^] | 27 | assert sys.version_info >= (3, 6), 'This module requires Python 3.6+' |
| 28 | |
| 29 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 30 | def GetParser(): |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 31 | """Build the argument parser.""" |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 32 | parser = commandline.ArgumentParser(description=__doc__) |
| 33 | |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 34 | parser.add_argument( |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 35 | 'service_method', |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 36 | help='The "chromite.api.Service/Method" that is being called.') |
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 | '--input-json', |
| 39 | type='path', |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 40 | help='Path to the JSON serialized input argument protobuf message.') |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 41 | parser.add_argument( |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 42 | '--output-json', |
| 43 | type='path', |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 44 | help='The path to which the result protobuf message should be written.') |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 45 | parser.add_argument( |
| 46 | '--config-json', |
| 47 | type='path', |
| 48 | help='The path to the Build API call configs.') |
| 49 | # TODO(crbug.com/1040978): Remove after usages removed. |
| 50 | parser.add_argument( |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 51 | '--tee-log', |
| 52 | type='path', |
| 53 | help='The path to which stdout and stderr should be teed to.') |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 54 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 55 | return parser |
| 56 | |
| 57 | |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 58 | def _ParseArgs(argv, router): |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 59 | """Parse and validate arguments.""" |
| 60 | parser = GetParser() |
Alex Klein | 7cc434f | 2019-12-17 14:58:57 -0700 | [diff] [blame] | 61 | opts, unknown = parser.parse_known_args( |
| 62 | argv, namespace=commandline.ArgumentNamespace()) |
| 63 | parser.DoPostParseSetup(opts, unknown) |
| 64 | |
| 65 | if unknown: |
| 66 | logging.warning('Unknown args ignored: %s', ' '.join(unknown)) |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 67 | |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 68 | methods = router.ListMethods() |
George Engelbrecht | d3de8df | 2019-09-04 18:15:05 -0600 | [diff] [blame] | 69 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 70 | # Positional service_method argument validation. |
George Engelbrecht | d3de8df | 2019-09-04 18:15:05 -0600 | [diff] [blame] | 71 | parts = opts.service_method.split('/') |
| 72 | if len(parts) != 2: |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 73 | parser.error('Invalid service/method specification format. It should be ' |
| 74 | 'something like chromite.api.SdkService/Create.') |
George Engelbrecht | d3de8df | 2019-09-04 18:15:05 -0600 | [diff] [blame] | 75 | |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 76 | if opts.service_method not in methods: |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 77 | # Unknown method, try to match against known methods and make a suggestion. |
| 78 | # This is just for developer sanity, e.g. misspellings when testing. |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 79 | matched = matching.GetMostLikelyMatchedObject( |
| 80 | methods, opts.service_method, matched_score_threshold=0.6) |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 81 | error = 'Unrecognized service name.' |
| 82 | if matched: |
| 83 | error += '\nDid you mean: \n%s' % '\n'.join(matched) |
| 84 | parser.error(error) |
| 85 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 86 | opts.service = parts[0] |
| 87 | opts.method = parts[1] |
| 88 | |
Alex Klein | 2008aee | 2019-08-20 16:25:27 -0600 | [diff] [blame] | 89 | # --input-json and --output-json validation. |
| 90 | if not opts.input_json or not opts.output_json: |
| 91 | parser.error('--input-json and --output-json are both required.') |
| 92 | |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 93 | if not os.path.exists(opts.input_json): |
| 94 | parser.error('Input file does not exist.') |
| 95 | |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 96 | config_msg = build_api_config_pb2.BuildApiConfig() |
| 97 | if opts.config_json: |
| 98 | try: |
| 99 | json_format.Parse(osutils.ReadFile(opts.config_json), config_msg, |
| 100 | ignore_unknown_fields=True) |
| 101 | except IOError as e: |
| 102 | parser.error(e) |
| 103 | |
| 104 | opts.config = api_config_lib.build_config_from_proto(config_msg) |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 105 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 106 | opts.Freeze() |
| 107 | return opts |
| 108 | |
| 109 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 110 | def main(argv): |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 111 | with cros_build_lib.ContextManagerStack() as stack: |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 112 | |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 113 | router = router_lib.GetRouter() |
| 114 | opts = _ParseArgs(argv, router) |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 115 | |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 116 | if opts.tee_log: |
| 117 | stack.Add(tee.Tee, opts.tee_log) |
| 118 | logging.info('Teeing stdout and stderr to %s', opts.tee_log) |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 119 | if opts.config.log_path: |
| 120 | stack.Add(tee.Tee, opts.config.log_path) |
| 121 | logging.info('Teeing stdout and stderr to %s', opts.config.log_path) |
Michael Mortensen | a0515d9 | 2020-01-02 11:39:34 -0700 | [diff] [blame] | 122 | tee_log_env_value = os.environ.get('BUILD_API_TEE_LOG_FILE') |
| 123 | if tee_log_env_value: |
| 124 | stack.Add(tee.Tee, tee_log_env_value) |
| 125 | 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] | 126 | |
Alex Klein | d815ca6 | 2020-01-10 12:21:30 -0700 | [diff] [blame] | 127 | if opts.config.mock_invalid: |
Michael Mortensen | 3e86c1e | 2019-11-21 15:51:54 -0700 | [diff] [blame] | 128 | # --mock-invalid handling. We print error messages, but no output is ever |
| 129 | # set for validation errors, so we can handle it by just giving back the |
| 130 | # correct return code here. |
| 131 | return controller.RETURN_CODE_INVALID_INPUT |
| 132 | |
| 133 | try: |
| 134 | return router.Route(opts.service, opts.method, opts.input_json, |
| 135 | opts.output_json, opts.config) |
| 136 | except router_lib.Error as e: |
| 137 | # Handle router_lib.Error derivatives nicely, but let anything else bubble |
| 138 | # up. |
| 139 | cros_build_lib.Die(e) |