Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
Doug Anderson | e91900d | 2011-01-26 11:37:17 -0800 | [diff] [blame] | 2 | # |
| 3 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
Doug Anderson | e91900d | 2011-01-26 11:37:17 -0800 | [diff] [blame] | 7 | """Main file for the chromite shell.""" |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 8 | |
| 9 | # Python imports |
Simon Glass | f4616ab | 2011-03-30 13:19:16 -0700 | [diff] [blame] | 10 | import optparse |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 11 | import os |
| 12 | import sys |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 13 | |
| 14 | |
Doug Anderson | 4b6d396 | 2011-01-21 09:41:50 -0800 | [diff] [blame] | 15 | # Local imports |
Doug Anderson | e91900d | 2011-01-26 11:37:17 -0800 | [diff] [blame] | 16 | from chromite.lib import text_menu |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 17 | import chromite.lib.cros_build_lib as cros_lib |
| 18 | from chromite.shell import utils |
| 19 | from chromite.shell.subcmds import build_cmd |
| 20 | from chromite.shell.subcmds import clean_cmd |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 21 | from chromite.shell.subcmds import shell_cmd |
| 22 | from chromite.shell.subcmds import workon_cmd |
Simon Glass | 2159960 | 2011-02-11 13:57:29 -0800 | [diff] [blame] | 23 | from chromite.shell import chromite_env |
| 24 | from chromite.lib import operation |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 25 | |
| 26 | |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 27 | # Define command handlers and command strings. |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 28 | # |
| 29 | # ORDER MATTERS here when we show the menu. |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 30 | _COMMAND_HANDLERS = [ |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 31 | build_cmd.BuildCmd, |
| 32 | clean_cmd.CleanCmd, |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 33 | shell_cmd.ShellCmd, |
| 34 | workon_cmd.WorkonCmd, |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 35 | ] |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 36 | _COMMAND_STRS = [cls.__name__[:-len('Cmd')].lower() |
| 37 | for cls in _COMMAND_HANDLERS] |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 38 | |
| 39 | |
| 40 | def _FindCommand(cmd_name): |
| 41 | """Find the command that matches the given command name. |
| 42 | |
| 43 | This tries to be smart. See the cmd_name parameter for details. |
| 44 | |
| 45 | Args: |
| 46 | cmd_name: Can be any of the following: |
| 47 | 1. The full name of a command. This is checked first so that if one |
| 48 | command name is a substring of another, you can still specify |
| 49 | the shorter spec name and know you won't get a menu (the exact |
| 50 | match prevents the menu). |
| 51 | 2. A _prefix_ that will be used to pare-down a menu of commands |
| 52 | Can be the empty string to show a menu of all commands |
| 53 | |
| 54 | Returns: |
| 55 | The command name. |
| 56 | """ |
| 57 | # Always make cmd_name lower. Commands are case-insensitive. |
| 58 | cmd_name = cmd_name.lower() |
| 59 | |
| 60 | # If we're an exact match, we're done! |
| 61 | if cmd_name in _COMMAND_STRS: |
| 62 | return cmd_name |
| 63 | |
| 64 | # Find ones that match and put them in a menu... |
| 65 | possible_cmds = [] |
| 66 | possible_choices = [] |
| 67 | for cmd_num, this_cmd in enumerate(_COMMAND_STRS): |
| 68 | if this_cmd.startswith(cmd_name): |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 69 | handler = _COMMAND_HANDLERS[cmd_num] |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 70 | assert hasattr(handler, '__doc__'), \ |
| 71 | ('All handlers must have docstrings: %s' % cmd_name) |
| 72 | desc = handler.__doc__.splitlines()[0] |
| 73 | |
| 74 | possible_cmds.append(this_cmd) |
| 75 | possible_choices.append('%s - %s' % (this_cmd, desc)) |
| 76 | |
| 77 | if not possible_choices: |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 78 | cros_lib.Die('No commands matched: "%s". ' |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 79 | 'Try running with no arguments for a menu.' % |
| 80 | cmd_name) |
| 81 | |
| 82 | if cmd_name and len(possible_choices) == 1: |
| 83 | # Avoid showing the user a menu if the user's search string matched exactly |
| 84 | # one item. |
| 85 | choice = 0 |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 86 | else: |
| 87 | choice = text_menu.TextMenu(possible_choices, 'Which chromite command', |
| 88 | menu_width=0) |
| 89 | |
| 90 | if choice is None: |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 91 | cros_lib.Die('OK, cancelling...') |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 92 | else: |
| 93 | return possible_cmds[choice] |
| 94 | |
| 95 | |
Simon Glass | f5298ef | 2011-04-08 14:00:45 -0700 | [diff] [blame] | 96 | def _ParseArguments(parser, argv): |
| 97 | '''Helper function to separate arguments for a main program and sub-command. |
| 98 | |
| 99 | We split arguments into ones we understand, and ones to pass on to |
| 100 | sub-commands. For the former, we put them through the given optparse and |
| 101 | return the result options and sub-command name. For the latter, we just |
| 102 | return a list of options and arguments not intended for us. |
| 103 | |
| 104 | We want to parse only the options that we understand at the top level of |
| 105 | Chromite. Our heuristic here is that anything after the first positional |
| 106 | parameter (which we assume is the command) is ignored at this level, and |
| 107 | is passed down to the command level to handle. |
| 108 | |
| 109 | TODO(sjg): Revisit this to include tolerant option parser instead |
| 110 | http://codereview.chromium.org/6469035/ |
| 111 | |
| 112 | Args: |
| 113 | parser: Option parser. |
| 114 | argv: List of program arguments |
| 115 | |
| 116 | Returns: |
| 117 | options: Top level options (returned from optparse). |
| 118 | cmd_str: Subcommand to run |
| 119 | cmd_args: Arguments intended for subcommands. |
| 120 | ''' |
| 121 | our_args = [] |
| 122 | cmd_args = list(argv) |
| 123 | cmd_str = '' |
| 124 | args = [] # Nothing until we call optparse |
| 125 | while not cmd_str: |
| 126 | if our_args: |
| 127 | (options, args) = parser.parse_args(our_args) |
| 128 | if len(args) > 1: |
| 129 | cmd_str = args[1].lower() |
| 130 | elif cmd_args: |
| 131 | # We don't have a command yet. Transfer a positional arg from from |
| 132 | # cmd_args to our_args to see if that does the trick. We move over any |
| 133 | # options we find also. |
| 134 | while cmd_args: |
| 135 | arg = cmd_args.pop(0) |
| 136 | our_args.append(arg) |
| 137 | if not arg.startswith( '-'): |
| 138 | break |
| 139 | else: |
| 140 | # No more cmd_args to consume. |
| 141 | break |
| 142 | |
| 143 | # We must run the parser, even if it dies due to lack of arguments |
| 144 | if not args: |
| 145 | (options, args) = parser.parse_args(our_args) |
| 146 | return options, cmd_str, cmd_args |
| 147 | |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 148 | def main(): |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 149 | """Main function for the chromite shell.""" |
| 150 | |
| 151 | # Hack it so that argv[0] is 'chromite' so that it doesn't tell user to run |
| 152 | # 'main.py' in help commands... |
| 153 | # TODO(dianders): Remove this hack, since it is ugly. Shouldn't be needed |
| 154 | # once we change the way that the chromite wrapper works. |
| 155 | sys.argv[0] = 'chromite' |
| 156 | |
| 157 | # Support EnterChroot(). |
Simon Glass | 5329b93 | 2011-03-14 16:49:04 -0700 | [diff] [blame] | 158 | # This may raise a ChromiteError if the child dies, so we must handle this. |
| 159 | try: |
| 160 | did_resume = utils.ResumeEnterChrootIfNeeded(sys.argv) |
| 161 | if did_resume: |
| 162 | return |
| 163 | except chromite_env.ChromiteError: |
| 164 | # The error has been reported, but we must exit indicating failure |
| 165 | sys.exit(1) |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 166 | |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 167 | # TODO(dianders): Make help a little better. Specifically: |
| 168 | # 1. Add a command called 'help' |
| 169 | # 2. Make the help string below include command list and descriptions (like |
| 170 | # the menu, but without being interactive). |
| 171 | # 3. Make "help command" and "--help command" equivalent to "command --help". |
| 172 | help_str = ( |
Simon Glass | f4616ab | 2011-03-30 13:19:16 -0700 | [diff] [blame] | 173 | """%(prog)s [chromite_options] [cmd [args]]\n""" |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 174 | """\n""" |
| 175 | """The chromite script is a wrapper to make it easy to do various\n""" |
Simon Glass | f4616ab | 2011-03-30 13:19:16 -0700 | [diff] [blame] | 176 | """build tasks. For a list of commands, run without any arguments.""" |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 177 | ) % {'prog': os.path.basename(sys.argv[0])} |
Simon Glass | f4616ab | 2011-03-30 13:19:16 -0700 | [diff] [blame] | 178 | |
| 179 | parser = optparse.OptionParser() |
| 180 | |
| 181 | # Verbose defaults to full for now, just to keep people acclimatized to |
| 182 | # vast amounts of comforting output. |
Simon Glass | f5298ef | 2011-04-08 14:00:45 -0700 | [diff] [blame] | 183 | parser.add_option('-v', dest='verbose', default=3, type='int', |
Simon Glass | f4616ab | 2011-03-30 13:19:16 -0700 | [diff] [blame] | 184 | help='Control verbosity: 0=silent, 1=progress, 3=full') |
| 185 | parser.add_option('-q', action='store_const', dest='verbose', const=0, |
| 186 | help='Be quieter (sets verbosity to 1)') |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 187 | if not cros_lib.IsInsideChroot(): |
Simon Glass | f4616ab | 2011-03-30 13:19:16 -0700 | [diff] [blame] | 188 | parser.add_option('--chroot', action='store', type='string', |
| 189 | dest='chroot_name', default='chroot', |
| 190 | help="Chroot spec to use. Can be an absolute path to a spec file " |
| 191 | "or a substring of a chroot spec name (without .spec suffix)") |
| 192 | parser.usage = help_str |
Simon Glass | f5298ef | 2011-04-08 14:00:45 -0700 | [diff] [blame] | 193 | |
| 194 | # Parse the arguments and separate them into top-level and subcmd arguments. |
| 195 | options, cmd_str, cmd_args = _ParseArguments(parser, sys.argv) |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 196 | |
Simon Glass | eea2a2b | 2011-03-30 13:36:15 -0700 | [diff] [blame] | 197 | # Set up the cros system. |
| 198 | cros_env = chromite_env.ChromiteEnv() |
| 199 | |
| 200 | # Configure the operation setup. |
| 201 | oper = cros_env.GetOperation() |
| 202 | oper.verbose = options.verbose >= 3 |
| 203 | oper.progress = options.verbose >= 1 |
| 204 | |
| 205 | # Look for special "--chroot" argument to allow for alternate chroots |
| 206 | if not cros_lib.IsInsideChroot(): |
| 207 | chroot_spec_path = utils.FindSpec(options.chroot_name, |
| 208 | spec_type=utils.CHROOT_SPEC_TYPE) |
| 209 | |
| 210 | oper.Info('Using chroot "%s"' % os.path.relpath(chroot_spec_path)) |
| 211 | |
| 212 | chroot_config = utils.ReadConfig(chroot_spec_path) |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 213 | else: |
Simon Glass | eea2a2b | 2011-03-30 13:36:15 -0700 | [diff] [blame] | 214 | # Already in the chroot; no need to get config... |
| 215 | chroot_config = None |
Simon Glass | 5329b93 | 2011-03-14 16:49:04 -0700 | [diff] [blame] | 216 | |
Simon Glass | eea2a2b | 2011-03-30 13:36:15 -0700 | [diff] [blame] | 217 | # Validate the subcmd, popping a menu if needed. |
| 218 | cmd_str = _FindCommand(cmd_str) |
| 219 | oper.Info("Running command '%s'." % cmd_str) |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 220 | |
Simon Glass | eea2a2b | 2011-03-30 13:36:15 -0700 | [diff] [blame] | 221 | # Finally, call the function w/ standard argv. |
| 222 | cmd_cls = _COMMAND_HANDLERS[_COMMAND_STRS.index(cmd_str)] |
| 223 | cmd_obj = cmd_cls() |
| 224 | cmd_obj.SetChromiteEnv(cros_env) |
| 225 | try: |
Simon Glass | f5298ef | 2011-04-08 14:00:45 -0700 | [diff] [blame] | 226 | cmd_obj.Run([cmd_str] + cmd_args, chroot_config=chroot_config) |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 227 | |
Simon Glass | eea2a2b | 2011-03-30 13:36:15 -0700 | [diff] [blame] | 228 | # Handle an error in one of the scripts: print a message and exit. |
| 229 | except chromite_env.ChromiteError, msg: |
| 230 | sys.exit(1) |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 231 | |
| 232 | if __name__ == '__main__': |
| 233 | main() |