diff --git a/blender_asset_tracer/pack/__init__.py b/blender_asset_tracer/pack/__init__.py index f5d4d27..3553983 100644 --- a/blender_asset_tracer/pack/__init__.py +++ b/blender_asset_tracer/pack/__init__.py @@ -403,9 +403,11 @@ class Packer: log.info('Would copy %d files to %s', self._file_count, self.target) return self._file_transferer.done_and_join() + self._on_file_transfer_finished(file_transfer_completed=True) except KeyboardInterrupt: log.info('File transfer interrupted with Ctrl+C, aborting.') self._file_transferer.abort_and_join() + self._on_file_transfer_finished(file_transfer_completed=False) raise finally: self._tscb.flush() @@ -416,6 +418,13 @@ class Packer: # self.abort(). self._file_transferer = None + def _on_file_transfer_finished(self, *, file_transfer_completed: bool) -> None: + """Called when the file transfer is finished. + + This can be used in subclasses to perform cleanup on the file transferer, + or to obtain information from it before we destroy it. + """ + def _rewrite_paths(self) -> None: """Rewrite paths to the new location of the assets. diff --git a/blender_asset_tracer/pack/shaman/__init__.py b/blender_asset_tracer/pack/shaman/__init__.py index a4b14b9..3d4e491 100644 --- a/blender_asset_tracer/pack/shaman/__init__.py +++ b/blender_asset_tracer/pack/shaman/__init__.py @@ -26,6 +26,7 @@ import requests import blender_asset_tracer.pack as bat_pack import blender_asset_tracer.pack.transfer as bat_transfer +from blender_asset_tracer.pack.shaman.transfer import ShamanTransferrer log = logging.getLogger(__name__) @@ -42,11 +43,13 @@ class ShamanPacker(bat_pack.Packer): **kwargs) -> None: """Constructor + :param target: mock target '/' to construct project-relative paths. :param endpoint: URL of the Shaman endpoint. """ super().__init__(bfile, project, target, **kwargs) self.checkout_id = checkout_id self.shaman_endpoint = endpoint + self._checkout_location = '' def _get_auth_token(self) -> str: # TODO: get a token from the Flamenco Server. @@ -55,13 +58,30 @@ class ShamanPacker(bat_pack.Packer): return resp.text def _create_file_transferer(self) -> bat_transfer.FileTransferer: - from . import transfer - # TODO: pass self._get_auth_token itself, so that the Transferer will be able to # decide when to get this token (and how many times). auth_token = self._get_auth_token() - return transfer.ShamanTransferrer(auth_token, self.project, self.shaman_endpoint, - self.checkout_id) + return ShamanTransferrer(auth_token, self.project, self.shaman_endpoint, self.checkout_id) def _make_target_path(self, target: str) -> pathlib.PurePath: return pathlib.PurePosixPath('/') + + def _on_file_transfer_finished(self, *, file_transfer_completed: bool): + super()._on_file_transfer_finished(file_transfer_completed=file_transfer_completed) + + assert isinstance(self._file_transferer, ShamanTransferrer) + self._checkout_location = self._file_transferer.checkout_location + + @property + def checkout_location(self) -> str: + """Return the checkout location of the packed blend file.""" + return self._checkout_location + + @property + def output_path(self) -> pathlib.PurePath: + """The path of the packed blend file in the target directory.""" + assert self._output_path is not None + + checkout_location = pathlib.PurePosixPath(self._checkout_location) + rel_output = self._output_path.relative_to(self._target_path) + return checkout_location / rel_output diff --git a/tests/test_pack_shaman.py b/tests/test_pack_shaman.py new file mode 100644 index 0000000..3668b71 --- /dev/null +++ b/tests/test_pack_shaman.py @@ -0,0 +1,52 @@ +# ***** BEGIN GPL LICENSE BLOCK ***** +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# ***** END GPL LICENCE BLOCK ***** +# +# (c) 2019, Blender Foundation - Sybren A. Stüvel +import pathlib + +import responses + +from test_pack import AbstractPackTest +from blender_asset_tracer.pack import shaman + +httpmock = responses.RequestsMock() + + +class ShamanPackTest(AbstractPackTest): + @httpmock.activate + def test_all_files_already_uploaded(self): + infile = self.blendfiles / 'basic_file_ñønæščii.blend' + + packer = shaman.ShamanPacker(infile, infile.parent, '/', + endpoint='http://shaman.local', + checkout_id='DA-JOBBY-ID') + + # Temporary hack + httpmock.add('GET', 'http://shaman.local/get-token', body='AUTH-TOKEN') + + # Just fake that everything is already available on the server. + httpmock.add('POST', 'http://shaman.local/checkout/requirements', body='') + httpmock.add('POST', 'http://shaman.local/checkout/create/DA-JOBBY-ID', + body='DA/-JOBBY-ID') + + with packer: + packer.strategise() + packer.execute() + + self.assertEqual(pathlib.PurePosixPath('DA/-JOBBY-ID/basic_file_ñønæščii.blend'), + packer.output_path) diff --git a/tests/test_pack_zipped.py b/tests/test_pack_zipped.py index e393aa5..b1682fc 100644 --- a/tests/test_pack_zipped.py +++ b/tests/test_pack_zipped.py @@ -1,3 +1,22 @@ +# ***** BEGIN GPL LICENSE BLOCK ***** +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software Foundation, +# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +# +# ***** END GPL LICENCE BLOCK ***** +# +# (c) 2019, Blender Foundation - Sybren A. Stüvel import zipfile from test_pack import AbstractPackTest diff --git a/tests/test_shaman_transfer.py b/tests/test_shaman_transfer.py index a213685..cdced05 100644 --- a/tests/test_shaman_transfer.py +++ b/tests/test_shaman_transfer.py @@ -17,9 +17,7 @@ # ***** END GPL LICENCE BLOCK ***** # # (c) 2019, Blender Foundation - Sybren A. Stüvel -import json import pathlib -from unittest import mock import responses