blob: d2fdaa8f03711fc9aecf422ac42675c4b57f75f8 [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 Klein69339cc2019-07-22 14:08:35 -060012from chromite.api import api_config as api_config_lib
Alex Klein146d4772019-06-20 13:48:25 -060013from chromite.api import router as router_lib
Alex Kleinf4dc4f52018-12-05 13:55:12 -070014from chromite.lib import commandline
Alex Klein2bfacb22019-02-04 11:42:17 -070015from chromite.lib import cros_build_lib
Alex Klein00b1f1e2019-02-08 13:53:42 -070016from chromite.utils import matching
Alex Kleinf4dc4f52018-12-05 13:55:12 -070017
18
Alex Kleinf4dc4f52018-12-05 13:55:12 -070019def GetParser():
Alex Klein00b1f1e2019-02-08 13:53:42 -070020 """Build the argument parser."""
Alex Kleinf4dc4f52018-12-05 13:55:12 -070021 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 Klein7a115172019-02-08 14:14:20 -070028 '--input-json', type='path', required=True,
Alex Kleinf4dc4f52018-12-05 13:55:12 -070029 help='Path to the JSON serialized input argument protobuf message.')
30 parser.add_argument(
Alex Klein7a115172019-02-08 14:14:20 -070031 '--output-json', type='path', required=True,
Alex Kleinf4dc4f52018-12-05 13:55:12 -070032 help='The path to which the result protobuf message should be written.')
Alex Klein69339cc2019-07-22 14:08:35 -060033 # 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 Kleinf4dc4f52018-12-05 13:55:12 -070039
40 return parser
41
42
Alex Klein00b1f1e2019-02-08 13:53:42 -070043def _ParseArgs(argv, router):
Alex Kleinf4dc4f52018-12-05 13:55:12 -070044 """Parse and validate arguments."""
45 parser = GetParser()
46 opts = parser.parse_args(argv)
47
Alex Klein00b1f1e2019-02-08 13:53:42 -070048 methods = router.ListMethods()
49 if opts.service_method not in methods:
Alex Klein00aa8072019-04-15 16:36:00 -060050 # 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 Klein00b1f1e2019-02-08 13:53:42 -070052 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 Kleinf4dc4f52018-12-05 13:55:12 -070059 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 Klein5bcb4d22019-03-21 13:51:54 -060067 if not os.path.exists(opts.input_json):
68 parser.error('Input file does not exist.')
69
Alex Klein69339cc2019-07-22 14:08:35 -060070 opts.config = api_config_lib.ApiConfig(validate_only=opts.validate_only)
71
Alex Kleinf4dc4f52018-12-05 13:55:12 -070072 opts.Freeze()
73 return opts
74
75
Alex Kleinf4dc4f52018-12-05 13:55:12 -070076def main(argv):
Alex Klein146d4772019-06-20 13:48:25 -060077 router = router_lib.GetRouter()
Alex Kleinf4dc4f52018-12-05 13:55:12 -070078
Alex Klein00b1f1e2019-02-08 13:53:42 -070079 opts = _ParseArgs(argv, router)
80
Alex Klein7a115172019-02-08 14:14:20 -070081 try:
Alex Klein5bcb4d22019-03-21 13:51:54 -060082 return router.Route(opts.service, opts.method, opts.input_json,
Alex Klein69339cc2019-07-22 14:08:35 -060083 opts.output_json, opts.config)
Amin Hassani1e2dfd22019-06-24 10:34:17 -070084 except router_lib.Error as e:
85 # Handle router_lib.Error derivatives nicely, but let anything else bubble
86 # up.
Alex Klein7a115172019-02-08 14:14:20 -070087 cros_build_lib.Die(e.message)