Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../usr/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
__all__ = ["copyfileobj", "copyfile", "copymode", "copystat", "copy", "copy2",
[44] Fix | Delete
"copytree", "move", "rmtree", "Error", "SpecialFileError",
[45] Fix | Delete
"ExecError", "make_archive", "get_archive_formats",
[46] Fix | Delete
"register_archive_format", "unregister_archive_format",
[47] Fix | Delete
"get_unpack_formats", "register_unpack_format",
[48] Fix | Delete
"unregister_unpack_format", "unpack_archive",
[49] Fix | Delete
"ignore_patterns", "chown", "which", "get_terminal_size",
[50] Fix | Delete
"SameFileError"]
[51] Fix | Delete
# disk_usage is added later, if available on the platform
[52] Fix | Delete
[53] Fix | Delete
class Error(OSError):
[54] Fix | Delete
pass
[55] Fix | Delete
[56] Fix | Delete
class SameFileError(Error):
[57] Fix | Delete
"""Raised when source and destination are the same file."""
[58] Fix | Delete
[59] Fix | Delete
class SpecialFileError(OSError):
[60] Fix | Delete
"""Raised when trying to do a kind of operation (e.g. copying) which is
[61] Fix | Delete
not supported on a special file (e.g. a named pipe)"""
[62] Fix | Delete
[63] Fix | Delete
class ExecError(OSError):
[64] Fix | Delete
"""Raised when a command could not be executed"""
[65] Fix | Delete
[66] Fix | Delete
class ReadError(OSError):
[67] Fix | Delete
"""Raised when an archive cannot be read"""
[68] Fix | Delete
[69] Fix | Delete
class RegistryError(Exception):
[70] Fix | Delete
"""Raised when a registry operation with the archiving
[71] Fix | Delete
and unpacking registries fails"""
[72] Fix | Delete
[73] Fix | Delete
[74] Fix | Delete
def copyfileobj(fsrc, fdst, length=16*1024):
[75] Fix | Delete
"""copy data from file-like object fsrc to file-like object fdst"""
[76] Fix | Delete
while 1:
[77] Fix | Delete
buf = fsrc.read(length)
[78] Fix | Delete
if not buf:
[79] Fix | Delete
break
[80] Fix | Delete
fdst.write(buf)
[81] Fix | Delete
[82] Fix | Delete
def _samefile(src, dst):
[83] Fix | Delete
# Macintosh, Unix.
[84] Fix | Delete
if hasattr(os.path, 'samefile'):
[85] Fix | Delete
try:
[86] Fix | Delete
return os.path.samefile(src, dst)
[87] Fix | Delete
except OSError:
[88] Fix | Delete
return False
[89] Fix | Delete
[90] Fix | Delete
# All other platforms: check for same pathname.
[91] Fix | Delete
return (os.path.normcase(os.path.abspath(src)) ==
[92] Fix | Delete
os.path.normcase(os.path.abspath(dst)))
[93] Fix | Delete
[94] Fix | Delete
def copyfile(src, dst, *, follow_symlinks=True):
[95] Fix | Delete
"""Copy data from src to dst.
[96] Fix | Delete
[97] Fix | Delete
If follow_symlinks is not set and src is a symbolic link, a new
[98] Fix | Delete
symlink will be created instead of copying the file it points to.
[99] Fix | Delete
[100] Fix | Delete
"""
[101] Fix | Delete
if _samefile(src, dst):
[102] Fix | Delete
raise SameFileError("{!r} and {!r} are the same file".format(src, dst))
[103] Fix | Delete
[104] Fix | Delete
for fn in [src, dst]:
[105] Fix | Delete
try:
[106] Fix | Delete
st = os.stat(fn)
[107] Fix | Delete
except OSError:
[108] Fix | Delete
# File most likely does not exist
[109] Fix | Delete
pass
[110] Fix | Delete
else:
[111] Fix | Delete
# XXX What about other special files? (sockets, devices...)
[112] Fix | Delete
if stat.S_ISFIFO(st.st_mode):
[113] Fix | Delete
raise SpecialFileError("`%s` is a named pipe" % fn)
[114] Fix | Delete
[115] Fix | Delete
if not follow_symlinks and os.path.islink(src):
[116] Fix | Delete
os.symlink(os.readlink(src), dst)
[117] Fix | Delete
else:
[118] Fix | Delete
with open(src, 'rb') as fsrc:
[119] Fix | Delete
with open(dst, 'wb') as fdst:
[120] Fix | Delete
copyfileobj(fsrc, fdst)
[121] Fix | Delete
return dst
[122] Fix | Delete
[123] Fix | Delete
def copymode(src, dst, *, follow_symlinks=True):
[124] Fix | Delete
"""Copy mode bits from src to dst.
[125] Fix | Delete
[126] Fix | Delete
If follow_symlinks is not set, symlinks aren't followed if and only
[127] Fix | Delete
if both `src` and `dst` are symlinks. If `lchmod` isn't available
[128] Fix | Delete
(e.g. Linux) this method does nothing.
[129] Fix | Delete
[130] Fix | Delete
"""
[131] Fix | Delete
if not follow_symlinks and os.path.islink(src) and os.path.islink(dst):
[132] Fix | Delete
if hasattr(os, 'lchmod'):
[133] Fix | Delete
stat_func, chmod_func = os.lstat, os.lchmod
[134] Fix | Delete
else:
[135] Fix | Delete
return
[136] Fix | Delete
elif hasattr(os, 'chmod'):
[137] Fix | Delete
stat_func, chmod_func = os.stat, os.chmod
[138] Fix | Delete
else:
[139] Fix | Delete
return
[140] Fix | Delete
[141] Fix | Delete
st = stat_func(src)
[142] Fix | Delete
chmod_func(dst, stat.S_IMODE(st.st_mode))
[143] Fix | Delete
[144] Fix | Delete
if hasattr(os, 'listxattr'):
[145] Fix | Delete
def _copyxattr(src, dst, *, follow_symlinks=True):
[146] Fix | Delete
"""Copy extended filesystem attributes from `src` to `dst`.
[147] Fix | Delete
[148] Fix | Delete
Overwrite existing attributes.
[149] Fix | Delete
[150] Fix | Delete
If `follow_symlinks` is false, symlinks won't be followed.
[151] Fix | Delete
[152] Fix | Delete
"""
[153] Fix | Delete
[154] Fix | Delete
try:
[155] Fix | Delete
names = os.listxattr(src, follow_symlinks=follow_symlinks)
[156] Fix | Delete
except OSError as e:
[157] Fix | Delete
if e.errno not in (errno.ENOTSUP, errno.ENODATA):
[158] Fix | Delete
raise
[159] Fix | Delete
return
[160] Fix | Delete
for name in names:
[161] Fix | Delete
try:
[162] Fix | Delete
value = os.getxattr(src, name, follow_symlinks=follow_symlinks)
[163] Fix | Delete
os.setxattr(dst, name, value, follow_symlinks=follow_symlinks)
[164] Fix | Delete
except OSError as e:
[165] Fix | Delete
if e.errno not in (errno.EPERM, errno.ENOTSUP, errno.ENODATA):
[166] Fix | Delete
raise
[167] Fix | Delete
else:
[168] Fix | Delete
def _copyxattr(*args, **kwargs):
[169] Fix | Delete
pass
[170] Fix | Delete
[171] Fix | Delete
def copystat(src, dst, *, follow_symlinks=True):
[172] Fix | Delete
"""Copy file metadata
[173] Fix | Delete
[174] Fix | Delete
Copy the permission bits, last access time, last modification time, and
[175] Fix | Delete
flags from `src` to `dst`. On Linux, copystat() also copies the "extended
[176] Fix | Delete
attributes" where possible. The file contents, owner, and group are
[177] Fix | Delete
unaffected. `src` and `dst` are path names given as strings.
[178] Fix | Delete
[179] Fix | Delete
If the optional flag `follow_symlinks` is not set, symlinks aren't
[180] Fix | Delete
followed if and only if both `src` and `dst` are symlinks.
[181] Fix | Delete
"""
[182] Fix | Delete
def _nop(*args, ns=None, follow_symlinks=None):
[183] Fix | Delete
pass
[184] Fix | Delete
[185] Fix | Delete
# follow symlinks (aka don't not follow symlinks)
[186] Fix | Delete
follow = follow_symlinks or not (os.path.islink(src) and os.path.islink(dst))
[187] Fix | Delete
if follow:
[188] Fix | Delete
# use the real function if it exists
[189] Fix | Delete
def lookup(name):
[190] Fix | Delete
return getattr(os, name, _nop)
[191] Fix | Delete
else:
[192] Fix | Delete
# use the real function only if it exists
[193] Fix | Delete
# *and* it supports follow_symlinks
[194] Fix | Delete
def lookup(name):
[195] Fix | Delete
fn = getattr(os, name, _nop)
[196] Fix | Delete
if fn in os.supports_follow_symlinks:
[197] Fix | Delete
return fn
[198] Fix | Delete
return _nop
[199] Fix | Delete
[200] Fix | Delete
st = lookup("stat")(src, follow_symlinks=follow)
[201] Fix | Delete
mode = stat.S_IMODE(st.st_mode)
[202] Fix | Delete
lookup("utime")(dst, ns=(st.st_atime_ns, st.st_mtime_ns),
[203] Fix | Delete
follow_symlinks=follow)
[204] Fix | Delete
try:
[205] Fix | Delete
lookup("chmod")(dst, mode, follow_symlinks=follow)
[206] Fix | Delete
except NotImplementedError:
[207] Fix | Delete
# if we got a NotImplementedError, it's because
[208] Fix | Delete
# * follow_symlinks=False,
[209] Fix | Delete
# * lchown() is unavailable, and
[210] Fix | Delete
# * either
[211] Fix | Delete
# * fchownat() is unavailable or
[212] Fix | Delete
# * fchownat() doesn't implement AT_SYMLINK_NOFOLLOW.
[213] Fix | Delete
# (it returned ENOSUP.)
[214] Fix | Delete
# therefore we're out of options--we simply cannot chown the
[215] Fix | Delete
# symlink. give up, suppress the error.
[216] Fix | Delete
# (which is what shutil always did in this circumstance.)
[217] Fix | Delete
pass
[218] Fix | Delete
if hasattr(st, 'st_flags'):
[219] Fix | Delete
try:
[220] Fix | Delete
lookup("chflags")(dst, st.st_flags, follow_symlinks=follow)
[221] Fix | Delete
except OSError as why:
[222] Fix | Delete
for err in 'EOPNOTSUPP', 'ENOTSUP':
[223] Fix | Delete
if hasattr(errno, err) and why.errno == getattr(errno, err):
[224] Fix | Delete
break
[225] Fix | Delete
else:
[226] Fix | Delete
raise
[227] Fix | Delete
_copyxattr(src, dst, follow_symlinks=follow)
[228] Fix | Delete
[229] Fix | Delete
def copy(src, dst, *, follow_symlinks=True):
[230] Fix | Delete
"""Copy data and mode bits ("cp src dst"). Return the file's destination.
[231] Fix | Delete
[232] Fix | Delete
The destination may be a directory.
[233] Fix | Delete
[234] Fix | Delete
If follow_symlinks is false, symlinks won't be followed. This
[235] Fix | Delete
resembles GNU's "cp -P src dst".
[236] Fix | Delete
[237] Fix | Delete
If source and destination are the same file, a SameFileError will be
[238] Fix | Delete
raised.
[239] Fix | Delete
[240] Fix | Delete
"""
[241] Fix | Delete
if os.path.isdir(dst):
[242] Fix | Delete
dst = os.path.join(dst, os.path.basename(src))
[243] Fix | Delete
copyfile(src, dst, follow_symlinks=follow_symlinks)
[244] Fix | Delete
copymode(src, dst, follow_symlinks=follow_symlinks)
[245] Fix | Delete
return dst
[246] Fix | Delete
[247] Fix | Delete
def copy2(src, dst, *, follow_symlinks=True):
[248] Fix | Delete
"""Copy data and metadata. Return the file's destination.
[249] Fix | Delete
[250] Fix | Delete
Metadata is copied with copystat(). Please see the copystat function
[251] Fix | Delete
for more information.
[252] Fix | Delete
[253] Fix | Delete
The destination may be a directory.
[254] Fix | Delete
[255] Fix | Delete
If follow_symlinks is false, symlinks won't be followed. This
[256] Fix | Delete
resembles GNU's "cp -P src dst".
[257] Fix | Delete
[258] Fix | Delete
"""
[259] Fix | Delete
if os.path.isdir(dst):
[260] Fix | Delete
dst = os.path.join(dst, os.path.basename(src))
[261] Fix | Delete
copyfile(src, dst, follow_symlinks=follow_symlinks)
[262] Fix | Delete
copystat(src, dst, follow_symlinks=follow_symlinks)
[263] Fix | Delete
return dst
[264] Fix | Delete
[265] Fix | Delete
def ignore_patterns(*patterns):
[266] Fix | Delete
"""Function that can be used as copytree() ignore parameter.
[267] Fix | Delete
[268] Fix | Delete
Patterns is a sequence of glob-style patterns
[269] Fix | Delete
that are used to exclude files"""
[270] Fix | Delete
def _ignore_patterns(path, names):
[271] Fix | Delete
ignored_names = []
[272] Fix | Delete
for pattern in patterns:
[273] Fix | Delete
ignored_names.extend(fnmatch.filter(names, pattern))
[274] Fix | Delete
return set(ignored_names)
[275] Fix | Delete
return _ignore_patterns
[276] Fix | Delete
[277] Fix | Delete
def copytree(src, dst, symlinks=False, ignore=None, copy_function=copy2,
[278] Fix | Delete
ignore_dangling_symlinks=False):
[279] Fix | Delete
"""Recursively copy a directory tree.
[280] Fix | Delete
[281] Fix | Delete
The destination directory must not already exist.
[282] Fix | Delete
If exception(s) occur, an Error is raised with a list of reasons.
[283] Fix | Delete
[284] Fix | Delete
If the optional symlinks flag is true, symbolic links in the
[285] Fix | Delete
source tree result in symbolic links in the destination tree; if
[286] Fix | Delete
it is false, the contents of the files pointed to by symbolic
[287] Fix | Delete
links are copied. If the file pointed by the symlink doesn't
[288] Fix | Delete
exist, an exception will be added in the list of errors raised in
[289] Fix | Delete
an Error exception at the end of the copy process.
[290] Fix | Delete
[291] Fix | Delete
You can set the optional ignore_dangling_symlinks flag to true if you
[292] Fix | Delete
want to silence this exception. Notice that this has no effect on
[293] Fix | Delete
platforms that don't support os.symlink.
[294] Fix | Delete
[295] Fix | Delete
The optional ignore argument is a callable. If given, it
[296] Fix | Delete
is called with the `src` parameter, which is the directory
[297] Fix | Delete
being visited by copytree(), and `names` which is the list of
[298] Fix | Delete
`src` contents, as returned by os.listdir():
[299] Fix | Delete
[300] Fix | Delete
callable(src, names) -> ignored_names
[301] Fix | Delete
[302] Fix | Delete
Since copytree() is called recursively, the callable will be
[303] Fix | Delete
called once for each directory that is copied. It returns a
[304] Fix | Delete
list of names relative to the `src` directory that should
[305] Fix | Delete
not be copied.
[306] Fix | Delete
[307] Fix | Delete
The optional copy_function argument is a callable that will be used
[308] Fix | Delete
to copy each file. It will be called with the source path and the
[309] Fix | Delete
destination path as arguments. By default, copy2() is used, but any
[310] Fix | Delete
function that supports the same signature (like copy()) can be used.
[311] Fix | Delete
[312] Fix | Delete
"""
[313] Fix | Delete
names = os.listdir(src)
[314] Fix | Delete
if ignore is not None:
[315] Fix | Delete
ignored_names = ignore(src, names)
[316] Fix | Delete
else:
[317] Fix | Delete
ignored_names = set()
[318] Fix | Delete
[319] Fix | Delete
os.makedirs(dst)
[320] Fix | Delete
errors = []
[321] Fix | Delete
for name in names:
[322] Fix | Delete
if name in ignored_names:
[323] Fix | Delete
continue
[324] Fix | Delete
srcname = os.path.join(src, name)
[325] Fix | Delete
dstname = os.path.join(dst, name)
[326] Fix | Delete
try:
[327] Fix | Delete
if os.path.islink(srcname):
[328] Fix | Delete
linkto = os.readlink(srcname)
[329] Fix | Delete
if symlinks:
[330] Fix | Delete
# We can't just leave it to `copy_function` because legacy
[331] Fix | Delete
# code with a custom `copy_function` may rely on copytree
[332] Fix | Delete
# doing the right thing.
[333] Fix | Delete
os.symlink(linkto, dstname)
[334] Fix | Delete
copystat(srcname, dstname, follow_symlinks=not symlinks)
[335] Fix | Delete
else:
[336] Fix | Delete
# ignore dangling symlink if the flag is on
[337] Fix | Delete
if not os.path.exists(linkto) and ignore_dangling_symlinks:
[338] Fix | Delete
continue
[339] Fix | Delete
# otherwise let the copy occurs. copy2 will raise an error
[340] Fix | Delete
if os.path.isdir(srcname):
[341] Fix | Delete
copytree(srcname, dstname, symlinks, ignore,
[342] Fix | Delete
copy_function)
[343] Fix | Delete
else:
[344] Fix | Delete
copy_function(srcname, dstname)
[345] Fix | Delete
elif os.path.isdir(srcname):
[346] Fix | Delete
copytree(srcname, dstname, symlinks, ignore, copy_function)
[347] Fix | Delete
else:
[348] Fix | Delete
# Will raise a SpecialFileError for unsupported file types
[349] Fix | Delete
copy_function(srcname, dstname)
[350] Fix | Delete
# catch the Error from the recursive copytree so that we can
[351] Fix | Delete
# continue with other files
[352] Fix | Delete
except Error as err:
[353] Fix | Delete
errors.extend(err.args[0])
[354] Fix | Delete
except OSError as why:
[355] Fix | Delete
errors.append((srcname, dstname, str(why)))
[356] Fix | Delete
try:
[357] Fix | Delete
copystat(src, dst)
[358] Fix | Delete
except OSError as why:
[359] Fix | Delete
# Copying file access times may fail on Windows
[360] Fix | Delete
if getattr(why, 'winerror', None) is None:
[361] Fix | Delete
errors.append((src, dst, str(why)))
[362] Fix | Delete
if errors:
[363] Fix | Delete
raise Error(errors)
[364] Fix | Delete
return dst
[365] Fix | Delete
[366] Fix | Delete
# version vulnerable to race conditions
[367] Fix | Delete
def _rmtree_unsafe(path, onerror):
[368] Fix | Delete
try:
[369] Fix | Delete
if os.path.islink(path):
[370] Fix | Delete
# symlinks to directories are forbidden, see bug #1669
[371] Fix | Delete
raise OSError("Cannot call rmtree on a symbolic link")
[372] Fix | Delete
except OSError:
[373] Fix | Delete
onerror(os.path.islink, path, sys.exc_info())
[374] Fix | Delete
# can't continue even if onerror hook returns
[375] Fix | Delete
return
[376] Fix | Delete
names = []
[377] Fix | Delete
try:
[378] Fix | Delete
names = os.listdir(path)
[379] Fix | Delete
except OSError:
[380] Fix | Delete
onerror(os.listdir, path, sys.exc_info())
[381] Fix | Delete
for name in names:
[382] Fix | Delete
fullname = os.path.join(path, name)
[383] Fix | Delete
try:
[384] Fix | Delete
mode = os.lstat(fullname).st_mode
[385] Fix | Delete
except OSError:
[386] Fix | Delete
mode = 0
[387] Fix | Delete
if stat.S_ISDIR(mode):
[388] Fix | Delete
_rmtree_unsafe(fullname, onerror)
[389] Fix | Delete
else:
[390] Fix | Delete
try:
[391] Fix | Delete
os.unlink(fullname)
[392] Fix | Delete
except OSError:
[393] Fix | Delete
onerror(os.unlink, fullname, sys.exc_info())
[394] Fix | Delete
try:
[395] Fix | Delete
os.rmdir(path)
[396] Fix | Delete
except OSError:
[397] Fix | Delete
onerror(os.rmdir, path, sys.exc_info())
[398] Fix | Delete
[399] Fix | Delete
# Version using fd-based APIs to protect against races
[400] Fix | Delete
def _rmtree_safe_fd(topfd, path, onerror):
[401] Fix | Delete
names = []
[402] Fix | Delete
try:
[403] Fix | Delete
names = os.listdir(topfd)
[404] Fix | Delete
except OSError as err:
[405] Fix | Delete
err.filename = path
[406] Fix | Delete
onerror(os.listdir, path, sys.exc_info())
[407] Fix | Delete
for name in names:
[408] Fix | Delete
fullname = os.path.join(path, name)
[409] Fix | Delete
try:
[410] Fix | Delete
orig_st = os.stat(name, dir_fd=topfd, follow_symlinks=False)
[411] Fix | Delete
mode = orig_st.st_mode
[412] Fix | Delete
except OSError:
[413] Fix | Delete
mode = 0
[414] Fix | Delete
if stat.S_ISDIR(mode):
[415] Fix | Delete
try:
[416] Fix | Delete
dirfd = os.open(name, os.O_RDONLY, dir_fd=topfd)
[417] Fix | Delete
except OSError:
[418] Fix | Delete
onerror(os.open, fullname, sys.exc_info())
[419] Fix | Delete
else:
[420] Fix | Delete
try:
[421] Fix | Delete
if os.path.samestat(orig_st, os.fstat(dirfd)):
[422] Fix | Delete
_rmtree_safe_fd(dirfd, fullname, onerror)
[423] Fix | Delete
try:
[424] Fix | Delete
os.rmdir(name, dir_fd=topfd)
[425] Fix | Delete
except OSError:
[426] Fix | Delete
onerror(os.rmdir, fullname, sys.exc_info())
[427] Fix | Delete
else:
[428] Fix | Delete
try:
[429] Fix | Delete
# This can only happen if someone replaces
[430] Fix | Delete
# a directory with a symlink after the call to
[431] Fix | Delete
# stat.S_ISDIR above.
[432] Fix | Delete
raise OSError("Cannot call rmtree on a symbolic "
[433] Fix | Delete
"link")
[434] Fix | Delete
except OSError:
[435] Fix | Delete
onerror(os.path.islink, fullname, sys.exc_info())
[436] Fix | Delete
finally:
[437] Fix | Delete
os.close(dirfd)
[438] Fix | Delete
else:
[439] Fix | Delete
try:
[440] Fix | Delete
os.unlink(name, dir_fd=topfd)
[441] Fix | Delete
except OSError:
[442] Fix | Delete
onerror(os.unlink, fullname, sys.exc_info())
[443] Fix | Delete
[444] Fix | Delete
_use_fd_functions = ({os.open, os.stat, os.unlink, os.rmdir} <=
[445] Fix | Delete
os.supports_dir_fd and
[446] Fix | Delete
os.listdir in os.supports_fd and
[447] Fix | Delete
os.stat in os.supports_follow_symlinks)
[448] Fix | Delete
[449] Fix | Delete
def rmtree(path, ignore_errors=False, onerror=None):
[450] Fix | Delete
"""Recursively delete a directory tree.
[451] Fix | Delete
[452] Fix | Delete
If ignore_errors is set, errors are ignored; otherwise, if onerror
[453] Fix | Delete
is set, it is called to handle the error with arguments (func,
[454] Fix | Delete
path, exc_info) where func is platform and implementation dependent;
[455] Fix | Delete
path is the argument to that function that caused it to fail; and
[456] Fix | Delete
exc_info is a tuple returned by sys.exc_info(). If ignore_errors
[457] Fix | Delete
is false and onerror is None, an exception is raised.
[458] Fix | Delete
[459] Fix | Delete
"""
[460] Fix | Delete
if ignore_errors:
[461] Fix | Delete
def onerror(*args):
[462] Fix | Delete
pass
[463] Fix | Delete
elif onerror is None:
[464] Fix | Delete
def onerror(*args):
[465] Fix | Delete
raise
[466] Fix | Delete
if _use_fd_functions:
[467] Fix | Delete
# While the unsafe rmtree works fine on bytes, the fd based does not.
[468] Fix | Delete
if isinstance(path, bytes):
[469] Fix | Delete
path = os.fsdecode(path)
[470] Fix | Delete
# Note: To guard against symlink races, we use the standard
[471] Fix | Delete
# lstat()/open()/fstat() trick.
[472] Fix | Delete
try:
[473] Fix | Delete
orig_st = os.lstat(path)
[474] Fix | Delete
except Exception:
[475] Fix | Delete
onerror(os.lstat, path, sys.exc_info())
[476] Fix | Delete
return
[477] Fix | Delete
try:
[478] Fix | Delete
fd = os.open(path, os.O_RDONLY)
[479] Fix | Delete
except Exception:
[480] Fix | Delete
onerror(os.lstat, path, sys.exc_info())
[481] Fix | Delete
return
[482] Fix | Delete
try:
[483] Fix | Delete
if os.path.samestat(orig_st, os.fstat(fd)):
[484] Fix | Delete
_rmtree_safe_fd(fd, path, onerror)
[485] Fix | Delete
try:
[486] Fix | Delete
os.rmdir(path)
[487] Fix | Delete
except OSError:
[488] Fix | Delete
onerror(os.rmdir, path, sys.exc_info())
[489] Fix | Delete
else:
[490] Fix | Delete
try:
[491] Fix | Delete
# symlinks to directories are forbidden, see bug #1669
[492] Fix | Delete
raise OSError("Cannot call rmtree on a symbolic link")
[493] Fix | Delete
except OSError:
[494] Fix | Delete
onerror(os.path.islink, path, sys.exc_info())
[495] Fix | Delete
finally:
[496] Fix | Delete
os.close(fd)
[497] Fix | Delete
else:
[498] Fix | Delete
return _rmtree_unsafe(path, onerror)
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function