OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Access the commit queue from the command line. | 6 """Access the commit queue from the command line. |
7 """ | 7 """ |
8 | 8 |
9 __version__ = '0.1' | 9 __version__ = '0.1' |
10 | 10 |
(...skipping 16 matching lines...) Expand all Loading... |
27 return fn | 27 return fn |
28 return hook | 28 return hook |
29 | 29 |
30 | 30 |
31 def need_issue(fn): | 31 def need_issue(fn): |
32 """Post-parse args to create a Rietveld object.""" | 32 """Post-parse args to create a Rietveld object.""" |
33 @functools.wraps(fn) | 33 @functools.wraps(fn) |
34 def hook(parser, args, *extra_args, **kwargs): | 34 def hook(parser, args, *extra_args, **kwargs): |
35 old_parse_args = parser.parse_args | 35 old_parse_args = parser.parse_args |
36 | 36 |
37 def new_parse_args(args): | 37 def new_parse_args(args=None, values=None): |
38 options, args = old_parse_args(args) | 38 options, args = old_parse_args(args, values) |
39 if not options.issue: | 39 if not options.issue: |
40 parser.error('Require --issue') | 40 parser.error('Require --issue') |
41 obj = rietveld.Rietveld(options.server, options.user, None) | 41 obj = rietveld.Rietveld(options.server, options.user, None) |
42 return options, args, obj | 42 return options, args, obj |
43 | 43 |
44 parser.parse_args = new_parse_args | 44 parser.parse_args = new_parse_args |
45 | 45 |
46 parser.add_option( | 46 parser.add_option( |
47 '-u', '--user', | 47 '-u', '--user', |
48 metavar='U', | 48 metavar='U', |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 options, args, obj = parser.parse_args(args) | 96 options, args, obj = parser.parse_args(args) |
97 if args: | 97 if args: |
98 parser.error('Unrecognized args: %s' % ' '.join(args)) | 98 parser.error('Unrecognized args: %s' % ' '.join(args)) |
99 return set_commit(obj, options.issue, '0') | 99 return set_commit(obj, options.issue, '0') |
100 | 100 |
101 | 101 |
102 ############################################################################### | 102 ############################################################################### |
103 ## Boilerplate code | 103 ## Boilerplate code |
104 | 104 |
105 | 105 |
106 def gen_parser(): | 106 class OptionParser(optparse.OptionParser): |
107 """Returns an OptionParser instance with default options. | 107 """An OptionParser instance with default options. |
108 | 108 |
109 It should be then processed with gen_usage() before being used. | 109 It should be then processed with gen_usage() before being used. |
110 """ | 110 """ |
111 parser = optparse.OptionParser(version=__version__) | 111 def __init__(self, *args, **kwargs): |
112 # Remove description formatting | 112 optparse.OptionParser.__init__(self, *args, **kwargs) |
113 parser.format_description = ( | 113 self.add_option( |
114 lambda _: parser.description) # pylint: disable=E1101 | 114 '-v', '--verbose', action='count', default=0, |
115 # Add common parsing. | 115 help='Use multiple times to increase logging level') |
116 old_parser_args = parser.parse_args | |
117 | 116 |
118 def Parse(*args, **kwargs): | 117 def parse_args(self, args=None, values=None): |
119 options, args = old_parser_args(*args, **kwargs) | 118 options, args = optparse.OptionParser.parse_args(self, args, values) |
| 119 levels = [logging.WARNING, logging.INFO, logging.DEBUG] |
120 logging.basicConfig( | 120 logging.basicConfig( |
121 level=[logging.WARNING, logging.INFO, logging.DEBUG][ | 121 level=levels[min(len(levels) - 1, options.verbose)], |
122 min(2, options.verbose)], | |
123 format='%(levelname)s %(filename)s(%(lineno)d): %(message)s') | 122 format='%(levelname)s %(filename)s(%(lineno)d): %(message)s') |
124 return options, args | 123 return options, args |
125 | 124 |
126 parser.parse_args = Parse | 125 def format_description(self, _): |
127 | 126 """Removes description formatting.""" |
128 parser.add_option( | 127 return self.description.rstrip() + '\n' |
129 '-v', '--verbose', action='count', default=0, | |
130 help='Use multiple times to increase logging level') | |
131 return parser | |
132 | 128 |
133 | 129 |
134 def Command(name): | 130 def Command(name): |
135 return getattr(sys.modules[__name__], 'CMD' + name, None) | 131 return getattr(sys.modules[__name__], 'CMD' + name, None) |
136 | 132 |
137 | 133 |
138 @usage('<command>') | 134 @usage('<command>') |
139 def CMDhelp(parser, args): | 135 def CMDhelp(parser, args): |
140 """Print list of commands or use 'help <command>'.""" | 136 """Print list of commands or use 'help <command>'.""" |
141 # Strip out the help command description and replace it with the module | 137 # Strip out the help command description and replace it with the module |
(...skipping 19 matching lines...) Expand all Loading... |
161 obj = Command(command) | 157 obj = Command(command) |
162 more = getattr(obj, 'func_usage_more') | 158 more = getattr(obj, 'func_usage_more') |
163 # OptParser.description prefer nicely non-formatted strings. | 159 # OptParser.description prefer nicely non-formatted strings. |
164 parser.description = obj.__doc__ + '\n' | 160 parser.description = obj.__doc__ + '\n' |
165 parser.set_usage('usage: %%prog %s %s' % (command, more)) | 161 parser.set_usage('usage: %%prog %s %s' % (command, more)) |
166 | 162 |
167 | 163 |
168 def main(args=None): | 164 def main(args=None): |
169 # Do it late so all commands are listed. | 165 # Do it late so all commands are listed. |
170 # pylint: disable=E1101 | 166 # pylint: disable=E1101 |
171 parser = gen_parser() | 167 parser = OptionParser(version=__version__) |
172 if args is None: | 168 if args is None: |
173 args = sys.argv[1:] | 169 args = sys.argv[1:] |
174 if args: | 170 if args: |
175 command = Command(args[0]) | 171 command = Command(args[0]) |
176 if command: | 172 if command: |
177 # "fix" the usage and the description now that we know the subcommand. | 173 # "fix" the usage and the description now that we know the subcommand. |
178 gen_usage(parser, args[0]) | 174 gen_usage(parser, args[0]) |
179 return command(parser, args[1:]) | 175 return command(parser, args[1:]) |
180 | 176 |
181 # Not a known command. Default to help. | 177 # Not a known command. Default to help. |
182 gen_usage(parser, 'help') | 178 gen_usage(parser, 'help') |
183 return CMDhelp(parser, args) | 179 return CMDhelp(parser, args) |
184 | 180 |
185 | 181 |
186 if __name__ == "__main__": | 182 if __name__ == "__main__": |
187 fix_encoding.fix_encoding() | 183 fix_encoding.fix_encoding() |
188 sys.exit(main()) | 184 sys.exit(main()) |
OLD | NEW |