blob: 44c3218df8f445444449a77b9c77a5d97e6ade90 [file] [log] [blame]
Alex Kleinf4dc4f52018-12-05 13:55:12 -07001# -*- 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
8from __future__ import print_function
9
Alex Klein5bcb4d22019-03-21 13:51:54 -060010import os
Alex Kleinf4dc4f52018-12-05 13:55:12 -070011
Alex Klein146d4772019-06-20 13:48:25 -060012from chromite.api import router as router_lib
Alex Kleinf4dc4f52018-12-05 13:55:12 -070013from chromite.lib import commandline
Alex Klein2bfacb22019-02-04 11:42:17 -070014from chromite.lib import cros_build_lib
Alex Klein00b1f1e2019-02-08 13:53:42 -070015from chromite.utils import matching
Alex Kleinf4dc4f52018-12-05 13:55:12 -070016
17
Alex Kleinf4dc4f52018-12-05 13:55:12 -070018def GetParser():
Alex Klein00b1f1e2019-02-08 13:53:42 -070019 """Build the argument parser."""
Alex Kleinf4dc4f52018-12-05 13:55:12 -070020 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 Klein7a115172019-02-08 14:14:20 -070027 '--input-json', type='path', required=True,
Alex Kleinf4dc4f52018-12-05 13:55:12 -070028 help='Path to the JSON serialized input argument protobuf message.')
29 parser.add_argument(
Alex Klein7a115172019-02-08 14:14:20 -070030 '--output-json', type='path', required=True,
Alex Kleinf4dc4f52018-12-05 13:55:12 -070031 help='The path to which the result protobuf message should be written.')
32
33 return parser
34
35
Alex Klein00b1f1e2019-02-08 13:53:42 -070036def _ParseArgs(argv, router):
Alex Kleinf4dc4f52018-12-05 13:55:12 -070037 """Parse and validate arguments."""
38 parser = GetParser()
39 opts = parser.parse_args(argv)
40
Alex Klein00b1f1e2019-02-08 13:53:42 -070041 methods = router.ListMethods()
42 if opts.service_method not in methods:
Alex Klein00aa8072019-04-15 16:36:00 -060043 # 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 Klein00b1f1e2019-02-08 13:53:42 -070045 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 Kleinf4dc4f52018-12-05 13:55:12 -070052 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 Klein5bcb4d22019-03-21 13:51:54 -060060 if not os.path.exists(opts.input_json):
61 parser.error('Input file does not exist.')
62
Alex Kleinf4dc4f52018-12-05 13:55:12 -070063 opts.Freeze()
64 return opts
65
66
Alex Kleinf4dc4f52018-12-05 13:55:12 -070067def main(argv):
Alex Klein146d4772019-06-20 13:48:25 -060068 router = router_lib.GetRouter()
Alex Kleinf4dc4f52018-12-05 13:55:12 -070069
Alex Klein00b1f1e2019-02-08 13:53:42 -070070 opts = _ParseArgs(argv, router)
71
Alex Klein7a115172019-02-08 14:14:20 -070072 try:
Alex Klein5bcb4d22019-03-21 13:51:54 -060073 return router.Route(opts.service, opts.method, opts.input_json,
74 opts.output_json)
Amin Hassani1e2dfd22019-06-24 10:34:17 -070075 except router_lib.Error as e:
76 # Handle router_lib.Error derivatives nicely, but let anything else bubble
77 # up.
Alex Klein7a115172019-02-08 14:14:20 -070078 cros_build_lib.Die(e.message)