use context manager for tempfile

This commit is contained in:
Robert Kaussow 2024-10-21 20:25:28 +02:00 committed by renovate[bot]
parent 607f201888
commit 468d452996

View File

@ -174,11 +174,10 @@ class PrometheusSD:
output.append(host.to_sd_json()) output.append(host.to_sd_json())
# Write to tmp file and move after write # Write to tmp file and move after write
temp_file = tempfile.NamedTemporaryFile(mode="w", prefix="prometheus-pve-sd", delete=False) with tempfile.NamedTemporaryFile(mode="w", prefix="prometheus-pve-sd", delete=False) as tf:
with temp_file as tf:
json.dump(output, tf, indent=4) json.dump(output, tf, indent=4)
shutil.move(temp_file.name, self.config.config["output_file"]) shutil.move(tf.name, self.config.config["output_file"])
chmod(self.config.config["output_file"], int(self.config.config["output_file_mode"], 8)) chmod(self.config.config["output_file"], int(self.config.config["output_file_mode"], 8))
def _terminate(self, signal, frame): # noqa def _terminate(self, signal, frame): # noqa