Chromium Code Reviews| Index: third_party/WebKit/Tools/Scripts/webkitpy/tool/webkit_patch.py |
| diff --git a/third_party/WebKit/Tools/Scripts/webkitpy/tool/webkit_patch.py b/third_party/WebKit/Tools/Scripts/webkitpy/tool/webkit_patch.py |
| index 428894871700aaeeded90619770b462b62056a2d..a0e7bc51f59e0665a5e5731e9b682927c304cd04 100644 |
| --- a/third_party/WebKit/Tools/Scripts/webkitpy/tool/webkit_patch.py |
| +++ b/third_party/WebKit/Tools/Scripts/webkitpy/tool/webkit_patch.py |
| @@ -26,13 +26,20 @@ |
| # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| -# |
| -# A tool for automating dealing with bugzilla, posting patches, committing patches, etc. |
| +"""Webkit-patch is a tool with multiple sub-commands with different purposes. |
| + |
| +Historically, it had commands related to dealing with bugzilla, and posting |
| +and comitting patches to WebKit. More recently, it has commands for printing |
| +expectations, fetching new test baselines, starting a commit-announcer IRC bot, |
| +etc. These commands don't necessarily have anything to do with each other. |
| +""" |
| + |
| +import logging |
| +import sys |
| from optparse import make_option |
| from webkitpy.common.host import Host |
| -from webkitpy.tool.multi_command_tool import MultiCommandTool |
| from webkitpy.tool.commands.analyze_baselines import AnalyzeBaselines |
| from webkitpy.tool.commands.commit_announcer import CommitAnnouncerCommand |
| @@ -52,16 +59,27 @@ from webkitpy.tool.commands.rebaseline import RebaselineTest |
| from webkitpy.tool.commands.rebaseline_server import RebaselineServer |
| from webkitpy.tool.commands.rebaseline_from_try_jobs import RebaselineFromTryJobs |
| +import optparse |
| + |
| +import sys |
| + |
| +from webkitpy.tool.commands.command import HelpPrintingOptionParser |
| +from webkitpy.tool.commands.help_command import HelpCommand |
| + |
| + |
| +_log = logging.getLogger(__name__) |
| + |
| -class WebKitPatch(MultiCommandTool, Host): |
| +class WebKitPatch(Host): |
| global_options = [ |
| make_option("-v", "--verbose", action="store_true", dest="verbose", default=False, help="enable all logging"), |
| make_option("-d", "--directory", action="append", dest="patch_directories", |
| default=[], help="Directory to look at for changed files"), |
| ] |
| - def __init__(self, path): |
| - MultiCommandTool.__init__(self, commands=[ |
| + def __init__(self): |
| + Host.__init__(self) |
|
Dirk Pranke
2016/07/01 22:55:42
I'd probably use super() here to be consistent w/
qyearsley
2016/07/02 17:26:29
Done
|
| + self.commands = [ |
| AnalyzeBaselines(), |
| AutoRebaseline(), |
| CommitAnnouncerCommand(), |
| @@ -79,27 +97,81 @@ class WebKitPatch(MultiCommandTool, Host): |
| RebaselineJson(), |
| RebaselineServer(), |
| RebaselineTest(), |
| - ]) |
| - Host.__init__(self) |
| - self._path = path |
| + ] |
| + self.help_command = HelpCommand() |
| + self.commands.append(self.help_command) |
| + # FIXME: Since tool is passed to Command.execute, it may not be necessary to set a tool attribute on the |
| + # command objects here - maybe this should be done inside of Command.execute for commands that use self._tool. |
| + for command in self.commands: |
| + command.bind_to_tool(self) |
| - def path(self): |
| - return self._path |
| + def main(self, argv=None): |
| + argv = argv or sys.argv |
| + (command_name, args) = self._split_command_name_from_args(argv[1:]) |
| - def should_show_in_main_help(self, command): |
| - if not command.show_in_main_help: |
| - return False |
| - if command.requires_local_commits: |
| - return self.scm().supports_local_commits() |
| - return True |
| + option_parser = self._create_option_parser() |
| + self._add_global_options(option_parser) |
| + |
| + command = self.command_by_name(command_name) or self.help_command |
| + if not command: |
| + option_parser.error("%s is not a recognized command" % command_name) |
| + |
| + command.set_option_parser(option_parser) |
| + (options, args) = command.parse_args(args) |
| + self._handle_global_options(options) |
| + |
| + (should_execute, failure_reason) = self._should_execute_command(command) |
| + if not should_execute: |
| + _log.error(failure_reason) |
| + return 0 # FIXME: Should this really be 0? |
| + |
| + result = command.check_arguments_and_execute(options, args, self) |
| + return result |
| + |
| + @staticmethod |
| + def _split_command_name_from_args(args): |
| + # Assume the first argument which doesn't start with "-" is the command name. |
| + command_index = 0 |
| + for arg in args: |
| + if arg[0] != "-": |
| + break |
| + command_index += 1 |
| + else: |
| + return (None, args[:]) |
| + |
| + command = args[command_index] |
| + return (command, args[:command_index] + args[command_index + 1:]) |
| + |
| + def _create_option_parser(self): |
| + usage = "Usage: %prog [options] COMMAND [ARGS]" |
| + name = optparse.OptionParser().get_prog_name() |
| + return HelpPrintingOptionParser(epilog_method=self.help_command._help_epilog, prog=name, usage=usage) |
| + |
| + def _add_global_options(self, option_parser): |
| + global_options = self.global_options or [] |
| + for option in global_options: |
| + option_parser.add_option(option) |
| # FIXME: This may be unnecessary since we pass global options to all commands during execute() as well. |
| - def handle_global_options(self, options): |
| + def _handle_global_options(self, options): |
| self.initialize_scm(options.patch_directories) |
| - def should_execute_command(self, command): |
| + def _should_execute_command(self, command): |
| if command.requires_local_commits and not self.scm().supports_local_commits(): |
| failure_reason = "%s requires local commits using %s in %s." % ( |
| command.name, self.scm().display_name(), self.scm().checkout_root) |
| return (False, failure_reason) |
| return (True, None) |
| + |
| + def should_show_in_main_help(self, command): |
| + if not command.show_in_main_help: |
| + return False |
| + if command.requires_local_commits: |
| + return self.scm().supports_local_commits() |
| + return True |
| + |
| + def command_by_name(self, command_name): |
| + for command in self.commands: |
| + if command_name == command.name: |
| + return command |
| + return None |