Copy file metadata Copy the permission bits, last access time, last modification time, and flags from `src` to `dst`. On Linux, copystat() also copies the "extended attributes" where possible. The file contents, owner, and group are unaffected. `src` and `dst` are path-like objects
(src, dst, *, follow_symlinks=True)
| 407 | pass |
| 408 | |
| 409 | def copystat(src, dst, *, follow_symlinks=True): |
| 410 | """Copy file metadata |
| 411 | |
| 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. |
| 417 | |
| 418 | If the optional flag `follow_symlinks` is not set, symlinks aren't |
| 419 | followed if and only if both `src` and `dst` are symlinks. |
| 420 | """ |
| 421 | sys.audit("shutil.copystat", src, dst) |
| 422 | |
| 423 | def _nop(*args, ns=None, follow_symlinks=None): |
| 424 | pass |
| 425 | |
| 426 | # follow symlinks (aka don't not follow symlinks) |
| 427 | follow = follow_symlinks or not (_islink(src) and os.path.islink(dst)) |
| 428 | if follow: |
| 429 | # use the real function if it exists |
| 430 | def lookup(name): |
| 431 | return getattr(os, name, _nop) |
| 432 | else: |
| 433 | # use the real function only if it exists |
| 434 | # *and* it supports follow_symlinks |
| 435 | def lookup(name): |
| 436 | fn = getattr(os, name, _nop) |
| 437 | if fn in os.supports_follow_symlinks: |
| 438 | return fn |
| 439 | return _nop |
| 440 | |
| 441 | if isinstance(src, os.DirEntry): |
| 442 | st = src.stat(follow_symlinks=follow) |
| 443 | else: |
| 444 | st = lookup("stat")(src, follow_symlinks=follow) |
| 445 | mode = stat.S_IMODE(st.st_mode) |
| 446 | lookup("utime")(dst, ns=(st.st_atime_ns, st.st_mtime_ns), |
| 447 | follow_symlinks=follow) |
| 448 | # We must copy extended attributes before the file is (potentially) |
| 449 | # chmod()'ed read-only, otherwise setxattr() will error with -EACCES. |
| 450 | _copyxattr(src, dst, follow_symlinks=follow) |
| 451 | try: |
| 452 | lookup("chmod")(dst, mode, follow_symlinks=follow) |
| 453 | except NotImplementedError: |
| 454 | # if we got a NotImplementedError, it's because |
| 455 | # * follow_symlinks=False, |
| 456 | # * lchown() is unavailable, and |
| 457 | # * either |
| 458 | # * fchownat() is unavailable or |
| 459 | # * fchownat() doesn't implement AT_SYMLINK_NOFOLLOW. |
| 460 | # (it returned ENOSUP.) |
| 461 | # therefore we're out of options--we simply cannot chown the |
| 462 | # symlink. give up, suppress the error. |
| 463 | # (which is what shutil always did in this circumstance.) |
| 464 | pass |
| 465 | if hasattr(st, 'st_flags'): |
| 466 | try: |