def launch(self, data_file): """ Execute import operations: unpack ZIP, build and store generic DataType objects. :param data_file: an archive (ZIP / HDF5) containing the `DataType` :raises LaunchException: when data_file is None, nonexistent, or invalid \ (e.g. incomplete meta-data, not in ZIP / HDF5 format etc. ) """ if data_file is None: raise LaunchException( "Please select file which contains data to import") if os.path.exists(data_file): if zipfile.is_zipfile(data_file): current_op = dao.get_operation_by_id(self.operation_id) # Creates a new TMP folder where to extract data tmp_folder = os.path.join(self.storage_path, "tmp_import") FilesHelper().unpack_zip(data_file, tmp_folder) operations = ImportService().import_project_operations( current_op.project, self.storage_path) shutil.rmtree(tmp_folder) self.nr_of_datatypes += len(operations) else: # upgrade file if necessary file_update_manager = FilesUpdateManager() file_update_manager.upgrade_file(data_file) folder, h5file = os.path.split(data_file) manager = HDF5StorageManager(folder, h5file) if manager.is_valid_hdf5_file(): datatype = None try: service = ImportService() datatype = service.load_datatype_from_file( folder, h5file, self.operation_id, final_storage=self.storage_path) service.store_datatype(datatype) self.nr_of_datatypes += 1 except Exception as excep: # If import operation failed delete file from disk. if datatype is not None and os.path.exists( datatype.get_storage_file_path()): os.remove(datatype.get_storage_file_path()) self.log.exception(excep) raise LaunchException( "Invalid file received as input. Most probably incomplete " "meta-data ... " + str(excep)) else: raise LaunchException( "Uploaded file: %s is neither in ZIP or HDF5 format" % data_file) else: raise LaunchException("File: %s to import does not exists." % data_file)
def launch(self, view_model): # type: (TVBImporterModel) -> [] """ Execute import operations: unpack ZIP, build and store generic DataType objects. :raises LaunchException: when data_file is None, nonexistent, or invalid \ (e.g. incomplete meta-data, not in ZIP / HDF5 format etc. ) """ if view_model.data_file is None: raise LaunchException( "Please select file which contains data to import") service = ImportService() if os.path.exists(view_model.data_file): if zipfile.is_zipfile(view_model.data_file): current_op = dao.get_operation_by_id(self.operation_id) # Creates a new TMP folder where to extract data tmp_folder = os.path.join(self.storage_path, "tmp_import") FilesHelper().unpack_zip(view_model.data_file, tmp_folder) operations = service.import_project_operations( current_op.project, tmp_folder) shutil.rmtree(tmp_folder) self.nr_of_datatypes += len(operations) else: # upgrade file if necessary file_update_manager = FilesUpdateManager() file_update_manager.upgrade_file(view_model.data_file) folder, h5file = os.path.split(view_model.data_file) manager = HDF5StorageManager(folder, h5file) if manager.is_valid_hdf5_file(): datatype = None try: datatype = service.load_datatype_from_file( view_model.data_file, self.operation_id) service.check_import_references( view_model.data_file, datatype) service.store_datatype(datatype, view_model.data_file) self.nr_of_datatypes += 1 except ImportException as excep: self.log.exception(excep) if datatype is not None: target_path = h5.path_for_stored_index(datatype) if os.path.exists(target_path): os.remove(target_path) raise LaunchException( "Invalid file received as input. " + str(excep)) else: raise LaunchException( "Uploaded file: %s is neither in ZIP or HDF5 format" % view_model.data_file) else: raise LaunchException("File: %s to import does not exists." % view_model.data_file)
def launch(self, data_file): """ Execute import operations: unpack ZIP, build and store generic DataType objects. :param data_file: an archive (ZIP / HDF5) containing the `DataType` :raises LaunchException: when data_file is None, nonexistent, or invalid \ (e.g. incomplete meta-data, not in ZIP / HDF5 format etc. ) """ if data_file is None: raise LaunchException("Please select file which contains data to import") if os.path.exists(data_file): if zipfile.is_zipfile(data_file): current_op = dao.get_operation_by_id(self.operation_id) # Creates a new TMP folder where to extract data tmp_folder = os.path.join(self.storage_path, "tmp_import") FilesHelper().unpack_zip(data_file, tmp_folder) operations = ImportService().import_project_operations(current_op.project, self.storage_path) shutil.rmtree(tmp_folder) self.nr_of_datatypes += len(operations) else: # upgrade file if necessary file_update_manager = FilesUpdateManager() file_update_manager.upgrade_file(data_file) folder, h5file = os.path.split(data_file) manager = HDF5StorageManager(folder, h5file) if manager.is_valid_hdf5_file(): datatype = None try: service = ImportService() datatype = service.load_datatype_from_file(folder, h5file, self.operation_id) service.store_datatype(datatype) self.nr_of_datatypes += 1 except Exception as excep: # If import operation failed delete file from disk. if datatype is not None and os.path.exists(datatype.get_storage_file_path()): os.remove(datatype.get_storage_file_path()) self.log.exception(excep) raise LaunchException("Invalid file received as input. Most probably incomplete " "meta-data ... " + str(excep)) else: raise LaunchException("Uploaded file: %s is neither in ZIP or HDF5 format" % data_file) else: raise LaunchException("File: %s to import does not exists." % data_file)