def _do_clone(self): """ Cloning operation itself. Assumes that @see self.to_clone was populated before. """ # Get the mappings in source project, in order to determines the useful columns custom_mapping = ProjectMapping().load_from_project(self.prj) obj_mapping = custom_mapping.object_mappings used_columns = set(obj_mapping.real_cols_to_tsv.keys()) used_columns.add("orig_id") # By safety # Create a DB writer writer = DBWriter(self.session) # Narrow the writes in ObjectFields thanks to mappings of original project writer.generators({"obj_field": used_columns}) # Use import helpers dest_prj_id = self.dest_prj.projid import_how = ImportHow(prj_id=dest_prj_id, update_mode="No", custom_mapping=ProjectMapping(), skip_object_duplicates=False, loaded_files=[]) # Get parent (enclosing) Sample, Acquisition, Process. There should be 0 in this context... import_how.existing_parents = InBundle.fetch_existing_parents( self.session, prj_id=dest_prj_id) self._clone_all(import_how, writer) # Copy mappings to destination. We could narrow them to the minimum? custom_mapping.write_to_project(self.dest_prj)
def _send_to_writer(self, import_how: ImportHow, writer: DBWriter, db_tuple: DBObjectTupleT): """ Send a single tuple from DB to DB :param import_how: :param writer: :param db_tuple: :return: """ obj_orm, fields_orm, cnn_features_orm, image_orm, sample_orm, acquisition_orm, process_orm = db_tuple # Transform all to key-less beans so they can be absorbed by DBWriter obj, fields, cnn_features, image, sample, acquisition, process = \ bean_of(obj_orm), bean_of(fields_orm), bean_of(cnn_features_orm), \ bean_of(image_orm), bean_of(sample_orm), \ bean_of(acquisition_orm), bean_of(process_orm) assert obj is not None and fields is not None # A few fields need adjustment obj.img0id = None # Cut images if asked so if not self.req.do_images: image = None # Write parent entities assert sample and acquisition and process dict_of_parents = {Sample.__tablename__: sample, Acquisition.__tablename__: acquisition, Process.__tablename__: process} TSVFile.add_parent_objects(import_how, self.session, obj, dict_of_parents) # Write object and children new_records = TSVFile.create_or_link_slaves(how=import_how, session=self.session, object_head_to_write=obj, object_fields_to_write=fields, image_to_write=image) writer.add_db_entities(obj, fields, image, new_records) # Keep track of existing objects if new_records > 1: # We now have an Id from sequences, so ref. it. import_how.existing_objects[obj.orig_id] = obj.objid if cnn_features is not None: writer.add_cnn_features(obj, cnn_features) # Do images if new_records > 0 and self.req.do_images and image and image.file_name is not None: # We have an image, with a new imgid but old paths have been copied old_imgpath = Path(self.vault.path_to(image.file_name)) image.file_name = None # In case, don't reference a non-existing file try: sub_path = self.vault.store_image(old_imgpath, image.imgid) image.file_name = sub_path except FileNotFoundError: pass # Proceed to thumbnail if any if image.thumb_file_name is not None: old_thumbnail_path = self.vault.path_to(image.thumb_file_name) thumb_relative_path, thumb_full_path = self.vault.thumbnail_paths(image.imgid) image.thumb_file_name = None # In case, don't reference a non-existing file try: # TODO: Call a primitive in Vault instead shutil.copyfile(old_thumbnail_path, thumb_full_path) image.thumb_file_name = thumb_relative_path except FileNotFoundError: pass
def do_run(self, current_user_id: int) -> ImportRealRsp: """ Do the real job using injected parameters. :return: """ # Security check RightsBO.user_wants(self.session, current_user_id, Action.ADMINISTRATE, self.prj_id) # OK loaded_files = none_to_empty(self.prj.fileloaded).splitlines() logger.info("Previously loaded files: %s", loaded_files) # Save mappings straight away self.save_mapping(self.custom_mapping) source_bundle = InBundle( self.req.source_path, Path(self.temp_for_task.data_dir_for(self.task_id))) # Configure the import to come, destination db_writer = DBWriter(self.session) import_where = ImportWhere( db_writer, self.vault, self.temp_for_task.base_dir_for(self.task_id)) # Configure the import to come, directives import_how = ImportHow(self.prj_id, self.req.update_mode, self.custom_mapping, self.req.skip_existing_objects, loaded_files) import_how.taxo_mapping = self.req.taxo_mappings import_how.taxo_found = self.req.found_taxa import_how.found_users = self.req.found_users if self.req.skip_loaded_files: import_how.compute_skipped(source_bundle, logger) if not self.req.skip_existing_objects: with CodeTimer("run: Existing images for %d: " % self.prj_id, logger): import_how.objects_and_images_to_skip = Image.fetch_existing_images( self.session, self.prj_id) import_how.do_thumbnail_above(int(self.config['THUMBSIZELIMIT'])) # Do the bulk job of import row_count = source_bundle.do_import(import_where, import_how, self.req.rowcount, self.report_progress) # Update loaded files in DB, removing duplicates self.prj.fileloaded = "\n".join(set(import_how.loaded_files)) self.session.commit() # Recompute stats ProjectBO.do_after_load(self.session, self.prj_id) self.session.commit() logger.info("Total of %d rows loaded" % row_count) # Prepare response ret = ImportRealRsp() return ret
def do_real(self) -> None: """ Do the real job, i.e. write everywhere (DB/filesystem) """ loaded_files = none_to_empty(self.prj.fileloaded).splitlines() logger.info("Previously loaded files: %s", loaded_files) found_users, taxo_found, col_mapping_dict, \ nb_rows, source_path = self._load_vars_from_state(self.STATE_KEYS) # Save mappings straight away col_mapping = ProjectMapping().load_from_dict(col_mapping_dict) col_mapping.write_to_project(self.prj) self.session.commit() # TODO: Duplicated code source_bundle = InBundle( source_path, Path(self.temp_for_jobs.data_dir_for(self.job_id))) # Configure the import to come, destination db_writer = DBWriter(self.session) import_where = ImportWhere( db_writer, self.vault, self.temp_for_jobs.base_dir_for(self.job_id)) # Configure the import to come, directives import_how = ImportHow(self.prj_id, self.req.update_mode, col_mapping, self.req.skip_existing_objects, loaded_files) import_how.taxo_mapping = self.req.taxo_mappings import_how.found_taxa = taxo_found import_how.found_users = found_users if self.req.skip_loaded_files: import_how.compute_skipped(source_bundle, logger) if self.req.skip_existing_objects: # If we must skip existing objects then do an inventory of what's in already with CodeTimer("run: Existing images for %d: " % self.prj_id, logger): import_how.objects_and_images_to_skip = Image.fetch_existing_images( self.session, self.prj_id) import_how.do_thumbnail_above(int(self.config['THUMBSIZELIMIT'])) # Do the bulk job of import rowcount_from_validate = nb_rows row_count = source_bundle.do_import(import_where, import_how, rowcount_from_validate, self.report_progress) # Update loaded files in DB, removing duplicates self.prj.fileloaded = "\n".join(set(import_how.loaded_files)) self.session.commit() # Recompute stats ProjectBO.do_after_load(self.session, self.prj_id) self.session.commit() msg = "Total of %d rows loaded" % row_count logger.info(msg) self.set_job_result(errors=[], infos={"rowcount": row_count})
def do_import(self): """ Do the real job, i.e. copy files while creating records. """ errors = [] self.manage_uploaded() self.unzip_if_needed() # Use a Bundle source_bundle = InBundle( self.source_dir_or_zip, Path(self.temp_for_task.data_dir_for(self.task_id))) # Clean it, in case the ZIP contains a CSV source_bundle.remove_all_tsvs() images = source_bundle.list_image_files() # Configure the import to come, destination db_writer = DBWriter(self.session) import_where = ImportWhere( db_writer, self.vault, self.temp_for_task.base_dir_for(self.task_id)) # Configure the import to come, directives import_how = ImportHow(prj_id=self.prj_id, update_mode="", custom_mapping=ProjectMapping(), skip_object_duplicates=False, loaded_files=[]) import_how.do_thumbnail_above(int(self.config['THUMBSIZELIMIT'])) # Generate TSV req_values = self.req.values if req_values.get(SimpleImportFields.userlb, ""): import_how.found_users["user"] = { "id": req_values.get(SimpleImportFields.userlb) } req_values[SimpleImportFields.userlb] = "user" if req_values.get(SimpleImportFields.status, ""): req_values[SimpleImportFields.status] = classif_qual.get( req_values[SimpleImportFields.status], "") self.make_tsv(source_bundle, images) # Import nb_image_files = len(images) nb_images = source_bundle.do_import(import_where, import_how, nb_image_files, self.report_progress) self.session.commit() # Recompute stats and so on ProjectBO.do_after_load(self.session, self.prj_id) self.session.commit() ret = SimpleImportRsp(errors=errors, nb_images=nb_images) return ret