From 9157ed1dc07e185fbbc230f002da972c8108be1f Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 5 Nov 2014 11:24:57 +0100 Subject: [PATCH] extract zipfile on checkout --- client/cli/bam.py | 24 ++++++++++++++++++++---- webservice/bam/application/__init__.py | 7 ++++--- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/client/cli/bam.py b/client/cli/bam.py index 8fe7225..e2bfd36 100755 --- a/client/cli/bam.py +++ b/client/cli/bam.py @@ -117,12 +117,17 @@ class bam_utils: @staticmethod def checkout(paths): import sys + import os import requests # TODO(cam) multiple paths path = paths[0] del paths + # TODO(cam) we may want to checkout a single file? how to handle this? + # we may want to checkout a dir too + dst_dir = os.path.basename(path) + payload = { "filepath": path, "command": "checkout", @@ -140,12 +145,12 @@ class bam_utils: return # TODO(cam) how to tell if we get back a message payload? or real data??? - local_filename = payload['filepath'].split('/')[-1] + dst_dir_data = payload['filepath'].split('/')[-1] if 1: - local_filename += ".zip" + dst_dir_data += ".zip" - with open(local_filename, 'wb') as f: + with open(dst_dir_data, 'wb') as f: import struct ID_MESSAGE = 1 ID_PAYLOAD = 2 @@ -172,7 +177,18 @@ class bam_utils: sys.stdout.write("\rdownload: [%03d%%]" % ((100 * tot_size) // msg_size)) sys.stdout.flush() - sys.stdout.write("\nwritten: %r\n" % local_filename) + + # --------------- + # extract the zip + import zipfile + with open(dst_dir_data, 'rb') as zip_file: + zip_handle = zipfile.ZipFile(zip_file) + zip_handle.extractall(dst_dir) + del zipfile, zip_file + + os.remove(dst_dir_data) + + sys.stdout.write("\nwritten: %r\n" % dst_dir) @staticmethod def commit(paths, message): diff --git a/webservice/bam/application/__init__.py b/webservice/bam/application/__init__.py index 248c975..430b450 100644 --- a/webservice/bam/application/__init__.py +++ b/webservice/bam/application/__init__.py @@ -220,9 +220,10 @@ class FileAPI(Resource): import zipfile tmp_extracted_folder = os.path.splitext(tmp_filepath)[0] - with open(tmp_filepath, 'rb') as zipped_file: - z = zipfile.ZipFile(zipped_file) - z.extractall(tmp_extracted_folder) + with open(tmp_filepath, 'rb') as zip_file: + zip_handle = zipfile.ZipFile(zip_file) + zip_handle.extractall(tmp_extracted_folder) + del zip_file, zip_handle with open(os.path.join(tmp_extracted_folder, '.bam_paths_remap.json'), 'r') as path_remap: path_remap = json.load(path_remap)