From 5635895d0ce6f3d7a8edfac7b71a86f570fc25c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Thu, 20 Dec 2018 15:15:49 +0100 Subject: [PATCH] Attempt at making BAT work on Windows All BlendPath instances will use forward slashes, and there should be more use of PurePosixPath instead of Path. --- blender_asset_tracer/bpathlib.py | 12 ++++++------ blender_asset_tracer/trace/result.py | 2 +- tests/test_bpathlib.py | 7 ++++--- tests/test_pack.py | 7 ++++--- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/blender_asset_tracer/bpathlib.py b/blender_asset_tracer/bpathlib.py index 7de4e31..3e3f592 100644 --- a/blender_asset_tracer/bpathlib.py +++ b/blender_asset_tracer/bpathlib.py @@ -33,12 +33,12 @@ class BlendPath(bytes): """A path within Blender is always stored as bytes.""" def __new__(cls, path): - if isinstance(path, pathlib.Path): + if isinstance(path, pathlib.PurePath): path = str(path).encode('utf-8') if not isinstance(path, bytes): raise TypeError('path must be bytes or pathlib.Path, but is %r' % path) - return super().__new__(cls, path) + return super().__new__(cls, path.replace(b'\\', b'/')) @classmethod def mkrelative(cls, asset_path: pathlib.Path, bfile_path: pathlib.Path) -> 'BlendPath': @@ -57,7 +57,7 @@ class BlendPath(bytes): bdir_parts.popleft() asset_parts.popleft() - rel_asset = pathlib.Path(*asset_parts) + rel_asset = pathlib.PurePath(*asset_parts) # TODO(Sybren): should we use sys.getfilesystemencoding() instead? rel_bytes = str(rel_asset).encode('utf-8') as_bytes = b'//' + len(bdir_parts) * b'../' + rel_bytes @@ -84,8 +84,8 @@ class BlendPath(bytes): raise ValueError("'a / b' only works when 'b' is a relative path") return BlendPath(os.path.join(parentpath, self)) - def to_path(self) -> pathlib.Path: - """Convert this path to a pathlib.Path. + def to_path(self) -> pathlib.PurePath: + """Convert this path to a pathlib.PurePath. Interprets the path as UTF-8, and if that fails falls back to the local filesystem encoding. @@ -98,7 +98,7 @@ class BlendPath(bytes): decoded = self.decode('utf8') except UnicodeDecodeError: decoded = self.decode(sys.getfilesystemencoding()) - return pathlib.Path(decoded) + return pathlib.PurePath(decoded) def is_blendfile_relative(self) -> bool: return self[:2] == b'//' diff --git a/blender_asset_tracer/trace/result.py b/blender_asset_tracer/trace/result.py index 5f4431e..26ec1ae 100644 --- a/blender_asset_tracer/trace/result.py +++ b/blender_asset_tracer/trace/result.py @@ -148,7 +148,7 @@ class BlockUsage: """ if self._abspath is None: bpath = self.block.bfile.abspath(self.asset_path) - as_path = bpath.to_path() + as_path = pathlib.Path(bpath.to_path()) try: self._abspath = as_path.resolve() except FileNotFoundError: diff --git a/tests/test_bpathlib.py b/tests/test_bpathlib.py index 4f2b1ad..43bfe99 100644 --- a/tests/test_bpathlib.py +++ b/tests/test_bpathlib.py @@ -1,4 +1,4 @@ -from pathlib import Path +from pathlib import Path, PurePosixPath import unittest from blender_asset_tracer.bpathlib import BlendPath @@ -6,11 +6,12 @@ from blender_asset_tracer.bpathlib import BlendPath class BlendPathTest(unittest.TestCase): def test_string_path(self): - p = BlendPath(Path('//some/file.blend')) + p = BlendPath(PurePosixPath('//some/file.blend')) + self.assertEqual('//some/file.blend', str(PurePosixPath('//some/file.blend'))) self.assertEqual(b'//some/file.blend', p) p = BlendPath(Path(r'C:\some\file.blend')) - self.assertEqual(b'C:\\some\\file.blend', p) + self.assertEqual(b'C:/some/file.blend', p) def test_is_absolute(self): self.assertFalse(BlendPath(b'//some/file.blend').is_absolute()) diff --git a/tests/test_pack.py b/tests/test_pack.py index 0fcfcbc..fe65004 100644 --- a/tests/test_pack.py +++ b/tests/test_pack.py @@ -162,7 +162,7 @@ class PackTest(AbstractPackTest): def test_execute_rewrite(self): infile, _ = self._pack_with_rewrite() - extpath = pathlib.Path('//_outside_project', *self.blendfiles.parts[1:]) + extpath = pathlib.PurePosixPath('//_outside_project', *self.blendfiles.parts[1:]) extbpath = bpathlib.BlendPath(extpath) # Those libraries should be properly rewritten. @@ -209,8 +209,9 @@ class PackTest(AbstractPackTest): seq = ed.get_pointer((b'seqbase', b'first')) seq_strip = seq.get_pointer(b'strip') - as_bytes = str((self.blendfiles / 'imgseq').absolute()).encode() - relpath = b'//_outside_project%b' % as_bytes + imgseq_path = (self.blendfiles / 'imgseq').absolute() + as_bytes = str(imgseq_path.relative_to(imgseq_path.anchor)).encode() + relpath = bpathlib.BlendPath(b'//_outside_project') / as_bytes # The image sequence base path should be rewritten. self.assertEqual(b'SQ000210.png', seq[b'name'])