blob: 54a5e4bf66af758e6316d1e943fcd77fe12125af [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
Chris Sosa90c78502012-10-05 17:07:42 -07002# Copyright (c) 2012 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
David Purselle19f83b2015-07-01 12:57:07 -07006"""This implements the entry point for the `cros` CLI toolset.
Don Garrett25f309a2014-03-19 14:02:12 -07007
David Purselle19f83b2015-07-01 12:57:07 -07008This script is invoked by chromite/bin/cros, which sets up the
David Pursellffb90042015-03-23 09:21:41 -07009proper execution environment and calls this module's main() function.
Don Garrett25f309a2014-03-19 14:02:12 -070010
11In turn, this script looks for a subcommand based on how it was invoked. For
David Pursellffb90042015-03-23 09:21:41 -070012example, `cros lint` will use the cli/cros/cros_lint.py subcommand.
Don Garrett25f309a2014-03-19 14:02:12 -070013
David Pursellffb90042015-03-23 09:21:41 -070014See cli/ for actual command implementations.
Don Garrett25f309a2014-03-19 14:02:12 -070015"""
16
Mike Frysinger383367e2014-09-16 15:06:17 -040017from __future__ import print_function
18
Mike Frysingerf0b53422020-02-21 02:49:38 -050019import sys
20
David Pursellf1c27c12015-03-18 09:51:38 -070021from chromite.cli import command
Chris Sosab445f792012-10-11 15:26:39 -070022from chromite.lib import commandline
Ralph Nathan91874ca2015-03-19 13:29:41 -070023from chromite.lib import cros_logging as logging
Chris Sosa90c78502012-10-05 17:07:42 -070024
25
Mike Frysingerf0b53422020-02-21 02:49:38 -050026assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
27
28
Mike Frysinger1a470812019-11-07 01:19:17 -050029def GetOptions(cmd_name=None):
David Pursell643ed342015-07-07 09:24:32 -070030 """Returns the parser to use for commandline parsing.
Chris Sosa90c78502012-10-05 17:07:42 -070031
David Pursell643ed342015-07-07 09:24:32 -070032 Args:
Mike Frysinger1a470812019-11-07 01:19:17 -050033 cmd_name: The subcommand to import & add.
David Pursell643ed342015-07-07 09:24:32 -070034
35 Returns:
36 A commandline.ArgumentParser object.
37 """
Pi-Hsun Shihfd5a91e2019-11-28 15:06:06 +080038 parser = commandline.ArgumentParser(caching=True, default_log_level='notice')
David Pursell643ed342015-07-07 09:24:32 -070039
Mike Frysinger1a470812019-11-07 01:19:17 -050040 subparsers = parser.add_subparsers(title='Subcommands', dest='subcommand')
41 subparsers.required = True
42
43 # We add all the commands so `cros --help ...` looks reasonable.
44 # We add them in order also so the --help output is stable for users.
45 for subcommand in sorted(command.ListCommands()):
46 if subcommand == cmd_name:
47 class_def = command.ImportCommand(cmd_name)
David Pursell643ed342015-07-07 09:24:32 -070048 epilog = getattr(class_def, 'EPILOG', None)
49 sub_parser = subparsers.add_parser(
50 cmd_name, description=class_def.__doc__, epilog=epilog,
51 caching=class_def.use_caching_options,
52 formatter_class=commandline.argparse.RawDescriptionHelpFormatter)
53 class_def.AddParser(sub_parser)
Mike Frysinger1a470812019-11-07 01:19:17 -050054 else:
55 subparsers.add_parser(subcommand, add_help=False)
Chris Sosa90c78502012-10-05 17:07:42 -070056
Harry Cuttsbf3ca762020-09-15 05:13:26 -070057 help_parser = subparsers.add_parser('help', add_help=False)
58 help_parser.add_argument('help_subcommand', nargs='?',
59 help='The command to show help for')
60
Chris Sosa90c78502012-10-05 17:07:42 -070061 return parser
62
63
Ryan Cui47f80e42013-04-01 19:01:54 -070064def _RunSubCommand(subcommand):
65 """Helper function for testing purposes."""
Ryan Cuide21f482013-08-06 11:50:59 -070066 return subcommand.Run()
Ryan Cui47f80e42013-04-01 19:01:54 -070067
68
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040069def main(argv):
David Purselldfbfbc82015-03-05 10:59:16 -080070 try:
Mike Frysinger1a470812019-11-07 01:19:17 -050071 # The first time we parse the commandline is only to figure out what
72 # subcommand the user wants to run. This allows us to avoid importing
73 # all subcommands which can be quite slow. This works because there is
74 # no way in Python to list all subcommands and their help output in a
75 # single run.
76 parser = GetOptions()
David Purselldfbfbc82015-03-05 10:59:16 -080077 if not argv:
78 parser.print_help()
79 return 1
80
Mike Frysinger1a470812019-11-07 01:19:17 -050081 namespace, _ = parser.parse_known_args(argv)
Harry Cuttsbf3ca762020-09-15 05:13:26 -070082
83 if namespace.subcommand == 'help':
84 if namespace.help_subcommand is None:
85 parser.print_help()
86 return
87
88 parser = GetOptions(namespace.help_subcommand)
89 parser.parse_args([namespace.help_subcommand, '--help'])
90
Mike Frysinger1a470812019-11-07 01:19:17 -050091 # The user has selected a subcommand now, so get the full parser after we
92 # import the single subcommand.
93 parser = GetOptions(namespace.subcommand)
David Purselldfbfbc82015-03-05 10:59:16 -080094 namespace = parser.parse_args(argv)
David Pursellffb90042015-03-23 09:21:41 -070095 subcommand = namespace.command_class(namespace)
Aviv Keshet01a82e92017-03-02 17:39:59 -080096 try:
97 code = _RunSubCommand(subcommand)
98 except (commandline.ChrootRequiredError, commandline.ExecRequiredError):
99 # The higher levels want these passed back, so oblige.
100 raise
101 except Exception as e:
102 code = 1
Mike Frysinger1a470812019-11-07 01:19:17 -0500103 logging.error('cros %s failed before completing.', namespace.subcommand)
Aviv Keshet01a82e92017-03-02 17:39:59 -0800104 if namespace.debug:
Mike Frysinger684d36e2015-06-03 05:03:34 -0400105 raise
Aviv Keshet01a82e92017-03-02 17:39:59 -0800106 else:
107 logging.error(e)
Mike Frysingera46f57d2018-07-19 12:18:22 -0400108 logging.error('(Re-run with --debug for more details.)')
Mike Frysinger684d36e2015-06-03 05:03:34 -0400109
Aviv Keshet01a82e92017-03-02 17:39:59 -0800110 if code is not None:
111 return code
David Purselldfbfbc82015-03-05 10:59:16 -0800112
113 return 0
114 except KeyboardInterrupt:
115 logging.debug('Aborted due to keyboard interrupt.')
Chris Sosa90c78502012-10-05 17:07:42 -0700116 return 1