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