def stash_logs(job_defs, success_jobs, failure_jobs, queue_name, method='local', job_name_prefix=None, tag='stash', ids_stashed=None):
    if (ids_stashed is None):
        ids_stashed = set()
    success_ids = _get_job_ids_to_stash(success_jobs, ids_stashed)
    failure_ids = _get_job_ids_to_stash(failure_jobs, ids_stashed)
    if (method == 's3'):
        s3_client = boto3.client('s3')

        def stash_log(log_str, name_base):
            name = ('%s_%s.log' % (name_base, tag))
            s3_client.put_object(Bucket=bucket_name, Key=('reading_results/%s/logs/%s/%s' % (job_name_prefix, queue_name, name)), Body=log_str)
    elif (method == 'local'):
        if (job_name_prefix is None):
            job_name_prefix = ('batch_%s' % tag)
        dirname = ('%s_job_logs' % job_name_prefix)
        os.mkdir(dirname)

        def stash_log(log_str, name_base):
            with open(os.path.join(dirname, (name_base + '.log')), 'w') as f:
                f.write(log_str)
    else:
        raise ValueError(('Invalid method: %s' % method))
    for (jobId, job_def_tpl) in job_defs.items():
        if ((jobId not in success_ids) and (jobId not in failure_ids)):
            continue
        try:
            job_def = dict(job_def_tpl)
            lines = get_job_log(job_def, write_file=False)
            if (lines is None):
                logger.warning(('No logs found for %s.' % job_def['jobName']))
                continue
            log_str = ''.join(lines)
            base_name = job_def['jobName']
            if (job_def['jobId'] in success_ids):
                base_name += '/SUCCESS'
            elif (job_def['jobId'] in failure_ids):
                base_name += '/FAILED'
            else:
                logger.error('Job cannot be logged unless completed.')
                continue
            logger.info(('Stashing ' + base_name))
            stash_log(log_str, base_name)
        except Exception as e:
            logger.error(('Failed to save logs for: %s' % str(job_def_tpl)))
            logger.exception(e)
    ids_stashed |= {jid for jids in [success_ids, failure_ids] for jid in jids}
    return
def stash_logs(job_defs, success_jobs, failure_jobs, queue_name, method='local', job_name_prefix=None, tag='stash', ids_stashed=None):
    if (ids_stashed is None):
        ids_stashed = set()
    success_ids = _get_job_ids_to_stash(success_jobs, ids_stashed)
    failure_ids = _get_job_ids_to_stash(failure_jobs, ids_stashed)
    if (method == 's3'):
        s3_client = boto3.client('s3')

        def stash_log(log_str, name_base):
            name = ('%s_%s.log' % (name_base, tag))
            s3_client.put_object(Bucket=bucket_name, Key=('reading_results/%s/logs/%s/%s' % (job_name_prefix, queue_name, name)), Body=log_str)
    elif (method == 'local'):
        if (job_name_prefix is None):
            job_name_prefix = ('batch_%s' % tag)
        dirname = ('%s_job_logs' % job_name_prefix)
        os.mkdir(dirname)

        def stash_log(log_str, name_base):
            with open(os.path.join(dirname, (name_base + '.log')), 'w') as f:
                f.write(log_str)
    else:
        raise ValueError(('Invalid method: %s' % method))
    for (jobId, job_def_tpl) in job_defs.items():
        if ((jobId not in success_ids) and (jobId not in failure_ids)):
            continue
        try:
            job_def = dict(job_def_tpl)
            lines = get_job_log(job_def, write_file=False)
            if (lines is None):
                logger.warning(('No logs found for %s.' % job_def['jobName']))
                continue
            log_str = .join(lines)
            base_name = job_def['jobName']
            if (job_def['jobId'] in success_ids):
                base_name += '/SUCCESS'
            elif (job_def['jobId'] in failure_ids):
                base_name += '/FAILED'
            else:
                logger.error('Job cannot be logged unless completed.')
                continue
            logger.info(('Stashing ' + base_name))
            stash_log(log_str, base_name)
        except Exception as e:
            logger.error(('Failed to save logs for: %s' % str(job_def_tpl)))
            logger.exception(e)
    ids_stashed |= {jid for jids in [success_ids, failure_ids] for jid in jids}
    return