Chromium Code Reviews| 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 optparse | 10 import optparse | 
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 121 """%(prog)s [chromite_options] [cmd [args]]\n""" | 121 """%(prog)s [chromite_options] [cmd [args]]\n""" | 
| 122 """\n""" | 122 """\n""" | 
| 123 """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""" | 
| 124 """build tasks. For a list of commands, run without any arguments.""" | 124 """build tasks. For a list of commands, run without any arguments.""" | 
| 125 ) % {'prog': os.path.basename(sys.argv[0])} | 125 ) % {'prog': os.path.basename(sys.argv[0])} | 
| 126 | 126 | 
| 127 parser = optparse.OptionParser() | 127 parser = optparse.OptionParser() | 
| 128 | 128 | 
| 129 # Verbose defaults to full for now, just to keep people acclimatized to | 129 # Verbose defaults to full for now, just to keep people acclimatized to | 
| 130 # vast amounts of comforting output. | 130 # vast amounts of comforting output. | 
| 131 parser.add_option('-v', dest='verbose', default=3, | 131 parser.add_option('-v', dest='verbose', default=3, type='int', | 
| 132 help='Control verbosity: 0=silent, 1=progress, 3=full') | 132 help='Control verbosity: 0=silent, 1=progress, 3=full') | 
| 133 parser.add_option('-q', action='store_const', dest='verbose', const=0, | 133 parser.add_option('-q', action='store_const', dest='verbose', const=0, | 
| 134 help='Be quieter (sets verbosity to 1)') | 134 help='Be quieter (sets verbosity to 1)') | 
| 135 if not cros_lib.IsInsideChroot(): | 135 if not cros_lib.IsInsideChroot(): | 
| 136 parser.add_option('--chroot', action='store', type='string', | 136 parser.add_option('--chroot', action='store', type='string', | 
| 137 dest='chroot_name', default='chroot', | 137 dest='chroot_name', default='chroot', | 
| 138 help="Chroot spec to use. Can be an absolute path to a spec file " | 138 help="Chroot spec to use. Can be an absolute path to a spec file " | 
| 139 "or a substring of a chroot spec name (without .spec suffix)") | 139 "or a substring of a chroot spec name (without .spec suffix)") | 
| 140 parser.usage = help_str | 140 parser.usage = help_str | 
| 141 try: | 141 | 
| 142 (options, args) = parser.parse_args() | 142 # We want to parse only the options that we understand at the top level of | 
| 
 
diandersAtChromium
2011/04/08 00:43:46
Maybe put this whole chunk of parsing code in a he
 
sjg
2011/04/08 20:25:26
done - added a few tests also.
 
 | |
| 143 except: | 143 # Chromite. Our heuristic here is that anything after the first positional | 
| 144 sys.exit(1) | 144 # parameter (which we assume is the command) is ignored at this level, and | 
| 145 # is passed down to the command level to handle. | |
| 146 # TODO(sjg): Revisit this to include tolerant option parser instead | |
| 147 # http://codereview.chromium.org/6469035/ | |
| 148 our_args = [] # Arguments that we will parse here. | |
| 149 cmd_args = [] # Arguments that we will pass down to the command level. | |
| 150 positional = 0 | |
| 151 for arg in sys.argv: | |
| 
 
diandersAtChromium
2011/04/08 00:43:46
This loop keeps confusing me the way it's written.
 
sjg
2011/04/08 20:25:26
Yes, and in fact this inspired me to simplify it a
 
 | |
| 152 | |
| 153 # Option arguments are collected as is, but we only allow two positional | |
| 154 # arguments: argv[0] is the program name, the next is the command name. | |
| 155 if positional == 2: | |
| 156 cmd_args = sys.argv[len(our_args):] | |
| 157 break | |
| 158 if arg[0] != '-': | |
| 159 positional += 1 | |
| 160 our_args.append(arg) | |
| 161 | |
| 162 # Loop until we have enough arguments to get a command. | |
| 163 cmd_str = '' | |
| 164 while not cmd_str: | |
| 165 (options, args) = parser.parse_args(our_args) | |
| 166 | |
| 167 # Get the command from the resulting arg list. | |
| 168 if len(args) > 1: | |
| 169 cmd_str = args[1].lower() | |
| 170 elif cmd_args: | |
| 171 # We don't have a command yet. Transfer a positional arg from from | |
| 172 # cmd_args to our_args to see if that does the trick. We move over any | |
| 173 # options we find also. | |
| 174 while cmd_args: | |
| 175 arg = cmd_args[0] | |
| 
 
