Skip to content

Commit

Permalink
Backport of Python 3.8 shutil.copytree, refs #744
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed May 27, 2020
1 parent cee671a commit 6f80757
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 2 deletions.
5 changes: 3 additions & 2 deletions datasette/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import urllib
import numbers
import yaml
from .shutil_backport import copytree

try:
import pysqlite3 as sqlite3
Expand Down Expand Up @@ -602,9 +603,9 @@ def link_or_copy(src, dst):

def link_or_copy_directory(src, dst):
try:
shutil.copytree(src, dst, copy_function=os.link, dirs_exist_ok=True)
copytree(src, dst, copy_function=os.link, dirs_exist_ok=True)
except OSError:
shutil.copytree(src, dst, dirs_exist_ok=True)
copytree(src, dst, dirs_exist_ok=True)


def module_from_path(path, name):
Expand Down
101 changes: 101 additions & 0 deletions datasette/utils/shutil_backport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
"""
Backported from Python 3.8.
This code is licensed under the Python License:
https://github.com/python/cpython/blob/v3.8.3/LICENSE
"""
import os
from shutil import copy, copy2, copystat, Error


def _copytree(
entries,
src,
dst,
symlinks,
ignore,
copy_function,
ignore_dangling_symlinks,
dirs_exist_ok=False,
):
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:
continue
srcname = os.path.join(src, srcentry.name)
dstname = os.path.join(dst, srcentry.name)
srcobj = srcentry if use_srcentry else srcname
try:
if srcentry.is_symlink():
linkto = os.readlink(srcname)
if symlinks:
os.symlink(linkto, dstname)
copystat(srcobj, dstname, follow_symlinks=not symlinks)
else:
if not os.path.exists(linkto) and ignore_dangling_symlinks:
continue
if srcentry.is_dir():
copytree(
srcobj,
dstname,
symlinks,
ignore,
copy_function,
dirs_exist_ok=dirs_exist_ok,
)
else:
copy_function(srcobj, dstname)
elif srcentry.is_dir():
copytree(
srcobj,
dstname,
symlinks,
ignore,
copy_function,
dirs_exist_ok=dirs_exist_ok,
)
else:
copy_function(srcentry, dstname)
except Error as err:
errors.extend(err.args[0])
except OSError as why:
errors.append((srcname, dstname, str(why)))
try:
copystat(src, dst)
except OSError as why:
# Copying file access times may fail on Windows
if getattr(why, "winerror", None) is None:
errors.append((src, dst, str(why)))
if errors:
raise Error(errors)
return dst


def copytree(
src,
dst,
symlinks=False,
ignore=None,
copy_function=copy2,
ignore_dangling_symlinks=False,
dirs_exist_ok=False,
):
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,
)

0 comments on commit 6f80757

Please sign in to comment.