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