def tempfile_writer(target):
    '''write cache data to a temporary location.  when writing is
    complete, rename the file to the actual location.  delete
    the temporary file on any error'''
    tmp = target.parent / ('_%s' % target.name)
    try:
        with tmp.open('wb') as fd:
            yield fd
    except:
        tmp.unlink()
        raise
    LOG.debug('rename %s -> %s', tmp, target)
    tmp.rename(target)
write cache data to a temporary location.  when writing is
    complete, rename the file to the actual location.  delete
    the temporary file on any error