From 485bb49aec48b4f8f5108e1a40880d5b6fc70be4 Mon Sep 17 00:00:00 2001 From: Aidan Epstein Date: Sat, 24 Feb 2024 22:40:49 -0800 Subject: [PATCH] Use shutil.move() to move files. --- beets/util/__init__.py | 38 ++------------------------------------ 1 file changed, 2 insertions(+), 36 deletions(-) diff --git a/beets/util/__init__.py b/beets/util/__init__.py index 00558e90acc..cf378b694bb 100644 --- a/beets/util/__init__.py +++ b/beets/util/__init__.py @@ -24,7 +24,6 @@ import shutil import subprocess import sys -import tempfile import traceback from collections import Counter, namedtuple from enum import Enum @@ -503,10 +502,7 @@ def copy(path: bytes, dest: bytes, replace: bool = False): def move(path: bytes, dest: bytes, replace: bool = False): """Rename a file. `dest` may not be a directory. If `dest` already exists, raises an OSError unless `replace` is True. Has no effect if - `path` is the same as `dest`. If the paths are on different - filesystems (or the rename otherwise fails), a copy is attempted - instead, in which case metadata will *not* be preserved. Paths are - translated to system paths. + `path` is the same as `dest`. Paths are translated to system paths. """ if os.path.isdir(syspath(path)): raise FilesystemError("source is directory", "move", (path, dest)) @@ -517,37 +513,7 @@ def move(path: bytes, dest: bytes, replace: bool = False): if os.path.exists(syspath(dest)) and not replace: raise FilesystemError("file exists", "rename", (path, dest)) - # First, try renaming the file. - try: - os.replace(syspath(path), syspath(dest)) - except OSError: - # Copy the file to a temporary destination. - basename = os.path.basename(bytestring_path(dest)) - dirname = os.path.dirname(bytestring_path(dest)) - tmp = tempfile.NamedTemporaryFile( - suffix=syspath(b".beets", prefix=False), - prefix=syspath(b"." + basename, prefix=False), - dir=syspath(dirname), - delete=False, - ) - try: - with open(syspath(path), "rb") as f: - shutil.copyfileobj(f, tmp) - finally: - tmp.close() - - # Move the copied file into place. - try: - os.replace(tmp.name, syspath(dest)) - tmp = None - os.remove(syspath(path)) - except OSError as exc: - raise FilesystemError( - exc, "move", (path, dest), traceback.format_exc() - ) - finally: - if tmp is not None: - os.remove(tmp) + shutil.move(syspath(path), syspath(dest)) def link(path: bytes, dest: bytes, replace: bool = False):