| 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 17 matching lines...) Expand all Loading... |
| 28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 # | 29 # |
| 30 # MultiCommandTool provides a framework for writing svn-like/git-like tools | 30 # MultiCommandTool provides a framework for writing svn-like/git-like tools |
| 31 # which are called with the following format: | 31 # which are called with the following format: |
| 32 # tool-name [global options] command-name [command options] | 32 # tool-name [global options] command-name [command options] |
| 33 | 33 |
| 34 import optparse | 34 import optparse |
| 35 import logging | 35 import logging |
| 36 import sys | 36 import sys |
| 37 | 37 |
| 38 from webkitpy.tool.commands.command import Command | |
| 39 from webkitpy.tool.commands.command import HelpPrintingOptionParser | 38 from webkitpy.tool.commands.command import HelpPrintingOptionParser |
| 40 from webkitpy.tool.commands.help_command import HelpCommand | 39 from webkitpy.tool.commands.help_command import HelpCommand |
| 41 | 40 |
| 42 _log = logging.getLogger(__name__) | 41 _log = logging.getLogger(__name__) |
| 43 | 42 |
| 44 | 43 |
| 45 class MultiCommandTool(object): | 44 class MultiCommandTool(object): |
| 46 | 45 |
| 47 global_options = None | 46 global_options = None |
| 48 | 47 |
| 49 def __init__(self, name=None, commands=None): | 48 def __init__(self, commands): |
| 50 self._name = name or optparse.OptionParser(prog=name).get_prog_name() #
OptionParser has nice logic for fetching the name. | 49 self.commands = commands |
| 51 # Allow the unit tests to disable command auto-discovery. | |
| 52 self.commands = commands or [cls() for cls in self._find_all_commands()
if cls.name] | |
| 53 self.help_command = self.command_by_name(HelpCommand.name) | 50 self.help_command = self.command_by_name(HelpCommand.name) |
| 54 # Require a help command, even if the manual test list doesn't include o
ne. | 51 # Require the help command, even if the manual test list doesn't include
one. |
| 55 if not self.help_command: | 52 if not self.help_command: |
| 56 self.help_command = HelpCommand() | 53 self.help_command = HelpCommand() |
| 57 self.commands.append(self.help_command) | 54 self.commands.append(self.help_command) |
| 55 # FIXME: Since tool is passed to Command.execute, it may not be necessar
y to set a tool attribute on the |
| 56 # command objects here - maybe this should be done inside of Command.exe
cute for commands that use self._tool. |
| 58 for command in self.commands: | 57 for command in self.commands: |
| 59 command.bind_to_tool(self) | 58 command.bind_to_tool(self) |
| 60 | 59 |
| 61 @classmethod | |
| 62 def _add_all_subclasses(cls, class_to_crawl, seen_classes): | |
| 63 for subclass in class_to_crawl.__subclasses__(): | |
| 64 if subclass not in seen_classes: | |
| 65 seen_classes.add(subclass) | |
| 66 cls._add_all_subclasses(subclass, seen_classes) | |
| 67 | |
| 68 @classmethod | |
| 69 def _find_all_commands(cls): | |
| 70 commands = set() | |
| 71 cls._add_all_subclasses(Command, commands) | |
| 72 return sorted(commands) | |
| 73 | |
| 74 def name(self): | 60 def name(self): |
| 75 return self._name | 61 return optparse.OptionParser().get_prog_name() |
| 76 | 62 |
| 77 def _create_option_parser(self): | 63 def _create_option_parser(self): |
| 78 usage = "Usage: %prog [options] COMMAND [ARGS]" | 64 usage = "Usage: %prog [options] COMMAND [ARGS]" |
| 79 return HelpPrintingOptionParser(epilog_method=self.help_command._help_ep
ilog, prog=self.name(), usage=usage) | 65 return HelpPrintingOptionParser(epilog_method=self.help_command._help_ep
ilog, prog=self.name(), usage=usage) |
| 80 | 66 |
| 81 @staticmethod | 67 @staticmethod |
| 82 def _split_command_name_from_args(args): | 68 def _split_command_name_from_args(args): |
| 83 # Assume the first argument which doesn't start with "-" is the command
name. | 69 # Assume the first argument which doesn't start with "-" is the command
name. |
| 84 command_index = 0 | 70 command_index = 0 |
| 85 for arg in args: | 71 for arg in args: |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 | 121 |
| 136 (should_execute, failure_reason) = self.should_execute_command(command) | 122 (should_execute, failure_reason) = self.should_execute_command(command) |
| 137 if not should_execute: | 123 if not should_execute: |
| 138 _log.error(failure_reason) | 124 _log.error(failure_reason) |
| 139 return 0 # FIXME: Should this really be 0? | 125 return 0 # FIXME: Should this really be 0? |
| 140 | 126 |
| 141 result = command.check_arguments_and_execute(options, args, self) | 127 result = command.check_arguments_and_execute(options, args, self) |
| 142 | 128 |
| 143 self.command_completed() | 129 self.command_completed() |
| 144 return result | 130 return result |
| OLD | NEW |