Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(110)

Side by Side Diff: third_party/WebKit/Tools/Scripts/webkitpy/tool/multi_command_tool.py

Issue 2110893003: Merge webkit_patch and multi_command_tool. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sort imports; remove out-of-date comment. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 # Copyright (c) 2009 Google Inc. All rights reserved.
2 # Copyright (c) 2009 Apple Inc. All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions are
6 # met:
7 #
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above
11 # copyright notice, this list of conditions and the following disclaimer
12 # in the documentation and/or other materials provided with the
13 # distribution.
14 # * Neither the name of Google Inc. nor the names of its
15 # contributors may be used to endorse or promote products derived from
16 # this software without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #
30 # MultiCommandTool provides a framework for writing svn-like/git-like tools
31 # which are called with the following format:
32 # tool-name [global options] command-name [command options]
33
34 import optparse
35 import logging
36 import sys
37
38 from webkitpy.tool.commands.command import HelpPrintingOptionParser
39 from webkitpy.tool.commands.help_command import HelpCommand
40
41 _log = logging.getLogger(__name__)
42
43
44 class MultiCommandTool(object):
45
46 global_options = None
47
48 def __init__(self, commands):
49 self.commands = commands
50 self.help_command = self.command_by_name(HelpCommand.name)
51 # Require the help command, even if the manual test list doesn't include one.
52 if not self.help_command:
53 self.help_command = HelpCommand()
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.
57 for command in self.commands:
58 command.bind_to_tool(self)
59
60 def name(self):
61 return optparse.OptionParser().get_prog_name()
62
63 def _create_option_parser(self):
64 usage = "Usage: %prog [options] COMMAND [ARGS]"
65 return HelpPrintingOptionParser(epilog_method=self.help_command._help_ep ilog, prog=self.name(), usage=usage)
66
67 @staticmethod
68 def _split_command_name_from_args(args):
69 # Assume the first argument which doesn't start with "-" is the command name.
70 command_index = 0
71 for arg in args:
72 if arg[0] != "-":
73 break
74 command_index += 1
75 else:
76 return (None, args[:])
77
78 command = args[command_index]
79 return (command, args[:command_index] + args[command_index + 1:])
80
81 def command_by_name(self, command_name):
82 for command in self.commands:
83 if command_name == command.name:
84 return command
85 return None
86
87 def path(self):
88 raise NotImplementedError("subclasses must implement")
89
90 def command_completed(self):
91 pass
92
93 def should_show_in_main_help(self, command):
94 return command.show_in_main_help
95
96 def should_execute_command(self, command): # Argument may be used in subcla sses - pylint: disable=unused-argument
97 return (True, None)
98
99 def _add_global_options(self, option_parser):
100 global_options = self.global_options or []
101 for option in global_options:
102 option_parser.add_option(option)
103
104 def handle_global_options(self, options):
105 pass
106
107 def main(self, argv=None):
108 argv = argv or sys.argv
109 (command_name, args) = self._split_command_name_from_args(argv[1:])
110
111 option_parser = self._create_option_parser()
112 self._add_global_options(option_parser)
113
114 command = self.command_by_name(command_name) or self.help_command
115 if not command:
116 option_parser.error("%s is not a recognized command" % command_name)
117
118 command.set_option_parser(option_parser)
119 (options, args) = command.parse_args(args)
120 self.handle_global_options(options)
121
122 (should_execute, failure_reason) = self.should_execute_command(command)
123 if not should_execute:
124 _log.error(failure_reason)
125 return 0 # FIXME: Should this really be 0?
126
127 result = command.check_arguments_and_execute(options, args, self)
128
129 self.command_completed()
130 return result
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698