From 8367cdc4a637e72adc80fd54c040a5c353530608 Mon Sep 17 00:00:00 2001 From: Nate Coraor Date: Mon, 25 Oct 2010 16:10:09 -0400 Subject: [PATCH] "Fix" zip uploads for Python < 2.6. The scare quotes are because in Python < 2.6 you can't read a zip member without loading the entire member into memory... --- tools/data_source/upload.py | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/tools/data_source/upload.py b/tools/data_source/upload.py index 3c5d2f9279f..a5e943d05ff 100644 --- a/tools/data_source/upload.py +++ b/tools/data_source/upload.py @@ -270,22 +270,36 @@ def add_file( dataset, registry, json_file, output_path ): stdout = 'ZIP file contained more than one file, only the first file was added to Galaxy.' break fd, uncompressed = tempfile.mkstemp( prefix='data_id_%s_upload_zip_' % dataset.dataset_id, dir=os.path.dirname( dataset.path ), text=False ) - zipped_file = z.open( name ) - while 1: + if sys.version_info[:2] >= ( 2, 6 ): + zipped_file = z.open( name ) + while 1: + try: + chunk = zipped_file.read( CHUNK_SIZE ) + except IOError: + os.close( fd ) + os.remove( uncompressed ) + file_err( 'Problem decompressing zipped data', dataset, json_file ) + return + if not chunk: + break + os.write( fd, chunk ) + os.close( fd ) + zipped_file.close() + uncompressed_name = name + unzipped = True + else: + # python < 2.5 doesn't have a way to read members in chunks(!) try: - chunk = zipped_file.read( CHUNK_SIZE ) + outfile = open( uncompressed, 'wb' ) + outfile.write( z.read( name ) ) + outfile.close() + uncompressed_name = name + unzipped = True except IOError: os.close( fd ) os.remove( uncompressed ) file_err( 'Problem decompressing zipped data', dataset, json_file ) return - if not chunk: - break - os.write( fd, chunk ) - os.close( fd ) - zipped_file.close() - uncompressed_name = name - unzipped = True z.close() # Replace the zipped file with the decompressed file if uncompressed is not None: