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 obj
(src, dst, *, follow_symlinks=True)
| 341 | pass |
| 342 | |
| 343 | def copystat(src, dst, *, follow_symlinks=True): |
| 344 | """Copy file metadata |
| 345 | |
| 346 | Copy the permission bits, last access time, last modification time, and |
| 347 | flags from `src` to `dst`. On Linux, copystat() also copies the "extended |
| 348 | attributes" where possible. The file contents, owner, and group are |
| 349 | unaffected. `src` and `dst` are path-like objects or path names given as |
| 350 | strings. |
| 351 | |
| 352 | If the optional flag `follow_symlinks` is not set, symlinks aren't |
| 353 | followed if and only if both `src` and `dst` are symlinks. |
| 354 | """ |
| 355 | sys.audit("shutil.copystat", src, dst) |
| 356 | |
| 357 | def _nop(*args, ns=None, follow_symlinks=None): |
| 358 | pass |
| 359 | |
| 360 | # follow symlinks (aka don't not follow symlinks) |
| 361 | follow = follow_symlinks or not (_islink(src) and os.path.islink(dst)) |
| 362 | if follow: |
| 363 | # use the real function if it exists |
| 364 | def lookup(name): |
| 365 | return getattr(os, name, _nop) |
| 366 | else: |
| 367 | # use the real function only if it exists |
| 368 | # *and* it supports follow_symlinks |
| 369 | def lookup(name): |
| 370 | fn = getattr(os, name, _nop) |
| 371 | if fn in os.supports_follow_symlinks: |
| 372 | return fn |
| 373 | return _nop |
| 374 | |
| 375 | if isinstance(src, os.DirEntry): |
| 376 | st = src.stat(follow_symlinks=follow) |
| 377 | else: |
| 378 | st = lookup("stat")(src, follow_symlinks=follow) |
| 379 | mode = stat.S_IMODE(st.st_mode) |
| 380 | lookup("utime")(dst, ns=(st.st_atime_ns, st.st_mtime_ns), |
| 381 | follow_symlinks=follow) |
| 382 | # We must copy extended attributes before the file is (potentially) |
| 383 | # chmod()'ed read-only, otherwise setxattr() will error with -EACCES. |
| 384 | _copyxattr(src, dst, follow_symlinks=follow) |
| 385 | _chmod = lookup("chmod") |
| 386 | if os.name == 'nt': |
| 387 | if follow: |
| 388 | if os.path.islink(dst): |
| 389 | dst = os.path.realpath(dst, strict=True) |
| 390 | else: |
| 391 | def _chmod(*args, **kwargs): |
| 392 | os.chmod(*args) |
| 393 | try: |
| 394 | _chmod(dst, mode, follow_symlinks=follow) |
| 395 | except NotImplementedError: |
| 396 | # if we got a NotImplementedError, it's because |
| 397 | # * follow_symlinks=False, |
| 398 | # * lchown() is unavailable, and |
| 399 | # * either |
| 400 | # * fchownat() is unavailable or |