From 71ca928ec997df87018dacb00ffc810f700134d5 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Thu, 17 Oct 2024 17:59:35 -0700 Subject: [PATCH] ruff: Fix SIM115 Use a context manager for opening files. Signed-off-by: Anders Kaseorg --- scripts/zulip-puppet-apply | 23 +++++++++++------------ tools/renumber-migrations | 6 +++--- zerver/management/commands/backup.py | 18 +++++++++++------- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/scripts/zulip-puppet-apply b/scripts/zulip-puppet-apply index c6ba797347..9e3de36a0e 100755 --- a/scripts/zulip-puppet-apply +++ b/scripts/zulip-puppet-apply @@ -66,25 +66,24 @@ puppet_env["FACTER_zulip_scripts_path"] = scripts_path def noop_would_change(puppet_cmd: list[str]) -> bool: # --noop does not work with --detailed-exitcodes; see # https://tickets.puppetlabs.com/browse/PUP-686 - try: - lastrun_file = tempfile.NamedTemporaryFile() - subprocess.check_call( - # puppet_cmd may already contain --noop, but it is safe to - # supply twice - [*puppet_cmd, "--noop", "--lastrunfile", lastrun_file.name], - env=puppet_env, - ) + with tempfile.NamedTemporaryFile() as lastrun_file: + try: + subprocess.check_call( + # puppet_cmd may already contain --noop, but it is safe to + # supply twice + [*puppet_cmd, "--noop", "--lastrunfile", lastrun_file.name], + env=puppet_env, + ) + except subprocess.CalledProcessError: + sys.exit(2) + # Reopen the file since Puppet rewrote it with a new inode with open(lastrun_file.name) as lastrun: lastrun_data = yaml.safe_load(lastrun) resources = lastrun_data.get("resources", {}) if resources.get("failed", 0) != 0: sys.exit(2) return resources.get("out_of_sync", 0) != 0 - except subprocess.CalledProcessError: - sys.exit(2) - finally: - lastrun_file.close() if not args.noop and not args.force: diff --git a/tools/renumber-migrations b/tools/renumber-migrations index eb32ed4e60..1e664a45b3 100755 --- a/tools/renumber-migrations +++ b/tools/renumber-migrations @@ -24,9 +24,9 @@ def renumber_migration(conflicts: list[str], order: list[int], last_correct_migr stack.append(conflicts[i - 1][0:4]) else: # Replace dependencies with the last correct migration - file = fileinput.FileInput("zerver/migrations/" + conflicts[i - 1], inplace=True) - for line in file: - print(re.sub(r"[\d]+(_[a-z0-9]+)+", last_correct_migration, line), end="") + with fileinput.FileInput("zerver/migrations/" + conflicts[i - 1], inplace=True) as file: + for line in file: + print(re.sub(r"[\d]+(_[a-z0-9]+)+", last_correct_migration, line), end="") # Rename the migration indexing at the end new_name = conflicts[i - 1].replace( diff --git a/zerver/management/commands/backup.py b/zerver/management/commands/backup.py index 041d0e9838..193fe64050 100644 --- a/zerver/management/commands/backup.py +++ b/zerver/management/commands/backup.py @@ -2,6 +2,7 @@ import os import re import tempfile from argparse import ArgumentParser, RawTextHelpFormatter +from contextlib import ExitStack from typing import Any from django.conf import settings @@ -33,9 +34,10 @@ class Command(ZulipBaseCommand): @override def handle(self, *args: Any, **options: Any) -> None: timestamp = timezone_now().strftime(TIMESTAMP_FORMAT) - with tempfile.TemporaryDirectory( - prefix=f"zulip-backup-{timestamp}-", - ) as tmp: + with ExitStack() as stack: + tmp = stack.enter_context( + tempfile.TemporaryDirectory(prefix=f"zulip-backup-{timestamp}-") + ) os.mkdir(os.path.join(tmp, "zulip-backup")) members = [] paths = [] @@ -121,10 +123,12 @@ class Command(ZulipBaseCommand): try: if options["output"] is None: - tarball_path = tempfile.NamedTemporaryFile( - prefix=f"zulip-backup-{timestamp}-", - suffix=".tar.gz", - delete=False, + tarball_path = stack.enter_context( + tempfile.NamedTemporaryFile( + prefix=f"zulip-backup-{timestamp}-", + suffix=".tar.gz", + delete=False, + ) ).name else: tarball_path = options["output"]