Skip to content

bpo-35652 Add use_srcentry parameter to shutil.copytree() II #11425

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion Doc/library/shutil.rst
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ Directory and files operations

.. function:: copytree(src, dst, symlinks=False, ignore=None, \
copy_function=copy2, ignore_dangling_symlinks=False, \
dirs_exist_ok=False)
dirs_exist_ok=False, use_srcentry=True)

Recursively copy an entire directory tree rooted at *src* to a directory
named *dst* and return the destination directory. *dirs_exist_ok* dictates
Expand Down Expand Up @@ -248,6 +248,9 @@ Directory and files operations
each file. It will be called with the source path and the destination path
as arguments. By default, :func:`shutil.copy2` is used, but any function
that supports the same signature (like :func:`shutil.copy`) can be used.
If *use_srcentry* is true, the *copy_function* gets the srcentry, otherwise
it gets the srcname. The functions copy() and copy2() can handle the
srcentry.

.. versionchanged:: 3.3
Copy metadata when *symlinks* is false.
Expand All @@ -263,6 +266,8 @@ Directory and files operations
Platform-specific fast-copy syscalls may be used internally in order to
copy the file more efficiently. See
:ref:`shutil-platform-dependent-efficient-copy-operations` section.
Added *use_srcentry* parameter to control if the srcentry or the srcname
is passed to the *copy_function*.

.. versionadded:: 3.8
The *dirs_exist_ok* parameter.
Expand Down
13 changes: 9 additions & 4 deletions Lib/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,15 +432,14 @@ def _ignore_patterns(path, names):
return _ignore_patterns

def _copytree(entries, src, dst, symlinks, ignore, copy_function,
ignore_dangling_symlinks, dirs_exist_ok=False):
ignore_dangling_symlinks, dirs_exist_ok, use_srcentry):
if ignore is not None:
ignored_names = ignore(src, set(os.listdir(src)))
else:
ignored_names = set()

os.makedirs(dst, exist_ok=dirs_exist_ok)
errors = []
use_srcentry = copy_function is copy2 or copy_function is copy

for srcentry in entries:
if srcentry.name in ignored_names:
Expand Down Expand Up @@ -490,7 +489,8 @@ def _copytree(entries, src, dst, symlinks, ignore, copy_function,
return dst

def copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2,
ignore_dangling_symlinks=False, dirs_exist_ok=False):
ignore_dangling_symlinks=False, dirs_exist_ok=False,
use_srcentry=True):
"""Recursively copy a directory tree and return the destination directory.

dirs_exist_ok dictates whether to raise an exception in case dst or any
Expand Down Expand Up @@ -526,12 +526,17 @@ def copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2,
destination path as arguments. By default, copy2() is used, but any
function that supports the same signature (like copy()) can be used.

When the copy_function should recieve a srcentry (like copy2() and
copy()), set use_srcentry to true, otherwise the function will pass
only the srcname to the copy_function.

"""
with os.scandir(src) as entries:
return _copytree(entries=entries, src=src, dst=dst, symlinks=symlinks,
ignore=ignore, copy_function=copy_function,
ignore_dangling_symlinks=ignore_dangling_symlinks,
dirs_exist_ok=dirs_exist_ok)
dirs_exist_ok=dirs_exist_ok,
use_srcentry=use_srcentry)

# version vulnerable to race conditions
def _rmtree_unsafe(path, onerror):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
shutil.copytree has now the `use_srcentry` argument. With this it's possible to control if the *copy_function* will get a srcentry or srcname.