diandersAtChromium
2011/04/08 00:43:46
arg = cmd_args.pop(0)
 
sjg
2011/04/08 20:25:26
done
 
 | |
| 176 our_args.append(arg) | |
| 177 cmd_args = cmd_args[1:] | |
| 178 if arg[0] != '-': | |
| 
 
diandersAtChromium
2011/04/08 00:43:46
startswith
 
sjg
2011/04/08 20:25:26
done
 
 | |
| 179 break | |
| 180 else: | |
| 181 # No more cmd_args to consume. | |
| 182 break | |
| 145 | 183 | 
| 146 # Set up the cros system. | 184 # Set up the cros system. | 
| 147 cros_env = chromite_env.ChromiteEnv() | 185 cros_env = chromite_env.ChromiteEnv() | 
| 148 | 186 | 
| 149 # Configure the operation setup. | 187 # Configure the operation setup. | 
| 150 oper = cros_env.GetOperation() | 188 oper = cros_env.GetOperation() | 
| 151 oper.verbose = options.verbose >= 3 | 189 oper.verbose = options.verbose >= 3 | 
| 152 oper.progress = options.verbose >= 1 | 190 oper.progress = options.verbose >= 1 | 
| 153 | 191 | 
| 154 # Look for special "--chroot" argument to allow for alternate chroots | 192 # Look for special "--chroot" argument to allow for alternate chroots | 
| 155 if not cros_lib.IsInsideChroot(): | 193 if not cros_lib.IsInsideChroot(): | 
| 156 chroot_spec_path = utils.FindSpec(options.chroot_name, | 194 chroot_spec_path = utils.FindSpec(options.chroot_name, | 
| 157 spec_type=utils.CHROOT_SPEC_TYPE) | 195 spec_type=utils.CHROOT_SPEC_TYPE) | 
| 158 | 196 | 
| 159 oper.Info('Using chroot "%s"' % os.path.relpath(chroot_spec_path)) | 197 oper.Info('Using chroot "%s"' % os.path.relpath(chroot_spec_path)) | 
| 160 | 198 | 
| 161 chroot_config = utils.ReadConfig(chroot_spec_path) | 199 chroot_config = utils.ReadConfig(chroot_spec_path) | 
| 162 else: | 200 else: | 
| 163 # Already in the chroot; no need to get config... | 201 # Already in the chroot; no need to get config... | 
| 164 chroot_config = None | 202 chroot_config = None | 
| 165 | 203 | 
| 166 # Get command and arguments | |
| 167 if args: | |
| 168 cmd_str = args[0].lower() | |
| 169 args = args[1:] | |
| 170 else: | |
| 171 cmd_str = '' | |
| 172 | |
| 173 # Validate the subcmd, popping a menu if needed. | 204 # Validate the subcmd, popping a menu if needed. | 
| 174 cmd_str = _FindCommand(cmd_str) | 205 cmd_str = _FindCommand(cmd_str) | 
| 175 oper.Info("Running command '%s'." % cmd_str) | 206 oper.Info("Running command '%s'." % cmd_str) | 
| 176 | 207 | 
| 177 # Finally, call the function w/ standard argv. | 208 # Finally, call the function w/ standard argv. | 
| 178 cmd_cls = _COMMAND_HANDLERS[_COMMAND_STRS.index(cmd_str)] | 209 cmd_cls = _COMMAND_HANDLERS[_COMMAND_STRS.index(cmd_str)] | 
| 179 cmd_obj = cmd_cls() | 210 cmd_obj = cmd_cls() | 
| 180 cmd_obj.SetChromiteEnv(cros_env) | 211 cmd_obj.SetChromiteEnv(cros_env) | 
| 181 try: | 212 try: | 
| 182 cmd_obj.Run([cmd_str] + args, chroot_config=chroot_config) | 213 cmd_obj.Run([cmd_str] + cmd_args, chroot_config=chroot_config) | 
| 183 | 214 | 
| 184 # Handle an error in one of the scripts: print a message and exit. | 215 # Handle an error in one of the scripts: print a message and exit. | 
| 185 except chromite_env.ChromiteError, msg: | 216 except chromite_env.ChromiteError, msg: | 
| 186 sys.exit(1) | 217 sys.exit(1) | 
| 187 | 218 | 
| 188 if __name__ == '__main__': | 219 if __name__ == '__main__': | 
| 189 main() | 220 main() | 
| OLD | NEW |