Skip to content

Commit a75c576

Browse files
committed
cleanup on mv failure
1 parent bfdd42e commit a75c576

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

library/utils/shell_utils.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,29 @@
99
from library.utils.log_utils import log
1010

1111

12-
def rename_move_file(source_file, destination_file, simulate=False):
12+
def rename_move_file(src, dst, simulate=False):
1313
if simulate:
14-
print("mv", source_file, destination_file)
14+
print("mv", src, dst)
1515
else:
1616
try:
17-
os.rename(source_file, destination_file) # performance
17+
return shutil.move(src, dst)
18+
except FileNotFoundError:
19+
log.error("FileNotFoundError. %s", src)
1820
except PermissionError:
19-
log.warning("PermissionError. Could not rename %s into %s", source_file, os.path.dirname(destination_file))
21+
log.warning("PermissionError. Could not move %s into %s", src, os.path.dirname(dst))
22+
# raise
2023
except OSError as excinfo:
21-
if excinfo.errno == errno.ENOENT:
22-
try:
23-
os.makedirs(os.path.dirname(destination_file), exist_ok=True)
24-
os.rename(source_file, destination_file) # try again
25-
except FileNotFoundError:
26-
log.error("FileNotFoundError. %s", source_file)
27-
except OSError as excinfo:
28-
if excinfo.errno == errno.EXDEV: # Cross-device
29-
shutil.move(source_file, destination_file) # Fallback to shutil.move
30-
else:
31-
raise
32-
elif excinfo.errno == errno.EXDEV: # Cross-device
33-
shutil.move(source_file, destination_file) # Fallback to shutil.move
34-
else:
35-
raise
24+
if excinfo.errno == errno.ENOENT: # no parent folder
25+
parent = os.path.dirname(dst)
26+
if not parent or not os.path.exists(src):
27+
raise
28+
os.makedirs(parent, exist_ok=True)
29+
return rename_move_file(src, dst, simulate)
30+
31+
if excinfo.errno in (errno.EIO, errno.ENOSPC, errno.EDQUOT, errno.EFBIG, errno.ENOMEM):
32+
if os.path.isfile(src) and os.path.isfile(dst):
33+
os.unlink(dst)
34+
raise
3635

3736

3837
def rename_no_replace(src, dst):

0 commit comments

Comments
 (0)