8000 GH-73991: Support preserving metadata in `pathlib.Path.copy()` by barneygale · Pull Request #120806 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-73991: Support preserving metadata in pathlib.Path.copy() #120806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jul 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename methods.
  • Loading branch information
barneygale committed Jul 3, 2024
commit f5846438af47486d493c3dffd35c64f475148897
4 changes: 2 additions & 2 deletions Doc/library/pathlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1551,8 +1551,8 @@ Copying, renaming and deleting
If *preserve_metadata* is false (the default), only the file data is
guaranteed to be copied. Set *preserve_metadata* to true to ensure that the
file mode (permissions), flags, last access and modification times, and
extended attributes are all copied where supported. This argument has no
effect on Windows, where metadata is always preserved when copying.
extended attributes are copied where supported. This argument has no effect
on Windows, where metadata is always preserved when copying.

.. versionadded:: 3.14

Expand Down
17 changes: 9 additions & 8 deletions Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,28 +781,29 @@ def mkdir(self, mode=0o777, parents=False, exist_ok=False):
"""
raise UnsupportedOperation(self._unsupported_msg('mkdir()'))

_metadata_keys = frozenset()
# Metadata keys supported by this path type.
_readable_metadata = _writable_metadata = frozenset()

def _get_metadata(self, keys, follow_symlinks):
def _read_metadata(self, keys, follow_symlinks):
"""
Returns path metadata as a dict with string keys.
"""
raise UnsupportedOperation(self._unsupported_msg('_get_metadata()'))
raise UnsupportedOperation(self._unsupported_msg('_read_metadata()'))

def _set_metadata(self, metadata, follow_symlinks):
def _write_metadata(self, metadata, follow_symlinks):
"""
Sets path metadata from the given dict with string keys.
"""
raise UnsupportedOperation(self._unsupported_msg('_set_metadata()'))
raise UnsupportedOperation(self._unsupported_msg('_write_metadata()'))

def _copy_metadata(self, target, follow_symlinks):
"""
Copies metadata (permissions, timestamps, etc) from this path to target.
"""
metadata_keys = self._metadata_keys & target._metadata_keys
metadata_keys = self._readable_metadata & target._writable_metadata
if metadata_keys:
metadata = self._get_metadata(metadata_keys, follow_symlinks)
target._set_metadata(metadata, follow_symlinks)
metadata = self._read_metadata(metadata_keys, follow_symlinks)
target._write_metadata(metadata, follow_symlinks)

def copy(self, target, *, follow_symlinks=True, preserve_metadata=False):
"""
Expand Down
8 changes: 4 additions & 4 deletions Lib/pathlib/_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
grp = None

from ._os import (UnsupportedOperation, copyfile, file_metadata_keys,
get_file_metadata, set_file_metadata)
read_file_metadata, write_file_metadata)
from ._abc import PurePathBase, PathBase


Expand Down Expand Up @@ -782,9 +782,9 @@ def mkdir(self, mode=0o777, parents=False, exist_ok=False):
if not exist_ok or not self.is_dir():
raise

_metadata_keys = file_metadata_keys
_get_metadata = get_file_metadata
_set_metadata = set_file_metadata
_readable_metadata = _writable_metadata = file_metadata_keys
_read_metadata = read_file_metadata
_write_metadata = write_file_metadata

if copyfile:
def copy(self, target, *, follow_symlinks=True, preserve_metadata=False):
Expand Down
5 changes: 3 additions & 2 deletions Lib/pathlib/_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ def copyfileobj(source_f, target_f):
write_target(buf)


# Kinds of metadata supported by the operating system.
file_metadata_keys = {'mode', 'times_ns'}
if hasattr(os.stat_result, 'st_flags'):
file_metadata_keys.add('flags')
Expand All @@ -188,7 +189,7 @@ def copyfileobj(source_f, target_f):
file_metadata_keys = frozenset(file_metadata_keys)


def get_file_metadata(path, keys, follow_symlinks):
def read_file_metadata(path, keys, follow_symlinks):
"""
Returns local path metadata as a dict with string keys.
"""
Expand All @@ -214,7 +215,7 @@ def get_file_metadata(path, keys, follow_symlinks):
return result


def set_file_metadata(path, metadata, follow_symlinks):
def write_file_metadata(path, metadata, follow_symlinks):
"""
Sets local path metadata from the given dict with string keys.
"""
Expand Down
Loading
0