From e8f41f2735642ba1a6843f278a4cb6ad751547f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 9 Mar 2018 10:00:46 +0100 Subject: [PATCH] Store rewrites in AssetAction This is in preparation of interchanging copying and rewriting. --- blender_asset_tracer/pack/__init__.py | 25 ++++++++++++++++++++----- tests/test_pack.py | 17 ++++++++++++----- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/blender_asset_tracer/pack/__init__.py b/blender_asset_tracer/pack/__init__.py index 93e4482..a6c0400 100644 --- a/blender_asset_tracer/pack/__init__.py +++ b/blender_asset_tracer/pack/__init__.py @@ -21,8 +21,21 @@ class PathAction(enum.Enum): class AssetAction: def __init__(self): self.path_action = PathAction.KEEP_PATH - self.usages = [] # which data blocks are referring to this asset + self.usages = [] + """BlockUsage objects referring to this asset. + + Those BlockUsage objects could refer to data blocks in this blend file + (if the asset is a blend file) or in another blend file. + """ + self.new_path = None + """Absolute path to the asset in the BAT Pack.""" + + self.rewrites = [] + """BlockUsage objects in this asset that may require rewriting. + + Empty list if this AssetAction is not for a blend file. + """ class Packer: @@ -43,7 +56,6 @@ class Packer: # Filled by strategise() self._actions = collections.defaultdict(AssetAction) - self._rewrites = collections.defaultdict(list) # Number of files we would copy, if not for --noop self._file_count = 0 @@ -116,7 +128,7 @@ class Packer: for usage in action.usages: bfile_path = usage.block.bfile.filepath.absolute().resolve() - self._rewrites[bfile_path].append(usage) + self._actions[bfile_path].rewrites.append(usage) def _path_in_project(self, path: pathlib.Path) -> bool: try: @@ -157,7 +169,10 @@ class Packer: def _rewrite_paths(self): """Rewrite paths to the new location of the assets.""" - for bfile_path, rewrites in self._rewrites.items(): + for bfile_path, action in self._actions.items(): + if not action.rewrites: + continue + assert isinstance(bfile_path, pathlib.Path) bfile_pp = self._actions[bfile_path].new_path @@ -168,7 +183,7 @@ class Packer: bfile = blendfile.open_cached(bfile_path, assert_cached=True) bfile.rebind(bfile_pp, mode='rb+') - for usage in rewrites: + for usage in action.rewrites: assert isinstance(usage, result.BlockUsage) asset_pp = self._actions[usage.abspath].new_path assert isinstance(asset_pp, pathlib.Path) diff --git a/tests/test_pack.py b/tests/test_pack.py index c5c2dfd..75e3c9e 100644 --- a/tests/test_pack.py +++ b/tests/test_pack.py @@ -24,6 +24,12 @@ class AbstractPackTest(AbstractBlendFileTest): def tearDown(self): self.tdir.cleanup() + @staticmethod + def rewrites(packer: pack.Packer): + return {path: action.rewrites + for path, action in packer._actions.items() + if action.rewrites} + def test_strategise_no_rewrite_required(self): infile = self.blendfiles / 'doubly_linked.blend' @@ -44,7 +50,7 @@ class AbstractPackTest(AbstractBlendFileTest): self.assertEqual(pack.PathAction.KEEP_PATH, act.path_action, 'for %s' % pf) self.assertEqual(self.tpath / pf, act.new_path, 'for %s' % pf) - self.assertEqual({}, packer._rewrites) + self.assertEqual({}, self.rewrites(packer)) def test_strategise_rewrite(self): ppath = self.blendfiles / 'subdir' @@ -78,17 +84,18 @@ class AbstractPackTest(AbstractBlendFileTest): 'material_textures.blend', 'subdir/doubly_linked_up.blend', ) + rewrites = self.rewrites(packer) self.assertEqual([self.blendfiles / fn for fn in to_rewrite], - sorted(packer._rewrites.keys())) + sorted(rewrites.keys())) # Library link referencing basic_file.blend should (maybe) be rewritten. - rw_linked_cube = packer._rewrites[self.blendfiles / 'linked_cube.blend'] + rw_linked_cube = rewrites[self.blendfiles / 'linked_cube.blend'] self.assertEqual(1, len(rw_linked_cube)) self.assertEqual(b'LILib', rw_linked_cube[0].block_name) self.assertEqual(b'//basic_file.blend', rw_linked_cube[0].asset_path) # Texture links to image assets should (maybe) be rewritten. - rw_mattex = packer._rewrites[self.blendfiles / 'material_textures.blend'] + rw_mattex = rewrites[self.blendfiles / 'material_textures.blend'] self.assertEqual(2, len(rw_mattex)) rw_mattex.sort() # for repeatable tests self.assertEqual(b'IMbrick_dotted_04-bump', rw_mattex[0].block_name) @@ -97,7 +104,7 @@ class AbstractPackTest(AbstractBlendFileTest): self.assertEqual(b'//textures/Bricks/brick_dotted_04-color.jpg', rw_mattex[1].asset_path) # Library links from doubly_linked_up.blend to the above to blend files should be rewritten. - rw_dbllink = packer._rewrites[self.blendfiles / 'subdir/doubly_linked_up.blend'] + rw_dbllink = rewrites[self.blendfiles / 'subdir/doubly_linked_up.blend'] self.assertEqual(2, len(rw_dbllink)) rw_dbllink.sort() # for repeatable tests self.assertEqual(b'LILib', rw_dbllink[0].block_name)