@@ -406,14 +406,15 @@ def _copyxattr(src, dst, *, follow_symlinks=True):
406406 def _copyxattr (* args , ** kwargs ):
407407 pass
408408
409- def copystat (src , dst , * , follow_symlinks = True ):
409+ def copystat (src , dst , * , follow_symlinks = True , preserve_timestamps = True ):
410410 """Copy file metadata
411411
412- Copy the permission bits, last access time, last modification time, and
413- flags from `src` to `dst`. On Linux, copystat() also copies the "extended
414- attributes" where possible. The file contents, owner, and group are
415- unaffected. `src` and `dst` are path-like objects or path names given as
416- strings.
412+ Copy the permission bits and flags from `src` to `dst`; the last access
413+ time and last modification time are also copied if `preserve_timestamps`
414+ is true. On Linux, copystat() also copies the "extended attributes" where
415+ possible. The file contents, owner, and group are unaffected.
416+
417+ `src` and `dst` are path-like objects or path names given as strings.
417418
418419 If the optional flag `follow_symlinks` is not set, symlinks aren't
419420 followed if and only if both `src` and `dst` are symlinks.
@@ -443,8 +444,9 @@ def lookup(name):
443444 else :
444445 st = lookup ("stat" )(src , follow_symlinks = follow )
445446 mode = stat .S_IMODE (st .st_mode )
446- lookup ("utime" )(dst , ns = (st .st_atime_ns , st .st_mtime_ns ),
447- follow_symlinks = follow )
447+ if preserve_timestamps :
448+ lookup ("utime" )(dst , ns = (st .st_atime_ns , st .st_mtime_ns ),
449+ follow_symlinks = follow )
448450 # We must copy extended attributes before the file is (potentially)
449451 # chmod()'ed read-only, otherwise setxattr() will error with -EACCES.
450452 _copyxattr (src , dst , follow_symlinks = follow )
@@ -490,7 +492,7 @@ def copy(src, dst, *, follow_symlinks=True):
490492 copymode (src , dst , follow_symlinks = follow_symlinks )
491493 return dst
492494
493- def copy2 (src , dst , * , follow_symlinks = True ):
495+ def copy2 (src , dst , * , follow_symlinks = True , preserve_timestamps = True ):
494496 """Copy data and metadata. Return the file's destination.
495497
496498 Metadata is copied with copystat(). Please see the copystat function
@@ -527,7 +529,8 @@ def copy2(src, dst, *, follow_symlinks=True):
527529 raise
528530
529531 copyfile (src , dst , follow_symlinks = follow_symlinks )
530- copystat (src , dst , follow_symlinks = follow_symlinks )
532+ copystat (src , dst , follow_symlinks = follow_symlinks ,
533+ preserve_timestamps = preserve_timestamps )
531534 return dst
532535
533536def ignore_patterns (* patterns ):
0 commit comments