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=None, values=None): | 37 def new_parse_args(args): |
38 options, args = old_parse_args(args, values) | 38 options, args = old_parse_args(args) |
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 class OptionParser(optparse.OptionParser): | 106 def gen_parser(): |
107 """An OptionParser instance with default options. | 107 """Returns 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 def __init__(self, *args, **kwargs): | 111 parser = optparse.OptionParser(version=__version__) |
112 optparse.OptionParser.__init__(self, *args, **kwargs) | 112 # Remove description formatting |
113 self.add_option( | 113 parser.format_description = ( |
114 '-v', '--verbose', action='count', default=0, | 114 lambda _: parser.description) # pylint: disable=E1101 |
115 help='Use multiple times to increase logging level') | 115 # Add common parsing. |
| 116 old_parser_args = parser.parse_args |
116 | 117 |
117 def parse_args(self, args=None, values=None): | 118 def Parse(*args, **kwargs): |
118 options, args = optparse.OptionParser.parse_args(self, args, values) | 119 options, args = old_parser_args(*args, **kwargs) |
119 levels = [logging.WARNING, logging.INFO, logging.DEBUG] | |
120 logging.basicConfig( | 120 logging.basicConfig( |
121 level=levels[min(len(levels) - 1, options.verbose)], | 121 level=[logging.WARNING, logging.INFO, logging.DEBUG][ |
| 122 min(2, options.verbose)], |
122 format='%(levelname)s %(filename)s(%(lineno)d): %(message)s') | 123 format='%(levelname)s %(filename)s(%(lineno)d): %(message)s') |
123 return options, args | 124 return options, args |
124 | 125 |
125 def format_description(self, _): | 126 parser.parse_args = Parse |
126 """Removes description formatting.""" | 127 |
127 return self.description.rstrip() + '\n' | 128 parser.add_option( |
| 129 '-v', '--verbose', action='count', default=0, |
| 130 help='Use multiple times to increase logging level') |
| 131 return parser |
128 | 132 |
129 | 133 |
130 def Command(name): | 134 def Command(name): |
131 return getattr(sys.modules[__name__], 'CMD' + name, None) | 135 return getattr(sys.modules[__name__], 'CMD' + name, None) |
132 | 136 |
133 | 137 |
134 @usage('<command>') | 138 @usage('<command>') |
135 def CMDhelp(parser, args): | 139 def CMDhelp(parser, args): |
136 """Print list of commands or use 'help <command>'.""" | 140 """Print list of commands or use 'help <command>'.""" |
137 # Strip out the help command description and replace it with the module | 141 # Strip out the help command description and replace it with the module |
(...skipping 19 matching lines...) Expand all Loading... |
157 obj = Command(command) | 161 obj = Command(command) |
158 more = getattr(obj, 'func_usage_more') | 162 more = getattr(obj, 'func_usage_more') |
159 # OptParser.description prefer nicely non-formatted strings. | 163 # OptParser.description prefer nicely non-formatted strings. |
160 parser.description = obj.__doc__ + '\n' | 164 parser.description = obj.__doc__ + '\n' |
161 parser.set_usage('usage: %%prog %s %s' % (command, more)) | 165 parser.set_usage('usage: %%prog %s %s' % (command, more)) |
162 | 166 |
163 | 167 |
164 def main(args=None): | 168 def main(args=None): |
165 # Do it late so all commands are listed. | 169 # Do it late so all commands are listed. |
166 # pylint: disable=E1101 | 170 # pylint: disable=E1101 |
167 parser = OptionParser(version=__version__) | 171 parser = gen_parser() |
168 if args is None: | 172 if args is None: |
169 args = sys.argv[1:] | 173 args = sys.argv[1:] |
170 if args: | 174 if args: |
171 command = Command(args[0]) | 175 command = Command(args[0]) |
172 if command: | 176 if command: |
173 # "fix" the usage and the description now that we know the subcommand. | 177 # "fix" the usage and the description now that we know the subcommand. |
174 gen_usage(parser, args[0]) | 178 gen_usage(parser, args[0]) |
175 return command(parser, args[1:]) | 179 return command(parser, args[1:]) |
176 | 180 |
177 # Not a known command. Default to help. | 181 # Not a known command. Default to help. |
178 gen_usage(parser, 'help') | 182 gen_usage(parser, 'help') |
179 return CMDhelp(parser, args) | 183 return CMDhelp(parser, args) |
180 | 184 |
181 | 185 |
182 if __name__ == "__main__": | 186 if __name__ == "__main__": |
183 fix_encoding.fix_encoding() | 187 fix_encoding.fix_encoding() |
184 sys.exit(main()) | 188 sys.exit(main()) |
OLD | NEW |