def heal(script, metric, data): """Heal the metric in metric with the WSP data stored as a string in data.""" path = toPath(script.options.prefix, metric) error = False # Make a tmp file fd, filename = tempfile.mkstemp(prefix="whisper-backup") fd = os.fdopen(fd, "wb") fd.write(data) fd.close() # Figure out what to do if os.path.exists(path): logger.debug("Healing existing whisper file: %s" % path) try: fill_archives(filename, path, time.time()) except Exception as e: logger.warning("Exception during heal of %s will overwrite." % path) logger.warning(str(e)) error = True # Last ditch effort, we just copy the file in place if error or not os.path.exists(path): logger.debug("Copying restored DB file into place") try: os.makedirs(os.path.dirname(path)) except os.error: # Directory exists pass shutil.copyfile(filename, path) os.unlink(filename)