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 |
| 21 | from chromite.shell.subcmds import portage_cmds |
| 22 | from chromite.shell.subcmds import shell_cmd |
| 23 | from chromite.shell.subcmds import workon_cmd |
Simon Glass | 2159960 | 2011-02-11 13:57:29 -0800 | [diff] [blame] | 24 | from chromite.shell import chromite_env |
| 25 | from chromite.lib import operation |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 26 | |
| 27 | |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 28 | # Define command handlers and command strings. |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 29 | # |
| 30 | # ORDER MATTERS here when we show the menu. |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 31 | _COMMAND_HANDLERS = [ |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 32 | build_cmd.BuildCmd, |
| 33 | clean_cmd.CleanCmd, |
| 34 | portage_cmds.EbuildCmd, |
| 35 | portage_cmds.EmergeCmd, |
| 36 | portage_cmds.EqueryCmd, |
| 37 | portage_cmds.PortageqCmd, |
| 38 | shell_cmd.ShellCmd, |
| 39 | workon_cmd.WorkonCmd, |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 40 | ] |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 41 | _COMMAND_STRS = [cls.__name__[:-len('Cmd')].lower() |
| 42 | for cls in _COMMAND_HANDLERS] |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 43 | |
| 44 | |
| 45 | def _FindCommand(cmd_name): |
| 46 | """Find the command that matches the given command name. |
| 47 | |
| 48 | This tries to be smart. See the cmd_name parameter for details. |
| 49 | |
| 50 | Args: |
| 51 | cmd_name: Can be any of the following: |
| 52 | 1. The full name of a command. This is checked first so that if one |
| 53 | command name is a substring of another, you can still specify |
| 54 | the shorter spec name and know you won't get a menu (the exact |
| 55 | match prevents the menu). |
| 56 | 2. A _prefix_ that will be used to pare-down a menu of commands |
| 57 | Can be the empty string to show a menu of all commands |
| 58 | |
| 59 | Returns: |
| 60 | The command name. |
| 61 | """ |
| 62 | # Always make cmd_name lower. Commands are case-insensitive. |
| 63 | cmd_name = cmd_name.lower() |
| 64 | |
| 65 | # If we're an exact match, we're done! |
| 66 | if cmd_name in _COMMAND_STRS: |
| 67 | return cmd_name |
| 68 | |
| 69 | # Find ones that match and put them in a menu... |
| 70 | possible_cmds = [] |
| 71 | possible_choices = [] |
| 72 | for cmd_num, this_cmd in enumerate(_COMMAND_STRS): |
| 73 | if this_cmd.startswith(cmd_name): |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 74 | handler = _COMMAND_HANDLERS[cmd_num] |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 75 | assert hasattr(handler, '__doc__'), \ |
| 76 | ('All handlers must have docstrings: %s' % cmd_name) |
| 77 | desc = handler.__doc__.splitlines()[0] |
| 78 | |
| 79 | possible_cmds.append(this_cmd) |
| 80 | possible_choices.append('%s - %s' % (this_cmd, desc)) |
| 81 | |
| 82 | if not possible_choices: |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 83 | cros_lib.Die('No commands matched: "%s". ' |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 84 | 'Try running with no arguments for a menu.' % |
| 85 | cmd_name) |
| 86 | |
| 87 | if cmd_name and len(possible_choices) == 1: |
| 88 | # Avoid showing the user a menu if the user's search string matched exactly |
| 89 | # one item. |
| 90 | choice = 0 |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 91 | else: |
| 92 | choice = text_menu.TextMenu(possible_choices, 'Which chromite command', |
| 93 | menu_width=0) |
| 94 | |
| 95 | if choice is None: |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 96 | cros_lib.Die('OK, cancelling...') |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 97 | else: |
| 98 | return possible_cmds[choice] |
| 99 | |
| 100 | |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 101 | def main(): |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 102 | """Main function for the chromite shell.""" |
| 103 | |
| 104 | # Hack it so that argv[0] is 'chromite' so that it doesn't tell user to run |
| 105 | # 'main.py' in help commands... |
| 106 | # TODO(dianders): Remove this hack, since it is ugly. Shouldn't be needed |
| 107 | # once we change the way that the chromite wrapper works. |
| 108 | sys.argv[0] = 'chromite' |
| 109 | |
| 110 | # Support EnterChroot(). |
Simon Glass | 5329b93 | 2011-03-14 16:49:04 -0700 | [diff] [blame] | 111 | # This may raise a ChromiteError if the child dies, so we must handle this. |
| 112 | try: |
| 113 | did_resume = utils.ResumeEnterChrootIfNeeded(sys.argv) |
| 114 | if did_resume: |
| 115 | return |
| 116 | except chromite_env.ChromiteError: |
| 117 | # The error has been reported, but we must exit indicating failure |
| 118 | sys.exit(1) |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 119 | |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 120 | # TODO(dianders): Make help a little better. Specifically: |
| 121 | # 1. Add a command called 'help' |
| 122 | # 2. Make the help string below include command list and descriptions (like |
| 123 | # the menu, but without being interactive). |
| 124 | # 3. Make "help command" and "--help command" equivalent to "command --help". |
| 125 | help_str = ( |
Simon Glass | f4616ab | 2011-03-30 13:19:16 -0700 | [diff] [blame^] | 126 | """%(prog)s [chromite_options] [cmd [args]]\n""" |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 127 | """\n""" |
| 128 | """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^] | 129 | """build tasks. For a list of commands, run without any arguments.""" |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 130 | ) % {'prog': os.path.basename(sys.argv[0])} |
Simon Glass | f4616ab | 2011-03-30 13:19:16 -0700 | [diff] [blame^] | 131 | |
| 132 | parser = optparse.OptionParser() |
| 133 | |
| 134 | # Verbose defaults to full for now, just to keep people acclimatized to |
| 135 | # vast amounts of comforting output. |
| 136 | parser.add_option('-v', dest='verbose', default=3, |
| 137 | help='Control verbosity: 0=silent, 1=progress, 3=full') |
| 138 | parser.add_option('-q', action='store_const', dest='verbose', const=0, |
| 139 | help='Be quieter (sets verbosity to 1)') |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 140 | if not cros_lib.IsInsideChroot(): |
Simon Glass | f4616ab | 2011-03-30 13:19:16 -0700 | [diff] [blame^] | 141 | parser.add_option('--chroot', action='store', type='string', |
| 142 | dest='chroot_name', default='chroot', |
| 143 | help="Chroot spec to use. Can be an absolute path to a spec file " |
| 144 | "or a substring of a chroot spec name (without .spec suffix)") |
| 145 | parser.usage = help_str |
| 146 | try: |
| 147 | (options, args) = parser.parse_args() |
| 148 | except: |
| 149 | sys.exit(1) |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 150 | |
Simon Glass | f4616ab | 2011-03-30 13:19:16 -0700 | [diff] [blame^] | 151 | if False: # To preserve indent and minimize changes in this CL |
| 152 | pass |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 153 | else: |
Simon Glass | 5329b93 | 2011-03-14 16:49:04 -0700 | [diff] [blame] | 154 | # Set up the cros system. |
| 155 | cros_env = chromite_env.ChromiteEnv() |
| 156 | |
| 157 | # Configure the operation setup. |
| 158 | oper = cros_env.GetOperation() |
Simon Glass | f4616ab | 2011-03-30 13:19:16 -0700 | [diff] [blame^] | 159 | oper.verbose = options.verbose >= 3 |
| 160 | oper.progress = options.verbose >= 1 |
Simon Glass | 5329b93 | 2011-03-14 16:49:04 -0700 | [diff] [blame] | 161 | |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 162 | # Look for special "--chroot" argument to allow for alternate chroots |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 163 | if not cros_lib.IsInsideChroot(): |
Simon Glass | f4616ab | 2011-03-30 13:19:16 -0700 | [diff] [blame^] | 164 | chroot_spec_path = utils.FindSpec(options.chroot_name, |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 165 | spec_type=utils.CHROOT_SPEC_TYPE) |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 166 | |
Simon Glass | 5329b93 | 2011-03-14 16:49:04 -0700 | [diff] [blame] | 167 | oper.Info('Using chroot "%s"' % os.path.relpath(chroot_spec_path)) |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 168 | |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 169 | chroot_config = utils.ReadConfig(chroot_spec_path) |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 170 | else: |
| 171 | # Already in the chroot; no need to get config... |
| 172 | chroot_config = None |
| 173 | |
| 174 | # Get command and arguments |
Simon Glass | f4616ab | 2011-03-30 13:19:16 -0700 | [diff] [blame^] | 175 | if args: |
| 176 | cmd_str = args[0].lower() |
| 177 | args = args[1:] |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 178 | else: |
Doug Anderson | 4b6d396 | 2011-01-21 09:41:50 -0800 | [diff] [blame] | 179 | cmd_str = '' |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 180 | |
Doug Anderson | e91900d | 2011-01-26 11:37:17 -0800 | [diff] [blame] | 181 | # Validate the subcmd, popping a menu if needed. |
Doug Anderson | 4b6d396 | 2011-01-21 09:41:50 -0800 | [diff] [blame] | 182 | cmd_str = _FindCommand(cmd_str) |
Simon Glass | 5329b93 | 2011-03-14 16:49:04 -0700 | [diff] [blame] | 183 | oper.Info("Running command '%s'." % cmd_str) |
Simon Glass | 2159960 | 2011-02-11 13:57:29 -0800 | [diff] [blame] | 184 | |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 185 | # Finally, call the function w/ standard argv. |
Doug Anderson | a9b1090 | 2011-02-01 17:54:31 -0800 | [diff] [blame] | 186 | cmd_cls = _COMMAND_HANDLERS[_COMMAND_STRS.index(cmd_str)] |
Doug Anderson | 4b6d396 | 2011-01-21 09:41:50 -0800 | [diff] [blame] | 187 | cmd_obj = cmd_cls() |
Simon Glass | 2159960 | 2011-02-11 13:57:29 -0800 | [diff] [blame] | 188 | cmd_obj.SetChromiteEnv(cros_env) |
Simon Glass | 5329b93 | 2011-03-14 16:49:04 -0700 | [diff] [blame] | 189 | try: |
Simon Glass | f4616ab | 2011-03-30 13:19:16 -0700 | [diff] [blame^] | 190 | cmd_obj.Run([cmd_str] + args, chroot_config=chroot_config) |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 191 | |
Simon Glass | 5329b93 | 2011-03-14 16:49:04 -0700 | [diff] [blame] | 192 | # Handle an error in one of the scripts: print a message and exit. |
| 193 | except chromite_env.ChromiteError, msg: |
| 194 | sys.exit(1) |
Doug Anderson | f0c7395 | 2011-01-18 13:46:07 -0800 | [diff] [blame] | 195 | |
| 196 | if __name__ == '__main__': |
| 197 | main() |