blob: c2838f3f6d925c8b4b825a7b4dea1a0505a6c279 [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 Frysinger383367e2014-09-16 15:06:17 -040016from __future__ import print_function
17
David Pursellf1c27c12015-03-18 09:51:38 -070018from chromite.cli import command
Chris Sosab445f792012-10-11 15:26:39 -070019from chromite.lib import commandline
Ralph Nathan91874ca2015-03-19 13:29:41 -070020from chromite.lib import cros_logging as logging
Chris Sosa90c78502012-10-05 17:07:42 -070021
22
Brian Harring984988f2012-10-10 22:53:30 -070023def GetOptions(my_commands):
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:
27 my_commands: A dictionary mapping subcommand names to classes.
28
29 Returns:
30 A commandline.ArgumentParser object.
31 """
32 parser = commandline.ArgumentParser(caching=True, default_log_level='notice')
33
34 if my_commands:
35 subparsers = parser.add_subparsers(title='Subcommands')
36 for cmd_name in sorted(my_commands.iterkeys()):
37 class_def = my_commands[cmd_name]
38 epilog = getattr(class_def, 'EPILOG', None)
39 sub_parser = subparsers.add_parser(
40 cmd_name, description=class_def.__doc__, epilog=epilog,
41 caching=class_def.use_caching_options,
42 formatter_class=commandline.argparse.RawDescriptionHelpFormatter)
43 class_def.AddParser(sub_parser)
Chris Sosa90c78502012-10-05 17:07:42 -070044
45 return parser
46
47
Ryan Cui47f80e42013-04-01 19:01:54 -070048def _RunSubCommand(subcommand):
49 """Helper function for testing purposes."""
Ryan Cuide21f482013-08-06 11:50:59 -070050 return subcommand.Run()
Ryan Cui47f80e42013-04-01 19:01:54 -070051
52
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040053def main(argv):
David Purselldfbfbc82015-03-05 10:59:16 -080054 try:
David Pursellf1c27c12015-03-18 09:51:38 -070055 parser = GetOptions(command.ListCommands())
David Purselldfbfbc82015-03-05 10:59:16 -080056 # Cros currently does nothing without a subcmd. Print help if no args are
57 # specified.
58 if not argv:
59 parser.print_help()
60 return 1
61
62 namespace = parser.parse_args(argv)
David Pursellffb90042015-03-23 09:21:41 -070063 subcommand = namespace.command_class(namespace)
Aviv Keshet01a82e92017-03-02 17:39:59 -080064 try:
65 code = _RunSubCommand(subcommand)
66 except (commandline.ChrootRequiredError, commandline.ExecRequiredError):
67 # The higher levels want these passed back, so oblige.
68 raise
69 except Exception as e:
70 code = 1
71 logging.error('cros %s failed before completing.',
72 subcommand.command_name)
73 if namespace.debug:
Mike Frysinger684d36e2015-06-03 05:03:34 -040074 raise
Aviv Keshet01a82e92017-03-02 17:39:59 -080075 else:
76 logging.error(e)
Mike Frysinger684d36e2015-06-03 05:03:34 -040077
Aviv Keshet01a82e92017-03-02 17:39:59 -080078 if code is not None:
79 return code
David Purselldfbfbc82015-03-05 10:59:16 -080080
81 return 0
82 except KeyboardInterrupt:
83 logging.debug('Aborted due to keyboard interrupt.')
Chris Sosa90c78502012-10-05 17:07:42 -070084 return 1