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 | |
Don Garrett | 25f309a | 2014-03-19 14:02:12 -0700 | [diff] [blame] | 5 | """This implements the metacommand cros. |
| 6 | |
| 7 | This script is normally invoked via depot_tools/cros which discovers |
| 8 | where chromite is located and invokes this script. |
| 9 | |
| 10 | In turn, this script looks for a subcommand based on how it was invoked. For |
| 11 | example: cros lint. |
| 12 | |
| 13 | See cros/command/cros_XXX.py for actual command implementations. |
| 14 | """ |
| 15 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 16 | from __future__ import print_function |
| 17 | |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 18 | import sys |
| 19 | |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 20 | from chromite.cros import commands |
Chris Sosa | b445f79 | 2012-10-11 15:26:39 -0700 | [diff] [blame] | 21 | from chromite.lib import commandline |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 22 | from chromite.lib import stats |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 23 | |
| 24 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 25 | def GetOptions(my_commands): |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 26 | """Returns the argparse to use for Cros.""" |
Ryan Cui | 445658e | 2012-12-14 18:28:05 -0800 | [diff] [blame] | 27 | parser = commandline.ArgumentParser(caching=True) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 28 | if not commands: |
| 29 | return parser |
| 30 | |
| 31 | subparsers = parser.add_subparsers(title='cros commands') |
Brian Harring | cb68df3 | 2012-12-05 04:26:29 -0800 | [diff] [blame] | 32 | 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] | 33 | epilog = getattr(class_def, 'EPILOG', None) |
| 34 | sub_parser = subparsers.add_parser( |
Ryan Cui | 8b86451 | 2013-01-29 15:40:24 -0800 | [diff] [blame] | 35 | cmd_name, description=class_def.__doc__, epilog=epilog, |
Ryo Hashimoto | 8bc997b | 2014-01-22 18:46:17 +0900 | [diff] [blame] | 36 | caching=class_def.use_caching_options, |
David James | 57d82a2 | 2012-12-04 11:24:58 -0800 | [diff] [blame] | 37 | formatter_class=commandline.argparse.RawDescriptionHelpFormatter) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 38 | class_def.AddParser(sub_parser) |
| 39 | |
| 40 | return parser |
| 41 | |
| 42 | |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 43 | def _RunSubCommand(subcommand): |
| 44 | """Helper function for testing purposes.""" |
Ryan Cui | de21f48 | 2013-08-06 11:50:59 -0700 | [diff] [blame] | 45 | return subcommand.Run() |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 46 | |
| 47 | |
Mike Frysinger | 9ad5fab | 2013-05-30 13:37:26 -0400 | [diff] [blame] | 48 | def main(argv): |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 49 | parser = GetOptions(commands.ListCommands()) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 50 | # Cros currently does nothing without a subcmd. Print help if no args are |
| 51 | # specified. |
Mike Frysinger | 9ad5fab | 2013-05-30 13:37:26 -0400 | [diff] [blame] | 52 | if not argv: |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 53 | parser.print_help() |
| 54 | return 1 |
| 55 | |
Mike Frysinger | 9ad5fab | 2013-05-30 13:37:26 -0400 | [diff] [blame] | 56 | namespace = parser.parse_args(argv) |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 57 | subcommand = namespace.cros_class(namespace) |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 58 | with stats.UploadContext() as queue: |
Ryan Cui | 04a9146 | 2013-04-19 16:09:57 -0700 | [diff] [blame] | 59 | if subcommand.upload_stats: |
| 60 | cmd_base = subcommand.options.cros_class.command_name |
Ryan Cui | cbd9bb6 | 2013-04-30 11:17:02 -0700 | [diff] [blame] | 61 | cmd_stats = stats.Stats.SafeInit(cmd_line=sys.argv, cmd_base=cmd_base) |
| 62 | if cmd_stats: |
| 63 | queue.put([cmd_stats, stats.StatsUploader.URL, |
| 64 | subcommand.upload_stats_timeout]) |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 65 | # TODO: to make command completion faster, send an interrupt signal to the |
| 66 | # stats uploader task after the subcommand completes. |
Ryan Cui | de21f48 | 2013-08-06 11:50:59 -0700 | [diff] [blame] | 67 | code = _RunSubCommand(subcommand) |
| 68 | if code is not None: |
| 69 | return code |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 70 | |
Chris Sosa | 90c7850 | 2012-10-05 17:07:42 -0700 | [diff] [blame] | 71 | return 0 |