From c788289567d89084ab165523c032ce237c920d78 Mon Sep 17 00:00:00 2001 From: Blallo Date: Wed, 15 Sep 2021 22:02:12 +0200 Subject: [PATCH] Avoid using non-atomic shutil.move --- techrec/forge.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/techrec/forge.py b/techrec/forge.py index d9c5855..bbb193e 100644 --- a/techrec/forge.py +++ b/techrec/forge.py @@ -3,7 +3,6 @@ import logging import tempfile import os from datetime import datetime, timedelta -import shutil from subprocess import Popen from time import sleep from typing import Callable, Optional @@ -25,8 +24,7 @@ async def get_timefile_exact(time) -> str: remote_repo, time.strftime(get_config()["AUDIO_INPUT_FORMAT"]) ) if remote_path.startswith("http://") or remote_path.startswith("https://"): - logger.info(f"downloading {remote_path}") - print(f"DOWNLOADING -> {remote_path}") + logger.info(f"downloading: {remote_path}") local = await download(remote_path) return local return local_path @@ -125,7 +123,8 @@ async def create_mp3( raise OSError("file '%s' already exists" % outfile) for path, _s, _e in intervals: if not os.path.exists(path): - raise OSError("file '%s' does not exist; recording system broken?" % path) + raise OSError( + "file '%s' does not exist; recording system broken?" % path) # metadata date/time formatted according to # https://wiki.xiph.org/VorbisComment#Date_and_time @@ -133,7 +132,8 @@ async def create_mp3( if outfile.endswith(".mp3"): metadata["TRDC"] = start.replace(microsecond=0).isoformat() metadata["RECORDINGTIME"] = metadata["TRDC"] - metadata["ENCODINGTIME"] = datetime.now().replace(microsecond=0).isoformat() + metadata["ENCODINGTIME"] = datetime.now().replace( + microsecond=0).isoformat() else: metadata["DATE"] = start.replace(microsecond=0).isoformat() metadata["ENCODER"] = "https://git.lattuga.net/techbloc/techrec" @@ -154,7 +154,11 @@ async def create_mp3( prefix, suffix = os.path.basename(outfile).split(".", 1) tmp_file = tempfile.NamedTemporaryFile( - suffix=".%s" % suffix, prefix="forge-%s" % prefix, delete=False + suffix=".%s" % suffix, + prefix="forge-%s" % prefix, + delete=False, + # This is needed to avoid errors with the rename across different mounts + dir=os.path.dirname(outfile), ) cmd = ( mp3_join(intervals) @@ -187,10 +191,7 @@ async def create_mp3( if not validator(start, end, tmp_file.name): os.unlink(tmp_file.name) return False - try: - os.rename(tmp_file.name, outfile) - except OSError: - shutil.move(tmp_file.name, outfile) + os.rename(tmp_file.name, outfile) return True