blob: eb5ab76f5d7b143abc75ab27deb8a9d03984da2e [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
Ryan Cui47f80e42013-04-01 19:01:54 -07005import sys
6
Chris Sosa90c78502012-10-05 17:07:42 -07007from chromite.cros import commands
Chris Sosab445f792012-10-11 15:26:39 -07008from chromite.lib import commandline
Ryan Cui47f80e42013-04-01 19:01:54 -07009from chromite.lib import stats
Chris Sosa90c78502012-10-05 17:07:42 -070010
11
Brian Harring984988f2012-10-10 22:53:30 -070012def GetOptions(my_commands):
Chris Sosa90c78502012-10-05 17:07:42 -070013 """Returns the argparse to use for Cros."""
Ryan Cui445658e2012-12-14 18:28:05 -080014 parser = commandline.ArgumentParser(caching=True)
Chris Sosa90c78502012-10-05 17:07:42 -070015 if not commands:
16 return parser
17
18 subparsers = parser.add_subparsers(title='cros commands')
Brian Harringcb68df32012-12-05 04:26:29 -080019 for cmd_name, class_def in sorted(my_commands.iteritems(), key=lambda x:x[0]):
David James57d82a22012-12-04 11:24:58 -080020 epilog = getattr(class_def, 'EPILOG', None)
21 sub_parser = subparsers.add_parser(
Ryan Cui8b864512013-01-29 15:40:24 -080022 cmd_name, description=class_def.__doc__, epilog=epilog,
David James57d82a22012-12-04 11:24:58 -080023 formatter_class=commandline.argparse.RawDescriptionHelpFormatter)
Chris Sosa90c78502012-10-05 17:07:42 -070024 class_def.AddParser(sub_parser)
25
26 return parser
27
28
Ryan Cui47f80e42013-04-01 19:01:54 -070029def _RunSubCommand(subcommand):
30 """Helper function for testing purposes."""
31 subcommand.Run()
32
33
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040034def main(argv):
Brian Harring984988f2012-10-10 22:53:30 -070035 parser = GetOptions(commands.ListCommands())
Chris Sosa90c78502012-10-05 17:07:42 -070036 # Cros currently does nothing without a subcmd. Print help if no args are
37 # specified.
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040038 if not argv:
Chris Sosa90c78502012-10-05 17:07:42 -070039 parser.print_help()
40 return 1
41
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040042 namespace = parser.parse_args(argv)
Chris Sosa90c78502012-10-05 17:07:42 -070043 subcommand = namespace.cros_class(namespace)
Ryan Cui47f80e42013-04-01 19:01:54 -070044 with stats.UploadContext() as queue:
Ryan Cui04a91462013-04-19 16:09:57 -070045 if subcommand.upload_stats:
46 cmd_base = subcommand.options.cros_class.command_name
Ryan Cuicbd9bb62013-04-30 11:17:02 -070047 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 Cui47f80e42013-04-01 19:01:54 -070051 # 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 Sosa90c78502012-10-05 17:07:42 -070055 return 0