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 | |
Bertrand SIMONNET | 2b1ed05 | 2015-03-02 11:17:40 -0800 | [diff] [blame] | 25 | from chromite.lib import brick_lib |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 26 | from chromite.lib import cros_import |
| 27 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 28 | |
| 29 | _commands = dict() |
| 30 | |
| 31 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 32 | def _GetToolset(): |
| 33 | """Return the CLI toolset invoked by the user. |
| 34 | |
| 35 | For example, if the user is executing `cros flash`, this will return 'cros'. |
| 36 | |
| 37 | This won't work for unittests so if a certain toolset must be loaded for |
| 38 | a unittest this should be mocked out to return the desired string. |
| 39 | """ |
| 40 | return os.path.basename(sys.argv[0]) |
| 41 | |
| 42 | |
| 43 | def _FindModules(subdir_path, toolset): |
| 44 | """Returns a list of all the relevant python modules in |subdir_path|. |
| 45 | |
| 46 | The modules returned are based on |toolset|, so if |toolset| is 'cros' |
| 47 | then cros_xxx.py modules will be found. |
| 48 | |
| 49 | Args: |
| 50 | subdir_path: directory (string) to search for modules in. |
| 51 | toolset: toolset (string) to find. |
| 52 | |
| 53 | Returns: |
| 54 | List of filenames (strings). |
| 55 | """ |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 56 | modules = [] |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 57 | 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] | 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 | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 63 | def _ImportCommands(toolset): |
| 64 | """Directly imports all |toolset| python modules. |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 65 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 66 | This method imports the |toolset|_[!unittest] modules which may contain |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 67 | commands. When 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|. |
| 69 | |
| 70 | Args: |
| 71 | toolset: toolset (string) to import. |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 72 | """ |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 73 | subdir_path = os.path.join(os.path.dirname(__file__), toolset) |
| 74 | for file_path in _FindModules(subdir_path, toolset): |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 75 | file_name = os.path.basename(file_path) |
| 76 | mod_name = os.path.splitext(file_name)[0] |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 77 | cros_import.ImportModule(('chromite', 'cli', toolset, mod_name)) |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 78 | |
| 79 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 80 | def ListCommands(toolset=None): |
| 81 | """Return a dictionary mapping command names to classes. |
| 82 | |
| 83 | Args: |
| 84 | toolset: toolset (string) to list, None to determine from the commandline. |
| 85 | |
| 86 | Returns: |
| 87 | A dictionary mapping names (strings) to commands (classes). |
| 88 | """ |
| 89 | _ImportCommands(toolset or _GetToolset()) |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 90 | return _commands.copy() |
| 91 | |
| 92 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 93 | class InvalidCommandError(Exception): |
| 94 | """Error that occurs when command class fails sanity checks.""" |
| 95 | pass |
| 96 | |
| 97 | |
| 98 | def CommandDecorator(command_name): |
| 99 | """Decorator that sanity checks and adds class to list of usable commands.""" |
| 100 | |
| 101 | def InnerCommandDecorator(original_class): |
| 102 | """"Inner Decorator that actually wraps the class.""" |
| 103 | if not hasattr(original_class, '__doc__'): |
| 104 | raise InvalidCommandError('All handlers must have docstrings: %s' % |
| 105 | original_class) |
| 106 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 107 | if not issubclass(original_class, CliCommand): |
| 108 | raise InvalidCommandError('All Commands must derive from CliCommand: %s' % |
| 109 | original_class) |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 110 | |
| 111 | _commands[command_name] = original_class |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 112 | original_class.command_name = command_name |
| 113 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 114 | return original_class |
| 115 | |
| 116 | return InnerCommandDecorator |
| 117 | |
| 118 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 119 | class CliCommand(object): |
| 120 | """All CLI commands must derive from this class. |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 121 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 122 | This class provides the abstract interface for all CLI commands. When |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 123 | designing a new command, you must sub-class from this class and use the |
| 124 | CommandDecorator decorator. You must specify a class docstring as that will be |
| 125 | used as the usage for the sub-command. |
| 126 | |
| 127 | In addition your command should implement AddParser which is passed in a |
| 128 | parser that you can add your own custom arguments. See argparse for more |
| 129 | information. |
| 130 | """ |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 131 | # Indicates whether command stats should be uploaded for this command. |
| 132 | # Override to enable command stats uploading. |
| 133 | upload_stats = False |
| 134 | # We set the default timeout to 1 second, to prevent overly long waits for |
| 135 | # commands to complete. From manual tests, stat uploads usually take |
| 136 | # between 0.35s-0.45s in MTV. |
| 137 | upload_stats_timeout = 1 |
| 138 | |
Ryo Hashimoto | 8bc997b | 2014-01-22 18:46:17 +0900 | [diff] [blame] | 139 | # Indicates whether command uses cache related commandline options. |
| 140 | use_caching_options = False |
| 141 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 142 | def __init__(self, options): |
| 143 | self.options = options |
Bertrand SIMONNET | 2b1ed05 | 2015-03-02 11:17:40 -0800 | [diff] [blame] | 144 | brick = brick_lib.FindBrickInPath() |
Bertrand SIMONNET | 79e077d | 2015-03-12 18:31:12 -0700 | [diff] [blame] | 145 | self.curr_brick_locator = brick.brick_locator if brick else None |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 146 | |
| 147 | @classmethod |
| 148 | def AddParser(cls, parser): |
| 149 | """Add arguments for this command to the parser.""" |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 150 | parser.set_defaults(command_class=cls) |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 151 | |
| 152 | def Run(self): |
| 153 | """The command to run.""" |
| 154 | raise NotImplementedError() |