mirror of https://github.com/zulip/zulip.git
ruff: Fix SIM115 Use a context manager for opening files.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
parent
10271fb850
commit
71ca928ec9
|
@ -66,25 +66,24 @@ puppet_env["FACTER_zulip_scripts_path"] = scripts_path
|
||||||
def noop_would_change(puppet_cmd: list[str]) -> bool:
|
def noop_would_change(puppet_cmd: list[str]) -> bool:
|
||||||
# --noop does not work with --detailed-exitcodes; see
|
# --noop does not work with --detailed-exitcodes; see
|
||||||
# https://tickets.puppetlabs.com/browse/PUP-686
|
# https://tickets.puppetlabs.com/browse/PUP-686
|
||||||
|
with tempfile.NamedTemporaryFile() as lastrun_file:
|
||||||
try:
|
try:
|
||||||
lastrun_file = tempfile.NamedTemporaryFile()
|
|
||||||
subprocess.check_call(
|
subprocess.check_call(
|
||||||
# puppet_cmd may already contain --noop, but it is safe to
|
# puppet_cmd may already contain --noop, but it is safe to
|
||||||
# supply twice
|
# supply twice
|
||||||
[*puppet_cmd, "--noop", "--lastrunfile", lastrun_file.name],
|
[*puppet_cmd, "--noop", "--lastrunfile", lastrun_file.name],
|
||||||
env=puppet_env,
|
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:
|
with open(lastrun_file.name) as lastrun:
|
||||||
lastrun_data = yaml.safe_load(lastrun)
|
lastrun_data = yaml.safe_load(lastrun)
|
||||||
resources = lastrun_data.get("resources", {})
|
resources = lastrun_data.get("resources", {})
|
||||||
if resources.get("failed", 0) != 0:
|
if resources.get("failed", 0) != 0:
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
return resources.get("out_of_sync", 0) != 0
|
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:
|
if not args.noop and not args.force:
|
||||||
|
|
|
@ -24,7 +24,7 @@ def renumber_migration(conflicts: list[str], order: list[int], last_correct_migr
|
||||||
stack.append(conflicts[i - 1][0:4])
|
stack.append(conflicts[i - 1][0:4])
|
||||||
else:
|
else:
|
||||||
# Replace dependencies with the last correct migration
|
# Replace dependencies with the last correct migration
|
||||||
file = fileinput.FileInput("zerver/migrations/" + conflicts[i - 1], inplace=True)
|
with fileinput.FileInput("zerver/migrations/" + conflicts[i - 1], inplace=True) as file:
|
||||||
for line in file:
|
for line in file:
|
||||||
print(re.sub(r"[\d]+(_[a-z0-9]+)+", last_correct_migration, line), end="")
|
print(re.sub(r"[\d]+(_[a-z0-9]+)+", last_correct_migration, line), end="")
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ import os
|
||||||
import re
|
import re
|
||||||
import tempfile
|
import tempfile
|
||||||
from argparse import ArgumentParser, RawTextHelpFormatter
|
from argparse import ArgumentParser, RawTextHelpFormatter
|
||||||
|
from contextlib import ExitStack
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
@ -33,9 +34,10 @@ class Command(ZulipBaseCommand):
|
||||||
@override
|
@override
|
||||||
def handle(self, *args: Any, **options: Any) -> None:
|
def handle(self, *args: Any, **options: Any) -> None:
|
||||||
timestamp = timezone_now().strftime(TIMESTAMP_FORMAT)
|
timestamp = timezone_now().strftime(TIMESTAMP_FORMAT)
|
||||||
with tempfile.TemporaryDirectory(
|
with ExitStack() as stack:
|
||||||
prefix=f"zulip-backup-{timestamp}-",
|
tmp = stack.enter_context(
|
||||||
) as tmp:
|
tempfile.TemporaryDirectory(prefix=f"zulip-backup-{timestamp}-")
|
||||||
|
)
|
||||||
os.mkdir(os.path.join(tmp, "zulip-backup"))
|
os.mkdir(os.path.join(tmp, "zulip-backup"))
|
||||||
members = []
|
members = []
|
||||||
paths = []
|
paths = []
|
||||||
|
@ -121,10 +123,12 @@ class Command(ZulipBaseCommand):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if options["output"] is None:
|
if options["output"] is None:
|
||||||
tarball_path = tempfile.NamedTemporaryFile(
|
tarball_path = stack.enter_context(
|
||||||
|
tempfile.NamedTemporaryFile(
|
||||||
prefix=f"zulip-backup-{timestamp}-",
|
prefix=f"zulip-backup-{timestamp}-",
|
||||||
suffix=".tar.gz",
|
suffix=".tar.gz",
|
||||||
delete=False,
|
delete=False,
|
||||||
|
)
|
||||||
).name
|
).name
|
||||||
else:
|
else:
|
||||||
tarball_path = options["output"]
|
tarball_path = options["output"]
|
||||||
|
|
Loading…
Reference in New Issue