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 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
131 parser.add_option('-v', dest='verbose', default=3, | 131 parser.add_option('-v', dest='verbose', default=3, |
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 | |
142 # We want to parse only the options that we understand at the top level of | |
diandersAtChromium
2011/04/04 20:42:48
This code won't work, will it? Try:
chromite -v
| |
143 # Chromite. Our heuristic here is that anything after the first positional | |
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 # FIXME(sjg): Revisit this to include tolerant option parser instead | |
diandersAtChromium
2011/04/04 20:42:48
nit: please use TODO.
| |
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: | |
152 | |
153 # Option arguments are collected as it, but we only allow two positional | |
diandersAtChromium
2011/04/04 20:42:48
nit: "as is"
| |
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 | |
141 try: | 162 try: |
142 (options, args) = parser.parse_args() | 163 (options, args) = parser.parse_args(our_args) |
143 except: | 164 except: |
144 sys.exit(1) | 165 sys.exit(1) |
145 | 166 |
146 # Set up the cros system. | 167 # Set up the cros system. |
147 cros_env = chromite_env.ChromiteEnv() | 168 cros_env = chromite_env.ChromiteEnv() |
148 | 169 |
149 # Configure the operation setup. | 170 # Configure the operation setup. |
150 oper = cros_env.GetOperation() | 171 oper = cros_env.GetOperation() |
151 oper.verbose = options.verbose >= 3 | 172 oper.verbose = options.verbose >= 3 |
152 oper.progress = options.verbose >= 1 | 173 oper.progress = options.verbose >= 1 |
153 | 174 |
154 # Look for special "--chroot" argument to allow for alternate chroots | 175 # Look for special "--chroot" argument to allow for alternate chroots |
155 if not cros_lib.IsInsideChroot(): | 176 if not cros_lib.IsInsideChroot(): |
156 chroot_spec_path = utils.FindSpec(options.chroot_name, | 177 chroot_spec_path = utils.FindSpec(options.chroot_name, |
157 spec_type=utils.CHROOT_SPEC_TYPE) | 178 spec_type=utils.CHROOT_SPEC_TYPE) |
158 | 179 |
159 oper.Info('Using chroot "%s"' % os.path.relpath(chroot_spec_path)) | 180 oper.Info('Using chroot "%s"' % os.path.relpath(chroot_spec_path)) |
160 | 181 |
161 chroot_config = utils.ReadConfig(chroot_spec_path) | 182 chroot_config = utils.ReadConfig(chroot_spec_path) |
162 else: | 183 else: |
163 # Already in the chroot; no need to get config... | 184 # Already in the chroot; no need to get config... |
164 chroot_config = None | 185 chroot_config = None |
165 | 186 |
166 # Get command and arguments | 187 # Get command and arguments |
167 if args: | 188 if args: |
168 cmd_str = args[0].lower() | 189 cmd_str = args[1].lower() |
169 args = args[1:] | |
170 else: | 190 else: |
171 cmd_str = '' | 191 cmd_str = '' |
172 | 192 |
173 # Validate the subcmd, popping a menu if needed. | 193 # Validate the subcmd, popping a menu if needed. |
174 cmd_str = _FindCommand(cmd_str) | 194 cmd_str = _FindCommand(cmd_str) |
175 oper.Info("Running command '%s'." % cmd_str) | 195 oper.Info("Running command '%s'." % cmd_str) |
176 | 196 |
177 # Finally, call the function w/ standard argv. | 197 # Finally, call the function w/ standard argv. |
178 cmd_cls = _COMMAND_HANDLERS[_COMMAND_STRS.index(cmd_str)] | 198 cmd_cls = _COMMAND_HANDLERS[_COMMAND_STRS.index(cmd_str)] |
179 cmd_obj = cmd_cls() | 199 cmd_obj = cmd_cls() |
180 cmd_obj.SetChromiteEnv(cros_env) | 200 cmd_obj.SetChromiteEnv(cros_env) |
181 try: | 201 try: |
182 cmd_obj.Run([cmd_str] + args, chroot_config=chroot_config) | 202 cmd_obj.Run([cmd_str] + cmd_args, chroot_config=chroot_config) |
183 | 203 |
184 # Handle an error in one of the scripts: print a message and exit. | 204 # Handle an error in one of the scripts: print a message and exit. |
185 except chromite_env.ChromiteError, msg: | 205 except chromite_env.ChromiteError, msg: |
186 sys.exit(1) | 206 sys.exit(1) |
187 | 207 |
188 if __name__ == '__main__': | 208 if __name__ == '__main__': |
189 main() | 209 main() |
OLD | NEW |