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 | |
Alex Klein | 665aa07 | 2019-12-12 15:13:53 -0700 | [diff] [blame] | 16 | import importlib |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 17 | import os |
| 18 | |
Aviv Keshet | b7519e1 | 2016-10-04 00:50:00 -0700 | [diff] [blame] | 19 | from chromite.lib import constants |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 20 | from chromite.lib import commandline |
| 21 | from chromite.lib import cros_build_lib |
Ralph Nathan | 6993125 | 2015-04-14 16:49:21 -0700 | [diff] [blame] | 22 | from chromite.lib import cros_logging as logging |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 23 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 24 | |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 25 | # Paths for finding and importing subcommand modules. |
| 26 | _SUBCOMMAND_MODULE_DIRECTORY = os.path.join(os.path.dirname(__file__), 'cros') |
| 27 | _SUBCOMMAND_MODULE_PREFIX = 'cros_' |
| 28 | |
| 29 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 30 | _commands = dict() |
| 31 | |
| 32 | |
Ralph Nathan | 6993125 | 2015-04-14 16:49:21 -0700 | [diff] [blame] | 33 | def UseProgressBar(): |
| 34 | """Determine whether the progress bar is to be used or not. |
| 35 | |
| 36 | We only want the progress bar to display for the brillo commands which operate |
| 37 | at logging level NOTICE. If the user wants to see the noisy output, then they |
| 38 | can execute the command at logging level INFO or DEBUG. |
| 39 | """ |
| 40 | return logging.getLogger().getEffectiveLevel() == logging.NOTICE |
| 41 | |
| 42 | |
Mike Frysinger | 1a47081 | 2019-11-07 01:19:17 -0500 | [diff] [blame] | 43 | def ImportCommand(name): |
| 44 | """Directly import the specified subcommand. |
| 45 | |
| 46 | This method imports the module which must contain the single subcommand. When |
| 47 | the module is loaded, the declared command (those that use CommandDecorator) |
| 48 | will automatically get added to |_commands|. |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 49 | |
| 50 | Args: |
Mike Frysinger | 1a47081 | 2019-11-07 01:19:17 -0500 | [diff] [blame] | 51 | name: The subcommand to load. |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 52 | |
| 53 | Returns: |
Mike Frysinger | 1a47081 | 2019-11-07 01:19:17 -0500 | [diff] [blame] | 54 | A reference to the subcommand class. |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 55 | """ |
Mike Frysinger | 1a47081 | 2019-11-07 01:19:17 -0500 | [diff] [blame] | 56 | module_path = os.path.join(_SUBCOMMAND_MODULE_DIRECTORY, |
| 57 | 'cros_%s' % (name.replace('-', '_'),)) |
| 58 | import_path = os.path.relpath(os.path.realpath(module_path), |
| 59 | os.path.dirname(constants.CHROMITE_DIR)) |
Alex Klein | 665aa07 | 2019-12-12 15:13:53 -0700 | [diff] [blame] | 60 | module_parts = import_path.split(os.path.sep) |
| 61 | importlib.import_module('.'.join(module_parts)) |
Mike Frysinger | 1a47081 | 2019-11-07 01:19:17 -0500 | [diff] [blame] | 62 | return _commands[name] |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 63 | |
| 64 | |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 65 | def ListCommands(): |
Mike Frysinger | 1a47081 | 2019-11-07 01:19:17 -0500 | [diff] [blame] | 66 | """Return the set of available subcommands. |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 67 | |
Mike Frysinger | 1a47081 | 2019-11-07 01:19:17 -0500 | [diff] [blame] | 68 | We assume that there is a direct one-to-one relationship between the module |
| 69 | name on disk and the command that module implements. We assume this as a |
| 70 | performance requirement (to avoid importing every subcommand every time even |
| 71 | though we'd only ever run a single one), and to avoid 3rd party module usage |
| 72 | in one subcommand breaking all other subcommands (not a great solution). |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 73 | """ |
Mike Frysinger | 1a47081 | 2019-11-07 01:19:17 -0500 | [diff] [blame] | 74 | # Filenames use underscores due to python naming limitations, but subcommands |
| 75 | # use dashes as they're easier for humans to type. |
| 76 | # Strip off the leading "cros_" and the trailing ".py". |
| 77 | return set(x[5:-3].replace('_', '-') |
| 78 | for x in os.listdir(_SUBCOMMAND_MODULE_DIRECTORY) |
| 79 | if (x.startswith(_SUBCOMMAND_MODULE_PREFIX) and x.endswith('.py') |
| 80 | and not x.endswith('_unittest.py'))) |
David Pursell | f1c27c1 | 2015-03-18 09:51:38 -0700 | [diff] [blame] | 81 | |
| 82 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 83 | class InvalidCommandError(Exception): |
| 84 | """Error that occurs when command class fails sanity checks.""" |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 85 | |
| 86 | |
| 87 | def CommandDecorator(command_name): |
| 88 | """Decorator that sanity checks and adds class to list of usable commands.""" |
| 89 | |
| 90 | def InnerCommandDecorator(original_class): |
Mike Frysinger | 07475fa | 2019-08-01 14:44:55 -0400 | [diff] [blame] | 91 | """Inner Decorator that actually wraps the class.""" |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 92 | if not hasattr(original_class, '__doc__'): |
| 93 | raise InvalidCommandError('All handlers must have docstrings: %s' % |
| 94 | original_class) |
| 95 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 96 | if not issubclass(original_class, CliCommand): |
| 97 | raise InvalidCommandError('All Commands must derive from CliCommand: %s' % |
| 98 | original_class) |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 99 | |
| 100 | _commands[command_name] = original_class |
Ryan Cui | 47f80e4 | 2013-04-01 19:01:54 -0700 | [diff] [blame] | 101 | original_class.command_name = command_name |
| 102 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 103 | return original_class |
| 104 | |
| 105 | return InnerCommandDecorator |
| 106 | |
| 107 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 108 | class CliCommand(object): |
| 109 | """All CLI commands must derive from this class. |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 110 | |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 111 | This class provides the abstract interface for all CLI commands. When |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 112 | designing a new command, you must sub-class from this class and use the |
| 113 | CommandDecorator decorator. You must specify a class docstring as that will be |
| 114 | used as the usage for the sub-command. |
| 115 | |
| 116 | In addition your command should implement AddParser which is passed in a |
| 117 | parser that you can add your own custom arguments. See argparse for more |
| 118 | information. |
| 119 | """ |
Ryo Hashimoto | 8bc997b | 2014-01-22 18:46:17 +0900 | [diff] [blame] | 120 | # Indicates whether command uses cache related commandline options. |
| 121 | use_caching_options = False |
| 122 | |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 123 | def __init__(self, options): |
| 124 | self.options = options |
| 125 | |
| 126 | @classmethod |
| 127 | def AddParser(cls, parser): |
| 128 | """Add arguments for this command to the parser.""" |
David Pursell | ffb9004 | 2015-03-23 09:21:41 -0700 | [diff] [blame] | 129 | parser.set_defaults(command_class=cls) |
Brian Harring | 984988f | 2012-10-10 22:53:30 -0700 | [diff] [blame] | 130 | |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 131 | @classmethod |
Alex Klein | df03034 | 2020-02-14 10:07:38 -0700 | [diff] [blame] | 132 | def AddDeviceArgument(cls, parser, schemes=commandline.DEVICE_SCHEME_SSH, |
| 133 | positional=False): |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 134 | """Add a device argument to the parser. |
| 135 | |
David Pursell | c7ba784 | 2015-07-08 10:48:41 -0700 | [diff] [blame] | 136 | This standardizes the help message across all subcommands. |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 137 | |
| 138 | Args: |
| 139 | parser: The parser to add the device argument to. |
| 140 | schemes: List of device schemes or single scheme to allow. |
Alex Klein | df03034 | 2020-02-14 10:07:38 -0700 | [diff] [blame] | 141 | positional: Whether it should be a positional or named argument. |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 142 | """ |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 143 | help_strings = [] |
| 144 | schemes = list(cros_build_lib.iflatten_instance(schemes)) |
| 145 | if commandline.DEVICE_SCHEME_SSH in schemes: |
Mike Frysinger | 7163c3a | 2018-02-08 16:45:10 -0500 | [diff] [blame] | 146 | help_strings.append('Target a device with [user@]hostname[:port]. ' |
| 147 | 'IPv4/IPv6 addresses are allowed, but IPv6 must ' |
| 148 | 'use brackets (e.g. [::1]).') |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 149 | if commandline.DEVICE_SCHEME_USB in schemes: |
| 150 | help_strings.append('Target removable media with usb://[path].') |
Alex Klein | e83d86c | 2019-12-11 15:17:31 -0700 | [diff] [blame] | 151 | if commandline.DEVICE_SCHEME_SERVO in schemes: |
| 152 | help_strings.append('Target a servo by port or serial number with ' |
| 153 | 'servo:port[:port] or servo:serial:serial-number. ' |
| 154 | 'e.g. servo:port:1234 or servo:serial:C1230024192.') |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 155 | if commandline.DEVICE_SCHEME_FILE in schemes: |
| 156 | help_strings.append('Target a local file with file://path.') |
Alex Klein | df03034 | 2020-02-14 10:07:38 -0700 | [diff] [blame] | 157 | if positional: |
| 158 | parser.add_argument('device', |
| 159 | type=commandline.DeviceParser(schemes), |
| 160 | help=' '.join(help_strings)) |
| 161 | else: |
| 162 | parser.add_argument('-d', '--device', |
| 163 | type=commandline.DeviceParser(schemes), |
| 164 | help=' '.join(help_strings)) |
David Pursell | f80b8c5 | 2015-04-03 12:46:45 -0700 | [diff] [blame] | 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() |