From 0aa730089b404741d2d46ad03c68a66f4eae7168 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Thu, 4 Jun 2015 14:52:32 +1000 Subject: [PATCH] pack, support --exclude argument See: bam pack --help --- bam/__init__.py | 2 +- bam/blend/blendfile_pack.py | 8 ++++++++ bam/cli.py | 41 +++++++++++++++++++++++++++++++++++-- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/bam/__init__.py b/bam/__init__.py index 9124a26..260cb8b 100644 --- a/bam/__init__.py +++ b/bam/__init__.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- import sys -__version__ = "0.0.4.6" +__version__ = "0.0.4.7" def main(argv=sys.argv): from .cli import main diff --git a/bam/blend/blendfile_pack.py b/bam/blend/blendfile_pack.py index d7edf07..a74db95 100755 --- a/bam/blend/blendfile_pack.py +++ b/bam/blend/blendfile_pack.py @@ -130,6 +130,10 @@ def pack( # {file: [(ofs, bytes), ...], ...} # ... where the file is the relative 'packed' location. binary_edits=None, + + # Filename filter, allow to exclude files from the pack, + # function takes a string returns True if the files should be included. + filename_filter=None, ): """ :param deps_remap: Store path deps_remap info as follows. @@ -467,6 +471,8 @@ def pack( # in rare cases a filepath could point to a directory if (not os.path.exists(src)) or os.path.isdir(src): yield report(" %s: %r\n" % (colorize("source missing", color='red'), src)) + elif filename_filter and not filename_filter(src): + yield report(" %s: %r\n" % (colorize("exclude", color='yellow'), src)) else: yield report(" %s: %r -> %r\n" % (colorize("copying", color='blue'), src, dst)) shutil.copy(src, dst) @@ -505,6 +511,8 @@ def pack( # in rare cases a filepath could point to a directory if (not os.path.exists(src)) or os.path.isdir(src): yield report(" %s: %r\n" % (colorize("source missing", color='red'), src)) + elif filename_filter and not filename_filter(src): + yield report(" %s: %r\n" % (colorize("exclude", color='yellow'), src)) else: yield report(" %s: %r -> \n" % (colorize("copying", color='blue'), src)) zip_handle.write( diff --git a/bam/cli.py b/bam/cli.py index e96d61b..e948b9f 100755 --- a/bam/cli.py +++ b/bam/cli.py @@ -1321,6 +1321,7 @@ class bam_commands: all_deps=False, use_quiet=False, compress_level=-1, + filename_filter=None, ): # Local packing (don't use any project/session stuff) from .blend import blendfile_pack @@ -1343,6 +1344,26 @@ class bam_commands: else: report = lambda msg: print(msg, end="") + # replace var with a pattern matching callback + if filename_filter: + # convert string into regex callback + # "*.txt;*.png;*.rst" --> r".*\.txt$|.*\.png$|.*\.rst$" + import re + import fnmatch + + compiled_pattern = re.compile( + b'|'.join(fnmatch.translate(f).encode('utf-8') + for f in filename_filter.split(";") if f), + re.IGNORECASE, + ) + + def filename_filter(f): + return (not filename_filter.compiled_pattern.match(f)) + filename_filter.compiled_pattern = compiled_pattern + + del compiled_pattern + del re, fnmatch + for msg in blendfile_pack.pack( path.encode('utf-8'), output.encode('utf-8'), @@ -1350,6 +1371,7 @@ class bam_commands: all_deps=all_deps, compress_level=compress_level, report=report, + filename_filter=filename_filter, ): pass @@ -1674,6 +1696,19 @@ def create_argparse_pack(subparsers): choices=('ZIP', 'FILE'), help="Output file or a directory when multiple inputs are passed", ) + subparse.add_argument( + "-e", "--exclude", dest="exclude", metavar='PATTERN(S)', required=False, + default="", + help=""" + Optionally exclude files from the pack. + + Using Unix shell-style wildcards *(case insensitive)*. + ``--exclude="*.png"`` + + Multiple patterns can be passed using the ``;`` separator. + ``--exclude="*.txt;*.avi;*.wav"`` + """ + ) init_argparse_common(subparse, use_all_deps=True, use_quiet=True, use_compress_level=True) @@ -1687,8 +1722,10 @@ def create_argparse_pack(subparsers): args.mode, all_deps=args.all_deps, use_quiet=args.use_quiet, - compress_level=args.compress_level), - ) + compress_level=args.compress_level, + filename_filter=args.exclude, + ), + ) def create_argparse_remap(subparsers):