add path remapping on file pack

This commit is contained in:
2014-10-31 16:46:52 +01:00
parent 4b3a11f775
commit 95a8685f3e
3 changed files with 82 additions and 22 deletions

View File

@@ -30,6 +30,46 @@ Blender asset manager
# * definition of project (.bam (like .git)) ... json
class bam_config:
# fake module
__slots__ = ()
def __new__(cls, *args, **kwargs):
raise RuntimeError("%s should not be instantiated" % cls)
CONFIG_DIR = ".bam"
@staticmethod
def find_basedir():
"""
Return the config path (or None when not found)
"""
import os
parent = (os.path.normpath(
os.path.abspath(
os.getcwd())))
parent_prev = None
while parent != parent_prev:
test_dir = os.path.join(parent, bam_config.CONFIG_DIR)
if os.path.isdir(test_dir):
return test_dir
parent_prev = parent
parent = os.path.dirname(parent)
return None
@staticmethod
def load(id_):
# bam_config.find_basedir()
# data =
pass
@staticmethod
def write(id_, data):
pass
class bam_utils:
# fake module
@@ -44,7 +84,7 @@ class bam_utils:
@staticmethod
def session_request_url(req_path):
# TODO, get from config
BAM_SERVER = "http://localhost:5000"
BAM_SERVER = bam_utils.session_find_url()
result = "%s/%s" % (BAM_SERVER, req_path)
return result
@@ -95,6 +135,8 @@ class bam_utils:
import sys
import requests
# print(bam_config.find_config())
path = paths[0]
del paths

View File

@@ -23,12 +23,13 @@ import blendfile_path_walker
TIMEIT = True
def pack(blendfile_src, blendfile_dst, mode='FILE', pathmap=None):
def pack(blendfile_src, blendfile_dst, mode='FILE',
deps_remap=None, paths_remap=None):
"""
:param pathmap: Store path pathmap info as follows.
:param deps_remap: Store path deps_remap info as follows.
{"file.blend": {"path_new": "path_old", ...}, ...}
:type pathmap: dict or None
:type deps_remap: dict or None
"""
# Internal details:
@@ -111,9 +112,9 @@ def pack(blendfile_src, blendfile_dst, mode='FILE', pathmap=None):
if not isinstance(fp, blendfile_path_walker.FPElem_block_path) or fp.userdata[0].code != b'LI':
path_copy_files.add((path_src, path_dst))
if pathmap is not None:
if deps_remap is not None:
# this needs to become JSON later... ugh, need to use strings
pathmap.setdefault(
deps_remap.setdefault(
fp_blend_basename.decode('utf-8'),
{})[path_dst_final.decode('utf-8')] = path_src_orig.decode('utf-8')
@@ -122,16 +123,23 @@ def pack(blendfile_src, blendfile_dst, mode='FILE', pathmap=None):
if TIMEIT:
print(" Time: %.4f\n" % (time.time() - t))
# handle pathmap and file renaming
if pathmap is not None:
# handle deps_remap and file renaming
if deps_remap is not None:
blendfile_src_basename = os.path.basename(blendfile_src).decode('utf-8')
blendfile_dst_basename = os.path.basename(blendfile_dst).decode('utf-8')
if blendfile_src_basename != blendfile_dst_basename:
pathmap[blendfile_dst_basename] = pathmap[blendfile_src_basename]
del pathmap[blendfile_src_basename]
deps_remap[blendfile_dst_basename] = deps_remap[blendfile_src_basename]
del deps_remap[blendfile_src_basename]
del blendfile_src_basename, blendfile_dst_basename
# store path mapping {dst: src}
if paths_remap:
for src, dst in path_copy_files:
# TODO. relative to project-basepath
paths_remap[os.path.relpath(dst, base_dir_dst).decode('utf-8')] = src
# paths_remap[os.path.relpath(dst, base_dir_dst)] = blendfile_src
# --------------------
# Handle File Copy/Zip
@@ -154,6 +162,7 @@ def pack(blendfile_src, blendfile_dst, mode='FILE', pathmap=None):
print(" Copying %r -> %r" % (src, dst))
shutil.copy(src, dst)
print(" Written:", blendfile_dst)
elif mode == 'ZIP':
@@ -205,8 +214,11 @@ def create_argparse():
choices=('FILE', 'ZIP'), default='FILE',
help="Output file or a directory when multiple inputs are passed")
parser.add_argument(
"-r", "--remap", dest="path_remap", metavar='FILE',
"-r", "--deps_remap", dest="deps_remap", metavar='FILE',
help="Write out the path mapping to a JSON file")
parser.add_argument(
"-s", "--paths_remap", dest="paths_remap", metavar='FILE',
help="Write out the original paths to a JSON file")
return parser
@@ -219,23 +231,29 @@ def main():
encoding = sys.getfilesystemencoding()
if args.path_remap:
pathmap = {}
if args.deps_remap:
deps_remap = {}
else:
pathmap = None
deps_remap = None
if args.paths_remap:
paths_remap = {}
else:
paths_remap = None
pack(args.path_src.encode(encoding),
args.path_dst.encode(encoding),
args.mode,
pathmap,
deps_remap,
paths_remap,
)
if pathmap is not None:
if deps_remap is not None:
import json
with open(args.path_remap, 'w', encoding='utf-8') as f:
with open(args.deps_remap, 'w', encoding='utf-8') as f:
json.dump(
pathmap, f, ensure_ascii=False,
deps_remap, f, ensure_ascii=False,
# optional (pretty)
sort_keys=True, indent=4, separators=(',', ': '),
)

View File

@@ -29,7 +29,7 @@ VERBOSE = 1
import blendfile_path_walker
def blendfile_remap(blendfile_src, blendpath_dst, path_remap):
def blendfile_remap(blendfile_src, blendpath_dst, deps_remap):
import os
def temp_remap_cb(filepath, level):
@@ -57,7 +57,7 @@ def blendfile_remap(blendfile_src, blendpath_dst, path_remap):
# path_src_orig - original path from JSON.
path_dst_final = fp.filepath.decode('utf-8')
path_src_orig = path_remap.get(path_dst_final)
path_src_orig = deps_remap.get(path_dst_final)
if path_src_orig is not None:
fp.filepath = path_src_orig.encode('utf-8')
if VERBOSE:
@@ -100,7 +100,7 @@ def create_argparse():
"-o", "--output", dest="path_dst", metavar='DIR', required=True,
help="Output directory ")
parser.add_argument(
"-r", "--remap", dest="path_remap", metavar='JSON', required=True,
"-r", "--deps_remap", dest="deps_remap", metavar='JSON', required=True,
help="JSON file containing the path remapping info")
return parser
@@ -115,7 +115,7 @@ def main():
encoding = sys.getfilesystemencoding()
with open(args.path_remap, 'r', encoding='utf-8') as f:
with open(args.deps_remap, 'r', encoding='utf-8') as f:
pathmap = json.load(f)
pack_restore(