blob: b071f20a086109226f0d09cdfe48b339db5d95c3 [file] [log] [blame]
Chris Sosa90c78502012-10-05 17:07:42 -07001# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
David Purselle19f83b2015-07-01 12:57:07 -07005"""This implements the entry point for the `cros` CLI toolset.
Don Garrett25f309a2014-03-19 14:02:12 -07006
David Purselle19f83b2015-07-01 12:57:07 -07007This script is invoked by chromite/bin/cros, which sets up the
David Pursellffb90042015-03-23 09:21:41 -07008proper execution environment and calls this module's main() function.
Don Garrett25f309a2014-03-19 14:02:12 -07009
10In turn, this script looks for a subcommand based on how it was invoked. For
David Pursellffb90042015-03-23 09:21:41 -070011example, `cros lint` will use the cli/cros/cros_lint.py subcommand.
Don Garrett25f309a2014-03-19 14:02:12 -070012
David Pursellffb90042015-03-23 09:21:41 -070013See cli/ for actual command implementations.
Don Garrett25f309a2014-03-19 14:02:12 -070014"""
15
Mike Frysinger49c6e1f2022-04-14 15:41:40 -040016import argparse
Chris McDonald59650c32021-07-20 15:29:28 -060017import logging
18
David Pursellf1c27c12015-03-18 09:51:38 -070019from chromite.cli import command
Chris Sosab445f792012-10-11 15:26:39 -070020from chromite.lib import commandline
Chris Sosa90c78502012-10-05 17:07:42 -070021
22
Mike Frysinger1a470812019-11-07 01:19:17 -050023def GetOptions(cmd_name=None):
David Pursell643ed342015-07-07 09:24:32 -070024 """Returns the parser to use for commandline parsing.
Chris Sosa90c78502012-10-05 17:07:42 -070025
David Pursell643ed342015-07-07 09:24:32 -070026 Args:
Mike Frysinger1a470812019-11-07 01:19:17 -050027 cmd_name: The subcommand to import & add.
David Pursell643ed342015-07-07 09:24:32 -070028
29 Returns:
30 A commandline.ArgumentParser object.
31 """
Pi-Hsun Shihfd5a91e2019-11-28 15:06:06 +080032 parser = commandline.ArgumentParser(caching=True, default_log_level='notice')
David Pursell643ed342015-07-07 09:24:32 -070033
Mike Frysinger1a470812019-11-07 01:19:17 -050034 subparsers = parser.add_subparsers(title='Subcommands', dest='subcommand')
35 subparsers.required = True
36
37 # We add all the commands so `cros --help ...` looks reasonable.
38 # We add them in order also so the --help output is stable for users.
39 for subcommand in sorted(command.ListCommands()):
40 if subcommand == cmd_name:
41 class_def = command.ImportCommand(cmd_name)
David Pursell643ed342015-07-07 09:24:32 -070042 epilog = getattr(class_def, 'EPILOG', None)
43 sub_parser = subparsers.add_parser(
44 cmd_name, description=class_def.__doc__, epilog=epilog,
45 caching=class_def.use_caching_options,
Mike Frysinger49c6e1f2022-04-14 15:41:40 -040046 formatter_class=argparse.RawDescriptionHelpFormatter)
David Pursell643ed342015-07-07 09:24:32 -070047 class_def.AddParser(sub_parser)
Mike Frysinger1a470812019-11-07 01:19:17 -050048 else:
49 subparsers.add_parser(subcommand, add_help=False)
Chris Sosa90c78502012-10-05 17:07:42 -070050
Harry Cuttsbf3ca762020-09-15 05:13:26 -070051 help_parser = subparsers.add_parser('help', add_help=False)
52 help_parser.add_argument('help_subcommand', nargs='?',
53 help='The command to show help for')
54
Chris Sosa90c78502012-10-05 17:07:42 -070055 return parser
56
57
Ryan Cui47f80e42013-04-01 19:01:54 -070058def _RunSubCommand(subcommand):
59 """Helper function for testing purposes."""
Ryan Cuide21f482013-08-06 11:50:59 -070060 return subcommand.Run()
Ryan Cui47f80e42013-04-01 19:01:54 -070061
62
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040063def main(argv):
David Purselldfbfbc82015-03-05 10:59:16 -080064 try:
Mike Frysinger1a470812019-11-07 01:19:17 -050065 # The first time we parse the commandline is only to figure out what
66 # subcommand the user wants to run. This allows us to avoid importing
67 # all subcommands which can be quite slow. This works because there is
68 # no way in Python to list all subcommands and their help output in a
69 # single run.
70 parser = GetOptions()
David Purselldfbfbc82015-03-05 10:59:16 -080071 if not argv:
72 parser.print_help()
73 return 1
74
Mike Frysinger1a470812019-11-07 01:19:17 -050075 namespace, _ = parser.parse_known_args(argv)
Harry Cuttsbf3ca762020-09-15 05:13:26 -070076
77 if namespace.subcommand == 'help':
78 if namespace.help_subcommand is None:
79 parser.print_help()
80 return
81
82 parser = GetOptions(namespace.help_subcommand)
83 parser.parse_args([namespace.help_subcommand, '--help'])
84
Mike Frysinger1a470812019-11-07 01:19:17 -050085 # The user has selected a subcommand now, so get the full parser after we
86 # import the single subcommand.
87 parser = GetOptions(namespace.subcommand)
David Purselldfbfbc82015-03-05 10:59:16 -080088 namespace = parser.parse_args(argv)
Mike Frysinger57bb7b82021-12-14 10:22:37 -050089 namespace.command_class.ProcessOptions(parser, namespace)
David Pursellffb90042015-03-23 09:21:41 -070090 subcommand = namespace.command_class(namespace)
Mike Frysinger25829e22021-12-14 12:01:31 -050091 namespace.Freeze()
Aviv Keshet01a82e92017-03-02 17:39:59 -080092 try:
93 code = _RunSubCommand(subcommand)
94 except (commandline.ChrootRequiredError, commandline.ExecRequiredError):
95 # The higher levels want these passed back, so oblige.
96 raise
97 except Exception as e:
98 code = 1
Mike Frysinger1a470812019-11-07 01:19:17 -050099 logging.error('cros %s failed before completing.', namespace.subcommand)
Aviv Keshet01a82e92017-03-02 17:39:59 -0800100 if namespace.debug:
Mike Frysinger684d36e2015-06-03 05:03:34 -0400101 raise
Aviv Keshet01a82e92017-03-02 17:39:59 -0800102 else:
103 logging.error(e)
Mike Frysingera46f57d2018-07-19 12:18:22 -0400104 logging.error('(Re-run with --debug for more details.)')
Mike Frysinger684d36e2015-06-03 05:03:34 -0400105
Aviv Keshet01a82e92017-03-02 17:39:59 -0800106 if code is not None:
107 return code
David Purselldfbfbc82015-03-05 10:59:16 -0800108
109 return 0
110 except KeyboardInterrupt:
111 logging.debug('Aborted due to keyboard interrupt.')
Chris Sosa90c78502012-10-05 17:07:42 -0700112 return 1