2014-11-14 00:41:17 +00:00
|
|
|
from data.database import BUILD_PHASE
|
|
|
|
|
|
|
|
class StatusHandler(object):
|
|
|
|
""" Context wrapper for writing status to build logs. """
|
|
|
|
|
|
|
|
def __init__(self, build_logs, repository_build):
|
|
|
|
self._current_phase = None
|
|
|
|
self._repository_build = repository_build
|
|
|
|
self._uuid = repository_build.uuid
|
|
|
|
self._build_logs = build_logs
|
|
|
|
|
|
|
|
self._status = {
|
2014-11-18 20:45:56 +00:00
|
|
|
'total_commands': None,
|
|
|
|
'current_command': None,
|
|
|
|
'push_completion': 0.0,
|
|
|
|
'pull_completion': 0.0,
|
2014-11-14 00:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Write the initial status.
|
|
|
|
self.__exit__(None, None, None)
|
|
|
|
|
|
|
|
def _append_log_message(self, log_message, log_type=None, log_data=None):
|
|
|
|
self._build_logs.append_log_message(self._uuid, log_message, log_type, log_data)
|
|
|
|
|
|
|
|
def append_log(self, log_message, extra_data=None):
|
|
|
|
self._append_log_message(log_message, log_data=extra_data)
|
|
|
|
|
|
|
|
def set_command(self, command, extra_data=None):
|
|
|
|
self._append_log_message(command, self._build_logs.COMMAND, extra_data)
|
|
|
|
|
|
|
|
def set_error(self, error_message, extra_data=None):
|
|
|
|
self.set_phase(BUILD_PHASE.ERROR)
|
|
|
|
self._append_log_message(error_message, self._build_logs.ERROR, extra_data)
|
|
|
|
|
|
|
|
def set_phase(self, phase, extra_data=None):
|
|
|
|
if phase == self._current_phase:
|
2014-11-18 21:34:09 +00:00
|
|
|
return False
|
2014-11-18 20:45:56 +00:00
|
|
|
|
2014-11-14 00:41:17 +00:00
|
|
|
self._current_phase = phase
|
|
|
|
self._append_log_message(phase, self._build_logs.PHASE, extra_data)
|
|
|
|
self._repository_build.phase = phase
|
|
|
|
self._repository_build.save()
|
2014-11-18 21:34:09 +00:00
|
|
|
return True
|
2014-11-14 00:41:17 +00:00
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
return self._status
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, value, traceback):
|
2014-11-18 20:45:56 +00:00
|
|
|
self._build_logs.set_status(self._uuid, self._status)
|