blob: c21c183f83df0530af2bcbafea46c0c217ffffa8 [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
David Purselldfbfbc82015-03-05 10:59:16 -080018import logging
Ryan Cui47f80e42013-04-01 19:01:54 -070019import sys
20
Chris Sosa90c78502012-10-05 17:07:42 -070021from chromite.cros import commands
Chris Sosab445f792012-10-11 15:26:39 -070022from chromite.lib import commandline
Ryan Cui47f80e42013-04-01 19:01:54 -070023from chromite.lib import stats
Chris Sosa90c78502012-10-05 17:07:42 -070024
25
Brian Harring984988f2012-10-10 22:53:30 -070026def GetOptions(my_commands):
Chris Sosa90c78502012-10-05 17:07:42 -070027 """Returns the argparse to use for Cros."""
Ryan Cui445658e2012-12-14 18:28:05 -080028 parser = commandline.ArgumentParser(caching=True)
Chris Sosa90c78502012-10-05 17:07:42 -070029 if not commands:
30 return parser
31
32 subparsers = parser.add_subparsers(title='cros commands')
Mike Frysingerd6e2df02014-11-26 02:55:04 -050033 for cmd_name, class_def in sorted(my_commands.iteritems(),
34 key=lambda x: x[0]):
David James57d82a22012-12-04 11:24:58 -080035 epilog = getattr(class_def, 'EPILOG', None)
36 sub_parser = subparsers.add_parser(
Ryan Cui8b864512013-01-29 15:40:24 -080037 cmd_name, description=class_def.__doc__, epilog=epilog,
Ryo Hashimoto8bc997b2014-01-22 18:46:17 +090038 caching=class_def.use_caching_options,
David James57d82a22012-12-04 11:24:58 -080039 formatter_class=commandline.argparse.RawDescriptionHelpFormatter)
Chris Sosa90c78502012-10-05 17:07:42 -070040 class_def.AddParser(sub_parser)
41
42 return parser
43
44
Ryan Cui47f80e42013-04-01 19:01:54 -070045def _RunSubCommand(subcommand):
46 """Helper function for testing purposes."""
Ryan Cuide21f482013-08-06 11:50:59 -070047 return subcommand.Run()
Ryan Cui47f80e42013-04-01 19:01:54 -070048
49
Mike Frysinger9ad5fab2013-05-30 13:37:26 -040050def main(argv):
David Purselldfbfbc82015-03-05 10:59:16 -080051 try:
52 parser = GetOptions(commands.ListCommands())
53 # Cros currently does nothing without a subcmd. Print help if no args are
54 # specified.
55 if not argv:
56 parser.print_help()
57 return 1
58
59 namespace = parser.parse_args(argv)
60 subcommand = namespace.cros_class(namespace)
61 with stats.UploadContext() as queue:
62 if subcommand.upload_stats:
63 cmd_base = subcommand.options.cros_class.command_name
64 cmd_stats = stats.Stats.SafeInit(cmd_line=sys.argv, cmd_base=cmd_base)
65 if cmd_stats:
66 queue.put([cmd_stats, stats.StatsUploader.URL,
67 subcommand.upload_stats_timeout])
68 # TODO: to make command completion faster, send an interrupt signal to the
69 # stats uploader task after the subcommand completes.
70 code = _RunSubCommand(subcommand)
71 if code is not None:
72 return code
73
74 return 0
75 except KeyboardInterrupt:
76 logging.debug('Aborted due to keyboard interrupt.')
Chris Sosa90c78502012-10-05 17:07:42 -070077 return 1