Brian Harring | 984988f | 2012-10-10 22:53:30 -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 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 5 | """Module that contains meta-logic related to CLI commands. |
| 6 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 7 | This module contains two important definitions used by all commands: |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 8 | CliCommand: The parent class of all CLI commands. |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 9 | CommandDecorator: Decorator that must be used to ensure that the command shows |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 10 | up in |_commands| and is discoverable. |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 11 | |
| 12 | Commands can be either imported directly or looked up using this module's |
| 13 | ListCommands() function. |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 14 | """ |
| 15 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 16 | from __future__ import print_function |
| 17 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 18 | import glob |
| 19 | import os |
| 20 | |
Ralph Nathan | 74e864d | 2015-05-11 12:13:53 -0700 | [diff] [blame] | 21 | from chromite.cbuildbot import constants |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 22 | from chromite.lib import commandline |
| 23 | from chromite.lib import cros_build_lib |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 24 | from chromite.lib import cros_import |
Ralph Nathan | 6993125 | 2015-04-14 16:49:21 -0700 | [diff] [blame] | 25 | from chromite.lib import cros_logging as logging |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 26 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 27 | |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 28 | # Paths for finding and importing subcommand modules. |
| 29 | _SUBCOMMAND_MODULE_DIRECTORY = os.path.join(os.path.dirname(__file__), 'cros') |
| 30 | _SUBCOMMAND_MODULE_PREFIX = 'cros_' |
| 31 | |
| 32 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 33 | _commands = dict() |
| 34 | |
| 35 | |
Ralph Nathan | 6993125 | 2015-04-14 16:49:21 -0700 | [diff] [blame] | 36 | def UseProgressBar(): |
| 37 | """Determine whether the progress bar is to be used or not. |
| 38 | |
| 39 | We only want the progress bar to display for the brillo commands which operate |
| 40 | at logging level NOTICE. If the user wants to see the noisy output, then they |
| 41 | can execute the command at logging level INFO or DEBUG. |
| 42 | """ |
| 43 | return logging.getLogger().getEffectiveLevel() == logging.NOTICE |
| 44 | |
| 45 | |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 46 | def _FindModules(subdir_path): |
| 47 | """Returns a list of subcommand python modules in |subdir_path|. |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 48 | |
| 49 | Args: |
| 50 | subdir_path: directory (string) to search for modules in. |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 51 | |
| 52 | Returns: |
| 53 | List of filenames (strings). |
| 54 | """ |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 55 | modules = [] |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 56 | glob_path = os.path.join(subdir_path, '%s*.py' % _SUBCOMMAND_MODULE_PREFIX) |
| 57 | for file_path in glob.glob(glob_path): |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 58 | if not file_path.endswith('_unittest.py'): |
| 59 | modules.append(file_path) |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 60 | return modules |
| 61 | |
| 62 | |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 63 | def _ImportCommands(): |
| 64 | """Directly imports all subcommand python modules. |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 65 | |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 66 | This method imports the modules which may contain subcommands. When |
| 67 | these modules are loaded, declared commands (those that use |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 68 | CommandDecorator) will automatically get added to |_commands|. |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 69 | """ |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 70 | for file_path in _FindModules(_SUBCOMMAND_MODULE_DIRECTORY): |
| 71 | module_path = os.path.splitext(file_path)[0] |
| 72 | import_path = os.path.relpath(os.path.realpath(module_path), |
| 73 | os.path.dirname(constants.CHROMITE_DIR)) |
| 74 | cros_import.ImportModule(import_path.split(os.path.sep)) |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 75 | |
| 76 | |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 77 | def ListCommands(): |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 78 | """Return a dictionary mapping command names to classes. |
| 79 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 80 | Returns: |
| 81 | A dictionary mapping names (strings) to commands (classes). |
| 82 | """ |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 83 | _ImportCommands() |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 84 | return _commands.copy() |
| 85 | |
| 86 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 87 | class InvalidCommandError(Exception): |
| 88 | """Error that occurs when command class fails sanity checks.""" |
| 89 | pass |
| 90 | |
| 91 | |
| 92 | def CommandDecorator(command_name): |
| 93 | """Decorator that sanity checks and adds class to list of usable commands.""" |
| 94 | |
| 95 | def InnerCommandDecorator(original_class): |
| 96 | """"Inner Decorator that actually wraps the class.""" |
| 97 | if not hasattr(original_class, '__doc__'): |
| 98 | raise InvalidCommandError('All handlers must have docstrings: %s' % |
| 99 | original_class) |
| 100 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 101 | if not issubclass(original_class, CliCommand): |
| 102 | raise InvalidCommandError('All Commands must derive from CliCommand: %s' % |
| 103 | original_class) |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 104 | |
| 105 | _commands[command_name] = original_class |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 106 | original_class.command_name = command_name |
| 107 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 108 | return original_class |
| 109 | |
| 110 | return InnerCommandDecorator |
| 111 | |
| 112 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 113 | class CliCommand(object): |
| 114 | """All CLI commands must derive from this class. |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 115 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 116 | This class provides the abstract interface for all CLI commands. When |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 117 | designing a new command, you must sub-class from this class and use the |
| 118 | CommandDecorator decorator. You must specify a class docstring as that will be |
| 119 | used as the usage for the sub-command. |
| 120 | |
| 121 | In addition your command should implement AddParser which is passed in a |
| 122 | parser that you can add your own custom arguments. See argparse for more |
| 123 | information. |
| 124 | """ |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 125 | # Indicates whether command stats should be uploaded for this command. |
| 126 | # Override to enable command stats uploading. |
| 127 | upload_stats = False |
| 128 | # We set the default timeout to 1 second, to prevent overly long waits for |
| 129 | # commands to complete. From manual tests, stat uploads usually take |
| 130 | # between 0.35s-0.45s in MTV. |
| 131 | upload_stats_timeout = 1 |
| 132 | |
Ryo Hashimoto | 8bc997b | 2014-01-22 18:46:17 +0900 | [diff] [blame] | 133 | # Indicates whether command uses cache related commandline options. |
| 134 | use_caching_options = False |
| 135 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 136 | def __init__(self, options): |
| 137 | self.options = options |
| 138 | |
| 139 | @classmethod |
| 140 | def AddParser(cls, parser): |
| 141 | """Add arguments for this command to the parser.""" |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 142 | parser.set_defaults(command_class=cls) |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 143 | |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 144 | @classmethod |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 145 | def AddDeviceArgument(cls, parser, schemes=commandline.DEVICE_SCHEME_SSH): |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 146 | """Add a device argument to the parser. |
| 147 | |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 148 | This standardizes the help message across all subcommands. |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 149 | |
| 150 | Args: |
| 151 | parser: The parser to add the device argument to. |
| 152 | schemes: List of device schemes or single scheme to allow. |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 153 | """ |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 154 | help_strings = [] |
| 155 | schemes = list(cros_build_lib.iflatten_instance(schemes)) |
| 156 | if commandline.DEVICE_SCHEME_SSH in schemes: |
| 157 | help_strings.append('Target a device with [user@]hostname[:port].') |
| 158 | if commandline.DEVICE_SCHEME_USB in schemes: |
| 159 | help_strings.append('Target removable media with usb://[path].') |
| 160 | if commandline.DEVICE_SCHEME_FILE in schemes: |
| 161 | help_strings.append('Target a local file with file://path.') |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 162 | parser.add_argument('device', |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 163 | type=commandline.DeviceParser(schemes), |
| 164 | help=' '.join(help_strings)) |
| 165 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 166 | def Run(self): |
| 167 | """The command to run.""" |
| 168 | raise NotImplementedError() |