Allow aborting a running BAT Pack operation
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import threading
|
||||
import typing
|
||||
import pathlib
|
||||
|
||||
@@ -14,6 +15,9 @@ import bpy
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
_running_packer = None # type: pack.Packer
|
||||
_packer_lock = threading.RLock()
|
||||
|
||||
|
||||
class BatProgress(progress.Callback):
|
||||
"""Report progress of BAT Packing to the UI.
|
||||
@@ -30,10 +34,20 @@ class BatProgress(progress.Callback):
|
||||
|
||||
def txt(self, msg: str):
|
||||
"""Set a text in a thread-safe way."""
|
||||
|
||||
async def set_text():
|
||||
self.wm.flamenco_status_txt = msg
|
||||
|
||||
asyncio.run_coroutine_threadsafe(set_text(), loop=self.loop)
|
||||
|
||||
def status(self, status: str):
|
||||
"""Set the flamenco_status property in a thread-safe way."""
|
||||
|
||||
async def set_status():
|
||||
self.wm.flamenco_status = status
|
||||
|
||||
asyncio.run_coroutine_threadsafe(set_status(), loop=self.loop)
|
||||
|
||||
def pack_start(self) -> None:
|
||||
self.txt('Starting BAT Pack operation')
|
||||
|
||||
@@ -45,6 +59,10 @@ class BatProgress(progress.Callback):
|
||||
else:
|
||||
self.txt('Pack of %s done' % output_blendfile.name)
|
||||
|
||||
def pack_aborted(self):
|
||||
self.txt('Aborted')
|
||||
self.status('ABORTED')
|
||||
|
||||
def trace_blendfile(self, filename: pathlib.Path) -> None:
|
||||
"""Called for every blendfile opened when tracing dependencies."""
|
||||
self.txt('Inspecting %s' % filename.name)
|
||||
@@ -71,26 +89,29 @@ class BatProgress(progress.Callback):
|
||||
pass
|
||||
|
||||
|
||||
async def bat_copy(context,
|
||||
base_blendfile: pathlib.Path,
|
||||
project: pathlib.Path,
|
||||
target: pathlib.Path,
|
||||
exclusion_filter: str) -> typing.Tuple[pathlib.Path, typing.Set[pathlib.Path]]:
|
||||
async def copy(context,
|
||||
base_blendfile: pathlib.Path,
|
||||
project: pathlib.Path,
|
||||
target: pathlib.Path,
|
||||
exclusion_filter: str) -> typing.Tuple[pathlib.Path, typing.Set[pathlib.Path]]:
|
||||
"""Use BAT🦇 to copy the given file and dependencies to the target location.
|
||||
|
||||
:raises: FileTransferError if a file couldn't be transferred.
|
||||
:returns: the path of the packed blend file, and a set of missing sources.
|
||||
"""
|
||||
global _running_packer
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
wm = bpy.context.window_manager
|
||||
|
||||
with pack.Packer(base_blendfile, project, target) as packer:
|
||||
if exclusion_filter:
|
||||
packer.exclude(*exclusion_filter.split())
|
||||
with _packer_lock:
|
||||
if exclusion_filter:
|
||||
packer.exclude(*exclusion_filter.split())
|
||||
|
||||
packer.progress_cb = BatProgress(context)
|
||||
packer.progress_cb = BatProgress(context)
|
||||
_running_packer = packer
|
||||
|
||||
log.debug('awaiting strategise')
|
||||
wm.flamenco_status = 'INVESTIGATING'
|
||||
@@ -103,4 +124,21 @@ async def bat_copy(context,
|
||||
log.debug('done')
|
||||
wm.flamenco_status = 'DONE'
|
||||
|
||||
with _packer_lock:
|
||||
_running_packer = None
|
||||
|
||||
return packer.output_path, packer.missing_files
|
||||
|
||||
|
||||
def abort() -> None:
|
||||
"""Abort a running copy() call.
|
||||
|
||||
No-op when there is no running copy(). Can be called from any thread.
|
||||
"""
|
||||
|
||||
with _packer_lock:
|
||||
if _running_packer is None:
|
||||
log.debug('No running packer, ignoring call to bat_abort()')
|
||||
return
|
||||
log.info('Aborting running packer')
|
||||
_running_packer.abort()
|
||||
|
Reference in New Issue
Block a user