Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 1 | # 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 Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 5 | """This implements the entry point for CLI toolsets `cros` and `brillo`. |
Don Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 6 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 7 | This script is invoked by chromite/bin/{cros,brillo}, which sets up the |
| 8 | proper execution environment and calls this module's main() function. |
Don Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 9 | |
| 10 | In turn, this script looks for a subcommand based on how it was invoked. For |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 11 | example, `cros lint` will use the cli/cros/cros_lint.py subcommand. |
Don Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 12 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 13 | See cli/ for actual command implementations. |
Don Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 14 | """ |
| 15 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 16 | from __future__ import print_function |
| 17 | |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 18 | import sys |
| 19 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 20 | from chromite.cli import command |
Chris Sosa | b445f79 | 2012-10-11 15:26:39 -0700 | [diff] [blame] | 21 | from chromite.lib import commandline |
Ralph Nathan | 91874ca | 2015-03-19 13:29:41 -0700 | [diff] [blame] | 22 | from chromite.lib import cros_logging as logging |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 23 | from chromite.lib import stats |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 24 | |
| 25 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 26 | def GetOptions(my_commands): |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 27 | """Returns the argparse to use for commandline parsing.""" |
Ralph Nathan | 38372f4 | 2015-05-19 15:56:25 -0700 | [diff] [blame^] | 28 | parser = commandline.ArgumentParser(caching=True, default_log_level='notice') |
Ralph Nathan | 1fc77f2 | 2015-04-21 15:05:48 -0700 | [diff] [blame] | 29 | if command.GetToolset() == 'brillo': |
Ralph Nathan | 74e864d | 2015-05-11 12:13:53 -0700 | [diff] [blame] | 30 | # For brillo commands, we want to store the logs to a file for error |
| 31 | # handling at logging level notice. |
| 32 | command.SetupFileLogger() |
Ralph Nathan | 38372f4 | 2015-05-19 15:56:25 -0700 | [diff] [blame^] | 33 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 34 | if not command: |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 35 | return parser |
| 36 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 37 | subparsers = parser.add_subparsers(title='Subcommands') |
Mike Frysinger | d6e2df0 | 2014-11-26 02:55:04 -0500 | [diff] [blame] | 38 | for cmd_name, class_def in sorted(my_commands.iteritems(), |
| 39 | key=lambda x: x[0]): |
David James | 57d82a2 | 2012-12-04 11:24:58 -0800 | [diff] [blame] | 40 | epilog = getattr(class_def, 'EPILOG', None) |
| 41 | sub_parser = subparsers.add_parser( |
Ryan Cui | 8b86451 | 2013-01-29 15:40:24 -0800 | [diff] [blame] | 42 | cmd_name, description=class_def.__doc__, epilog=epilog, |
Ryo Hashimoto | 8bc997b | 2014-01-22 18:46:17 +0900 | [diff] [blame] | 43 | caching=class_def.use_caching_options, |
David James | 57d82a2 | 2012-12-04 11:24:58 -0800 | [diff] [blame] | 44 | formatter_class=commandline.argparse.RawDescriptionHelpFormatter) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 45 | class_def.AddParser(sub_parser) |
| 46 | |
| 47 | return parser |
| 48 | |
| 49 | |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 50 | def _RunSubCommand(subcommand): |
| 51 | """Helper function for testing purposes.""" |
Ryan Cui | de21f48 | 2013-08-06 11:50:59 -0700 | [diff] [blame] | 52 | return subcommand.Run() |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 53 | |
| 54 | |
Mike Frysinger | 9ad5fab | 2013-05-30 13:37:26 -0400 | [diff] [blame] | 55 | def main(argv): |
David Pursell | dfbfbc8 | 2015-03-05 10:59:16 -0800 | [diff] [blame] | 56 | try: |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 57 | parser = GetOptions(command.ListCommands()) |
David Pursell | dfbfbc8 | 2015-03-05 10:59:16 -0800 | [diff] [blame] | 58 | # Cros currently does nothing without a subcmd. Print help if no args are |
| 59 | # specified. |
| 60 | if not argv: |
| 61 | parser.print_help() |
| 62 | return 1 |
| 63 | |
| 64 | namespace = parser.parse_args(argv) |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 65 | subcommand = namespace.command_class(namespace) |
David Pursell | dfbfbc8 | 2015-03-05 10:59:16 -0800 | [diff] [blame] | 66 | with stats.UploadContext() as queue: |
| 67 | if subcommand.upload_stats: |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 68 | cmd_base = subcommand.options.command_class.command_name |
David Pursell | dfbfbc8 | 2015-03-05 10:59:16 -0800 | [diff] [blame] | 69 | cmd_stats = stats.Stats.SafeInit(cmd_line=sys.argv, cmd_base=cmd_base) |
| 70 | if cmd_stats: |
| 71 | queue.put([cmd_stats, stats.StatsUploader.URL, |
| 72 | subcommand.upload_stats_timeout]) |
| 73 | # TODO: to make command completion faster, send an interrupt signal to the |
| 74 | # stats uploader task after the subcommand completes. |
| 75 | code = _RunSubCommand(subcommand) |
| 76 | if code is not None: |
| 77 | return code |
| 78 | |
| 79 | return 0 |
| 80 | except KeyboardInterrupt: |
| 81 | logging.debug('Aborted due to keyboard interrupt.') |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 82 | return 1 |