python: Convert os.open(…, O_EXCL) to open(…, "x").

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2020-10-19 18:14:06 -07:00
parent 358f1f9ba7
commit 3a8cf869db
2 changed files with 5 additions and 7 deletions

View File

@ -85,13 +85,10 @@ def get_or_create_key_prefix() -> str:
filename = os.path.join(settings.DEPLOY_ROOT, "var", "remote_cache_prefix")
try:
fd = os.open(filename, os.O_CREAT | os.O_EXCL | os.O_RDWR, 0o444)
prefix = secrets.token_hex(16) + ':'
# This does close the underlying file
with os.fdopen(fd, 'w') as f:
with open(filename, 'x') as f:
prefix = secrets.token_hex(16) + ':'
f.write(prefix + "\n")
except OSError:
# The file already exists
except FileExistsError:
tries = 1
while tries < 10:
with open(filename) as f:

View File

@ -173,7 +173,8 @@ class Command(ZulipBaseCommand):
tarball_path = output_dir.rstrip("/") + ".tar.gz"
try:
os.close(os.open(tarball_path, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o666))
with open(tarball_path, "x"):
pass
except FileExistsError:
raise CommandError(f"Refusing to overwrite existing tarball: {tarball_path}. Aborting...")