|
9 | 9 | from library.utils.log_utils import log |
10 | 10 |
|
11 | 11 |
|
12 | | -def rename_move_file(source_file, destination_file, simulate=False): |
| 12 | +def rename_move_file(src, dst, simulate=False): |
13 | 13 | if simulate: |
14 | | - print("mv", source_file, destination_file) |
| 14 | + print("mv", src, dst) |
15 | 15 | else: |
16 | 16 | try: |
17 | | - os.rename(source_file, destination_file) # performance |
| 17 | + return shutil.move(src, dst) |
| 18 | + except FileNotFoundError: |
| 19 | + log.error("FileNotFoundError. %s", src) |
18 | 20 | 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 |
20 | 23 | 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 |
36 | 35 |
|
37 | 36 |
|
38 | 37 | def rename_no_replace(src, dst): |
|
0 commit comments