Beginning of CLI interface + protection against infinite recursion.

This commit is contained in:
Sybren A. Stüvel
2018-02-28 16:47:13 +01:00
parent 8ae400acbe
commit ca964181fe
14 changed files with 314 additions and 27 deletions
+50 -25
View File
@@ -17,38 +17,63 @@ codes_to_skip = {
}
class _Tracer:
"""Trace dependencies with protection against infinite loops.
Don't use this directly, use the function deps(...) instead.
"""
def __init__(self):
self.seen_files = set()
def deps(self, bfilepath: pathlib.Path, recursive=False) -> typing.Iterator[result.BlockUsage]:
"""Open the blend file and report its dependencies.
:param bfilepath: File to open.
:param recursive: Also report dependencies inside linked blend files.
"""
log.info('Tracing %s', bfilepath)
self.seen_files.add(bfilepath)
with blendfile.BlendFile(bfilepath) as bfile:
for block in asset_holding_blocks(bfile):
yield from block_walkers.from_block(block)
if recursive and block.code == b'LI':
yield from self._recurse_deps(block)
def _recurse_deps(self, lib_block: blendfile.BlendFileBlock) \
-> typing.Iterator[result.BlockUsage]:
"""Call deps() on the file linked from the library block."""
if lib_block.code != b'LI':
raise ValueError('Expected LI block, not %r' % lib_block)
relpath = bpathlib.BlendPath(lib_block[b'name'])
abspath = lib_block.bfile.abspath(relpath)
# Convert bytes to pathlib.Path object so we have a nice interface to work with.
# This assumes the path is encoded in UTF-8.
path = pathlib.Path(abspath.decode()).resolve()
if not path.exists():
log.warning('Linked blend file %s (%s) does not exist; skipping.', relpath, path)
return
# Avoid infinite recursion.
if path in self.seen_files:
log.debug('ignoring file, already seen %s', path)
return
yield from self.deps(path, recursive=True)
def deps(bfilepath: pathlib.Path, recursive=False) -> typing.Iterator[result.BlockUsage]:
"""Open the blend file and report its dependencies.
:param bfilepath: File to open.
:param recursive: Also report dependencies inside linked blend files.
"""
log.info('Tracing %s', bfilepath)
with blendfile.BlendFile(bfilepath) as bfile:
for block in asset_holding_blocks(bfile):
yield from block_walkers.from_block(block)
if recursive and block.code == b'LI':
yield from _recurse_deps(block)
def _recurse_deps(lib_block: blendfile.BlendFileBlock) -> typing.Iterator[result.BlockUsage]:
"""Call deps() on the file linked from the library block."""
if lib_block.code != b'LI':
raise ValueError('Expected LI block, not %r' % lib_block)
relpath = bpathlib.BlendPath(lib_block[b'name'])
abspath = lib_block.bfile.abspath(relpath)
# Convert bytes to pathlib.Path object so we have a nice interface to work with.
# This assumes the path is encoded in UTF-8.
path = pathlib.Path(abspath.decode())
if not path.exists():
log.warning('Linked blend file %s (%s) does not exist; skipping.', relpath, path)
return
yield from deps(path, recursive=True)
tracer = _Tracer()
yield from tracer.deps(bfilepath, recursive=recursive)
def asset_holding_blocks(bfile: blendfile.BlendFile) -> typing.Iterator[blendfile.BlendFileBlock]:
@@ -0,0 +1,51 @@
import logging
import pathlib
import typing
log = logging.getLogger(__name__)
class DoesNotExist(OSError):
"""Indicates a path does not exist on the filesystem."""
def __init__(self, path: pathlib.Path):
super().__init__(path)
self.path = path
def expand_sequence(path: pathlib.Path) -> typing.Iterator[pathlib.Path]:
"""Expand a file sequence path into the actual file paths.
:param path: can be either a glob pattern (must contain a * character),
a directory, or the path of the first file in the sequence.
"""
if '*' in str(path): # assume it is a glob
import glob
log.debug('expanding glob %s', path)
for fname in sorted(glob.glob(str(path), recursive=True)):
yield pathlib.Path(fname)
return
if not path.exists():
raise DoesNotExist(path)
if path.is_dir():
log.debug('expanding directory %s', path)
yield from sorted(path.rglob('*'))
return
log.debug('expanding file sequence %s', path)
import string
stem_no_digits = path.stem.rstrip(string.digits)
if stem_no_digits == path.stem:
# Just a single file, no digits here.
yield path
return
# Return everything start starts with 'stem_no_digits' and ends with the
# same suffix as the first file. This may result in more files than used
# by Blender, but at least it shouldn't miss any.
pattern = '%s*%s' % (stem_no_digits, path.suffix)
yield from sorted(path.parent.glob(pattern))
+31
View File
@@ -1,5 +1,12 @@
import logging
import pathlib
import typing
from blender_asset_tracer import blendfile, bpathlib
from blender_asset_tracer.blendfile import dna
from . import file_sequence
log = logging.getLogger(__name__)
class BlockUsage:
@@ -79,3 +86,27 @@ class BlockUsage:
self.path_full_field.name.name_full.decode(), self.asset_path,
' sequence' if self.is_sequence else ''
)
def files(self) -> typing.Iterator[pathlib.Path]:
"""Determine absolute path(s) of the asset file(s).
A relative path is interpreted relative to the blend file referring
to the asset. If this BlockUsage represents a sequence, the filesystem
is inspected and the actual files in the sequence are yielded.
It is assumed that paths are valid UTF-8.
"""
bpath = self.block.bfile.abspath(self.asset_path)
path = pathlib.Path(bpath.decode())
if not self.is_sequence:
if not path.exists():
log.warning('Path %s does not exist for %s', path, self)
return
yield path
return
try:
yield from file_sequence.expand_sequence(path)
except file_sequence.DoesNotExist:
log.warning('Path %s does not exist for %s', path, self)