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 | |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 5 | import sys |
| 6 | |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 7 | from chromite.cros import commands |
Chris Sosa | b445f79 | 2012-10-11 15:26:39 -0700 | [diff] [blame] | 8 | from chromite.lib import commandline |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 9 | from chromite.lib import stats |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 10 | |
| 11 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 12 | def GetOptions(my_commands): |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 13 | """Returns the argparse to use for Cros.""" |
Ryan Cui | 445658e | 2012-12-14 18:28:05 -0800 | [diff] [blame] | 14 | parser = commandline.ArgumentParser(caching=True) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 15 | if not commands: |
| 16 | return parser |
| 17 | |
| 18 | subparsers = parser.add_subparsers(title='cros commands') |
Brian Harring | cb68df3 | 2012-12-05 04:26:29 -0800 | [diff] [blame] | 19 | for cmd_name, class_def in sorted(my_commands.iteritems(), key=lambda x:x[0]): |
David James | 57d82a2 | 2012-12-04 11:24:58 -0800 | [diff] [blame] | 20 | epilog = getattr(class_def, 'EPILOG', None) |
| 21 | sub_parser = subparsers.add_parser( |
Ryan Cui | 8b86451 | 2013-01-29 15:40:24 -0800 | [diff] [blame] | 22 | cmd_name, description=class_def.__doc__, epilog=epilog, |
David James | 57d82a2 | 2012-12-04 11:24:58 -0800 | [diff] [blame] | 23 | formatter_class=commandline.argparse.RawDescriptionHelpFormatter) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 24 | class_def.AddParser(sub_parser) |
| 25 | |
| 26 | return parser |
| 27 | |
| 28 | |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 29 | def _RunSubCommand(subcommand): |
| 30 | """Helper function for testing purposes.""" |
| 31 | subcommand.Run() |
| 32 | |
| 33 | |
Mike Frysinger | 9ad5fab | 2013-05-30 13:37:26 -0400 | [diff] [blame] | 34 | def main(argv): |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 35 | parser = GetOptions(commands.ListCommands()) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 36 | # Cros currently does nothing without a subcmd. Print help if no args are |
| 37 | # specified. |
Mike Frysinger | 9ad5fab | 2013-05-30 13:37:26 -0400 | [diff] [blame] | 38 | if not argv: |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 39 | parser.print_help() |
| 40 | return 1 |
| 41 | |
Mike Frysinger | 9ad5fab | 2013-05-30 13:37:26 -0400 | [diff] [blame] | 42 | namespace = parser.parse_args(argv) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 43 | subcommand = namespace.cros_class(namespace) |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 44 | with stats.UploadContext() as queue: |
Ryan Cui | 04a9146 | 2013-04-19 16:09:57 -0700 | [diff] [blame] | 45 | if subcommand.upload_stats: |
| 46 | cmd_base = subcommand.options.cros_class.command_name |
Ryan Cui | cbd9bb6 | 2013-04-30 11:17:02 -0700 | [diff] [blame] | 47 | cmd_stats = stats.Stats.SafeInit(cmd_line=sys.argv, cmd_base=cmd_base) |
| 48 | if cmd_stats: |
| 49 | queue.put([cmd_stats, stats.StatsUploader.URL, |
| 50 | subcommand.upload_stats_timeout]) |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 51 | # TODO: to make command completion faster, send an interrupt signal to the |
| 52 | # stats uploader task after the subcommand completes. |
| 53 | _RunSubCommand(subcommand) |
| 54 | |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 55 | return 0 |