Store rewrites in AssetAction
This is in preparation of interchanging copying and rewriting.
This commit is contained in:
parent
d984870155
commit
e8f41f2735
@ -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)
|
||||
|
||||
@ -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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user