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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 else: | 86 else: |
87 choice = text_menu.TextMenu(possible_choices, 'Which chromite command', | 87 choice = text_menu.TextMenu(possible_choices, 'Which chromite command', |
88 menu_width=0) | 88 menu_width=0) |
89 | 89 |
90 if choice is None: | 90 if choice is None: |
91 cros_lib.Die('OK, cancelling...') | 91 cros_lib.Die('OK, cancelling...') |
92 else: | 92 else: |
93 return possible_cmds[choice] | 93 return possible_cmds[choice] |
94 | 94 |
95 | 95 |
96 # Helper function to parse arguments and split into one we understand, and | |
diandersAtChromium
2011/04/08 20:35:41
Last nit: can you make this a docstring instead of
sjg
2011/04/08 21:00:19
doh.
| |
97 # ones to pass on to sub-commands. | |
98 # We want to parse only the options that we understand at the top level of | |
99 # Chromite. Our heuristic here is that anything after the first positional | |
100 # parameter (which we assume is the command) is ignored at this level, and | |
101 # is passed down to the command level to handle. | |
102 # TODO(sjg): Revisit this to include tolerant option parser instead | |
103 # http://codereview.chromium.org/6469035/ | |
104 # | |
105 # Args: | |
106 # parser: Option parser. | |
107 # argv: List of program arguments | |
108 # | |
109 # Returns: | |
110 # options: Top level options (returned from optparse). | |
111 # cmd_str: Subcommand to run | |
112 # cmd_args: Arguments intended for subcommands. | |
113 def _ParseArguments(parser, argv): | |
114 our_args = [] | |
115 cmd_args = list(argv) | |
116 cmd_str = '' | |
117 args = [] # Nothing until we call optparse | |
118 while not cmd_str: | |
119 if our_args: | |
120 (options, args) = parser.parse_args(our_args) | |
121 if len(args) > 1: | |
122 cmd_str = args[1].lower() | |
123 elif cmd_args: | |
124 # We don't have a command yet. Transfer a positional arg from from | |
125 # cmd_args to our_args to see if that does the trick. We move over any | |
126 # options we find also. | |
127 while cmd_args: | |
128 arg = cmd_args.pop(0) | |
129 our_args.append(arg) | |
130 if not arg.startswith( '-'): | |
131 break | |
132 else: | |
133 # No more cmd_args to consume. | |
134 break | |
135 | |
136 # We must run the parser, even if it dies due to lack of arguments | |
137 if not args: | |
138 (options, args) = parser.parse_args(our_args) | |
139 return options, cmd_str, cmd_args | |
140 | |
96 def main(): | 141 def main(): |
97 """Main function for the chromite shell.""" | 142 """Main function for the chromite shell.""" |
98 | 143 |
99 # Hack it so that argv[0] is 'chromite' so that it doesn't tell user to run | 144 # Hack it so that argv[0] is 'chromite' so that it doesn't tell user to run |
100 # 'main.py' in help commands... | 145 # 'main.py' in help commands... |
101 # TODO(dianders): Remove this hack, since it is ugly. Shouldn't be needed | 146 # TODO(dianders): Remove this hack, since it is ugly. Shouldn't be needed |
102 # once we change the way that the chromite wrapper works. | 147 # once we change the way that the chromite wrapper works. |
103 sys.argv[0] = 'chromite' | 148 sys.argv[0] = 'chromite' |
104 | 149 |
105 # Support EnterChroot(). | 150 # Support EnterChroot(). |
(...skipping 15 matching lines...) Expand all Loading... | |
121 """%(prog)s [chromite_options] [cmd [args]]\n""" | 166 """%(prog)s [chromite_options] [cmd [args]]\n""" |
122 """\n""" | 167 """\n""" |
123 """The chromite script is a wrapper to make it easy to do various\n""" | 168 """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.""" | 169 """build tasks. For a list of commands, run without any arguments.""" |
125 ) % {'prog': os.path.basename(sys.argv[0])} | 170 ) % {'prog': os.path.basename(sys.argv[0])} |
126 | 171 |
127 parser = optparse.OptionParser() | 172 parser = optparse.OptionParser() |
128 | 173 |
129 # Verbose defaults to full for now, just to keep people acclimatized to | 174 # Verbose defaults to full for now, just to keep people acclimatized to |
130 # vast amounts of comforting output. | 175 # vast amounts of comforting output. |
131 parser.add_option('-v', dest='verbose', default=3, | 176 parser.add_option('-v', dest='verbose', default=3, type='int', |
132 help='Control verbosity: 0=silent, 1=progress, 3=full') | 177 help='Control verbosity: 0=silent, 1=progress, 3=full') |
133 parser.add_option('-q', action='store_const', dest='verbose', const=0, | 178 parser.add_option('-q', action='store_const', dest='verbose', const=0, |
134 help='Be quieter (sets verbosity to 1)') | 179 help='Be quieter (sets verbosity to 1)') |
135 if not cros_lib.IsInsideChroot(): | 180 if not cros_lib.IsInsideChroot(): |
136 parser.add_option('--chroot', action='store', type='string', | 181 parser.add_option('--chroot', action='store', type='string', |
137 dest='chroot_name', default='chroot', | 182 dest='chroot_name', default='chroot', |
138 help="Chroot spec to use. Can be an absolute path to a spec file " | 183 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)") | 184 "or a substring of a chroot spec name (without .spec suffix)") |
140 parser.usage = help_str | 185 parser.usage = help_str |
141 try: | 186 |
142 (options, args) = parser.parse_args() | 187 # Parse the arguments and separate them into top-level and subcmd arguments. |
143 except: | 188 options, cmd_str, cmd_args = _ParseArguments(parser, sys.argv) |
144 sys.exit(1) | |
145 | 189 |
146 # Set up the cros system. | 190 # Set up the cros system. |
147 cros_env = chromite_env.ChromiteEnv() | 191 cros_env = chromite_env.ChromiteEnv() |
148 | 192 |
149 # Configure the operation setup. | 193 # Configure the operation setup. |
150 oper = cros_env.GetOperation() | 194 oper = cros_env.GetOperation() |
151 oper.verbose = options.verbose >= 3 | 195 oper.verbose = options.verbose >= 3 |
152 oper.progress = options.verbose >= 1 | 196 oper.progress = options.verbose >= 1 |
153 | 197 |
154 # Look for special "--chroot" argument to allow for alternate chroots | 198 # Look for special "--chroot" argument to allow for alternate chroots |
155 if not cros_lib.IsInsideChroot(): | 199 if not cros_lib.IsInsideChroot(): |
156 chroot_spec_path = utils.FindSpec(options.chroot_name, | 200 chroot_spec_path = utils.FindSpec(options.chroot_name, |
157 spec_type=utils.CHROOT_SPEC_TYPE) | 201 spec_type=utils.CHROOT_SPEC_TYPE) |
158 | 202 |
159 oper.Info('Using chroot "%s"' % os.path.relpath(chroot_spec_path)) | 203 oper.Info('Using chroot "%s"' % os.path.relpath(chroot_spec_path)) |
160 | 204 |
161 chroot_config = utils.ReadConfig(chroot_spec_path) | 205 chroot_config = utils.ReadConfig(chroot_spec_path) |
162 else: | 206 else: |
163 # Already in the chroot; no need to get config... | 207 # Already in the chroot; no need to get config... |
164 chroot_config = None | 208 chroot_config = None |
165 | 209 |
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. | 210 # Validate the subcmd, popping a menu if needed. |
174 cmd_str = _FindCommand(cmd_str) | 211 cmd_str = _FindCommand(cmd_str) |
175 oper.Info("Running command '%s'." % cmd_str) | 212 oper.Info("Running command '%s'." % cmd_str) |
176 | 213 |
177 # Finally, call the function w/ standard argv. | 214 # Finally, call the function w/ standard argv. |
178 cmd_cls = _COMMAND_HANDLERS[_COMMAND_STRS.index(cmd_str)] | 215 cmd_cls = _COMMAND_HANDLERS[_COMMAND_STRS.index(cmd_str)] |
179 cmd_obj = cmd_cls() | 216 cmd_obj = cmd_cls() |
180 cmd_obj.SetChromiteEnv(cros_env) | 217 cmd_obj.SetChromiteEnv(cros_env) |
181 try: | 218 try: |
182 cmd_obj.Run([cmd_str] + args, chroot_config=chroot_config) | 219 cmd_obj.Run([cmd_str] + cmd_args, chroot_config=chroot_config) |
183 | 220 |
184 # Handle an error in one of the scripts: print a message and exit. | 221 # Handle an error in one of the scripts: print a message and exit. |
185 except chromite_env.ChromiteError, msg: | 222 except chromite_env.ChromiteError, msg: |
186 sys.exit(1) | 223 sys.exit(1) |
187 | 224 |
188 if __name__ == '__main__': | 225 if __name__ == '__main__': |
189 main() | 226 main() |
OLD | NEW |