def get_instance(self, db): """ Decompresses the instance blob if necessary and returns it as string. EDACC can store compressed and uncompressed instances. To distinguish between them, we prepend the ASCII characters "LZMA" to a compressed instance. """ table = db.metadata.tables['Instances'] c_instance = table.c['instance'] c_id = table.c['idInstance'] # get prefix instance_header = db.session.connection().execute(select([func.substring(c_instance, 1, 4)], c_id == self.idInstance).select_from( table)).first()[0] data_length = db.session.connection().execute(select([func.length(c_instance)], c_id == self.idInstance).select_from( table)).first()[0] if data_length > 32 * 1024 * 1024: return "Instance too large for processing. Please use the EDACC GUI application." if instance_header == 'LZMA': # compressed instance? # get blob without LZMA prefix instance_blob = db.session.connection().execute(select([func.substring(c_instance, 5)], c_id == self.idInstance).select_from( table)).first()[0] return utils.lzma_decompress(instance_blob) else: return self.instance
def test_lzma_compression(self): from edacc import utils uncompressed_data = "TestData with \x12\x65 weird bytes \n and everything" compressed_data = utils.lzma_compress(uncompressed_data) decompressed_data = utils.lzma_decompress(compressed_data) assert decompressed_data == uncompressed_data assert len(compressed_data) > 13 # there should always be a 13 bytes header len_bytes = struct.unpack('<Q', compressed_data[5:13])[0] assert len_bytes == len(uncompressed_data)
def test_lzma_compression(self): from edacc import utils uncompressed_data = "TestData with \x12\x65 weird bytes \n and everything" compressed_data = utils.lzma_compress(uncompressed_data) decompressed_data = utils.lzma_decompress(compressed_data) assert decompressed_data == uncompressed_data assert len( compressed_data) > 13 # there should always be a 13 bytes header len_bytes = struct.unpack('<Q', compressed_data[5:13])[0] assert len_bytes == len(uncompressed_data)