[Bf-extensions-cvs] [5754952] temp-blend-utils: Many small changes, pack operator is now working

Campbell Barton noreply at git.blender.org
Thu Jan 28 10:52:03 CET 2016


Commit: 57549524246fab1aa5753d9c16f91ff06b82f2e8
Author: Campbell Barton
Date:   Thu Jan 28 20:16:54 2016 +1100
Branches: temp-blend-utils
https://developer.blender.org/rBA57549524246fab1aa5753d9c16f91ff06b82f2e8

Many small changes, pack operator is now working

===================================================================

M	io_blend_utils/__init__.py
A	io_blend_utils/bl_utils/pipe_non_blocking.py
A	io_blend_utils/bl_utils/subprocess_helper.py
D	io_blend_utils/blend/blendfile_pack.py
M	io_blend_utils/blend/blendfile_path_walker.py
A	io_blend_utils/blendfile_pack.py
D	io_blend_utils/utils/pipe_non_blocking.py
D	io_blend_utils/utils/subprocess_helper.py
A	io_blend_utils/utils/system.py

===================================================================

diff --git a/io_blend_utils/__init__.py b/io_blend_utils/__init__.py
index e69de29..8726693 100644
--- a/io_blend_utils/__init__.py
+++ b/io_blend_utils/__init__.py
@@ -0,0 +1,93 @@
+# ***** BEGIN GPL LICENSE BLOCK *****
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+#
+# ***** END GPL LICENCE BLOCK *****
+
+bl_info = {
+    "name": "Blend File Utils",
+    "author": "Campbell Barton",
+    "version": (0, 1),
+    "blender": (2, 76, 0),
+    "location": "File > External Data > Blend Utils",
+    "description": "Utilities for packing, remapping and exporting blend files",
+    "warning": "",
+    "wiki_url": "",
+    "category": "Import-Export",
+    }
+
+
+import bpy
+from bpy.types import Operator
+from bpy_extras.io_utils import ExportHelper
+from bpy.props import PointerProperty, StringProperty
+
+from .bl_utils.subprocess_helper import SubprocessHelper
+
+
+class ExportBlendPack(Operator, ExportHelper, SubprocessHelper):
+    """Packs a blend file and all its dependencies into an archive for easy redistribution"""
+    bl_idname = "export_blend.pack"
+    bl_label = "Pack Blend to Archive"
+
+    filename_ext = ".zip"
+
+    @classmethod
+    def poll(cls, context):
+        return bpy.data.is_saved
+
+    # command attribute for 'SubprocessHelper'
+    @property
+    def command(self):
+        import os
+        return (
+            bpy.app.binary_path_python,
+            os.path.join(os.path.dirname(__file__), "blendfile_pack.py"),
+            # file to pack
+            "--input", bpy.data.filepath,
+            # file to write
+            "--output", bpy.path.ensure_ext(self.filepath, ".zip"),
+            )
+
+
+def menu_func(self, context):
+    layout = self.layout
+    layout.separator()
+    layout.operator(ExportBlendPack.bl_idname, icon='MESH_CUBE')
+
+
+classes = (
+    ExportBlendPack,
+    )
+
+
+def register():
+    for cls in classes:
+        bpy.utils.register_class(cls)
+
+    bpy.types.INFO_MT_file_external_data.append(menu_func)
+
+def unregister():
+    for cls in classes:
+        bpy.utils.unregister_class(cls)
+
+    bpy.types.INFO_MT_file_external_data.remove(menu_func)
+
+
+if __name__ == "__main__":
+    register()
+
+
+
diff --git a/io_blend_utils/utils/pipe_non_blocking.py b/io_blend_utils/bl_utils/pipe_non_blocking.py
similarity index 100%
rename from io_blend_utils/utils/pipe_non_blocking.py
rename to io_blend_utils/bl_utils/pipe_non_blocking.py
diff --git a/io_blend_utils/utils/subprocess_helper.py b/io_blend_utils/bl_utils/subprocess_helper.py
similarity index 89%
rename from io_blend_utils/utils/subprocess_helper.py
rename to io_blend_utils/bl_utils/subprocess_helper.py
index efe62fb..760f69d 100644
--- a/io_blend_utils/utils/subprocess_helper.py
+++ b/io_blend_utils/bl_utils/subprocess_helper.py
@@ -22,13 +22,6 @@
 Defines an operator mix-in to use for non-blocking command line access.
 """
 
-from pipe_non_blocking import (
-        pipe_non_blocking_set,
-        pipe_non_blocking_is_error_blocking,
-        PortableBlockingIOError,
-        )
-
-
 class SubprocessHelper:
     """
     Mix-in class for operators to run commands in a non-blocking way.
