From 3222d1027627bf5f39942f4db4b25309b8c9d490 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 4 Nov 2014 21:46:18 +0100 Subject: [PATCH] progress info when checking out --- client/cli/bam | 24 +++++++- packer/packer.py | 64 +++++++++++++++++--- webservice/bam/application/__init__.py | 82 +++++++++++++++++++++----- 3 files changed, 144 insertions(+), 26 deletions(-) diff --git a/client/cli/bam b/client/cli/bam index 3b4ce16..f8658a2 100755 --- a/client/cli/bam +++ b/client/cli/bam @@ -152,15 +152,33 @@ class bam_utils: local_filename += ".zip" with open(local_filename, 'wb') as f: + import struct + ID_MESSAGE = 1 + ID_PAYLOAD = 2 + head = r.raw.read(4) + if head != b'BAM\0': + print("Bad header...") + return + + while True: + msg_type, msg_size = struct.unpack(" %r" % (src, dst)) + yield report(" %s: %r -> %r\n" % (colorize("copying", color='blue'), src, dst)) shutil.copy(src, dst) - - print(" Written:", blendfile_dst) + yield report(" %s: %r\n" % (colorize("written", color='green'), blendfile_dst)) elif mode == 'ZIP': import zipfile with zipfile.ZipFile(blendfile_dst.decode('utf-8'), 'w', zipfile.ZIP_DEFLATED) as zip: for fn in path_temp_files: - print(" Copying %r -> " % fn) + yield report(" %s: %r -> \n" % (colorize("copying", color='blue'), fn)) zip.write(fn.decode('utf-8'), arcname=os.path.relpath(fn[:-1], base_dir_dst).decode('utf-8')) os.remove(fn) @@ -206,9 +221,9 @@ def pack(blendfile_src, blendfile_dst, mode='FILE', assert(b'.blend' not in dst) if not os.path.exists(src): - print(" Source missing! %r" % src) + yield report(" %s: %r\n" % (colorize("source missing", color='red'), src)) else: - print(" Copying %r -> " % src) + yield report(" %s: %r -> \n" % (colorize("copying", color='blue'), src)) zip.write(src.decode('utf-8'), arcname=os.path.relpath(dst, base_dir_dst).decode('utf-8')) @@ -233,7 +248,7 @@ def pack(blendfile_src, blendfile_dst, mode='FILE', del write_dict_as_json - print(" Written:", blendfile_dst) + yield report(" %s: %r\n" % (colorize("written", color='green'), blendfile_dst)) else: raise Exception("%s not a known mode" % mode) @@ -309,3 +324,34 @@ def main(): if __name__ == "__main__": main() + +# TODO(cam) de-duplicate +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', + 'bright_blue': '\033[1;34m', + 'cyan': '\033[0;36m', + 'bright_green': '\033[1;32m', + 'red': '\033[0;31m', + 'bright_cyan': '\033[1;36m', + 'purple': '\033[0;35m', + 'bright_red': '\033[1;31m', + 'yellow': '\033[0;33m', + 'bright_purple':'\033[1;35m', + 'dark_gray': '\033[1;30m', + 'bright_yellow':'\033[1;33m', + 'normal': '\033[0m', + } + + def colorize(msg, color=None): + return (color_codes[color] + msg + color_codes['normal']) +else: + def colorize(msg, color=None): + return msg + + diff --git a/webservice/bam/application/__init__.py b/webservice/bam/application/__init__.py index 416ef85..d516c10 100644 --- a/webservice/bam/application/__init__.py +++ b/webservice/bam/application/__init__.py @@ -150,16 +150,41 @@ class FileAPI(Resource): elif os.path.isdir(filepath): return jsonify(message="Path is a directory %r" % filepath) - # pack the file! - print("PACKING") - filepath_zip = self.pack_fn(filepath) + def response_message_iter(): + ID_MESSAGE = 1 + ID_PAYLOAD = 2 + import struct - # TODO, handle fail - if filepath_zip is None: - return jsonify(message="Path not found %r" % filepath) + def report(txt): + txt_bytes = txt.encode('utf-8') + return struct.pack('