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