/usr/share/pyshared/doit/reporter.py is in python-doit 0.24.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | """Reports doit execution status/results"""
import sys
import time
import datetime
import six
from six import StringIO
from .compat import json
class ConsoleReporter(object):
"""Default reporter. print results on console/terminal (stdout/stderr)
@ivar show_out (bool): include captured stdout on failure report
@ivar show_err (bool): include captured stderr on failure report
"""
def __init__(self, outstream, options):
# save non-succesful result information (include task errors)
self.failures = []
self.runtime_errors = []
self.show_out = options.get('show_out', True)
self.show_err = options.get('show_err', True)
self.outstream = outstream
def write(self, text):
self.outstream.write(text)
def initialize(self, tasks):
"""called just after tasks have benn loaded before execution starts"""
pass
def get_status(self, task):
"""called when task is selected (check if up-to-date)"""
pass
def execute_task(self, task):
"""called when excution starts"""
# ignore tasks that do not define actions
# ignore private/hidden tasks (tasks that start with an underscore)
if task.actions and (task.name[0] != '_'):
self.write('. %s\n' % task.title())
def add_failure(self, task, exception):
"""called when excution finishes with a failure"""
self.failures.append({'task': task, 'exception':exception})
def add_success(self, task):
"""called when excution finishes successfuly"""
pass
def skip_uptodate(self, task):
"""skipped up-to-date task"""
self.write("-- %s\n" % task.title())
def skip_ignore(self, task):
"""skipped ignored task"""
self.write("!! %s\n" % task.title())
def cleanup_error(self, exception):
"""error during cleanup"""
sys.stderr.write(exception.get_msg())
def runtime_error(self, msg):
"""error from doit (not from a task execution)"""
# saved so they are displayed after task failures messages
self.runtime_errors.append(msg)
def teardown_task(self, task):
"""called when starts the execution of teardown action"""
pass
def complete_run(self):
"""called when finshed running all tasks"""
# if test fails print output from failed task
for result in self.failures:
self.write("#"*40 + "\n")
msg = '%s - taskid:%s\n' % (result['exception'].get_name(),
result['task'].name)
self.write(msg)
self.write(result['exception'].get_msg())
self.write("\n")
task = result['task']
if self.show_out:
out = "".join([a.out for a in task.actions if a.out])
self.write("%s\n" % out)
if self.show_err:
err = "".join([a.err for a in task.actions if a.err])
self.write("%s\n" % err)
if self.runtime_errors:
self.write("#"*40 + "\n")
self.write("Execution aborted.\n")
self.write("\n".join(self.runtime_errors))
self.write("\n")
class ExecutedOnlyReporter(ConsoleReporter):
"""No output for skipped (up-to-date) and group tasks
Produces zero output unless a task is executed
"""
def skip_uptodate(self, task):
"""skipped up-to-date task"""
pass
def skip_ignore(self, task):
"""skipped ignored task"""
pass
class ZeroReporter(ConsoleReporter):
"""Report only internal errors from doit"""
def _just_pass(self, *args):
"""over-write base to do nothing"""
pass
get_status = execute_task = add_failure = add_success \
= skip_uptodate = skip_ignore = teardown_task = complete_run \
= _just_pass
def runtime_error(self, msg):
sys.stderr.write(msg)
class TaskResult(object):
"""result object used by JsonReporter"""
# FIXME what about returned value from python-actions ?
def __init__(self, task):
self.task = task
self.result = None # fail, success, up-to-date, ignore
self.out = None # stdout from task
self.err = None # stderr from task
self.error = None # error from doit (exception traceback)
self.started = None # datetime when task execution started
self.elapsed = None # time (in secs) taken to execute task
self._started_on = None # timestamp
self._finished_on = None # timestamp
def start(self):
"""called when task starts its execution"""
self._started_on = time.time()
def set_result(self, result, error=None):
"""called when task finishes its execution"""
self._finished_on = time.time()
self.result = result
line_sep = "\n<------------------------------------------------>\n"
self.out = line_sep.join([a.out for a in self.task.actions if a.out])
self.err = line_sep.join([a.err for a in self.task.actions if a.err])
self.error = error
def to_dict(self):
"""convert result data to dictionary"""
if self._started_on is not None:
started = datetime.datetime.utcfromtimestamp(self._started_on)
self.started = str(started)
self.elapsed = self._finished_on - self._started_on
return {'name': self.task.name,
'result': self.result,
'out': self.out,
'err': self.err,
'error': self.error,
'started': self.started,
'elapsed': self.elapsed}
class JsonReporter(object):
"""output results in JSON format
- out (str)
- err (str)
- tasks (list - dict):
- name (str)
- result (str)
- out (str)
- err (str)
- error (str)
- started (str)
- elapsed (float)
"""
def __init__(self, outstream, options=None): #pylint: disable=W0613
# options parameter is not used
# json result is sent to stdout when doit finishes running
self.t_results = {}
# when using json reporter output can not contain any other output
# than the json data. so anything that is sent to stdout/err needs to
# be captured.
self._old_out = sys.stdout
sys.stdout = StringIO()
self._old_err = sys.stderr
sys.stderr = StringIO()
self.outstream = outstream
# runtime and cleanup errors
self.errors = []
def get_status(self, task):
"""called when task is selected (check if up-to-date)"""
self.t_results[task.name] = TaskResult(task)
def execute_task(self, task):
"""called when excution starts"""
self.t_results[task.name].start()
def add_failure(self, task, exception):
"""called when excution finishes with a failure"""
self.t_results[task.name].set_result('fail', exception.get_msg())
def add_success(self, task):
"""called when excution finishes successfuly"""
self.t_results[task.name].set_result('success')
def skip_uptodate(self, task):
"""skipped up-to-date task"""
self.t_results[task.name].set_result('up-to-date')
def skip_ignore(self, task):
"""skipped ignored task"""
self.t_results[task.name].set_result('ignore')
def cleanup_error(self, exception):
"""error during cleanup"""
self.errors.append(exception.get_msg())
def runtime_error(self, msg):
"""error from doit (not from a task execution)"""
self.errors.append(msg)
def teardown_task(self, task):
"""called when starts the execution of teardown action"""
pass
def complete_run(self):
"""called when finshed running all tasks"""
# restore stdout
log_out = sys.stdout.getvalue()
sys.stdout = self._old_out
log_err = sys.stderr.getvalue()
sys.stderr = self._old_err
# add errors together with stderr output
if self.errors:
log_err += "\n".join(self.errors)
task_result_list = [tr.to_dict() for tr in six.itervalues(self.t_results)]
json_data = {'tasks': task_result_list,
'out': log_out,
'err': log_err}
# indent not available on simplejson 1.3 (debian etch)
# json.dump(json_data, sys.stdout, indent=4)
json.dump(json_data, self.outstream)
# name of reporters class available to be selected on cmd line
REPORTERS = {'default': ConsoleReporter,
'executed-only': ExecutedOnlyReporter,
'json': JsonReporter,
'zero': ZeroReporter,
}
|