def METHOD_NAME(self, metric_id, data):
    tmpfile = tempfile.NamedTemporaryFile(
        prefix='gnocchi', dir=self.basepath_tmp,
        delete=False)
    tmpfile.write(data)
    tmpfile.close()
    path = self._build_measure_path(metric_id, True)
    while True:
        try:
            os.rename(tmpfile.name, path)
            break
        except OSError as e:
            if e.errno != errno.ENOENT:
                raise
            try:
                os.mkdir(self._build_measure_path(metric_id))
            except OSError as e:
                # NOTE(jd) It's possible that another process created the
                # path just before us! In this case, good for us, let's do
                # nothing then! (see bug #1475684)
                if e.errno != errno.EEXIST:
                    raise