| 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(message, include_traceback=False): |
| 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 message: error message string, possibly multiple lines. |
| 127 include_traceback: True to append traceback of current exception. |
| 128 """ |
| 129 print >> sys.stderr, '[------ Swarming Error ------]' |
| 130 print >> sys.stderr, message |
| 131 if include_traceback: |
| 132 print >> sys.stderr, traceback.format_exc(), |
| 133 print >> sys.stderr, '[----------------------------]' |
| OLD | NEW |