Implemented JSON dependency logging output

This commit is contained in:
Sybren A. Stüvel 2018-02-28 17:18:18 +01:00
parent a7706f21b6
commit 6252b041d5
2 changed files with 45 additions and 9 deletions

View File

@ -1,8 +1,11 @@
"""List dependencies of a blend file.""" """List dependencies of a blend file."""
import json
import logging import logging
import pathlib import pathlib
import sys
from . import common from . import common
from blender_asset_tracer import tracer
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -19,23 +22,34 @@ def add_parser(subparsers):
def cli_list(args): def cli_list(args):
from blender_asset_tracer import tracer
bpath = args.blendfile bpath = args.blendfile
if not bpath.exists(): if not bpath.exists():
log.fatal('File %s does not exist', args.blendfile) log.fatal('File %s does not exist', args.blendfile)
return 3 return 3
cwd = pathlib.Path.cwd() recursive = not args.nonrecursive
if args.json:
report_json(bpath, recursive)
else:
report_text(bpath, recursive)
def report_text(bpath, recursive):
reported_assets = set() reported_assets = set()
last_reported_bfile = None last_reported_bfile = None
cwd = pathlib.Path.cwd()
def shorten(somepath: pathlib.Path) -> pathlib.Path:
"""Return 'somepath' relative to CWD if possible."""
try:
return somepath.relative_to(cwd)
except ValueError:
return somepath
recursive = not args.nonrecursive
for usage in tracer.deps(bpath, recursive=recursive): for usage in tracer.deps(bpath, recursive=recursive):
filepath = usage.block.bfile.filepath.absolute() filepath = usage.block.bfile.filepath.absolute()
if filepath != last_reported_bfile: if filepath != last_reported_bfile:
print(filepath.relative_to(cwd)) print(shorten(filepath))
last_reported_bfile = filepath last_reported_bfile = filepath
for assetpath in usage.files(): for assetpath in usage.files():
@ -44,8 +58,29 @@ def cli_list(args):
log.debug('Already reported %s', assetpath) log.debug('Already reported %s', assetpath)
continue continue
try: print(' ', shorten(assetpath))
print(' ', assetpath.relative_to(cwd))
except ValueError:
print(' ', assetpath)
reported_assets.add(assetpath) reported_assets.add(assetpath)
class JSONSerialiser(json.JSONEncoder):
def default(self, o):
if isinstance(o, pathlib.Path):
return str(o)
if isinstance(o, set):
return sorted(o)
return super().default(o)
def report_json(bpath, recursive):
import collections
# Mapping from blend file to its dependencies.
report = collections.defaultdict(set)
for usage in tracer.deps(bpath, recursive=recursive):
filepath = usage.block.bfile.filepath.absolute()
for assetpath in usage.files():
assetpath = assetpath.resolve()
report[str(filepath)].add(assetpath)
json.dump(report, sys.stdout, cls=JSONSerialiser, indent=4)

View File

@ -32,6 +32,7 @@ class _Tracer:
:param recursive: Also report dependencies inside linked blend files. :param recursive: Also report dependencies inside linked blend files.
""" """
log.info('Tracing %s', bfilepath) log.info('Tracing %s', bfilepath)
bfilepath = bfilepath.absolute().resolve()
self.seen_files.add(bfilepath) self.seen_files.add(bfilepath)
recurse_into = [] recurse_into = []