Skip to content
Open
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
2 changes: 2 additions & 0 deletions Lib/shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,8 @@ def _copytree(entries, src, dst, symlinks, ignore, copy_function,
# We can't just leave it to `copy_function` because legacy
# code with a custom `copy_function` may rely on copytree
# doing the right thing.
if dirs_exist_ok and os.path.lexists(dstname):
os.unlink(dstname)
os.symlink(linkto, dstname)
copystat(srcobj, dstname, follow_symlinks=not symlinks)
else:
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/test_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,22 @@ def test_copytree_symlinks(self):
if hasattr(os, 'lchflags'):
self.assertEqual(dst_stat.st_flags, src_stat.st_flags)

@os_helper.skip_unless_symlink
def test_copytree_symlink_replaces_existing(self):
src = self.mkdtemp()
os.symlink('new', os.path.join(src, 'link'))
for existing in ('file', 'symlink'):
with self.subTest(existing=existing):
dst = self.mkdtemp()
dst_link = os.path.join(dst, 'link')
if existing == 'file':
create_file(dst_link, 'original')
else:
os.symlink('old', dst_link)
shutil.copytree(src, dst, symlinks=True, dirs_exist_ok=True)
self.assertTrue(os.path.islink(dst_link))
self.assertEqual(os.readlink(dst_link), 'new')

def test_copytree_with_exclude(self):
# creating data
join = os.path.join
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fixed shutil.copytree(), now it overrides existing symbolic links correctly
Loading