Source code for workbench.workers.unzip

''' Unzip worker '''
import zipfile
import zerorpc
import pprint
import os
    from cStringIO import StringIO
except ImportError:
    from StringIO import StringIO

class Unzip(object):
[docs] ''' This worker unzips a zipped file ''' dependencies = ['sample'] def __init__(self): self.workbench = zerorpc.Client(timeout=300, heartbeat=60) self.workbench.connect("tcp://") def execute(self, input_data):
[docs] ''' Execute the Unzip worker ''' raw_bytes = input_data['sample']['raw_bytes'] zipfile_output = zipfile.ZipFile(StringIO(raw_bytes)) payload_md5s = [] for name in zipfile_output.namelist(): filename = os.path.basename(name) payload_md5s.append(self.workbench.store_sample(, name, 'unknown')) return {'payload_md5s': payload_md5s} def __del__(self):
[docs] ''' Class Cleanup ''' # Close zeroRPC client self.workbench.close() # Unit test: Create the class, the proper input and run the execute() method for a test def test():
[docs] ''' Unit test''' # This worker test requires a local server running workbench = zerorpc.Client(timeout=300, heartbeat=60) workbench.connect("tcp://") # Generate input for the worker data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../data/zip/') md5 = workbench.store_sample(open(data_path, 'rb').read(), '', 'zip') data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../data/zip/') md5_2 = workbench.store_sample(open(data_path, 'rb').read(), '', 'zip') input_data = workbench.get_sample(md5) input_data_2 = workbench.get_sample(md5_2) # Execute the worker (unit test) worker = Unzip() output = worker.execute(input_data) print '\n<<< Unit Test >>>' pprint.pprint(output) # For coverage output = worker.execute(input_data_2) # Execute the worker (server test) output = workbench.work_request('unzip', md5) print '\n<<< Server Test >>>' pprint.pprint(output) if __name__ == "__main__":