66
77import typing
88
9- from . import open_fs
109from .copy import copy_dir
1110from .copy import copy_file
1211from .errors import FSError
1312from .opener import manage_fs
13+ from .osfs import OSFS
1414from .path import frombase
1515from ._pathcompat import commonpath
1616
@@ -46,7 +46,7 @@ def move_file(
4646 dst_fs , # type: Union[Text, FS]
4747 dst_path , # type: Text
4848 preserve_time = False , # type: bool
49- cleanup_dest_on_error = True , # type: bool
49+ cleanup_dst_on_error = True , # type: bool
5050):
5151 # type: (...) -> None
5252 """Move a file from one filesystem to another.
@@ -58,8 +58,8 @@ def move_file(
5858 dst_path (str): Path to a file on ``dst_fs``.
5959 preserve_time (bool): If `True`, try to preserve mtime of the
6060 resources (defaults to `False`).
61- cleanup_dest_on_error (bool): If `True`, tries to delete the file copied to
62- dst_fs if deleting the file from src_fs fails.
61+ cleanup_dst_on_error (bool): If `True`, tries to delete the file copied to
62+ ` dst_fs` if deleting the file from ` src_fs` fails (defaults to `True`) .
6363
6464 """
6565 with manage_fs (src_fs , writeable = True ) as _src_fs :
@@ -78,12 +78,13 @@ def move_file(
7878 src_syspath = _src_fs .getsyspath (src_path )
7979 dst_syspath = _dst_fs .getsyspath (dst_path )
8080 common = commonpath ([src_syspath , dst_syspath ])
81- rel_src = frombase (common , src_syspath )
82- rel_dst = frombase (common , dst_syspath )
83- with _src_fs .lock (), _dst_fs .lock ():
84- with open_fs (common , writeable = True ) as base :
85- base .move (rel_src , rel_dst , preserve_time = preserve_time )
86- return # optimization worked, exit early
81+ if common :
82+ rel_src = frombase (common , src_syspath )
83+ rel_dst = frombase (common , dst_syspath )
84+ with _src_fs .lock (), _dst_fs .lock ():
85+ with OSFS (common ) as base :
86+ base .move (rel_src , rel_dst , preserve_time = preserve_time )
87+ return # optimization worked, exit early
8788 except ValueError :
8889 # This is raised if we cannot find a common base folder.
8990 # In this case just fall through to the standard method.
@@ -103,7 +104,7 @@ def move_file(
103104 except FSError as e :
104105 # if the source cannot be removed we delete the copy on the
105106 # destination
106- if cleanup_dest_on_error :
107+ if cleanup_dst_on_error :
107108 _dst_fs .remove (dst_path )
108109 raise e
109110
@@ -130,22 +131,16 @@ def move_dir(
130131 resources (defaults to `False`).
131132
132133 """
133-
134- def src ():
135- return manage_fs (src_fs , writeable = True )
136-
137- def dst ():
138- return manage_fs (dst_fs , writeable = True , create = True )
139-
140- with src () as _src_fs , dst () as _dst_fs :
141- with _src_fs .lock (), _dst_fs .lock ():
142- _dst_fs .makedir (dst_path , recreate = True )
143- copy_dir (
144- src_fs ,
145- src_path ,
146- dst_fs ,
147- dst_path ,
148- workers = workers ,
149- preserve_time = preserve_time ,
150- )
151- _src_fs .removetree (src_path )
134+ with manage_fs (src_fs , writeable = True ) as _src_fs :
135+ with manage_fs (dst_fs , writeable = True , create = True ) as _dst_fs :
136+ with _src_fs .lock (), _dst_fs .lock ():
137+ _dst_fs .makedir (dst_path , recreate = True )
138+ copy_dir (
139+ src_fs ,
140+ src_path ,
141+ dst_fs ,
142+ dst_path ,
143+ workers = workers ,
144+ preserve_time = preserve_time ,
145+ )
146+ _src_fs .removetree (src_path )
0 commit comments