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 | |
| 6 | """The build API entry point.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 10 | import os |
George Engelbrecht | d3de8df | 2019-09-04 18:15:05 -0600 | [diff] [blame^] | 11 | import sys |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 12 | |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 13 | from chromite.api import api_config as api_config_lib |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 14 | from chromite.api import router as router_lib |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 15 | from chromite.lib import commandline |
Alex Klein | 2bfacb2 | 2019-02-04 11:42:17 -0700 | [diff] [blame] | 16 | from chromite.lib import cros_build_lib |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 17 | from chromite.utils import matching |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 18 | |
| 19 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 20 | def GetParser(): |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 21 | """Build the argument parser.""" |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 22 | parser = commandline.ArgumentParser(description=__doc__) |
| 23 | |
George Engelbrecht | d3de8df | 2019-09-04 18:15:05 -0600 | [diff] [blame^] | 24 | parser.add_argument('service_method', nargs='?', |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 25 | help='The "chromite.api.Service/Method" that is being ' |
| 26 | 'called.') |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 27 | parser.add_argument( |
George Engelbrecht | d3de8df | 2019-09-04 18:15:05 -0600 | [diff] [blame^] | 28 | '--input-json', type='path', |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 29 | help='Path to the JSON serialized input argument protobuf message.') |
| 30 | parser.add_argument( |
George Engelbrecht | d3de8df | 2019-09-04 18:15:05 -0600 | [diff] [blame^] | 31 | '--output-json', type='path', |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 32 | help='The path to which the result protobuf message should be written.') |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 33 | parser.add_argument( |
| 34 | '--validate-only', action='store_true', default=False, |
| 35 | help='When set, only runs the argument validation logic. Calls produce' |
| 36 | 'a return code of 0 iff the arguments comprise a valid call to the' |
| 37 | 'endpoint, or 1 otherwise.') |
George Engelbrecht | d3de8df | 2019-09-04 18:15:05 -0600 | [diff] [blame^] | 38 | parser.add_argument( |
| 39 | '--list-services', action='store_true', |
| 40 | help='List the names of the registered services.') |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 41 | |
| 42 | return parser |
| 43 | |
| 44 | |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 45 | def _ParseArgs(argv, router): |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 46 | """Parse and validate arguments.""" |
| 47 | parser = GetParser() |
| 48 | opts = parser.parse_args(argv) |
| 49 | |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 50 | methods = router.ListMethods() |
George Engelbrecht | d3de8df | 2019-09-04 18:15:05 -0600 | [diff] [blame^] | 51 | |
| 52 | if opts.list_services: |
| 53 | for method in methods: |
| 54 | print(method) |
| 55 | sys.exit(0) |
| 56 | |
| 57 | if not opts.service_method: |
| 58 | parser.error('Must pass "Service/Method".') |
| 59 | |
| 60 | parts = opts.service_method.split('/') |
| 61 | if len(parts) != 2: |
| 62 | parser.error( |
| 63 | 'Must pass the correct format: (i.e. chromite.api.SdkService/Create)') |
| 64 | |
| 65 | if not opts.input_json or not opts.output_json: |
| 66 | parser.error('--input-json and --output-json are both required.') |
| 67 | |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 68 | if opts.service_method not in methods: |
Alex Klein | 00aa807 | 2019-04-15 16:36:00 -0600 | [diff] [blame] | 69 | # Unknown method, try to match against known methods and make a suggestion. |
| 70 | # This is just for developer sanity, e.g. misspellings when testing. |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 71 | matched = matching.GetMostLikelyMatchedObject(methods, opts.service_method, |
| 72 | matched_score_threshold=0.6) |
| 73 | error = 'Unrecognized service name.' |
| 74 | if matched: |
| 75 | error += '\nDid you mean: \n%s' % '\n'.join(matched) |
| 76 | parser.error(error) |
| 77 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 78 | opts.service = parts[0] |
| 79 | opts.method = parts[1] |
| 80 | |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 81 | if not os.path.exists(opts.input_json): |
| 82 | parser.error('Input file does not exist.') |
| 83 | |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 84 | opts.config = api_config_lib.ApiConfig(validate_only=opts.validate_only) |
| 85 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 86 | opts.Freeze() |
| 87 | return opts |
| 88 | |
| 89 | |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 90 | def main(argv): |
Alex Klein | 146d477 | 2019-06-20 13:48:25 -0600 | [diff] [blame] | 91 | router = router_lib.GetRouter() |
Alex Klein | f4dc4f5 | 2018-12-05 13:55:12 -0700 | [diff] [blame] | 92 | |
Alex Klein | 00b1f1e | 2019-02-08 13:53:42 -0700 | [diff] [blame] | 93 | opts = _ParseArgs(argv, router) |
| 94 | |
Alex Klein | 7a11517 | 2019-02-08 14:14:20 -0700 | [diff] [blame] | 95 | try: |
Alex Klein | 5bcb4d2 | 2019-03-21 13:51:54 -0600 | [diff] [blame] | 96 | return router.Route(opts.service, opts.method, opts.input_json, |
Alex Klein | 69339cc | 2019-07-22 14:08:35 -0600 | [diff] [blame] | 97 | opts.output_json, opts.config) |
Amin Hassani | 1e2dfd2 | 2019-06-24 10:34:17 -0700 | [diff] [blame] | 98 | except router_lib.Error as e: |
| 99 | # Handle router_lib.Error derivatives nicely, but let anything else bubble |
| 100 | # up. |
Alex Klein | 7a11517 | 2019-02-08 14:14:20 -0700 | [diff] [blame] | 101 | cros_build_lib.Die(e.message) |