blob: 8d49d0428479996d3c8ce5ac6c67359068191319 [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
Don Garrett25f309a2014-03-19 14:02:12 -07005"""This implements the metacommand cros.
6
7This script is normally invoked via depot_tools/cros which discovers
8where chromite is located and invokes this script.
9
10In turn, this script looks for a subcommand based on how it was invoked. For
11example: cros lint.
12
13See cros/command/cros_XXX.py for actual command implementations.
14"""
15
Ryan Cui47f80e42013-04-01 19:01:54 -070016import sys
17
Chris Sosa90c78502012-10-05 17:07:42 -070018from chromite.cros import commands
Chris Sosab445f792012-10-11 15:26:39 -070019from chromite.lib import commandline
Ryan Cui47f80e42013-04-01 19:01:54 -070020from chromite.lib import stats
Chris Sosa90c78502012-10-05 17:07:42 -070021
22
Brian Harring984988f2012-10-10 22:53:30 -070023def GetOptions(my_commands):
Chris Sosa90c78502012-10-05 17:07:42 -070024 """Returns the argparse to use for Cros."""
Ryan Cui445658e2012-12-14 18:28:05 -080025 parser = commandline.ArgumentParser(caching=True)
Chris Sosa90c78502012-10-05 17:07:42 -070026 if not commands:
27 return parser
28
29 subparsers = parser.add_subparsers(title='cros commands')
Brian Harringcb68df32012-12-05 04:26:29 -080030 for cmd_name, class_def in sorted(my_commands.iteritems(), key=lambda x:x[0]):
David James57d82a22012-12-04 11:24:58 -080031 epilog = getattr(class_def, 'EPILOG', None)
32 sub_parser = subparsers.add_parser(
Ryan Cui8b864512013-01-29 15:40:24 -080033 cmd_name, description=class_def.__doc__, epilog=epilog,
Ryo Hashimoto8bc997b2014-01-22 18:46:17 +090034 caching=class_def.use_caching_options,
David James57d82a22012-12-04 11:24:58 -080035 formatter_class=commandline.argparse.RawDescriptionHelpFormatter)
Chris Sosa90c78502012-10-05 17:07:42 -070036 class_def.AddParser(sub_parser)
37
38 return parser
39
40
Ryan Cui47f80e42013-04-01 19:01:54 -070041def _RunSubCommand(subcommand):
42 """Helper function for testing purposes."""
Ryan Cuide21f482013-08-06 11:50:59 -070043 return subcommand.Run()
Ryan Cui47f80e42013-04-01 19:01:54 -070044
45
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040046def main(argv):
Brian Harring984988f2012-10-10 22:53:30 -070047 parser = GetOptions(commands.ListCommands())
Chris Sosa90c78502012-10-05 17:07:42 -070048 # Cros currently does nothing without a subcmd. Print help if no args are
49 # specified.
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040050 if not argv:
Chris Sosa90c78502012-10-05 17:07:42 -070051 parser.print_help()
52 return 1
53
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040054 namespace = parser.parse_args(argv)
Chris Sosa90c78502012-10-05 17:07:42 -070055 subcommand = namespace.cros_class(namespace)
Ryan Cui47f80e42013-04-01 19:01:54 -070056 with stats.UploadContext() as queue:
Ryan Cui04a91462013-04-19 16:09:57 -070057 if subcommand.upload_stats:
58 cmd_base = subcommand.options.cros_class.command_name
Ryan Cuicbd9bb62013-04-30 11:17:02 -070059 cmd_stats = stats.Stats.SafeInit(cmd_line=sys.argv, cmd_base=cmd_base)
60 if cmd_stats:
61 queue.put([cmd_stats, stats.StatsUploader.URL,
62 subcommand.upload_stats_timeout])
Ryan Cui47f80e42013-04-01 19:01:54 -070063 # TODO: to make command completion faster, send an interrupt signal to the
64 # stats uploader task after the subcommand completes.
Ryan Cuide21f482013-08-06 11:50:59 -070065 code = _RunSubCommand(subcommand)
66 if code is not None:
67 return code
Ryan Cui47f80e42013-04-01 19:01:54 -070068
Chris Sosa90c78502012-10-05 17:07:42 -070069 return 0