Skip to content

Commit 20f612f

Browse files
committed
add preserve_timestamps to shutil.copystat and shutil.copy2
1 parent 7739943 commit 20f612f

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

Doc/library/shutil.rst

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,15 @@ Directory and files operations
122122
.. versionchanged:: 3.3
123123
Added *follow_symlinks* argument.
124124

125-
.. function:: copystat(src, dst, *, follow_symlinks=True)
125+
.. function:: copystat(src, dst, *, follow_symlinks=True, preserve_timestamps=True)
126126

127-
Copy the permission bits, last access time, last modification time, and
128-
flags from *src* to *dst*. On Linux, :func:`copystat` also copies the
129-
"extended attributes" where possible. The file contents, owner, and
130-
group are unaffected. *src* and *dst* are :term:`path-like objects <path-like object>` or path
127+
Copy the permission bits and flags from *src* to *dst*; the last access
128+
time and last modification time are also copied if *preserve_timestamps*
129+
is true (on Windows platforms, *preserve_timestamps* is ignored).
130+
On Linux, :func:`copystat` also copies the "extended attributes"
131+
where possible. The file contents, owner, and group are unaffected.
132+
133+
*src* and *dst* are :term:`path-like objects <path-like object>` or path
131134
names given as strings.
132135

133136
If *follow_symlinks* is false, and *src* and *dst* both
@@ -169,6 +172,9 @@ Directory and files operations
169172
.. versionchanged:: 3.3
170173
Added *follow_symlinks* argument and support for Linux extended attributes.
171174

175+
.. versionchanged:: next
176+
Added the *preserve_timestamps* argument.
177+
172178
.. function:: copy(src, dst, *, follow_symlinks=True)
173179

174180
Copies the file *src* to the file or directory *dst*. *src* and *dst*
@@ -201,7 +207,7 @@ Directory and files operations
201207
copy the file more efficiently. See
202208
:ref:`shutil-platform-dependent-efficient-copy-operations` section.
203209

204-
.. function:: copy2(src, dst, *, follow_symlinks=True)
210+
.. function:: copy2(src, dst, *, follow_symlinks=True, preserve_timestamps=True)
205211

206212
Identical to :func:`~shutil.copy` except that :func:`copy2`
207213
also attempts to preserve file metadata.
@@ -215,6 +221,9 @@ Directory and files operations
215221
it can; :func:`copy2` never raises an exception because it
216222
cannot preserve file metadata.
217223

224+
On non-Windows platforms, if *preserve_timestamps* is false,
225+
the last access time and last modification time are not copied.
226+
218227
:func:`copy2` uses :func:`copystat` to copy the file metadata.
219228
Please see :func:`copystat` for more information
220229
about platform support for modifying symbolic link metadata.
@@ -233,6 +242,9 @@ Directory and files operations
233242
copy the file more efficiently. See
234243
:ref:`shutil-platform-dependent-efficient-copy-operations` section.
235244

245+
.. versionadded:: next
246+
Added the *preserve_timestamps* argument.
247+
236248
.. function:: ignore_patterns(*patterns)
237249

238250
This factory function creates a function that can be used as a callable for

Lib/shutil.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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

533536
def ignore_patterns(*patterns):

0 commit comments

Comments
 (0)