'Perform setup for job to export a history into an archive. Method generates
attribute files for export, sets the corresponding attributes in the jeha
object, and returns a command line for running the job. The command line
includes the command, inputs, and options; it does not include the output
file because it must be set at runtime.'

def setup_job(self, trans, jeha, include_hidden=False, include_deleted=False):

    def get_item_tag_dict(item):
        "    Create    dictionary    of    an    item's    tags.    "
        tags = {}
        for tag in item.tags:
            tag_user_tname = to_unicode(tag.user_tname)
            tag_user_value = to_unicode(tag.user_value)
            tags[tag_user_tname] = tag_user_value
        return tags
    def prepare_metadata(metadata):
        '    Prepare    metatdata    for    exporting.    '
        for (name, value) in list(metadata.items()):
            if isinstance(value, trans.app.model.MetadataFile):
                del metadata[name]
        return metadata
    class HistoryDatasetAssociationEncoder(json.JSONEncoder, ):
        '    Custom    JSONEncoder    for    a    HistoryDatasetAssociation.    '
        def default(self, obj):
            '    Encode    an    HDA,    default    encoding    for    everything    else.    '
            if isinstance(obj, trans.app.model.HistoryDatasetAssociation):
                rval = {'__HistoryDatasetAssociation__': True, 'create_time': obj.create_time.__str__(), 'update_time': obj.update_time.__str__(), 'hid': obj.hid, 'name': to_unicode(obj.name), 'info': to_unicode(obj.info), 'blurb': obj.blurb, 'peek': obj.peek, 'extension': obj.extension, 'metadata': prepare_metadata(dict(obj.metadata.items())), 'parent_id': obj.parent_id, 'designation': obj.designation, 'deleted': obj.deleted, 'visible': obj.visible, 'file_name': obj.file_name, 'uuid': (lambda uuid: (str(uuid) if uuid else None))(obj.dataset.uuid), 'annotation': to_unicode(getattr(obj, 'annotation', '')), 'tags': get_item_tag_dict(obj), 'extra_files_path': obj.extra_files_path}
                if ((not obj.visible) and (not include_hidden)):
                    rval['exported'] = False
                elif (obj.deleted and (not include_deleted)):
                    rval['exported'] = False
                else:
                    rval['exported'] = True
                return rval
            return json.JSONEncoder.default(self, obj)
    temp_output_dir = tempfile.mkdtemp()
    history = jeha.history
    history_attrs = {'create_time': history.create_time.__str__(), 'update_time': history.update_time.__str__(), 'name': to_unicode(history.name), 'hid_counter': history.hid_counter, 'genome_build': history.genome_build, 'annotation': to_unicode(self.get_item_annotation_str(trans.sa_session, history.user, history)), 'tags': get_item_tag_dict(history), 'includes_hidden_datasets': include_hidden, 'includes_deleted_datasets': include_deleted}
    history_attrs_filename = tempfile.NamedTemporaryFile(dir=temp_output_dir).name
    history_attrs_out = open(history_attrs_filename, 'w')
    history_attrs_out.write(dumps(history_attrs))
    history_attrs_out.close()
    jeha.history_attrs_filename = history_attrs_filename
    datasets = self.get_history_datasets(trans, history)
    included_datasets = []
    datasets_attrs = []
    provenance_attrs = []
    for dataset in datasets:
        dataset.annotation = self.get_item_annotation_str(trans.sa_session, history.user, dataset)
        if (((not dataset.visible) and (not include_hidden)) or (dataset.deleted and (not include_deleted))):
            provenance_attrs.append(dataset)
        else:
            datasets_attrs.append(dataset)
            included_datasets.append(dataset)
    datasets_attrs_filename = tempfile.NamedTemporaryFile(dir=temp_output_dir).name
    datasets_attrs_out = open(datasets_attrs_filename, 'w')
    datasets_attrs_out.write(dumps(datasets_attrs, cls=HistoryDatasetAssociationEncoder))
    datasets_attrs_out.close()
    jeha.datasets_attrs_filename = datasets_attrs_filename
    provenance_attrs_out = open((datasets_attrs_filename + '.provenance'), 'w')
    provenance_attrs_out.write(dumps(provenance_attrs, cls=HistoryDatasetAssociationEncoder))
    provenance_attrs_out.close()
    jobs_dict = {}
    for hda in included_datasets:
        job_hda = hda
        while job_hda.copied_from_history_dataset_association:
            job_hda = job_hda.copied_from_history_dataset_association
        if (not job_hda.creating_job_associations):
            continue
        job = None
        for assoc in job_hda.creating_job_associations:
            job = assoc.job
            break
        if (not job):
            continue
        jobs_dict[job.id] = job
    jobs_attrs = []
    for (id, job) in jobs_dict.items():
        job_attrs = {}
        job_attrs['tool_id'] = job.tool_id
        job_attrs['tool_version'] = job.tool_version
        job_attrs['state'] = job.state
        job_attrs['info'] = job.info
        job_attrs['traceback'] = job.traceback
        job_attrs['command_line'] = job.command_line
        job_attrs['stderr'] = job.stderr
        job_attrs['stdout'] = job.stdout
        job_attrs['exit_code'] = job.exit_code
        job_attrs['create_time'] = job.create_time.isoformat()
        job_attrs['update_time'] = job.update_time.isoformat()
        try:
            params_objects = job.get_param_values(trans.app)
        except:
            continue
        params_dict = {}
        for (name, value) in params_objects.items():
            params_dict[name] = value
        job_attrs['params'] = params_dict
        input_datasets = []
        input_mapping = {}
        for assoc in job.input_datasets:
            if assoc.dataset:
                input_datasets.append(assoc.dataset.hid)
                input_mapping[assoc.name] = assoc.dataset.hid
        job_attrs['input_datasets'] = input_datasets
        job_attrs['input_mapping'] = input_mapping
        output_datasets = [assoc.dataset.hid for assoc in job.output_datasets]
        job_attrs['output_datasets'] = output_datasets
        jobs_attrs.append(job_attrs)
    jobs_attrs_filename = tempfile.NamedTemporaryFile(dir=temp_output_dir).name
    jobs_attrs_out = open(jobs_attrs_filename, 'w')
    jobs_attrs_out.write(dumps(jobs_attrs, cls=HistoryDatasetAssociationEncoder))
    jobs_attrs_out.close()
    jeha.jobs_attrs_filename = jobs_attrs_filename
    options = ''
    if jeha.compressed:
        options = '-G'
    return ('%s    %s    %s    %s' % (options, history_attrs_filename, datasets_attrs_filename, jobs_attrs_filename))
