When aborting, make sure the reason is available

This allows a GUI to display the reason of abortion.
This commit is contained in:
Sybren A. Stüvel 2019-02-28 12:28:57 +01:00
parent 6104bd6078
commit f34bf7c66f
3 changed files with 17 additions and 4 deletions

View File

@ -110,6 +110,7 @@ class Packer:
self.relative_only = relative_only self.relative_only = relative_only
self._aborted = threading.Event() self._aborted = threading.Event()
self._abort_lock = threading.RLock() self._abort_lock = threading.RLock()
self._abort_reason = ''
# Set this to a custom Callback() subclass instance before calling # Set this to a custom Callback() subclass instance before calling
# strategise() to receive progress reports. # strategise() to receive progress reports.
@ -176,7 +177,7 @@ class Packer:
self._progress_cb = new_progress_cb self._progress_cb = new_progress_cb
self._tscb = progress.ThreadSafeCallback(self._progress_cb) self._tscb = progress.ThreadSafeCallback(self._progress_cb)
def abort(self) -> None: def abort(self, reason='') -> None:
"""Aborts the current packing process. """Aborts the current packing process.
Can be called from any thread. Aborts as soon as the running strategise Can be called from any thread. Aborts as soon as the running strategise
@ -184,6 +185,7 @@ class Packer:
an Aborted exception. an Aborted exception.
""" """
with self._abort_lock: with self._abort_lock:
self._abort_reason = reason
if self._file_transferer: if self._file_transferer:
self._file_transferer.abort() self._file_transferer.abort()
self._aborted.set() self._aborted.set()
@ -192,7 +194,7 @@ class Packer:
"""Raises an Aborted exception when abort() was called.""" """Raises an Aborted exception when abort() was called."""
with self._abort_lock: with self._abort_lock:
reason = '' reason = self._abort_reason
if self._file_transferer is not None and self._file_transferer.has_error: if self._file_transferer is not None and self._file_transferer.has_error:
log.error('A transfer error occurred') log.error('A transfer error occurred')
reason = self._file_transferer.error_message() reason = self._file_transferer.error_message()
@ -201,7 +203,7 @@ class Packer:
log.warning('Aborting') log.warning('Aborting')
self._tscb.flush() self._tscb.flush()
self._progress_cb.pack_aborted() self._progress_cb.pack_aborted(reason)
raise Aborted(reason) raise Aborted(reason)
def exclude(self, *globs: str): def exclude(self, *globs: str):

View File

@ -42,7 +42,7 @@ class Callback(blender_asset_tracer.trace.progress.Callback):
missing_files: typing.Set[pathlib.Path]) -> None: missing_files: typing.Set[pathlib.Path]) -> None:
"""Called when packing is done.""" """Called when packing is done."""
def pack_aborted(self): def pack_aborted(self, reason: str):
"""Called when packing was aborted.""" """Called when packing was aborted."""
def trace_blendfile(self, filename: pathlib.Path) -> None: def trace_blendfile(self, filename: pathlib.Path) -> None:
@ -109,6 +109,9 @@ class ThreadSafeCallback(Callback):
missing_files: typing.Set[pathlib.Path]) -> None: missing_files: typing.Set[pathlib.Path]) -> None:
self._queue(self.wrapped.pack_done, output_blendfile, missing_files) self._queue(self.wrapped.pack_done, output_blendfile, missing_files)
def pack_aborted(self, reason: str):
self._queue(self.wrapped.pack_aborted, reason)
def trace_blendfile(self, filename: pathlib.Path) -> None: def trace_blendfile(self, filename: pathlib.Path) -> None:
self._queue(self.wrapped.trace_blendfile, filename) self._queue(self.wrapped.trace_blendfile, filename)

View File

@ -85,3 +85,11 @@ class ShamanPacker(bat_pack.Packer):
checkout_location = pathlib.PurePosixPath(self._checkout_location) checkout_location = pathlib.PurePosixPath(self._checkout_location)
rel_output = self._output_path.relative_to(self._target_path) rel_output = self._output_path.relative_to(self._target_path)
return checkout_location / rel_output return checkout_location / rel_output
def execute(self):
try:
super().execute()
except requests.exceptions.ConnectionError as ex:
log.exception('Error communicating with Shaman')
self.abort(str(ex))
self._check_aborted()