'Copy and return the given HDA.'

def copy(self, hda, history=None, **kwargs):

    copy = model.HistoryDatasetAssociation(name=hda.name, info=hda.info, blurb=hda.blurb, peek=hda.peek, tool_version=hda.tool_version, extension=hda.extension, dbkey=hda.dbkey, dataset=hda.dataset, visible=hda.visible, deleted=hda.deleted, parent_id=kwargs.get('parent_id', None))
    if history:
        history.add_dataset(copy)
    copy.copied_from_history_dataset_association = hda
    copy.set_size()
    original_annotation = self.annotation(hda)
    self.annotate(copy, original_annotation, user=history.user)
    self.session().add(copy)
    self.session().flush()
    copy.metadata = hda.metadata
    if (not hda.datatype.copy_safe_peek):
        copy.set_peek()
    self.session().flush()
    original_tags = self.get_tags(hda)
    self.set_tags(copy, original_tags, user=history.user)
    return copy
