Avoid using non-atomic shutil.move
This commit is contained in:
parent
bb8e4cdbfa
commit
c788289567
1 changed files with 11 additions and 10 deletions
|
@ -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
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue