blob: 5ecb413a91a7c47d36a9a5192cf307e9811289e1 [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
Ryan Cui47f80e42013-04-01 19:01:54 -070018import sys
19
David Pursellf1c27c12015-03-18 09:51:38 -070020from chromite.cli import command
Chris Sosab445f792012-10-11 15:26:39 -070021from chromite.lib import commandline
Ralph Nathan91874ca2015-03-19 13:29:41 -070022from chromite.lib import cros_logging as logging
Ryan Cui47f80e42013-04-01 19:01:54 -070023from chromite.lib import stats
Chris Sosa90c78502012-10-05 17:07:42 -070024
25
Brian Harring984988f2012-10-10 22:53:30 -070026def GetOptions(my_commands):
David Pursell643ed342015-07-07 09:24:32 -070027 """Returns the parser to use for commandline parsing.
Chris Sosa90c78502012-10-05 17:07:42 -070028
David Pursell643ed342015-07-07 09:24:32 -070029 Args:
30 my_commands: A dictionary mapping subcommand names to classes.
31
32 Returns:
33 A commandline.ArgumentParser object.
34 """
35 parser = commandline.ArgumentParser(caching=True, default_log_level='notice')
36
37 if my_commands:
38 subparsers = parser.add_subparsers(title='Subcommands')
39 for cmd_name in sorted(my_commands.iterkeys()):
40 class_def = my_commands[cmd_name]
41 epilog = getattr(class_def, 'EPILOG', None)
42 sub_parser = subparsers.add_parser(
43 cmd_name, description=class_def.__doc__, epilog=epilog,
44 caching=class_def.use_caching_options,
45 formatter_class=commandline.argparse.RawDescriptionHelpFormatter)
46 class_def.AddParser(sub_parser)
Chris Sosa90c78502012-10-05 17:07:42 -070047
48 return parser
49
50
Ryan Cui47f80e42013-04-01 19:01:54 -070051def _RunSubCommand(subcommand):
52 """Helper function for testing purposes."""
Ryan Cuide21f482013-08-06 11:50:59 -070053 return subcommand.Run()
Ryan Cui47f80e42013-04-01 19:01:54 -070054
55
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040056def main(argv):
David Purselldfbfbc82015-03-05 10:59:16 -080057 try:
David Pursellf1c27c12015-03-18 09:51:38 -070058 parser = GetOptions(command.ListCommands())
David Purselldfbfbc82015-03-05 10:59:16 -080059 # Cros currently does nothing without a subcmd. Print help if no args are
60 # specified.
61 if not argv:
62 parser.print_help()
63 return 1
64
65 namespace = parser.parse_args(argv)
David Pursellffb90042015-03-23 09:21:41 -070066 subcommand = namespace.command_class(namespace)
David Purselldfbfbc82015-03-05 10:59:16 -080067 with stats.UploadContext() as queue:
68 if subcommand.upload_stats:
David Pursellffb90042015-03-23 09:21:41 -070069 cmd_base = subcommand.options.command_class.command_name
David Purselldfbfbc82015-03-05 10:59:16 -080070 cmd_stats = stats.Stats.SafeInit(cmd_line=sys.argv, cmd_base=cmd_base)
71 if cmd_stats:
72 queue.put([cmd_stats, stats.StatsUploader.URL,
73 subcommand.upload_stats_timeout])
74 # TODO: to make command completion faster, send an interrupt signal to the
75 # stats uploader task after the subcommand completes.
Mike Frysinger684d36e2015-06-03 05:03:34 -040076 try:
77 code = _RunSubCommand(subcommand)
78 except (commandline.ChrootRequiredError, commandline.ExecRequiredError):
79 # The higher levels want these passed back, so oblige.
80 raise
81 except Exception as e:
82 code = 1
David Pursell643ed342015-07-07 09:24:32 -070083 logging.error('cros %s failed before completing.',
Mike Frysinger684d36e2015-06-03 05:03:34 -040084 subcommand.command_name)
85 if namespace.debug:
86 raise
87 else:
88 logging.error(e)
89
David Purselldfbfbc82015-03-05 10:59:16 -080090 if code is not None:
91 return code
92
93 return 0
94 except KeyboardInterrupt:
95 logging.debug('Aborted due to keyboard interrupt.')
Chris Sosa90c78502012-10-05 17:07:42 -070096 return 1