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 | |
| 7 | CLI commands are the subcommands available to the user, such as "deploy" in |
| 8 | `cros deploy` or "shell" in `brillo shell`. |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 9 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 10 | This module contains two important definitions used by all commands: |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 11 | CliCommand: The parent class of all CLI commands. |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 12 | CommandDecorator: Decorator that must be used to ensure that the command shows |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 13 | up in |_commands| and is discoverable. |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 14 | |
| 15 | Commands can be either imported directly or looked up using this module's |
| 16 | ListCommands() function. |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 17 | """ |
| 18 | |
Mike Frysinger | 383367e | 2014-09-16 15:06:17 -0400 | [diff] [blame] | 19 | from __future__ import print_function |
| 20 | |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 21 | import glob |
| 22 | import os |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 23 | import sys |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 24 | |
Ralph Nathan | 74e864d | 2015-05-11 12:13:53 -0700 | [diff] [blame] | 25 | from chromite.cbuildbot import constants |
Bertrand SIMONNET | 2b1ed05 | 2015-03-02 11:17:40 -0800 | [diff] [blame] | 26 | from chromite.lib import brick_lib |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 27 | from chromite.lib import commandline |
| 28 | from chromite.lib import cros_build_lib |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 29 | from chromite.lib import cros_import |
Ralph Nathan | 6993125 | 2015-04-14 16:49:21 -0700 | [diff] [blame] | 30 | from chromite.lib import cros_logging as logging |
Ralph Nathan | 74e864d | 2015-05-11 12:13:53 -0700 | [diff] [blame] | 31 | from chromite.lib import osutils |
| 32 | from chromite.lib import workspace_lib |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 33 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 34 | |
| 35 | _commands = dict() |
| 36 | |
| 37 | |
Ralph Nathan | 74e864d | 2015-05-11 12:13:53 -0700 | [diff] [blame] | 38 | def SetupFileLogger(filename='brillo.log', log_level=logging.DEBUG): |
| 39 | """Store log messages to a file. |
| 40 | |
| 41 | In case of an error, this file can be made visible to the user. |
| 42 | """ |
| 43 | workspace_path = workspace_lib.WorkspacePath() |
| 44 | if workspace_path is None: |
| 45 | return |
Ralph Nathan | 7a0bae2 | 2015-05-13 10:49:54 -0700 | [diff] [blame^] | 46 | path = os.path.join(workspace_path, workspace_lib.WORKSPACE_LOGS_DIR, |
| 47 | filename) |
Ralph Nathan | 74e864d | 2015-05-11 12:13:53 -0700 | [diff] [blame] | 48 | osutils.Touch(path, makedirs=True) |
| 49 | logger = logging.getLogger() |
| 50 | fh = logging.FileHandler(path, mode='w') |
| 51 | fh.setLevel(log_level) |
| 52 | fh.setFormatter( |
| 53 | logging.Formatter(fmt=constants.LOGGER_FMT, |
| 54 | datefmt=constants.LOGGER_DATE_FMT)) |
| 55 | logger.addHandler(fh) |
| 56 | |
| 57 | |
Ralph Nathan | 6993125 | 2015-04-14 16:49:21 -0700 | [diff] [blame] | 58 | def UseProgressBar(): |
| 59 | """Determine whether the progress bar is to be used or not. |
| 60 | |
| 61 | We only want the progress bar to display for the brillo commands which operate |
| 62 | at logging level NOTICE. If the user wants to see the noisy output, then they |
| 63 | can execute the command at logging level INFO or DEBUG. |
| 64 | """ |
| 65 | return logging.getLogger().getEffectiveLevel() == logging.NOTICE |
| 66 | |
| 67 | |
Ralph Nathan | 1fc77f2 | 2015-04-21 15:05:48 -0700 | [diff] [blame] | 68 | def GetToolset(): |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 69 | """Return the CLI toolset invoked by the user. |
| 70 | |
| 71 | For example, if the user is executing `cros flash`, this will return 'cros'. |
| 72 | |
| 73 | This won't work for unittests so if a certain toolset must be loaded for |
| 74 | a unittest this should be mocked out to return the desired string. |
| 75 | """ |
| 76 | return os.path.basename(sys.argv[0]) |
| 77 | |
| 78 | |
| 79 | def _FindModules(subdir_path, toolset): |
| 80 | """Returns a list of all the relevant python modules in |subdir_path|. |
| 81 | |
| 82 | The modules returned are based on |toolset|, so if |toolset| is 'cros' |
| 83 | then cros_xxx.py modules will be found. |
| 84 | |
| 85 | Args: |
| 86 | subdir_path: directory (string) to search for modules in. |
| 87 | toolset: toolset (string) to find. |
| 88 | |
| 89 | Returns: |
| 90 | List of filenames (strings). |
| 91 | """ |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 92 | modules = [] |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 93 | for file_path in glob.glob(os.path.join(subdir_path, toolset + '_*.py')): |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 94 | if not file_path.endswith('_unittest.py'): |
| 95 | modules.append(file_path) |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 96 | return modules |
| 97 | |
| 98 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 99 | def _ImportCommands(toolset): |
| 100 | """Directly imports all |toolset| python modules. |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 101 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 102 | This method imports the |toolset|_[!unittest] modules which may contain |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 103 | commands. When these modules are loaded, declared commands (those that use |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 104 | CommandDecorator) will automatically get added to |_commands|. |
| 105 | |
| 106 | Args: |
| 107 | toolset: toolset (string) to import. |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 108 | """ |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 109 | subdir_path = os.path.join(os.path.dirname(__file__), toolset) |
| 110 | for file_path in _FindModules(subdir_path, toolset): |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 111 | file_name = os.path.basename(file_path) |
| 112 | mod_name = os.path.splitext(file_name)[0] |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 113 | cros_import.ImportModule(('chromite', 'cli', toolset, mod_name)) |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 114 | |
| 115 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 116 | def ListCommands(toolset=None): |
| 117 | """Return a dictionary mapping command names to classes. |
| 118 | |
| 119 | Args: |
| 120 | toolset: toolset (string) to list, None to determine from the commandline. |
| 121 | |
| 122 | Returns: |
| 123 | A dictionary mapping names (strings) to commands (classes). |
| 124 | """ |
Ralph Nathan | 1fc77f2 | 2015-04-21 15:05:48 -0700 | [diff] [blame] | 125 | _ImportCommands(toolset or GetToolset()) |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 126 | return _commands.copy() |
| 127 | |
| 128 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 129 | class InvalidCommandError(Exception): |
| 130 | """Error that occurs when command class fails sanity checks.""" |
| 131 | pass |
| 132 | |
| 133 | |
| 134 | def CommandDecorator(command_name): |
| 135 | """Decorator that sanity checks and adds class to list of usable commands.""" |
| 136 | |
| 137 | def InnerCommandDecorator(original_class): |
| 138 | """"Inner Decorator that actually wraps the class.""" |
| 139 | if not hasattr(original_class, '__doc__'): |
| 140 | raise InvalidCommandError('All handlers must have docstrings: %s' % |
| 141 | original_class) |
| 142 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 143 | if not issubclass(original_class, CliCommand): |
| 144 | raise InvalidCommandError('All Commands must derive from CliCommand: %s' % |
| 145 | original_class) |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 146 | |
| 147 | _commands[command_name] = original_class |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 148 | original_class.command_name = command_name |
| 149 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 150 | return original_class |
| 151 | |
| 152 | return InnerCommandDecorator |
| 153 | |
| 154 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 155 | class CliCommand(object): |
| 156 | """All CLI commands must derive from this class. |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 157 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 158 | This class provides the abstract interface for all CLI commands. When |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 159 | designing a new command, you must sub-class from this class and use the |
| 160 | CommandDecorator decorator. You must specify a class docstring as that will be |
| 161 | used as the usage for the sub-command. |
| 162 | |
| 163 | In addition your command should implement AddParser which is passed in a |
| 164 | parser that you can add your own custom arguments. See argparse for more |
| 165 | information. |
| 166 | """ |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 167 | # Indicates whether command stats should be uploaded for this command. |
| 168 | # Override to enable command stats uploading. |
| 169 | upload_stats = False |
| 170 | # We set the default timeout to 1 second, to prevent overly long waits for |
| 171 | # commands to complete. From manual tests, stat uploads usually take |
| 172 | # between 0.35s-0.45s in MTV. |
| 173 | upload_stats_timeout = 1 |
| 174 | |
Ryo Hashimoto | 8bc997b | 2014-01-22 18:46:17 +0900 | [diff] [blame] | 175 | # Indicates whether command uses cache related commandline options. |
| 176 | use_caching_options = False |
| 177 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 178 | def __init__(self, options): |
| 179 | self.options = options |
Bertrand SIMONNET | 2b1ed05 | 2015-03-02 11:17:40 -0800 | [diff] [blame] | 180 | brick = brick_lib.FindBrickInPath() |
Bertrand SIMONNET | 79e077d | 2015-03-12 18:31:12 -0700 | [diff] [blame] | 181 | self.curr_brick_locator = brick.brick_locator if brick else None |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 182 | |
| 183 | @classmethod |
| 184 | def AddParser(cls, parser): |
| 185 | """Add arguments for this command to the parser.""" |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 186 | parser.set_defaults(command_class=cls) |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 187 | |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 188 | @classmethod |
| 189 | def AddDeviceArgument(cls, parser, schemes=commandline.DEVICE_SCHEME_SSH, |
| 190 | optional=None): |
| 191 | """Add a device argument to the parser. |
| 192 | |
| 193 | This has a few advantages over adding a device argument directly: |
| 194 | - Standardizes the device --help message for all tools. |
| 195 | - May allow `brillo` and `cros` to use the same source. |
| 196 | |
| 197 | The device argument is normally positional in cros but optional in |
| 198 | brillo. If that is the only difference between a cros and brillo |
| 199 | tool, this function allows the same source be shared for both. |
| 200 | |
| 201 | Args: |
| 202 | parser: The parser to add the device argument to. |
| 203 | schemes: List of device schemes or single scheme to allow. |
| 204 | optional: Whether the device is an optional or positional |
| 205 | argument; None to auto-determine based on toolset. |
| 206 | """ |
| 207 | if optional is None: |
Ralph Nathan | 1fc77f2 | 2015-04-21 15:05:48 -0700 | [diff] [blame] | 208 | optional = (GetToolset() == 'brillo') |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 209 | help_strings = [] |
| 210 | schemes = list(cros_build_lib.iflatten_instance(schemes)) |
| 211 | if commandline.DEVICE_SCHEME_SSH in schemes: |
| 212 | help_strings.append('Target a device with [user@]hostname[:port].') |
| 213 | if commandline.DEVICE_SCHEME_USB in schemes: |
| 214 | help_strings.append('Target removable media with usb://[path].') |
| 215 | if commandline.DEVICE_SCHEME_FILE in schemes: |
| 216 | help_strings.append('Target a local file with file://path.') |
| 217 | parser.add_argument('--device' if optional else 'device', |
| 218 | type=commandline.DeviceParser(schemes), |
| 219 | help=' '.join(help_strings)) |
| 220 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 221 | def Run(self): |
| 222 | """The command to run.""" |
| 223 | raise NotImplementedError() |