@@ -50,6 +43,13 @@ class SubprocessHelper:
         Iterate over lines, yielding b'' when nothings left
         or when new data is not yet available.
         """
+        import os
+
+        from .pipe_non_blocking import (
+                pipe_non_blocking_set,
+                pipe_non_blocking_is_error_blocking,
+                PortableBlockingIOError,
+                )
 
         fd = f.fileno()
         pipe_non_blocking_set(fd)
@@ -70,12 +70,15 @@ class SubprocessHelper:
                 yield b''
                 continue
 
-            n = data.find(b'\n')
-            if n != -1:
-                yield b''.join(blocks) + data[:n + 1]
-                data = data[n + 1:]
-                blocks.clear()
-            blocks.append(data)
+                while True:
+                    n = data.find(b'\n')
+                    if n == -1:
+                        break
+
+                    yield b''.join(blocks) + data[:n + 1]
+                    data = data[n + 1:]
+                    blocks.clear()
+                blocks.append(data)
 
     def _report_output(self):
         stdout_line_iter, stderr_line_iter = self._buffer_iter
diff --git a/io_blend_utils/blend/blendfile_path_walker.py b/io_blend_utils/blend/blendfile_path_walker.py
index 8c3dac1..163d468 100644
--- a/io_blend_utils/blend/blendfile_path_walker.py
+++ b/io_blend_utils/blend/blendfile_path_walker.py
@@ -395,7 +395,7 @@ class FilePath:
         # store info to pass along with each iteration
         extra_info = rootdir, os.path.basename(filepath)
 
-        from bam.blend import blendfile
+        from blend import blendfile
         with blendfile.open_blend(filepath_tmp, "rb" if readonly else "r+b") as blend:
 
             for code in blend.code_index.keys():
@@ -627,7 +627,7 @@ class bf_utils:
 
     def iter_array(block, length=-1):
         assert(block.code == b'DATA')
-        from bam.blend import blendfile
+        import blendfile
         import os
         handle = block.file.handle
         header = block.file.header
diff --git a/io_blend_utils/blend/blendfile_pack.py b/io_blend_utils/blendfile_pack.py
similarity index 97%
rename from io_blend_utils/blend/blendfile_pack.py
rename to io_blend_utils/blendfile_pack.py
index 1853c23..bd3ba18 100755
--- a/io_blend_utils/blend/blendfile_pack.py
+++ b/io_blend_utils/blendfile_pack.py
@@ -18,7 +18,7 @@
 #
 # ***** END GPL LICENCE BLOCK *****
 
-from bam.blend import blendfile_path_walker
+from blend import blendfile_path_walker
 
 TIMEIT = False
 
@@ -39,7 +39,7 @@ del os, sys, path
 # ... when internals _really_ fail & we want to know why
 def _dbg(text):
     import sys
-    from bam_utils.system import colorize
+    from utils.system import colorize
     if type(text) is bytes:
         text = text.decode('utf-8')
     sys.__stdout__.write(colorize(text, color='red') + "\n")
@@ -121,7 +121,7 @@ def pack(
         blendfile_src_dir_fakeroot=None,
 
         # Read variations from json files.
-        use_variations=True,
+        use_variations=False,
 
         # do _everything_ except to write the paths.
         # useful if we want to calculate deps to remap but postpone applying them.
@@ -153,8 +153,12 @@ def pack(
     #   also prevents cyclic loops from crashing.
 
     import os
+    import sys
 
-    from bam.utils.system import colorize
+    if sys.stdout.isatty():
+        from utils.system import colorize
+    else:
+        from utils.system import colorize_dummy as colorize
 
     # in case this is directly from the command line or user-input
     blendfile_src = os.path.normpath(os.path.abspath(blendfile_src))
@@ -434,7 +438,7 @@ def pack(
         del relbase
 
     if paths_uuid is not None:
-        from bam.utils.system import uuid_from_file
+        from utils.system import uuid_from_file
 
         for src, dst in path_copy_files:
             # reports are handled again, later on.
@@ -495,7 +499,7 @@ def pack(
         if _compress_mode == zipfile.ZIP_STORED:
             is_compressed_filetype = lambda fn: False
         else:
-            from bam.utils.system import is_compressed_filetype
+            from utils.system import is_compressed_filetype
 
         with zipfile.ZipFile(blendfile_dst.decode('utf-8'), 'w', _compress_mode) as zip_handle:
             for fn in path_temp_files:
@@ -554,7 +558,7 @@ def create_argparse():
             )
     parser.add_argument(
             "-m", "--mode", dest="mode", metavar='MODE', required=False,
-            choices=('FILE', 'ZIP'), default='FILE',
+            choices=('FILE', 'ZIP'), default='ZIP',
             help="Output file or a directory when multiple inputs are passed",
             )
     parser.add_argument(
@@ -572,9 +576,12 @@ def main():
     args = parser.parse_args(sys.argv[1:])
 
     if args.use_quiet:
-        report = lambda msg: None
+        def report(msg):
+            pass
     else:
-        report = lambda msg: print(msg, end="")
+        def report(msg):
+            sys.stdout.write(msg)
+            sys.stdout.flush()
 
     for msg in pack(
             args.path_src.encode('utf-8'),
diff --git a/io_blend_utils/utils/system.py b/io_blend_utils/utils/system.py
new file mode 100644
index 0000000..313173e
--- /dev/null
+++ b/io_blend_utils/utils/system.py
@@ -0,0 +1,143 @@
+# ***** BEGIN GPL LICENSE BLOCK *****
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+#
+# ***** END GPL LICENCE BLOCK *****
+
+def colorize_dummy(msg, color=None):
+    return msg
+
+_USE_COLOR = True
+if _USE_COLOR:
+    color_codes = {
+        'black':        '\033[0;30m',
+        'bright_gray':  '\033[0;37m',
+        'blue':         '\033[0;34m',
+        'white':        '\033[1;37m',
+        'green':        '\033[0;32m

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list