diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2012-05-12 17:02:01 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2012-05-12 17:02:01 (GMT) |
commit | 424246fbf3953ccdb3b34961db007a453873a3ea (patch) | |
tree | 64fc5c2f67d12ae1ca24201d0569748f7df9352b /Lib/shutil.py | |
parent | 4d688e3275d5a10e2321571392cdc31945bb8a89 (diff) | |
download | cpython-424246fbf3953ccdb3b34961db007a453873a3ea.zip cpython-424246fbf3953ccdb3b34961db007a453873a3ea.tar.gz cpython-424246fbf3953ccdb3b34961db007a453873a3ea.tar.bz2 |
Issue #14082: shutil.copy2() now copies extended attributes, if possible.
Patch by Hynek Schlawack.
Diffstat (limited to 'Lib/shutil.py')
-rw-r--r-- | Lib/shutil.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/Lib/shutil.py b/Lib/shutil.py index 9625d36..ce60c3b 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -166,6 +166,36 @@ def copystat(src, dst, symlinks=False): else: raise +if hasattr(os, 'listxattr'): + def _copyxattr(src, dst, symlinks=False): + """Copy extended filesystem attributes from `src` to `dst`. + + Overwrite existing attributes. + + If the optional flag `symlinks` is set, symlinks won't be followed. + + """ + if symlinks: + listxattr = os.llistxattr + removexattr = os.lremovexattr + setxattr = os.lsetxattr + getxattr = os.lgetxattr + else: + listxattr = os.listxattr + removexattr = os.removexattr + setxattr = os.setxattr + getxattr = os.getxattr + + for attr in listxattr(src): + try: + setxattr(dst, attr, getxattr(src, attr)) + except OSError as e: + if e.errno not in (errno.EPERM, errno.ENOTSUP, errno.ENODATA): + raise +else: + def _copyxattr(*args, **kwargs): + pass + def copy(src, dst, symlinks=False): """Copy data and mode bits ("cp src dst"). @@ -193,6 +223,7 @@ def copy2(src, dst, symlinks=False): dst = os.path.join(dst, os.path.basename(src)) copyfile(src, dst, symlinks=symlinks) copystat(src, dst, symlinks=symlinks) + _copyxattr(src, dst, symlinks=symlinks) def ignore_patterns(*patterns): """Function that can be used as copytree() ignore parameter. |