blob: 3120f625632623f0cd3239ea7f810724b6ed9e29 [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
Mike Frysinger383367e2014-09-16 15:06:17 -040016from __future__ import print_function
17
Ryan Cui47f80e42013-04-01 19:01:54 -070018import sys
19
Chris Sosa90c78502012-10-05 17:07:42 -070020from chromite.cros import commands
Chris Sosab445f792012-10-11 15:26:39 -070021from chromite.lib import commandline
Ryan Cui47f80e42013-04-01 19:01:54 -070022from chromite.lib import stats
Chris Sosa90c78502012-10-05 17:07:42 -070023
24
Brian Harring984988f2012-10-10 22:53:30 -070025def GetOptions(my_commands):
Chris Sosa90c78502012-10-05 17:07:42 -070026 """Returns the argparse to use for Cros."""
Ryan Cui445658e2012-12-14 18:28:05 -080027 parser = commandline.ArgumentParser(caching=True)
Chris Sosa90c78502012-10-05 17:07:42 -070028 if not commands:
29 return parser
30
31 subparsers = parser.add_subparsers(title='cros commands')
Mike Frysingerd6e2df02014-11-26 02:55:04 -050032 for cmd_name, class_def in sorted(my_commands.iteritems(),
33 key=lambda x: x[0]):
David James57d82a22012-12-04 11:24:58 -080034 epilog = getattr(class_def, 'EPILOG', None)
35 sub_parser = subparsers.add_parser(
Ryan Cui8b864512013-01-29 15:40:24 -080036 cmd_name, description=class_def.__doc__, epilog=epilog,
Ryo Hashimoto8bc997b2014-01-22 18:46:17 +090037 caching=class_def.use_caching_options,
David James57d82a22012-12-04 11:24:58 -080038 formatter_class=commandline.argparse.RawDescriptionHelpFormatter)
Chris Sosa90c78502012-10-05 17:07:42 -070039 class_def.AddParser(sub_parser)
40
41 return parser
42
43
Ryan Cui47f80e42013-04-01 19:01:54 -070044def _RunSubCommand(subcommand):
45 """Helper function for testing purposes."""
Ryan Cuide21f482013-08-06 11:50:59 -070046 return subcommand.Run()
Ryan Cui47f80e42013-04-01 19:01:54 -070047
48
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040049def main(argv):
Brian Harring984988f2012-10-10 22:53:30 -070050 parser = GetOptions(commands.ListCommands())
Chris Sosa90c78502012-10-05 17:07:42 -070051 # Cros currently does nothing without a subcmd. Print help if no args are
52 # specified.
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040053 if not argv:
Chris Sosa90c78502012-10-05 17:07:42 -070054 parser.print_help()
55 return 1
56
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040057 namespace = parser.parse_args(argv)
Chris Sosa90c78502012-10-05 17:07:42 -070058 subcommand = namespace.cros_class(namespace)
Ryan Cui47f80e42013-04-01 19:01:54 -070059 with stats.UploadContext() as queue:
Ryan Cui04a91462013-04-19 16:09:57 -070060 if subcommand.upload_stats:
61 cmd_base = subcommand.options.cros_class.command_name
Ryan Cuicbd9bb62013-04-30 11:17:02 -070062 cmd_stats = stats.Stats.SafeInit(cmd_line=sys.argv, cmd_base=cmd_base)
63 if cmd_stats:
64 queue.put([cmd_stats, stats.StatsUploader.URL,
65 subcommand.upload_stats_timeout])
Ryan Cui47f80e42013-04-01 19:01:54 -070066 # TODO: to make command completion faster, send an interrupt signal to the
67 # stats uploader task after the subcommand completes.
Ryan Cuide21f482013-08-06 11:50:59 -070068 code = _RunSubCommand(subcommand)
69 if code is not None:
70 return code
Ryan Cui47f80e42013-04-01 19:01:54 -070071
Chris Sosa90c78502012-10-05 17:07:42 -070072 return 0