OLD | NEW |
---|---|
1 # Copyright 2013 The Chromium Authors. All rights reserved. | 1 # Copyright 2013 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Various utility functions and classes not specific to any single area.""" | 5 """Various utility functions and classes not specific to any single area.""" |
6 | 6 |
7 import logging | 7 import logging |
8 import logging.handlers | 8 import logging.handlers |
9 import optparse | 9 import optparse |
10 import os | 10 import os |
11 import sys | 11 import sys |
12 import time | 12 import time |
13 import traceback | |
13 | 14 |
14 | 15 |
15 class OptionParserWithLogging(optparse.OptionParser): | 16 class OptionParserWithLogging(optparse.OptionParser): |
16 """Adds --verbose option.""" | 17 """Adds --verbose option.""" |
17 | 18 |
18 # Set to True to enable --log-file options. | 19 # Set to True to enable --log-file options. |
19 enable_log_file = True | 20 enable_log_file = True |
20 | 21 |
21 def __init__(self, verbose=0, log_file=None, **kwargs): | 22 def __init__(self, verbose=0, log_file=None, **kwargs): |
22 kwargs.setdefault('description', sys.modules['__main__'].__doc__) | 23 kwargs.setdefault('description', sys.modules['__main__'].__doc__) |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 | 106 |
106 | 107 |
107 def fix_python_path(cmd): | 108 def fix_python_path(cmd): |
108 """Returns the fixed command line to call the right python executable.""" | 109 """Returns the fixed command line to call the right python executable.""" |
109 out = cmd[:] | 110 out = cmd[:] |
110 if out[0] == 'python': | 111 if out[0] == 'python': |
111 out[0] = sys.executable | 112 out[0] = sys.executable |
112 elif out[0].endswith('.py'): | 113 elif out[0].endswith('.py'): |
113 out.insert(0, sys.executable) | 114 out.insert(0, sys.executable) |
114 return out | 115 return out |
116 | |
117 | |
118 def report_error(error): | |
119 """Prints a error to stderr, wrapping it into header and footer. | |
120 | |
121 That way errors can be reliably extracted from logs. It's indented to be used | |
122 only for non recoverable unexpected errors. Is should NOT be used for input | |
123 validation, command line argument errors, etc. | |
124 | |
125 Arguments: | |
126 error: error message string (possibly multiple lines) or an instance of | |
127 Exception subclass. In the later case a traceback will also be | |
128 reported. It's assumed that |report_error| is called in an except | |
129 block where |error| was caught. | |
130 """ | |
131 print >> sys.stderr, '[------ Swarming Error ------]' | |
132 print >> sys.stderr, str(error) | |
M-A Ruel
2013/10/30 01:08:09
Note that this will crash if it is an unicode stri
| |
133 if isinstance(error, Exception): | |
134 print >> sys.stderr, traceback.format_exc(), | |
135 print >> sys.stderr, '[----------------------------]' | |
OLD | NEW |