OLD | NEW |
1 # Copyright (c) 2009 Google Inc. All rights reserved. | 1 # Copyright (c) 2009 Google Inc. All rights reserved. |
2 # Copyright (c) 2009 Apple Inc. All rights reserved. | 2 # Copyright (c) 2009 Apple Inc. All rights reserved. |
3 # | 3 # |
4 # Redistribution and use in source and binary forms, with or without | 4 # Redistribution and use in source and binary forms, with or without |
5 # modification, are permitted provided that the following conditions are | 5 # modification, are permitted provided that the following conditions are |
6 # met: | 6 # met: |
7 # | 7 # |
8 # * Redistributions of source code must retain the above copyright | 8 # * Redistributions of source code must retain the above copyright |
9 # notice, this list of conditions and the following disclaimer. | 9 # notice, this list of conditions and the following disclaimer. |
10 # * Redistributions in binary form must reproduce the above | 10 # * Redistributions in binary form must reproduce the above |
(...skipping 30 matching lines...) Expand all Loading... |
41 _log = logging.getLogger(__name__) | 41 _log = logging.getLogger(__name__) |
42 | 42 |
43 | 43 |
44 class TryAgain(Exception): | 44 class TryAgain(Exception): |
45 pass | 45 pass |
46 | 46 |
47 | 47 |
48 class Command(object): | 48 class Command(object): |
49 name = None | 49 name = None |
50 show_in_main_help = False | 50 show_in_main_help = False |
| 51 |
51 def __init__(self, help_text, argument_names=None, options=None, long_help=N
one, requires_local_commits=False): | 52 def __init__(self, help_text, argument_names=None, options=None, long_help=N
one, requires_local_commits=False): |
52 self.help_text = help_text | 53 self.help_text = help_text |
53 self.long_help = long_help | 54 self.long_help = long_help |
54 self.argument_names = argument_names | 55 self.argument_names = argument_names |
55 self.required_arguments = self._parse_required_arguments(argument_names) | 56 self.required_arguments = self._parse_required_arguments(argument_names) |
56 self.options = options | 57 self.options = options |
57 self.requires_local_commits = requires_local_commits | 58 self.requires_local_commits = requires_local_commits |
58 self._tool = None | 59 self._tool = None |
59 # option_parser can be overriden by the tool using set_option_parser | 60 # option_parser can be overriden by the tool using set_option_parser |
60 # This default parser will be used for standalone_help printing. | 61 # This default parser will be used for standalone_help printing. |
(...skipping 25 matching lines...) Expand all Loading... |
86 def _parse_required_arguments(argument_names): | 87 def _parse_required_arguments(argument_names): |
87 required_args = [] | 88 required_args = [] |
88 if not argument_names: | 89 if not argument_names: |
89 return required_args | 90 return required_args |
90 split_args = argument_names.split(" ") | 91 split_args = argument_names.split(" ") |
91 for argument in split_args: | 92 for argument in split_args: |
92 if argument[0] == '[': | 93 if argument[0] == '[': |
93 # For now our parser is rather dumb. Do some minimal validation
that | 94 # For now our parser is rather dumb. Do some minimal validation
that |
94 # we haven't confused it. | 95 # we haven't confused it. |
95 if argument[-1] != ']': | 96 if argument[-1] != ']': |
96 raise Exception("Failure to parse argument string %s. Argum
ent %s is missing ending ]" % (argument_names, argument)) | 97 raise Exception("Failure to parse argument string %s. Argum
ent %s is missing ending ]" % |
| 98 (argument_names, argument)) |
97 else: | 99 else: |
98 required_args.append(argument) | 100 required_args.append(argument) |
99 return required_args | 101 return required_args |
100 | 102 |
101 def name_with_arguments(self): | 103 def name_with_arguments(self): |
102 usage_string = self.name | 104 usage_string = self.name |
103 if self.options: | 105 if self.options: |
104 usage_string += " [options]" | 106 usage_string += " [options]" |
105 if self.argument_names: | 107 if self.argument_names: |
106 usage_string += " " + self.argument_names | 108 usage_string += " " + self.argument_names |
(...skipping 30 matching lines...) Expand all Loading... |
137 (options, args) = self.parse_args(args) | 139 (options, args) = self.parse_args(args) |
138 # Some commands might require a dummy tool | 140 # Some commands might require a dummy tool |
139 return self.check_arguments_and_execute(options, args) | 141 return self.check_arguments_and_execute(options, args) |
140 | 142 |
141 | 143 |
142 # FIXME: This should just be rolled into Command. help_text and argument_names
do not need to be instance variables. | 144 # FIXME: This should just be rolled into Command. help_text and argument_names
do not need to be instance variables. |
143 class AbstractDeclarativeCommand(Command): | 145 class AbstractDeclarativeCommand(Command): |
144 help_text = None | 146 help_text = None |
145 argument_names = None | 147 argument_names = None |
146 long_help = None | 148 long_help = None |
| 149 |
147 def __init__(self, options=None, **kwargs): | 150 def __init__(self, options=None, **kwargs): |
148 Command.__init__(self, self.help_text, self.argument_names, options=opti
ons, long_help=self.long_help, **kwargs) | 151 Command.__init__(self, self.help_text, self.argument_names, options=opti
ons, long_help=self.long_help, **kwargs) |
149 | 152 |
150 | 153 |
151 class HelpPrintingOptionParser(OptionParser): | 154 class HelpPrintingOptionParser(OptionParser): |
| 155 |
152 def __init__(self, epilog_method=None, *args, **kwargs): | 156 def __init__(self, epilog_method=None, *args, **kwargs): |
153 self.epilog_method = epilog_method | 157 self.epilog_method = epilog_method |
154 OptionParser.__init__(self, *args, **kwargs) | 158 OptionParser.__init__(self, *args, **kwargs) |
155 | 159 |
156 def error(self, msg): | 160 def error(self, msg): |
157 self.print_usage(sys.stderr) | 161 self.print_usage(sys.stderr) |
158 error_message = "%s: error: %s\n" % (self.get_prog_name(), msg) | 162 error_message = "%s: error: %s\n" % (self.get_prog_name(), msg) |
159 # This method is overriden to add this one line to the output: | 163 # This method is overriden to add this one line to the output: |
160 error_message += "\nType \"%s --help\" to see usage.\n" % self.get_prog_
name() | 164 error_message += "\nType \"%s --help\" to see usage.\n" % self.get_prog_
name() |
161 self.exit(1, error_message) | 165 self.exit(1, error_message) |
162 | 166 |
163 # We override format_epilog to avoid the default formatting which would para
graph-wrap the epilog | 167 # We override format_epilog to avoid the default formatting which would para
graph-wrap the epilog |
164 # and also to allow us to compute the epilog lazily instead of in the constr
uctor (allowing it to be context sensitive). | 168 # and also to allow us to compute the epilog lazily instead of in the constr
uctor (allowing it to be context sensitive). |
165 def format_epilog(self, epilog): | 169 def format_epilog(self, epilog): |
166 if self.epilog_method: | 170 if self.epilog_method: |
167 return "\n%s\n" % self.epilog_method() | 171 return "\n%s\n" % self.epilog_method() |
168 return "" | 172 return "" |
169 | 173 |
170 | 174 |
171 class HelpCommand(AbstractDeclarativeCommand): | 175 class HelpCommand(AbstractDeclarativeCommand): |
172 name = "help" | 176 name = "help" |
173 help_text = "Display information about this program or its subcommands" | 177 help_text = "Display information about this program or its subcommands" |
174 argument_names = "[COMMAND]" | 178 argument_names = "[COMMAND]" |
175 | 179 |
176 def __init__(self): | 180 def __init__(self): |
177 options = [ | 181 options = [ |
178 make_option("-a", "--all-commands", action="store_true", dest="show_
all_commands", help="Print all available commands"), | 182 make_option("-a", "--all-commands", action="store_true", dest="show_
all_commands", help="Print all available commands"), |
179 ] | 183 ] |
180 AbstractDeclarativeCommand.__init__(self, options) | 184 AbstractDeclarativeCommand.__init__(self, options) |
181 self.show_all_commands = False # A hack used to pass --all-commands to _
help_epilog even though it's called by the OptionParser. | 185 # A hack used to pass --all-commands to _help_epilog even though it's ca
lled by the OptionParser. |
| 186 self.show_all_commands = False |
182 | 187 |
183 def _help_epilog(self): | 188 def _help_epilog(self): |
184 # Only show commands which are relevant to this checkout's SCM system.
Might this be confusing to some users? | 189 # Only show commands which are relevant to this checkout's SCM system.
Might this be confusing to some users? |
185 if self.show_all_commands: | 190 if self.show_all_commands: |
186 epilog = "All %prog commands:\n" | 191 epilog = "All %prog commands:\n" |
187 relevant_commands = self._tool.commands[:] | 192 relevant_commands = self._tool.commands[:] |
188 else: | 193 else: |
189 epilog = "Common %prog commands:\n" | 194 epilog = "Common %prog commands:\n" |
190 relevant_commands = filter(self._tool.should_show_in_main_help, self
._tool.commands) | 195 relevant_commands = filter(self._tool.should_show_in_main_help, self
._tool.commands) |
191 longest_name_length = max(map(lambda command: len(command.name), relevan
t_commands)) | 196 longest_name_length = max(map(lambda command: len(command.name), relevan
t_commands)) |
192 relevant_commands.sort(lambda a, b: cmp(a.name, b.name)) | 197 relevant_commands.sort(lambda a, b: cmp(a.name, b.name)) |
193 command_help_texts = map(lambda command: " %s %s\n" % (command.name.
ljust(longest_name_length), command.help_text), relevant_commands) | 198 command_help_texts = map(lambda command: " %s %s\n" % ( |
| 199 command.name.ljust(longest_name_length), command.help_text), relevan
t_commands) |
194 epilog += "%s\n" % "".join(command_help_texts) | 200 epilog += "%s\n" % "".join(command_help_texts) |
195 epilog += "See '%prog help --all-commands' to list all commands.\n" | 201 epilog += "See '%prog help --all-commands' to list all commands.\n" |
196 epilog += "See '%prog help COMMAND' for more information on a specific c
ommand.\n" | 202 epilog += "See '%prog help COMMAND' for more information on a specific c
ommand.\n" |
197 return epilog.replace("%prog", self._tool.name()) # Use of %prog here mi
mics OptionParser.expand_prog_name(). | 203 return epilog.replace("%prog", self._tool.name()) # Use of %prog here m
imics OptionParser.expand_prog_name(). |
198 | 204 |
199 # FIXME: This is a hack so that we don't show --all-commands as a global opt
ion: | 205 # FIXME: This is a hack so that we don't show --all-commands as a global opt
ion: |
200 def _remove_help_options(self): | 206 def _remove_help_options(self): |
201 for option in self.options: | 207 for option in self.options: |
202 self.option_parser.remove_option(option.get_opt_string()) | 208 self.option_parser.remove_option(option.get_opt_string()) |
203 | 209 |
204 def execute(self, options, args, tool): | 210 def execute(self, options, args, tool): |
205 if args: | 211 if args: |
206 command = self._tool.command_by_name(args[0]) | 212 command = self._tool.command_by_name(args[0]) |
207 if command: | 213 if command: |
208 print command.standalone_help() | 214 print command.standalone_help() |
209 return 0 | 215 return 0 |
210 | 216 |
211 self.show_all_commands = options.show_all_commands | 217 self.show_all_commands = options.show_all_commands |
212 self._remove_help_options() | 218 self._remove_help_options() |
213 self.option_parser.print_help() | 219 self.option_parser.print_help() |
214 return 0 | 220 return 0 |
215 | 221 |
216 | 222 |
217 class MultiCommandTool(object): | 223 class MultiCommandTool(object): |
218 global_options = None | 224 global_options = None |
219 | 225 |
220 def __init__(self, name=None, commands=None): | 226 def __init__(self, name=None, commands=None): |
221 self._name = name or OptionParser(prog=name).get_prog_name() # OptionPar
ser has nice logic for fetching the name. | 227 self._name = name or OptionParser(prog=name).get_prog_name() # OptionPa
rser has nice logic for fetching the name. |
222 # Allow the unit tests to disable command auto-discovery. | 228 # Allow the unit tests to disable command auto-discovery. |
223 self.commands = commands or [cls() for cls in self._find_all_commands()
if cls.name] | 229 self.commands = commands or [cls() for cls in self._find_all_commands()
if cls.name] |
224 self.help_command = self.command_by_name(HelpCommand.name) | 230 self.help_command = self.command_by_name(HelpCommand.name) |
225 # Require a help command, even if the manual test list doesn't include o
ne. | 231 # Require a help command, even if the manual test list doesn't include o
ne. |
226 if not self.help_command: | 232 if not self.help_command: |
227 self.help_command = HelpCommand() | 233 self.help_command = HelpCommand() |
228 self.commands.append(self.help_command) | 234 self.commands.append(self.help_command) |
229 for command in self.commands: | 235 for command in self.commands: |
230 command.bind_to_tool(self) | 236 command.bind_to_tool(self) |
231 | 237 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
299 if not command: | 305 if not command: |
300 option_parser.error("%s is not a recognized command" % command_name) | 306 option_parser.error("%s is not a recognized command" % command_name) |
301 | 307 |
302 command.set_option_parser(option_parser) | 308 command.set_option_parser(option_parser) |
303 (options, args) = command.parse_args(args) | 309 (options, args) = command.parse_args(args) |
304 self.handle_global_options(options) | 310 self.handle_global_options(options) |
305 | 311 |
306 (should_execute, failure_reason) = self.should_execute_command(command) | 312 (should_execute, failure_reason) = self.should_execute_command(command) |
307 if not should_execute: | 313 if not should_execute: |
308 _log.error(failure_reason) | 314 _log.error(failure_reason) |
309 return 0 # FIXME: Should this really be 0? | 315 return 0 # FIXME: Should this really be 0? |
310 | 316 |
311 while True: | 317 while True: |
312 try: | 318 try: |
313 result = command.check_arguments_and_execute(options, args, self
) | 319 result = command.check_arguments_and_execute(options, args, self
) |
314 break | 320 break |
315 except TryAgain, e: | 321 except TryAgain, e: |
316 pass | 322 pass |
317 | 323 |
318 self.command_completed() | 324 self.command_completed() |
319 return result | 325 return result |
OLD | NEW |