def save_to_subunit(sla_records, subunit_filename):
    LOG.debug('Writing subunit stream to: %s', subunit_filename)
    fd = None
    state2subunit = {sla.STATE_TRUE: 'success',
                     sla.STATE_FALSE: 'fail'}
    try:
        fd = open(subunit_filename, 'w')
        output = subunit_v2.StreamResultToBytes(fd)

        for item in sla_records:
            output.startTestRun()
            test_id = _get_location(item.record) + ':' + item.expression

            if item.state != sla.STATE_TRUE:
                output.status(test_id=test_id, file_name='results',
                              mime_type='text/plain; charset="utf8"', eof=True,
                              file_bytes=yaml.safe_dump(
                                  item.record, default_flow_style=False))

            output.status(test_id=test_id,
                          test_status=state2subunit.get(item.state, 'skip'))
            output.stopTestRun()

        LOG.info('Subunit stream saved to: %s', subunit_filename)
    except __HOLE__ as e:
        LOG.error('Error writing subunit stream: %s', e)
    finally:
        if fd:
            fd.close()
dataset/ETHPy150Open openstack/shaker/shaker/engine/report.py/save_to_subunit