Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python3....
File: shutil.py
"""Utility functions for copying and archiving files and directory trees.
[0] Fix | Delete
[1] Fix | Delete
XXX The functions here don't copy the resource fork or other metadata on Mac.
[2] Fix | Delete
[3] Fix | Delete
"""
[4] Fix | Delete
[5] Fix | Delete
import os
[6] Fix | Delete
import sys
[7] Fix | Delete
import stat
[8] Fix | Delete
import fnmatch
[9] Fix | Delete
import collections
[10] Fix | Delete
import errno
[11] Fix | Delete
[12] Fix | Delete
try:
[13] Fix | Delete
import zlib
[14] Fix | Delete
del zlib
[15] Fix | Delete
_ZLIB_SUPPORTED = True
[16] Fix | Delete
except ImportError:
[17] Fix | Delete
_ZLIB_SUPPORTED = False
[18] Fix | Delete
[19] Fix | Delete
try:
[20] Fix | Delete
import bz2
[21] Fix | Delete
del bz2
[22] Fix | Delete
_BZ2_SUPPORTED = True
[23] Fix | Delete
except ImportError:
[24] Fix | Delete
_BZ2_SUPPORTED = False
[25] Fix | Delete
[26] Fix | Delete
try:
[27] Fix | Delete
import lzma
[28] Fix | Delete
del lzma
[29] Fix | Delete
_LZMA_SUPPORTED = True
[30] Fix | Delete
except ImportError:
[31] Fix | Delete
_LZMA_SUPPORTED = False
[32] Fix | Delete
[33] Fix | Delete
try:
[34] Fix | Delete
from pwd import getpwnam
[35] Fix | Delete
except ImportError:
[36] Fix | Delete
getpwnam = None
[37] Fix | Delete
[38] Fix | Delete
try:
[39] Fix | Delete
from grp import getgrnam
[40] Fix | Delete
except ImportError:
[41] Fix | Delete
getgrnam = None
[42] Fix | Delete
[43] Fix | Delete
_WINDOWS = os.name == 'nt'
[44] Fix | Delete
posix = nt = None
[45] Fix | Delete
if os.name == 'posix':
[46] Fix | Delete
import posix
[47] Fix | Delete
elif _WINDOWS:
[48] Fix | Delete
import nt
[49] Fix | Delete
[50] Fix | Delete
COPY_BUFSIZE = 1024 * 1024 if _WINDOWS else 64 * 1024
[51] Fix | Delete
_USE_CP_SENDFILE = hasattr(os, "sendfile") and sys.platform.startswith("linux")
[52] Fix | Delete
_HAS_FCOPYFILE = posix and hasattr(posix, "_fcopyfile") # macOS
[53] Fix | Delete
[54] Fix | Delete
# CMD defaults in Windows 10
[55] Fix | Delete
_WIN_DEFAULT_PATHEXT = ".COM;.EXE;.BAT;.CMD;.VBS;.JS;.WS;.MSC"
[56] Fix | Delete
[57] Fix | Delete
__all__ = ["copyfileobj", "copyfile", "copymode", "copystat", "copy", "copy2",
[58] Fix | Delete
"copytree", "move", "rmtree", "Error", "SpecialFileError",
[59] Fix | Delete
"ExecError", "make_archive", "get_archive_formats",
[60] Fix | Delete
"register_archive_format", "unregister_archive_format",
[61] Fix | Delete
"get_unpack_formats", "register_unpack_format",
[62] Fix | Delete
"unregister_unpack_format", "unpack_archive",
[63] Fix | Delete
"ignore_patterns", "chown", "which", "get_terminal_size",
[64] Fix | Delete
"SameFileError"]
[65] Fix | Delete
# disk_usage is added later, if available on the platform
[66] Fix | Delete
[67] Fix | Delete
class Error(OSError):
[68] Fix | Delete
pass
[69] Fix | Delete
[70] Fix | Delete
class SameFileError(Error):
[71] Fix | Delete
"""Raised when source and destination are the same file."""
[72] Fix | Delete
[73] Fix | Delete
class SpecialFileError(OSError):
[74] Fix | Delete
"""Raised when trying to do a kind of operation (e.g. copying) which is
[75] Fix | Delete
not supported on a special file (e.g. a named pipe)"""
[76] Fix | Delete
[77] Fix | Delete
class ExecError(OSError):
[78] Fix | Delete
"""Raised when a command could not be executed"""
[79] Fix | Delete
[80] Fix | Delete
class ReadError(OSError):
[81] Fix | Delete
"""Raised when an archive cannot be read"""
[82] Fix | Delete
[83] Fix | Delete
class RegistryError(Exception):
[84] Fix | Delete
"""Raised when a registry operation with the archiving
[85] Fix | Delete
and unpacking registries fails"""
[86] Fix | Delete
[87] Fix | Delete
class _GiveupOnFastCopy(Exception):
[88] Fix | Delete
"""Raised as a signal to fallback on using raw read()/write()
[89] Fix | Delete
file copy when fast-copy functions fail to do so.
[90] Fix | Delete
"""
[91] Fix | Delete
[92] Fix | Delete
def _fastcopy_fcopyfile(fsrc, fdst, flags):
[93] Fix | Delete
"""Copy a regular file content or metadata by using high-performance
[94] Fix | Delete
fcopyfile(3) syscall (macOS).
[95] Fix | Delete
"""
[96] Fix | Delete
try:
[97] Fix | Delete
infd = fsrc.fileno()
[98] Fix | Delete
outfd = fdst.fileno()
[99] Fix | Delete
except Exception as err:
[100] Fix | Delete
raise _GiveupOnFastCopy(err) # not a regular file
[101] Fix | Delete
[102] Fix | Delete
try:
[103] Fix | Delete
posix._fcopyfile(infd, outfd, flags)
[104] Fix | Delete
except OSError as err:
[105] Fix | Delete
err.filename = fsrc.name
[106] Fix | Delete
err.filename2 = fdst.name
[107] Fix | Delete
if err.errno in {errno.EINVAL, errno.ENOTSUP}:
[108] Fix | Delete
raise _GiveupOnFastCopy(err)
[109] Fix | Delete
else:
[110] Fix | Delete
raise err from None
[111] Fix | Delete
[112] Fix | Delete
def _fastcopy_sendfile(fsrc, fdst):
[113] Fix | Delete
"""Copy data from one regular mmap-like fd to another by using
[114] Fix | Delete
high-performance sendfile(2) syscall.
[115] Fix | Delete
This should work on Linux >= 2.6.33 only.
[116] Fix | Delete
"""
[117] Fix | Delete
# Note: copyfileobj() is left alone in order to not introduce any
[118] Fix | Delete
# unexpected breakage. Possible risks by using zero-copy calls
[119] Fix | Delete
# in copyfileobj() are:
[120] Fix | Delete
# - fdst cannot be open in "a"(ppend) mode
[121] Fix | Delete
# - fsrc and fdst may be open in "t"(ext) mode
[122] Fix | Delete
# - fsrc may be a BufferedReader (which hides unread data in a buffer),
[123] Fix | Delete
# GzipFile (which decompresses data), HTTPResponse (which decodes
[124] Fix | Delete
# chunks).
[125] Fix | Delete
# - possibly others (e.g. encrypted fs/partition?)
[126] Fix | Delete
global _USE_CP_SENDFILE
[127] Fix | Delete
try:
[128] Fix | Delete
infd = fsrc.fileno()
[129] Fix | Delete
outfd = fdst.fileno()
[130] Fix | Delete
except Exception as err:
[131] Fix | Delete
raise _GiveupOnFastCopy(err) # not a regular file
[132] Fix | Delete
[133] Fix | Delete
# Hopefully the whole file will be copied in a single call.
[134] Fix | Delete
# sendfile() is called in a loop 'till EOF is reached (0 return)
[135] Fix | Delete
# so a bufsize smaller or bigger than the actual file size
[136] Fix | Delete
# should not make any difference, also in case the file content
[137] Fix | Delete
# changes while being copied.
[138] Fix | Delete
try:
[139] Fix | Delete
blocksize = max(os.fstat(infd).st_size, 2 ** 23) # min 8MiB
[140] Fix | Delete
except OSError:
[141] Fix | Delete
blocksize = 2 ** 27 # 128MiB
[142] Fix | Delete
# On 32-bit architectures truncate to 1GiB to avoid OverflowError,
[143] Fix | Delete
# see bpo-38319.
[144] Fix | Delete
if sys.maxsize < 2 ** 32:
[145] Fix | Delete
blocksize = min(blocksize, 2 ** 30)
[146] Fix | Delete
[147] Fix | Delete
offset = 0
[148] Fix | Delete
while True:
[149] Fix | Delete
try:
[150] Fix | Delete
sent = os.sendfile(outfd, infd, offset, blocksize)
[151] Fix | Delete
except OSError as err:
[152] Fix | Delete
# ...in oder to have a more informative exception.
[153] Fix | Delete
err.filename = fsrc.name
[154] Fix | Delete
err.filename2 = fdst.name
[155] Fix | Delete
[156] Fix | Delete
if err.errno == errno.ENOTSOCK:
[157] Fix | Delete
# sendfile() on this platform (probably Linux < 2.6.33)
[158] Fix | Delete
# does not support copies between regular files (only
[159] Fix | Delete
# sockets).
[160] Fix | Delete
_USE_CP_SENDFILE = False
[161] Fix | Delete
raise _GiveupOnFastCopy(err)
[162] Fix | Delete
[163] Fix | Delete
if err.errno == errno.ENOSPC: # filesystem is full
[164] Fix | Delete
raise err from None
[165] Fix | Delete
[166] Fix | Delete
# Give up on first call and if no data was copied.
[167] Fix | Delete
if offset == 0 and os.lseek(outfd, 0, os.SEEK_CUR) == 0:
[168] Fix | Delete
raise _GiveupOnFastCopy(err)
[169] Fix | Delete
[170] Fix | Delete
raise err
[171] Fix | Delete
else:
[172] Fix | Delete
if sent == 0:
[173] Fix | Delete
break # EOF
[174] Fix | Delete
offset += sent
[175] Fix | Delete
[176] Fix | Delete
def _copyfileobj_readinto(fsrc, fdst, length=COPY_BUFSIZE):
[177] Fix | Delete
"""readinto()/memoryview() based variant of copyfileobj().
[178] Fix | Delete
*fsrc* must support readinto() method and both files must be
[179] Fix | Delete
open in binary mode.
[180] Fix | Delete
"""
[181] Fix | Delete
# Localize variable access to minimize overhead.
[182] Fix | Delete
fsrc_readinto = fsrc.readinto
[183] Fix | Delete
fdst_write = fdst.write
[184] Fix | Delete
with memoryview(bytearray(length)) as mv:
[185] Fix | Delete
while True:
[186] Fix | Delete
n = fsrc_readinto(mv)
[187] Fix | Delete
if not n:
[188] Fix | Delete
break
[189] Fix | Delete
elif n < length:
[190] Fix | Delete
with mv[:n] as smv:
[191] Fix | Delete
fdst.write(smv)
[192] Fix | Delete
else:
[193] Fix | Delete
fdst_write(mv)
[194] Fix | Delete
[195] Fix | Delete
def copyfileobj(fsrc, fdst, length=0):
[196] Fix | Delete
"""copy data from file-like object fsrc to file-like object fdst"""
[197] Fix | Delete
# Localize variable access to minimize overhead.
[198] Fix | Delete
if not length:
[199] Fix | Delete
length = COPY_BUFSIZE
[200] Fix | Delete
fsrc_read = fsrc.read
[201] Fix | Delete
fdst_write = fdst.write
[202] Fix | Delete
while True:
[203] Fix | Delete
buf = fsrc_read(length)
[204] Fix | Delete
if not buf:
[205] Fix | Delete
break
[206] Fix | Delete
fdst_write(buf)
[207] Fix | Delete
[208] Fix | Delete
def _samefile(src, dst):
[209] Fix | Delete
# Macintosh, Unix.
[210] Fix | Delete
if isinstance(src, os.DirEntry) and hasattr(os.path, 'samestat'):
[211] Fix | Delete
try:
[212] Fix | Delete
return os.path.samestat(src.stat(), os.stat(dst))
[213] Fix | Delete
except OSError:
[214] Fix | Delete
return False
[215] Fix | Delete
[216] Fix | Delete
if hasattr(os.path, 'samefile'):
[217] Fix | Delete
try:
[218] Fix | Delete
return os.path.samefile(src, dst)
[219] Fix | Delete
except OSError:
[220] Fix | Delete
return False
[221] Fix | Delete
[222] Fix | Delete
# All other platforms: check for same pathname.
[223] Fix | Delete
return (os.path.normcase(os.path.abspath(src)) ==
[224] Fix | Delete
os.path.normcase(os.path.abspath(dst)))
[225] Fix | Delete
[226] Fix | Delete
def _stat(fn):
[227] Fix | Delete
return fn.stat() if isinstance(fn, os.DirEntry) else os.stat(fn)
[228] Fix | Delete
[229] Fix | Delete
def _islink(fn):
[230] Fix | Delete
return fn.is_symlink() if isinstance(fn, os.DirEntry) else os.path.islink(fn)
[231] Fix | Delete
[232] Fix | Delete
def copyfile(src, dst, *, follow_symlinks=True):
[233] Fix | Delete
"""Copy data from src to dst in the most efficient way possible.
[234] Fix | Delete
[235] Fix | Delete
If follow_symlinks is not set and src is a symbolic link, a new
[236] Fix | Delete
symlink will be created instead of copying the file it points to.
[237] Fix | Delete
[238] Fix | Delete
"""
[239] Fix | Delete
sys.audit("shutil.copyfile", src, dst)
[240] Fix | Delete
[241] Fix | Delete
if _samefile(src, dst):
[242] Fix | Delete
raise SameFileError("{!r} and {!r} are the same file".format(src, dst))
[243] Fix | Delete
[244] Fix | Delete
file_size = 0
[245] Fix | Delete
for i, fn in enumerate([src, dst]):
[246] Fix | Delete
try:
[247] Fix | Delete
st = _stat(fn)
[248] Fix | Delete
except OSError:
[249] Fix | Delete
# File most likely does not exist
[250] Fix | Delete
pass
[251] Fix | Delete
else:
[252] Fix | Delete
# XXX What about other special files? (sockets, devices...)
[253] Fix | Delete
if stat.S_ISFIFO(st.st_mode):
[254] Fix | Delete
fn = fn.path if isinstance(fn, os.DirEntry) else fn
[255] Fix | Delete
raise SpecialFileError("`%s` is a named pipe" % fn)
[256] Fix | Delete
if _WINDOWS and i == 0:
[257] Fix | Delete
file_size = st.st_size
[258] Fix | Delete
[259] Fix | Delete
if not follow_symlinks and _islink(src):
[260] Fix | Delete
os.symlink(os.readlink(src), dst)
[261] Fix | Delete
else:
[262] Fix | Delete
with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
[263] Fix | Delete
# macOS
[264] Fix | Delete
if _HAS_FCOPYFILE:
[265] Fix | Delete
try:
[266] Fix | Delete
_fastcopy_fcopyfile(fsrc, fdst, posix._COPYFILE_DATA)
[267] Fix | Delete
return dst
[268] Fix | Delete
except _GiveupOnFastCopy:
[269] Fix | Delete
pass
[270] Fix | Delete
# Linux
[271] Fix | Delete
elif _USE_CP_SENDFILE:
[272] Fix | Delete
try:
[273] Fix | Delete
_fastcopy_sendfile(fsrc, fdst)
[274] Fix | Delete
return dst
[275] Fix | Delete
except _GiveupOnFastCopy:
[276] Fix | Delete
pass
[277] Fix | Delete
# Windows, see:
[278] Fix | Delete
# https://github.com/python/cpython/pull/7160#discussion_r195405230
[279] Fix | Delete
elif _WINDOWS and file_size > 0:
[280] Fix | Delete
_copyfileobj_readinto(fsrc, fdst, min(file_size, COPY_BUFSIZE))
[281] Fix | Delete
return dst
[282] Fix | Delete
[283] Fix | Delete
copyfileobj(fsrc, fdst)
[284] Fix | Delete
[285] Fix | Delete
return dst
[286] Fix | Delete
[287] Fix | Delete
def copymode(src, dst, *, follow_symlinks=True):
[288] Fix | Delete
"""Copy mode bits from src to dst.
[289] Fix | Delete
[290] Fix | Delete
If follow_symlinks is not set, symlinks aren't followed if and only
[291] Fix | Delete
if both `src` and `dst` are symlinks. If `lchmod` isn't available
[292] Fix | Delete
(e.g. Linux) this method does nothing.
[293] Fix | Delete
[294] Fix | Delete
"""
[295] Fix | Delete
sys.audit("shutil.copymode", src, dst)
[296] Fix | Delete
[297] Fix | Delete
if not follow_symlinks and _islink(src) and os.path.islink(dst):
[298] Fix | Delete
if hasattr(os, 'lchmod'):
[299] Fix | Delete
stat_func, chmod_func = os.lstat, os.lchmod
[300] Fix | Delete
else:
[301] Fix | Delete
return
[302] Fix | Delete
else:
[303] Fix | Delete
stat_func, chmod_func = _stat, os.chmod
[304] Fix | Delete
[305] Fix | Delete
st = stat_func(src)
[306] Fix | Delete
chmod_func(dst, stat.S_IMODE(st.st_mode))
[307] Fix | Delete
[308] Fix | Delete
if hasattr(os, 'listxattr'):
[309] Fix | Delete
def _copyxattr(src, dst, *, follow_symlinks=True):
[310] Fix | Delete
"""Copy extended filesystem attributes from `src` to `dst`.
[311] Fix | Delete
[312] Fix | Delete
Overwrite existing attributes.
[313] Fix | Delete
[314] Fix | Delete
If `follow_symlinks` is false, symlinks won't be followed.
[315] Fix | Delete
[316] Fix | Delete
"""
[317] Fix | Delete
[318] Fix | Delete
try:
[319] Fix | Delete
names = os.listxattr(src, follow_symlinks=follow_symlinks)
[320] Fix | Delete
except OSError as e:
[321] Fix | Delete
if e.errno not in (errno.ENOTSUP, errno.ENODATA, errno.EINVAL):
[322] Fix | Delete
raise
[323] Fix | Delete
return
[324] Fix | Delete
for name in names:
[325] Fix | Delete
try:
[326] Fix | Delete
value = os.getxattr(src, name, follow_symlinks=follow_symlinks)
[327] Fix | Delete
os.setxattr(dst, name, value, follow_symlinks=follow_symlinks)
[328] Fix | Delete
except OSError as e:
[329] Fix | Delete
if e.errno not in (errno.EPERM, errno.ENOTSUP, errno.ENODATA,
[330] Fix | Delete
errno.EINVAL):
[331] Fix | Delete
raise
[332] Fix | Delete
else:
[333] Fix | Delete
def _copyxattr(*args, **kwargs):
[334] Fix | Delete
pass
[335] Fix | Delete
[336] Fix | Delete
def copystat(src, dst, *, follow_symlinks=True):
[337] Fix | Delete
"""Copy file metadata
[338] Fix | Delete
[339] Fix | Delete
Copy the permission bits, last access time, last modification time, and
[340] Fix | Delete
flags from `src` to `dst`. On Linux, copystat() also copies the "extended
[341] Fix | Delete
attributes" where possible. The file contents, owner, and group are
[342] Fix | Delete
unaffected. `src` and `dst` are path-like objects or path names given as
[343] Fix | Delete
strings.
[344] Fix | Delete
[345] Fix | Delete
If the optional flag `follow_symlinks` is not set, symlinks aren't
[346] Fix | Delete
followed if and only if both `src` and `dst` are symlinks.
[347] Fix | Delete
"""
[348] Fix | Delete
sys.audit("shutil.copystat", src, dst)
[349] Fix | Delete
[350] Fix | Delete
def _nop(*args, ns=None, follow_symlinks=None):
[351] Fix | Delete
pass
[352] Fix | Delete
[353] Fix | Delete
# follow symlinks (aka don't not follow symlinks)
[354] Fix | Delete
follow = follow_symlinks or not (_islink(src) and os.path.islink(dst))
[355] Fix | Delete
if follow:
[356] Fix | Delete
# use the real function if it exists
[357] Fix | Delete
def lookup(name):
[358] Fix | Delete
return getattr(os, name, _nop)
[359] Fix | Delete
else:
[360] Fix | Delete
# use the real function only if it exists
[361] Fix | Delete
# *and* it supports follow_symlinks
[362] Fix | Delete
def lookup(name):
[363] Fix | Delete
fn = getattr(os, name, _nop)
[364] Fix | Delete
if fn in os.supports_follow_symlinks:
[365] Fix | Delete
return fn
[366] Fix | Delete
return _nop
[367] Fix | Delete
[368] Fix | Delete
if isinstance(src, os.DirEntry):
[369] Fix | Delete
st = src.stat(follow_symlinks=follow)
[370] Fix | Delete
else:
[371] Fix | Delete
st = lookup("stat")(src, follow_symlinks=follow)
[372] Fix | Delete
mode = stat.S_IMODE(st.st_mode)
[373] Fix | Delete
lookup("utime")(dst, ns=(st.st_atime_ns, st.st_mtime_ns),
[374] Fix | Delete
follow_symlinks=follow)
[375] Fix | Delete
# We must copy extended attributes before the file is (potentially)
[376] Fix | Delete
# chmod()'ed read-only, otherwise setxattr() will error with -EACCES.
[377] Fix | Delete
_copyxattr(src, dst, follow_symlinks=follow)
[378] Fix | Delete
try:
[379] Fix | Delete
lookup("chmod")(dst, mode, follow_symlinks=follow)
[380] Fix | Delete
except NotImplementedError:
[381] Fix | Delete
# if we got a NotImplementedError, it's because
[382] Fix | Delete
# * follow_symlinks=False,
[383] Fix | Delete
# * lchown() is unavailable, and
[384] Fix | Delete
# * either
[385] Fix | Delete
# * fchownat() is unavailable or
[386] Fix | Delete
# * fchownat() doesn't implement AT_SYMLINK_NOFOLLOW.
[387] Fix | Delete
# (it returned ENOSUP.)
[388] Fix | Delete
# therefore we're out of options--we simply cannot chown the
[389] Fix | Delete
# symlink. give up, suppress the error.
[390] Fix | Delete
# (which is what shutil always did in this circumstance.)
[391] Fix | Delete
pass
[392] Fix | Delete
if hasattr(st, 'st_flags'):
[393] Fix | Delete
try:
[394] Fix | Delete
lookup("chflags")(dst, st.st_flags, follow_symlinks=follow)
[395] Fix | Delete
except OSError as why:
[396] Fix | Delete
for err in 'EOPNOTSUPP', 'ENOTSUP':
[397] Fix | Delete
if hasattr(errno, err) and why.errno == getattr(errno, err):
[398] Fix | Delete
break
[399] Fix | Delete
else:
[400] Fix | Delete
raise
[401] Fix | Delete
[402] Fix | Delete
def copy(src, dst, *, follow_symlinks=True):
[403] Fix | Delete
"""Copy data and mode bits ("cp src dst"). Return the file's destination.
[404] Fix | Delete
[405] Fix | Delete
The destination may be a directory.
[406] Fix | Delete
[407] Fix | Delete
If follow_symlinks is false, symlinks won't be followed. This
[408] Fix | Delete
resembles GNU's "cp -P src dst".
[409] Fix | Delete
[410] Fix | Delete
If source and destination are the same file, a SameFileError will be
[411] Fix | Delete
raised.
[412] Fix | Delete
[413] Fix | Delete
"""
[414] Fix | Delete
if os.path.isdir(dst):
[415] Fix | Delete
dst = os.path.join(dst, os.path.basename(src))
[416] Fix | Delete
copyfile(src, dst, follow_symlinks=follow_symlinks)
[417] Fix | Delete
copymode(src, dst, follow_symlinks=follow_symlinks)
[418] Fix | Delete
return dst
[419] Fix | Delete
[420] Fix | Delete
def copy2(src, dst, *, follow_symlinks=True):
[421] Fix | Delete
"""Copy data and metadata. Return the file's destination.
[422] Fix | Delete
[423] Fix | Delete
Metadata is copied with copystat(). Please see the copystat function
[424] Fix | Delete
for more information.
[425] Fix | Delete
[426] Fix | Delete
The destination may be a directory.
[427] Fix | Delete
[428] Fix | Delete
If follow_symlinks is false, symlinks won't be followed. This
[429] Fix | Delete
resembles GNU's "cp -P src dst".
[430] Fix | Delete
"""
[431] Fix | Delete
if os.path.isdir(dst):
[432] Fix | Delete
dst = os.path.join(dst, os.path.basename(src))
[433] Fix | Delete
copyfile(src, dst, follow_symlinks=follow_symlinks)
[434] Fix | Delete
copystat(src, dst, follow_symlinks=follow_symlinks)
[435] Fix | Delete
return dst
[436] Fix | Delete
[437] Fix | Delete
def ignore_patterns(*patterns):
[438] Fix | Delete
"""Function that can be used as copytree() ignore parameter.
[439] Fix | Delete
[440] Fix | Delete
Patterns is a sequence of glob-style patterns
[441] Fix | Delete
that are used to exclude files"""
[442] Fix | Delete
def _ignore_patterns(path, names):
[443] Fix | Delete
ignored_names = []
[444] Fix | Delete
for pattern in patterns:
[445] Fix | Delete
ignored_names.extend(fnmatch.filter(names, pattern))
[446] Fix | Delete
return set(ignored_names)
[447] Fix | Delete
return _ignore_patterns
[448] Fix | Delete
[449] Fix | Delete
def _copytree(entries, src, dst, symlinks, ignore, copy_function,
[450] Fix | Delete
ignore_dangling_symlinks, dirs_exist_ok=False):
[451] Fix | Delete
if ignore is not None:
[452] Fix | Delete
ignored_names = ignore(os.fspath(src), [x.name for x in entries])
[453] Fix | Delete
else:
[454] Fix | Delete
ignored_names = set()
[455] Fix | Delete
[456] Fix | Delete
os.makedirs(dst, exist_ok=dirs_exist_ok)
[457] Fix | Delete
errors = []
[458] Fix | Delete
use_srcentry = copy_function is copy2 or copy_function is copy
[459] Fix | Delete
[460] Fix | Delete
for srcentry in entries:
[461] Fix | Delete
if srcentry.name in ignored_names:
[462] Fix | Delete
continue
[463] Fix | Delete
srcname = os.path.join(src, srcentry.name)
[464] Fix | Delete
dstname = os.path.join(dst, srcentry.name)
[465] Fix | Delete
srcobj = srcentry if use_srcentry else srcname
[466] Fix | Delete
try:
[467] Fix | Delete
is_symlink = srcentry.is_symlink()
[468] Fix | Delete
if is_symlink and os.name == 'nt':
[469] Fix | Delete
# Special check for directory junctions, which appear as
[470] Fix | Delete
# symlinks but we want to recurse.
[471] Fix | Delete
lstat = srcentry.stat(follow_symlinks=False)
[472] Fix | Delete
if lstat.st_reparse_tag == stat.IO_REPARSE_TAG_MOUNT_POINT:
[473] Fix | Delete
is_symlink = False
[474] Fix | Delete
if is_symlink:
[475] Fix | Delete
linkto = os.readlink(srcname)
[476] Fix | Delete
if symlinks:
[477] Fix | Delete
# We can't just leave it to `copy_function` because legacy
[478] Fix | Delete
# code with a custom `copy_function` may rely on copytree
[479] Fix | Delete
# doing the right thing.
[480] Fix | Delete
os.symlink(linkto, dstname)
[481] Fix | Delete
copystat(srcobj, dstname, follow_symlinks=not symlinks)
[482] Fix | Delete
else:
[483] Fix | Delete
# ignore dangling symlink if the flag is on
[484] Fix | Delete
if not os.path.exists(linkto) and ignore_dangling_symlinks:
[485] Fix | Delete
continue
[486] Fix | Delete
# otherwise let the copy occur. copy2 will raise an error
[487] Fix | Delete
if srcentry.is_dir():
[488] Fix | Delete
copytree(srcobj, dstname, symlinks, ignore,
[489] Fix | Delete
copy_function, dirs_exist_ok=dirs_exist_ok)
[490] Fix | Delete
else:
[491] Fix | Delete
copy_function(srcobj, dstname)
[492] Fix | Delete
elif srcentry.is_dir():
[493] Fix | Delete
copytree(srcobj, dstname, symlinks, ignore, copy_function,
[494] Fix | Delete
dirs_exist_ok=dirs_exist_ok)
[495] Fix | Delete
else:
[496] Fix | Delete
# Will raise a SpecialFileError for unsupported file types
[497] Fix | Delete
copy_function(srcobj, dstname)
[498] Fix | Delete
# catch the Error from the recursive copytree so that we can
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function