| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # | 2 # |
| 3 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | 3 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 | 6 |
| 7 """Main file for the chromite shell.""" | 7 """Main file for the chromite shell.""" |
| 8 | 8 |
| 9 # Python imports | 9 # Python imports |
| 10 import os | 10 import os |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 | 80 |
| 81 if not possible_choices: | 81 if not possible_choices: |
| 82 cros_lib.Die('No commands matched: "%s". ' | 82 cros_lib.Die('No commands matched: "%s". ' |
| 83 'Try running with no arguments for a menu.' % | 83 'Try running with no arguments for a menu.' % |
| 84 cmd_name) | 84 cmd_name) |
| 85 | 85 |
| 86 if cmd_name and len(possible_choices) == 1: | 86 if cmd_name and len(possible_choices) == 1: |
| 87 # Avoid showing the user a menu if the user's search string matched exactly | 87 # Avoid showing the user a menu if the user's search string matched exactly |
| 88 # one item. | 88 # one item. |
| 89 choice = 0 | 89 choice = 0 |
| 90 cros_lib.Info("Running command '%s'." % possible_cmds[choice]) |
| 90 else: | 91 else: |
| 91 choice = text_menu.TextMenu(possible_choices, 'Which chromite command', | 92 choice = text_menu.TextMenu(possible_choices, 'Which chromite command', |
| 92 menu_width=0) | 93 menu_width=0) |
| 93 | 94 |
| 94 if choice is None: | 95 if choice is None: |
| 95 cros_lib.Die('OK, cancelling...') | 96 cros_lib.Die('OK, cancelling...') |
| 96 else: | 97 else: |
| 97 return possible_cmds[choice] | 98 return possible_cmds[choice] |
| 98 | 99 |
| 99 | 100 |
| 100 def main(): | 101 def main(): |
| 101 """Main function for the chromite shell.""" | 102 """Main function for the chromite shell.""" |
| 102 | 103 |
| 103 # Hack it so that argv[0] is 'chromite' so that it doesn't tell user to run | 104 # Hack it so that argv[0] is 'chromite' so that it doesn't tell user to run |
| 104 # 'main.py' in help commands... | 105 # 'main.py' in help commands... |
| 105 # TODO(dianders): Remove this hack, since it is ugly. Shouldn't be needed | 106 # TODO(dianders): Remove this hack, since it is ugly. Shouldn't be needed |
| 106 # once we change the way that the chromite wrapper works. | 107 # once we change the way that the chromite wrapper works. |
| 107 sys.argv[0] = 'chromite' | 108 sys.argv[0] = 'chromite' |
| 108 | 109 |
| 109 # Support EnterChroot(). | 110 # Support EnterChroot(). |
| 110 # This may raise a ChromiteError if the child dies, so we must handle this. | 111 did_resume = utils.ResumeEnterChrootIfNeeded(sys.argv) |
| 111 try: | 112 if did_resume: |
| 112 did_resume = utils.ResumeEnterChrootIfNeeded(sys.argv) | 113 return |
| 113 if did_resume: | |
| 114 return | |
| 115 except chromite_env.ChromiteError: | |
| 116 # The error has been reported, but we must exit indicating failure | |
| 117 sys.exit(1) | |
| 118 | 114 |
| 119 # TODO(dianders): Make help a little better. Specifically: | 115 # TODO(dianders): Make help a little better. Specifically: |
| 120 # 1. Add a command called 'help' | 116 # 1. Add a command called 'help' |
| 121 # 2. Make the help string below include command list and descriptions (like | 117 # 2. Make the help string below include command list and descriptions (like |
| 122 # the menu, but without being interactive). | 118 # the menu, but without being interactive). |
| 123 # 3. Make "help command" and "--help command" equivalent to "command --help". | 119 # 3. Make "help command" and "--help command" equivalent to "command --help". |
| 124 help_str = ( | 120 help_str = ( |
| 125 """Usage: %(prog)s [chromite_options] [cmd [args]]\n""" | 121 """Usage: %(prog)s [chromite_options] [cmd [args]]\n""" |
| 126 """\n""" | 122 """\n""" |
| 127 """The chromite script is a wrapper to make it easy to do various\n""" | 123 """The chromite script is a wrapper to make it easy to do various\n""" |
| 128 """build tasks. For a list of commands, run without any arguments.\n""" | 124 """build tasks. For a list of commands, run without any arguments.\n""" |
| 129 """\n""" | 125 """\n""" |
| 130 """Options:\n""" | 126 """Options:\n""" |
| 131 """ -h, --help show this help message and exit\n""" | 127 """ -h, --help show this help message and exit\n""" |
| 132 ) % {'prog': os.path.basename(sys.argv[0])} | 128 ) % {'prog': os.path.basename(sys.argv[0])} |
| 133 if not cros_lib.IsInsideChroot(): | 129 if not cros_lib.IsInsideChroot(): |
| 134 help_str += ( | 130 help_str += ( |
| 135 """ --chroot=CHROOT_NAME Chroot spec to use. Can be an absolute\n""" | 131 """ --chroot=CHROOT_NAME Chroot spec to use. Can be an absolute\n""" |
| 136 """ path to a spec file or a substring of a\n""" | 132 """ path to a spec file or a substring of a\n""" |
| 137 """ chroot spec name (without .spec suffix)\n""" | 133 """ chroot spec name (without .spec suffix)\n""" |
| 138 ) | 134 ) |
| 139 | 135 |
| 140 # We don't use OptionParser here, since options for different subcommands are | 136 # We don't use OptionParser here, since options for different subcommands are |
| 141 # so different. We just look for the chromite options here... | 137 # so different. We just look for the chromite options here... |
| 142 # TODO(sjg): I think we should use OptionParser for two reasons: | |
| 143 # 1. It allows us to find out what options/paths are in the scripts | |
| 144 # 2. It prevents people from adding new options to underlying scripts | |
| 145 # so that Chromite diverges through no fault of the authors. | |
| 146 if sys.argv[1:2] == ['--help']: | 138 if sys.argv[1:2] == ['--help']: |
| 147 print help_str | 139 print help_str |
| 148 sys.exit(0) | 140 sys.exit(0) |
| 149 else: | 141 else: |
| 150 # Start by skipping argv[0] | 142 # Start by skipping argv[0] |
| 151 argv = sys.argv[1:] | 143 argv = sys.argv[1:] |
| 152 | 144 |
| 153 # Set up the cros system. | |
| 154 cros_env = chromite_env.ChromiteEnv() | |
| 155 | |
| 156 # Configure the operation setup. | |
| 157 oper = cros_env.GetOperation() | |
| 158 oper.verbose = True | |
| 159 oper.progress = True | |
| 160 | |
| 161 # Do we want to be quiet? This is just a hack / demo | |
| 162 if argv and argv[0] == '-q': | |
| 163 oper.verbose = False | |
| 164 argv = argv[1:] | |
| 165 | |
| 166 # Look for special "--chroot" argument to allow for alternate chroots | 145 # Look for special "--chroot" argument to allow for alternate chroots |
| 167 if not cros_lib.IsInsideChroot(): | 146 if not cros_lib.IsInsideChroot(): |
| 168 # Default chroot name... | 147 # Default chroot name... |
| 169 chroot_name = 'chroot' | 148 chroot_name = 'chroot' |
| 170 | 149 |
| 171 # Get chroot spec name if specified; trim argv down if needed... | 150 # Get chroot spec name if specified; trim argv down if needed... |
| 172 if argv: | 151 if argv: |
| 173 if argv[0].startswith('--chroot='): | 152 if argv[0].startswith('--chroot='): |
| 174 _, chroot_name = argv[0].split('=', 2) | 153 _, chroot_name = argv[0].split('=', 2) |
| 175 argv = argv[1:] | 154 argv = argv[1:] |
| 176 elif argv[0] == '--chroot': | 155 elif argv[0] == '--chroot': |
| 177 if len(argv) < 2: | 156 if len(argv) < 2: |
| 178 cros_lib.Die('Chroot not specified.') | 157 cros_lib.Die('Chroot not specified.') |
| 179 | 158 |
| 180 chroot_name = argv[1] | 159 chroot_name = argv[1] |
| 181 argv = argv[2:] | 160 argv = argv[2:] |
| 182 | 161 |
| 183 chroot_spec_path = utils.FindSpec(chroot_name, | 162 chroot_spec_path = utils.FindSpec(chroot_name, |
| 184 spec_type=utils.CHROOT_SPEC_TYPE) | 163 spec_type=utils.CHROOT_SPEC_TYPE) |
| 185 | 164 |
| 186 oper.Info('Using chroot "%s"' % os.path.relpath(chroot_spec_path)) | 165 cros_lib.Info('Using chroot "%s"' % os.path.relpath(chroot_spec_path)) |
| 187 | 166 |
| 188 chroot_config = utils.ReadConfig(chroot_spec_path) | 167 chroot_config = utils.ReadConfig(chroot_spec_path) |
| 189 else: | 168 else: |
| 190 # Already in the chroot; no need to get config... | 169 # Already in the chroot; no need to get config... |
| 191 chroot_config = None | 170 chroot_config = None |
| 192 | 171 |
| 193 # Get command and arguments | 172 # Get command and arguments |
| 194 if argv: | 173 if argv: |
| 195 cmd_str = argv[0].lower() | 174 cmd_str = argv[0].lower() |
| 196 argv = argv[1:] | 175 argv = argv[1:] |
| 197 else: | 176 else: |
| 198 cmd_str = '' | 177 cmd_str = '' |
| 199 | 178 |
| 200 # Validate the subcmd, popping a menu if needed. | 179 # Validate the subcmd, popping a menu if needed. |
| 201 cmd_str = _FindCommand(cmd_str) | 180 cmd_str = _FindCommand(cmd_str) |
| 202 oper.Info("Running command '%s'." % cmd_str) | 181 |
| 182 # Set up the cros system. |
| 183 cros_env = chromite_env.ChromiteEnv() |
| 184 |
| 185 # Configure the operation setup. |
| 186 oper = cros_env.GetOperation() |
| 187 oper.SetVerbose(True) |
| 188 oper.SetProgress(True) |
| 203 | 189 |
| 204 # Finally, call the function w/ standard argv. | 190 # Finally, call the function w/ standard argv. |
| 205 cmd_cls = _COMMAND_HANDLERS[_COMMAND_STRS.index(cmd_str)] | 191 cmd_cls = _COMMAND_HANDLERS[_COMMAND_STRS.index(cmd_str)] |
| 206 cmd_obj = cmd_cls() | 192 cmd_obj = cmd_cls() |
| 207 cmd_obj.SetChromiteEnv(cros_env) | 193 cmd_obj.SetChromiteEnv(cros_env) |
| 208 try: | 194 cmd_obj.Run([cmd_str] + argv, chroot_config=chroot_config) |
| 209 cmd_obj.Run([cmd_str] + argv, chroot_config=chroot_config) | |
| 210 | 195 |
| 211 # Handle an error in one of the scripts: print a message and exit. | |
| 212 except chromite_env.ChromiteError, msg: | |
| 213 sys.exit(1) | |
| 214 | 196 |
| 215 if __name__ == '__main__': | 197 if __name__ == '__main__': |
| 216 main() | 198 main() |
| OLD | NEW |