22
22
import six
23
23
24
24
from . import copy , errors , fsencode , iotools , move , tools , walk , wildcard
25
+ from .copy import copy_modified_time
25
26
from .glob import BoundGlobber
26
27
from .mode import validate_open_mode
27
28
from .path import abspath , join , normpath
@@ -393,15 +394,23 @@ def close(self):
393
394
"""
394
395
self ._closed = True
395
396
396
- def copy (self , src_path , dst_path , overwrite = False ):
397
- # type: (Text, Text, bool) -> None
397
+ def copy (
398
+ self ,
399
+ src_path , # type: Text
400
+ dst_path , # type: Text
401
+ overwrite = False , # type: bool
402
+ preserve_time = False , # type: bool
403
+ ):
404
+ # type: (...) -> None
398
405
"""Copy file contents from ``src_path`` to ``dst_path``.
399
406
400
407
Arguments:
401
408
src_path (str): Path of source file.
402
409
dst_path (str): Path to destination file.
403
410
overwrite (bool): If `True`, overwrite the destination file
404
411
if it exists (defaults to `False`).
412
+ preserve_time (bool): If `True`, try to preserve mtime of the
413
+ resource (defaults to `False`).
405
414
406
415
Raises:
407
416
fs.errors.DestinationExists: If ``dst_path`` exists,
@@ -417,16 +426,26 @@ def copy(self, src_path, dst_path, overwrite=False):
417
426
with closing (self .open (src_path , "rb" )) as read_file :
418
427
# FIXME(@althonos): typing complains because open return IO
419
428
self .upload (dst_path , read_file ) # type: ignore
429
+ if preserve_time :
430
+ copy_modified_time (self , src_path , self , dst_path )
420
431
421
- def copydir (self , src_path , dst_path , create = False ):
422
- # type: (Text, Text, bool) -> None
432
+ def copydir (
433
+ self ,
434
+ src_path , # type: Text
435
+ dst_path , # type: Text
436
+ create = False , # type: bool
437
+ preserve_time = False , # type: bool
438
+ ):
439
+ # type: (...) -> None
423
440
"""Copy the contents of ``src_path`` to ``dst_path``.
424
441
425
442
Arguments:
426
443
src_path (str): Path of source directory.
427
444
dst_path (str): Path to destination directory.
428
445
create (bool): If `True`, then ``dst_path`` will be created
429
446
if it doesn't exist already (defaults to `False`).
447
+ preserve_time (bool): If `True`, try to preserve mtime of the
448
+ resource (defaults to `False`).
430
449
431
450
Raises:
432
451
fs.errors.ResourceNotFound: If the ``dst_path``
@@ -440,7 +459,7 @@ def copydir(self, src_path, dst_path, create=False):
440
459
raise errors .ResourceNotFound (dst_path )
441
460
if not self .getinfo (src_path ).is_dir :
442
461
raise errors .DirectoryExpected (src_path )
443
- copy .copy_dir (self , src_path , self , dst_path )
462
+ copy .copy_dir (self , src_path , self , dst_path , preserve_time = preserve_time )
444
463
445
464
def create (self , path , wipe = False ):
446
465
# type: (Text, bool) -> bool
@@ -1027,15 +1046,17 @@ def lock(self):
1027
1046
"""
1028
1047
return self ._lock
1029
1048
1030
- def movedir (self , src_path , dst_path , create = False ):
1031
- # type: (Text, Text, bool) -> None
1049
+ def movedir (self , src_path , dst_path , create = False , preserve_time = False ):
1050
+ # type: (Text, Text, bool, bool ) -> None
1032
1051
"""Move directory ``src_path`` to ``dst_path``.
1033
1052
1034
1053
Arguments:
1035
1054
src_path (str): Path of source directory on the filesystem.
1036
1055
dst_path (str): Path to destination directory.
1037
1056
create (bool): If `True`, then ``dst_path`` will be created
1038
1057
if it doesn't exist already (defaults to `False`).
1058
+ preserve_time (bool): If `True`, try to preserve mtime of the
1059
+ resources (defaults to `False`).
1039
1060
1040
1061
Raises:
1041
1062
fs.errors.ResourceNotFound: if ``dst_path`` does not exist,
@@ -1047,7 +1068,7 @@ def movedir(self, src_path, dst_path, create=False):
1047
1068
with self ._lock :
1048
1069
if not create and not self .exists (dst_path ):
1049
1070
raise errors .ResourceNotFound (dst_path )
1050
- move .move_dir (self , src_path , self , dst_path )
1071
+ move .move_dir (self , src_path , self , dst_path , preserve_time = preserve_time )
1051
1072
1052
1073
def makedirs (
1053
1074
self ,
@@ -1092,8 +1113,8 @@ def makedirs(
1092
1113
raise
1093
1114
return self .opendir (path )
1094
1115
1095
- def move (self , src_path , dst_path , overwrite = False ):
1096
- # type: (Text, Text, bool) -> None
1116
+ def move (self , src_path , dst_path , overwrite = False , preserve_time = False ):
1117
+ # type: (Text, Text, bool, bool ) -> None
1097
1118
"""Move a file from ``src_path`` to ``dst_path``.
1098
1119
1099
1120
Arguments:
@@ -1102,6 +1123,8 @@ def move(self, src_path, dst_path, overwrite=False):
1102
1123
file will be written to.
1103
1124
overwrite (bool): If `True`, destination path will be
1104
1125
overwritten if it exists.
1126
+ preserve_time (bool): If `True`, try to preserve mtime of the
1127
+ resources (defaults to `False`).
1105
1128
1106
1129
Raises:
1107
1130
fs.errors.FileExpected: If ``src_path`` maps to a
@@ -1128,11 +1151,15 @@ def move(self, src_path, dst_path, overwrite=False):
1128
1151
except OSError :
1129
1152
pass
1130
1153
else :
1154
+ if preserve_time :
1155
+ copy_modified_time (self , src_path , self , dst_path )
1131
1156
return
1132
1157
with self ._lock :
1133
1158
with self .open (src_path , "rb" ) as read_file :
1134
1159
# FIXME(@althonos): typing complains because open return IO
1135
1160
self .upload (dst_path , read_file ) # type: ignore
1161
+ if preserve_time :
1162
+ copy_modified_time (self , src_path , self , dst_path )
1136
1163
self .remove (src_path )
1137
1164
1138
1165
def open (
0 commit comments