forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split Command and HelpCommand into separate files.
This is a refactoring CL. Reason: The base Command class can logically be considered and tested separately from MultiCommandTool. This is also somewhat related to clearing out the imports from commands/__init__.py -- in order to not do imports there, we could explicitly import and enumerate the Command classes to include, either in webkit_patch.py or multicommandtool.py, and so individual Command classes shouldn't depend on the multicommandtool module (because that leads to circular imports). Review-Url: https://codereview.chromium.org/1971593002 Cr-Commit-Position: refs/heads/master@{#393777}
- Loading branch information
qyearsley
authored and
Commit bot
committed
May 15, 2016
1 parent
83810f7
commit 5bd243e
Showing
11 changed files
with
332 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
third_party/WebKit/Tools/Scripts/webkitpy/tool/commands/command.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
# Copyright (c) 2016 Google Inc. All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are | ||
# met: | ||
# | ||
# * Redistributions of source code must retain the above copyright | ||
# notice, this list of conditions and the following disclaimer. | ||
# * Redistributions in binary form must reproduce the above | ||
# copyright notice, this list of conditions and the following disclaimer | ||
# in the documentation and/or other materials provided with the | ||
# distribution. | ||
# * Neither the name of Google Inc. nor the names of its | ||
# contributors may be used to endorse or promote products derived from | ||
# this software without specific prior written permission. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
# 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. | ||
|
||
import optparse | ||
import logging | ||
import sys | ||
|
||
from webkitpy.tool.grammar import pluralize | ||
|
||
_log = logging.getLogger(__name__) | ||
|
||
|
||
class Command(object): | ||
# These class variables can be overridden in subclasses to set specific command behavior. | ||
name = None | ||
show_in_main_help = False | ||
help_text = None | ||
argument_names = None | ||
long_help = None | ||
|
||
def __init__(self, options=None, requires_local_commits=False): | ||
self.required_arguments = self._parse_required_arguments(self.argument_names) | ||
self.options = options | ||
self.requires_local_commits = requires_local_commits | ||
self._tool = None | ||
# option_parser can be overriden by the tool using set_option_parser | ||
# This default parser will be used for standalone_help printing. | ||
self.option_parser = HelpPrintingOptionParser(usage=optparse.SUPPRESS_USAGE, add_help_option=False, option_list=self.options) | ||
|
||
def _exit(self, code): | ||
sys.exit(code) | ||
|
||
# This design is slightly awkward, but we need the | ||
# the tool to be able to create and modify the option_parser | ||
# before it knows what Command to run. | ||
def set_option_parser(self, option_parser): | ||
self.option_parser = option_parser | ||
self._add_options_to_parser() | ||
|
||
def _add_options_to_parser(self): | ||
options = self.options or [] | ||
for option in options: | ||
self.option_parser.add_option(option) | ||
|
||
# The tool calls bind_to_tool on each Command after adding it to its list. | ||
def bind_to_tool(self, tool): | ||
# Command instances can only be bound to one tool at a time. | ||
if self._tool and tool != self._tool: | ||
raise Exception("Command already bound to tool!") | ||
self._tool = tool | ||
|
||
@staticmethod | ||
def _parse_required_arguments(argument_names): | ||
required_args = [] | ||
if not argument_names: | ||
return required_args | ||
split_args = argument_names.split(" ") | ||
for argument in split_args: | ||
if argument[0] == '[': | ||
# For now our parser is rather dumb. Do some minimal validation that | ||
# we haven't confused it. | ||
if argument[-1] != ']': | ||
raise Exception("Failure to parse argument string %s. Argument %s is missing ending ]" % | ||
(argument_names, argument)) | ||
else: | ||
required_args.append(argument) | ||
return required_args | ||
|
||
def name_with_arguments(self): | ||
usage_string = self.name | ||
if self.options: | ||
usage_string += " [options]" | ||
if self.argument_names: | ||
usage_string += " " + self.argument_names | ||
return usage_string | ||
|
||
def parse_args(self, args): | ||
return self.option_parser.parse_args(args) | ||
|
||
def check_arguments_and_execute(self, options, args, tool=None): | ||
if len(args) < len(self.required_arguments): | ||
_log.error("%s required, %s provided. Provided: %s Required: %s\nSee '%s help %s' for usage." % ( | ||
pluralize("argument", len(self.required_arguments)), | ||
pluralize("argument", len(args)), | ||
"'%s'" % " ".join(args), | ||
" ".join(self.required_arguments), | ||
tool.name(), | ||
self.name)) | ||
return 1 | ||
return self.execute(options, args, tool) or 0 | ||
|
||
def standalone_help(self): | ||
help_text = self.name_with_arguments().ljust(len(self.name_with_arguments()) + 3) + self.help_text + "\n\n" | ||
if self.long_help: | ||
help_text += "%s\n\n" % self.long_help | ||
help_text += self.option_parser.format_option_help(optparse.IndentedHelpFormatter()) | ||
return help_text | ||
|
||
def execute(self, options, args, tool): | ||
raise NotImplementedError("subclasses must implement") | ||
|
||
# main() exists so that Commands can be turned into stand-alone scripts. | ||
# Other parts of the code will likely require modification to work stand-alone. | ||
def main(self, args=sys.argv): | ||
(options, args) = self.parse_args(args) | ||
# Some commands might require a dummy tool | ||
return self.check_arguments_and_execute(options, args) | ||
|
||
|
||
class HelpPrintingOptionParser(optparse.OptionParser): | ||
|
||
def __init__(self, epilog_method=None, *args, **kwargs): | ||
self.epilog_method = epilog_method | ||
optparse.OptionParser.__init__(self, *args, **kwargs) | ||
|
||
def error(self, msg): | ||
self.print_usage(sys.stderr) | ||
error_message = "%s: error: %s\n" % (self.get_prog_name(), msg) | ||
# This method is overriden to add this one line to the output: | ||
error_message += "\nType \"%s --help\" to see usage.\n" % self.get_prog_name() | ||
self.exit(1, error_message) | ||
|
||
# We override format_epilog to avoid the default formatting which would paragraph-wrap the epilog | ||
# and also to allow us to compute the epilog lazily instead of in the constructor (allowing it to be context sensitive). | ||
def format_epilog(self, epilog): | ||
if self.epilog_method: | ||
return "\n%s\n" % self.epilog_method() | ||
return "" |
81 changes: 81 additions & 0 deletions
81
third_party/WebKit/Tools/Scripts/webkitpy/tool/commands/command_unittest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# Copyright (c) 2016 Google Inc. All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are | ||
# met: | ||
# | ||
# * Redistributions of source code must retain the above copyright | ||
# notice, this list of conditions and the following disclaimer. | ||
# * Redistributions in binary form must reproduce the above | ||
# copyright notice, this list of conditions and the following disclaimer | ||
# in the documentation and/or other materials provided with the | ||
# distribution. | ||
# * Neither the name of Google Inc. nor the names of its | ||
# contributors may be used to endorse or promote products derived from | ||
# this software without specific prior written permission. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
# 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. | ||
|
||
import optparse | ||
import unittest | ||
|
||
from webkitpy.common.system.outputcapture import OutputCapture | ||
from webkitpy.tool.commands.command import Command | ||
|
||
|
||
class TrivialCommand(Command): | ||
name = "trivial" | ||
help_text = "help text" | ||
long_help = "trivial command long help text" | ||
show_in_main_help = True | ||
|
||
def __init__(self, **kwargs): | ||
super(TrivialCommand, self).__init__(**kwargs) | ||
|
||
def execute(self, options, args, tool): | ||
pass | ||
|
||
|
||
class DummyTool(object): | ||
def name(self): | ||
return "dummy-tool" | ||
|
||
|
||
class CommandTest(unittest.TestCase): | ||
|
||
def test_name_with_arguments_with_two_args(self): | ||
class TrivialCommandWithArgs(TrivialCommand): | ||
argument_names = "ARG1 ARG2" | ||
command = TrivialCommandWithArgs() | ||
self.assertEqual(command.name_with_arguments(), "trivial ARG1 ARG2") | ||
|
||
def test_name_with_arguments_with_options(self): | ||
command = TrivialCommand(options=[optparse.make_option("--my_option")]) | ||
self.assertEqual(command.name_with_arguments(), "trivial [options]") | ||
|
||
def test_parse_required_arguments(self): | ||
self.assertEqual(Command._parse_required_arguments("ARG1 ARG2"), ["ARG1", "ARG2"]) | ||
self.assertEqual(Command._parse_required_arguments("[ARG1] [ARG2]"), []) | ||
self.assertEqual(Command._parse_required_arguments("[ARG1] ARG2"), ["ARG2"]) | ||
# Note: We might make our arg parsing smarter in the future and allow this type of arguments string. | ||
self.assertRaises(Exception, Command._parse_required_arguments, "[ARG1 ARG2]") | ||
|
||
def test_required_arguments(self): | ||
class TrivialCommandWithRequiredAndOptionalArgs(TrivialCommand): | ||
argument_names = "ARG1 ARG2 [ARG3]" | ||
two_required_arguments = TrivialCommandWithRequiredAndOptionalArgs() | ||
expected_logs = ("2 arguments required, 1 argument provided. Provided: 'foo' Required: ARG1 ARG2\n" | ||
"See 'dummy-tool help trivial' for usage.\n") | ||
exit_code = OutputCapture().assert_outputs(self, two_required_arguments.check_arguments_and_execute, | ||
[None, ["foo"], DummyTool()], expected_logs=expected_logs) | ||
self.assertEqual(exit_code, 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
third_party/WebKit/Tools/Scripts/webkitpy/tool/commands/helpcommand.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Copyright (c) 2016 Google Inc. All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are | ||
# met: | ||
# | ||
# * Redistributions of source code must retain the above copyright | ||
# notice, this list of conditions and the following disclaimer. | ||
# * Redistributions in binary form must reproduce the above | ||
# copyright notice, this list of conditions and the following disclaimer | ||
# in the documentation and/or other materials provided with the | ||
# distribution. | ||
# * Neither the name of Google Inc. nor the names of its | ||
# contributors may be used to endorse or promote products derived from | ||
# this software without specific prior written permission. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
# 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. | ||
|
||
import optparse | ||
|
||
from webkitpy.tool.commands.command import Command | ||
|
||
|
||
class HelpCommand(Command): | ||
name = "help" | ||
help_text = "Display information about this program or its subcommands" | ||
argument_names = "[COMMAND]" | ||
|
||
def __init__(self): | ||
options = [ | ||
optparse.make_option("-a", "--all-commands", action="store_true", dest="show_all_commands", help="Print all available commands"), | ||
] | ||
super(HelpCommand, self).__init__(options) | ||
# A hack used to pass --all-commands to _help_epilog even though it's called by the OptionParser. | ||
self.show_all_commands = False | ||
|
||
def _help_epilog(self): | ||
# Only show commands which are relevant to this checkout's SCM system. Might this be confusing to some users? | ||
if self.show_all_commands: | ||
epilog = "All %prog commands:\n" | ||
relevant_commands = self._tool.commands[:] | ||
else: | ||
epilog = "Common %prog commands:\n" | ||
relevant_commands = filter(self._tool.should_show_in_main_help, self._tool.commands) | ||
longest_name_length = max(map(lambda command: len(command.name), relevant_commands)) | ||
relevant_commands.sort(lambda a, b: cmp(a.name, b.name)) | ||
command_help_texts = map(lambda command: " %s %s\n" % ( | ||
command.name.ljust(longest_name_length), command.help_text), relevant_commands) | ||
epilog += "%s\n" % "".join(command_help_texts) | ||
epilog += "See '%prog help --all-commands' to list all commands.\n" | ||
epilog += "See '%prog help COMMAND' for more information on a specific command.\n" | ||
return epilog.replace("%prog", self._tool.name()) # Use of %prog here mimics OptionParser.expand_prog_name(). | ||
|
||
# FIXME: This is a hack so that we don't show --all-commands as a global option: | ||
def _remove_help_options(self): | ||
for option in self.options: | ||
self.option_parser.remove_option(option.get_opt_string()) | ||
|
||
def execute(self, options, args, tool): | ||
if args: | ||
command = self._tool.command_by_name(args[0]) | ||
if command: | ||
# TODO(qyearsley): Replace print with _log.info | ||
print command.standalone_help() # pylint: disable=print-statement | ||
return 0 | ||
|
||
self.show_all_commands = options.show_all_commands | ||
self._remove_help_options() | ||
self.option_parser.print_help() | ||
return 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.