Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../usr/lib64/python3....
File: os.py
r"""OS routines for NT or Posix depending on what system we're on.
[0] Fix | Delete
[1] Fix | Delete
This exports:
[2] Fix | Delete
- all functions from posix or nt, e.g. unlink, stat, etc.
[3] Fix | Delete
- os.path is either posixpath or ntpath
[4] Fix | Delete
- os.name is either 'posix' or 'nt'
[5] Fix | Delete
- os.curdir is a string representing the current directory (always '.')
[6] Fix | Delete
- os.pardir is a string representing the parent directory (always '..')
[7] Fix | Delete
- os.sep is the (or a most common) pathname separator ('/' or '\\')
[8] Fix | Delete
- os.extsep is the extension separator (always '.')
[9] Fix | Delete
- os.altsep is the alternate pathname separator (None or '/')
[10] Fix | Delete
- os.pathsep is the component separator used in $PATH etc
[11] Fix | Delete
- os.linesep is the line separator in text files ('\r' or '\n' or '\r\n')
[12] Fix | Delete
- os.defpath is the default search path for executables
[13] Fix | Delete
- os.devnull is the file path of the null device ('/dev/null', etc.)
[14] Fix | Delete
[15] Fix | Delete
Programs that import and use 'os' stand a better chance of being
[16] Fix | Delete
portable between different platforms. Of course, they must then
[17] Fix | Delete
only use functions that are defined by all platforms (e.g., unlink
[18] Fix | Delete
and opendir), and leave all pathname manipulation to os.path
[19] Fix | Delete
(e.g., split and join).
[20] Fix | Delete
"""
[21] Fix | Delete
[22] Fix | Delete
#'
[23] Fix | Delete
import abc
[24] Fix | Delete
import sys, errno
[25] Fix | Delete
import stat as st
[26] Fix | Delete
[27] Fix | Delete
_names = sys.builtin_module_names
[28] Fix | Delete
[29] Fix | Delete
# Note: more names are added to __all__ later.
[30] Fix | Delete
__all__ = ["altsep", "curdir", "pardir", "sep", "pathsep", "linesep",
[31] Fix | Delete
"defpath", "name", "path", "devnull", "SEEK_SET", "SEEK_CUR",
[32] Fix | Delete
"SEEK_END", "fsencode", "fsdecode", "get_exec_path", "fdopen",
[33] Fix | Delete
"popen", "extsep"]
[34] Fix | Delete
[35] Fix | Delete
def _exists(name):
[36] Fix | Delete
return name in globals()
[37] Fix | Delete
[38] Fix | Delete
def _get_exports_list(module):
[39] Fix | Delete
try:
[40] Fix | Delete
return list(module.__all__)
[41] Fix | Delete
except AttributeError:
[42] Fix | Delete
return [n for n in dir(module) if n[0] != '_']
[43] Fix | Delete
[44] Fix | Delete
# Any new dependencies of the os module and/or changes in path separator
[45] Fix | Delete
# requires updating importlib as well.
[46] Fix | Delete
if 'posix' in _names:
[47] Fix | Delete
name = 'posix'
[48] Fix | Delete
linesep = '\n'
[49] Fix | Delete
from posix import *
[50] Fix | Delete
try:
[51] Fix | Delete
from posix import _exit
[52] Fix | Delete
__all__.append('_exit')
[53] Fix | Delete
except ImportError:
[54] Fix | Delete
pass
[55] Fix | Delete
import posixpath as path
[56] Fix | Delete
[57] Fix | Delete
try:
[58] Fix | Delete
from posix import _have_functions
[59] Fix | Delete
except ImportError:
[60] Fix | Delete
pass
[61] Fix | Delete
[62] Fix | Delete
import posix
[63] Fix | Delete
__all__.extend(_get_exports_list(posix))
[64] Fix | Delete
del posix
[65] Fix | Delete
[66] Fix | Delete
elif 'nt' in _names:
[67] Fix | Delete
name = 'nt'
[68] Fix | Delete
linesep = '\r\n'
[69] Fix | Delete
from nt import *
[70] Fix | Delete
try:
[71] Fix | Delete
from nt import _exit
[72] Fix | Delete
__all__.append('_exit')
[73] Fix | Delete
except ImportError:
[74] Fix | Delete
pass
[75] Fix | Delete
import ntpath as path
[76] Fix | Delete
[77] Fix | Delete
import nt
[78] Fix | Delete
__all__.extend(_get_exports_list(nt))
[79] Fix | Delete
del nt
[80] Fix | Delete
[81] Fix | Delete
try:
[82] Fix | Delete
from nt import _have_functions
[83] Fix | Delete
except ImportError:
[84] Fix | Delete
pass
[85] Fix | Delete
[86] Fix | Delete
else:
[87] Fix | Delete
raise ImportError('no os specific module found')
[88] Fix | Delete
[89] Fix | Delete
sys.modules['os.path'] = path
[90] Fix | Delete
from os.path import (curdir, pardir, sep, pathsep, defpath, extsep, altsep,
[91] Fix | Delete
devnull)
[92] Fix | Delete
[93] Fix | Delete
del _names
[94] Fix | Delete
[95] Fix | Delete
[96] Fix | Delete
if _exists("_have_functions"):
[97] Fix | Delete
_globals = globals()
[98] Fix | Delete
def _add(str, fn):
[99] Fix | Delete
if (fn in _globals) and (str in _have_functions):
[100] Fix | Delete
_set.add(_globals[fn])
[101] Fix | Delete
[102] Fix | Delete
_set = set()
[103] Fix | Delete
_add("HAVE_FACCESSAT", "access")
[104] Fix | Delete
_add("HAVE_FCHMODAT", "chmod")
[105] Fix | Delete
_add("HAVE_FCHOWNAT", "chown")
[106] Fix | Delete
_add("HAVE_FSTATAT", "stat")
[107] Fix | Delete
_add("HAVE_FUTIMESAT", "utime")
[108] Fix | Delete
_add("HAVE_LINKAT", "link")
[109] Fix | Delete
_add("HAVE_MKDIRAT", "mkdir")
[110] Fix | Delete
_add("HAVE_MKFIFOAT", "mkfifo")
[111] Fix | Delete
_add("HAVE_MKNODAT", "mknod")
[112] Fix | Delete
_add("HAVE_OPENAT", "open")
[113] Fix | Delete
_add("HAVE_READLINKAT", "readlink")
[114] Fix | Delete
_add("HAVE_RENAMEAT", "rename")
[115] Fix | Delete
_add("HAVE_SYMLINKAT", "symlink")
[116] Fix | Delete
_add("HAVE_UNLINKAT", "unlink")
[117] Fix | Delete
_add("HAVE_UNLINKAT", "rmdir")
[118] Fix | Delete
_add("HAVE_UTIMENSAT", "utime")
[119] Fix | Delete
supports_dir_fd = _set
[120] Fix | Delete
[121] Fix | Delete
_set = set()
[122] Fix | Delete
_add("HAVE_FACCESSAT", "access")
[123] Fix | Delete
supports_effective_ids = _set
[124] Fix | Delete
[125] Fix | Delete
_set = set()
[126] Fix | Delete
_add("HAVE_FCHDIR", "chdir")
[127] Fix | Delete
_add("HAVE_FCHMOD", "chmod")
[128] Fix | Delete
_add("HAVE_FCHOWN", "chown")
[129] Fix | Delete
_add("HAVE_FDOPENDIR", "listdir")
[130] Fix | Delete
_add("HAVE_FEXECVE", "execve")
[131] Fix | Delete
_set.add(stat) # fstat always works
[132] Fix | Delete
_add("HAVE_FTRUNCATE", "truncate")
[133] Fix | Delete
_add("HAVE_FUTIMENS", "utime")
[134] Fix | Delete
_add("HAVE_FUTIMES", "utime")
[135] Fix | Delete
_add("HAVE_FPATHCONF", "pathconf")
[136] Fix | Delete
if _exists("statvfs") and _exists("fstatvfs"): # mac os x10.3
[137] Fix | Delete
_add("HAVE_FSTATVFS", "statvfs")
[138] Fix | Delete
supports_fd = _set
[139] Fix | Delete
[140] Fix | Delete
_set = set()
[141] Fix | Delete
_add("HAVE_FACCESSAT", "access")
[142] Fix | Delete
# Some platforms don't support lchmod(). Often the function exists
[143] Fix | Delete
# anyway, as a stub that always returns ENOSUP or perhaps EOPNOTSUPP.
[144] Fix | Delete
# (No, I don't know why that's a good design.) ./configure will detect
[145] Fix | Delete
# this and reject it--so HAVE_LCHMOD still won't be defined on such
[146] Fix | Delete
# platforms. This is Very Helpful.
[147] Fix | Delete
#
[148] Fix | Delete
# However, sometimes platforms without a working lchmod() *do* have
[149] Fix | Delete
# fchmodat(). (Examples: Linux kernel 3.2 with glibc 2.15,
[150] Fix | Delete
# OpenIndiana 3.x.) And fchmodat() has a flag that theoretically makes
[151] Fix | Delete
# it behave like lchmod(). So in theory it would be a suitable
[152] Fix | Delete
# replacement for lchmod(). But when lchmod() doesn't work, fchmodat()'s
[153] Fix | Delete
# flag doesn't work *either*. Sadly ./configure isn't sophisticated
[154] Fix | Delete
# enough to detect this condition--it only determines whether or not
[155] Fix | Delete
# fchmodat() minimally works.
[156] Fix | Delete
#
[157] Fix | Delete
# Therefore we simply ignore fchmodat() when deciding whether or not
[158] Fix | Delete
# os.chmod supports follow_symlinks. Just checking lchmod() is
[159] Fix | Delete
# sufficient. After all--if you have a working fchmodat(), your
[160] Fix | Delete
# lchmod() almost certainly works too.
[161] Fix | Delete
#
[162] Fix | Delete
# _add("HAVE_FCHMODAT", "chmod")
[163] Fix | Delete
_add("HAVE_FCHOWNAT", "chown")
[164] Fix | Delete
_add("HAVE_FSTATAT", "stat")
[165] Fix | Delete
_add("HAVE_LCHFLAGS", "chflags")
[166] Fix | Delete
_add("HAVE_LCHMOD", "chmod")
[167] Fix | Delete
if _exists("lchown"): # mac os x10.3
[168] Fix | Delete
_add("HAVE_LCHOWN", "chown")
[169] Fix | Delete
_add("HAVE_LINKAT", "link")
[170] Fix | Delete
_add("HAVE_LUTIMES", "utime")
[171] Fix | Delete
_add("HAVE_LSTAT", "stat")
[172] Fix | Delete
_add("HAVE_FSTATAT", "stat")
[173] Fix | Delete
_add("HAVE_UTIMENSAT", "utime")
[174] Fix | Delete
_add("MS_WINDOWS", "stat")
[175] Fix | Delete
supports_follow_symlinks = _set
[176] Fix | Delete
[177] Fix | Delete
del _set
[178] Fix | Delete
del _have_functions
[179] Fix | Delete
del _globals
[180] Fix | Delete
del _add
[181] Fix | Delete
[182] Fix | Delete
[183] Fix | Delete
# Python uses fixed values for the SEEK_ constants; they are mapped
[184] Fix | Delete
# to native constants if necessary in posixmodule.c
[185] Fix | Delete
# Other possible SEEK values are directly imported from posixmodule.c
[186] Fix | Delete
SEEK_SET = 0
[187] Fix | Delete
SEEK_CUR = 1
[188] Fix | Delete
SEEK_END = 2
[189] Fix | Delete
[190] Fix | Delete
# Super directory utilities.
[191] Fix | Delete
# (Inspired by Eric Raymond; the doc strings are mostly his)
[192] Fix | Delete
[193] Fix | Delete
def makedirs(name, mode=0o777, exist_ok=False):
[194] Fix | Delete
"""makedirs(name [, mode=0o777][, exist_ok=False])
[195] Fix | Delete
[196] Fix | Delete
Super-mkdir; create a leaf directory and all intermediate ones. Works like
[197] Fix | Delete
mkdir, except that any intermediate path segment (not just the rightmost)
[198] Fix | Delete
will be created if it does not exist. If the target directory already
[199] Fix | Delete
exists, raise an OSError if exist_ok is False. Otherwise no exception is
[200] Fix | Delete
raised. This is recursive.
[201] Fix | Delete
[202] Fix | Delete
"""
[203] Fix | Delete
head, tail = path.split(name)
[204] Fix | Delete
if not tail:
[205] Fix | Delete
head, tail = path.split(head)
[206] Fix | Delete
if head and tail and not path.exists(head):
[207] Fix | Delete
try:
[208] Fix | Delete
makedirs(head, mode, exist_ok)
[209] Fix | Delete
except FileExistsError:
[210] Fix | Delete
# Defeats race condition when another thread created the path
[211] Fix | Delete
pass
[212] Fix | Delete
cdir = curdir
[213] Fix | Delete
if isinstance(tail, bytes):
[214] Fix | Delete
cdir = bytes(curdir, 'ASCII')
[215] Fix | Delete
if tail == cdir: # xxx/newdir/. exists if xxx/newdir exists
[216] Fix | Delete
return
[217] Fix | Delete
try:
[218] Fix | Delete
mkdir(name, mode)
[219] Fix | Delete
except OSError:
[220] Fix | Delete
# Cannot rely on checking for EEXIST, since the operating system
[221] Fix | Delete
# could give priority to other errors like EACCES or EROFS
[222] Fix | Delete
if not exist_ok or not path.isdir(name):
[223] Fix | Delete
raise
[224] Fix | Delete
[225] Fix | Delete
def removedirs(name):
[226] Fix | Delete
"""removedirs(name)
[227] Fix | Delete
[228] Fix | Delete
Super-rmdir; remove a leaf directory and all empty intermediate
[229] Fix | Delete
ones. Works like rmdir except that, if the leaf directory is
[230] Fix | Delete
successfully removed, directories corresponding to rightmost path
[231] Fix | Delete
segments will be pruned away until either the whole path is
[232] Fix | Delete
consumed or an error occurs. Errors during this latter phase are
[233] Fix | Delete
ignored -- they generally mean that a directory was not empty.
[234] Fix | Delete
[235] Fix | Delete
"""
[236] Fix | Delete
rmdir(name)
[237] Fix | Delete
head, tail = path.split(name)
[238] Fix | Delete
if not tail:
[239] Fix | Delete
head, tail = path.split(head)
[240] Fix | Delete
while head and tail:
[241] Fix | Delete
try:
[242] Fix | Delete
rmdir(head)
[243] Fix | Delete
except OSError:
[244] Fix | Delete
break
[245] Fix | Delete
head, tail = path.split(head)
[246] Fix | Delete
[247] Fix | Delete
def renames(old, new):
[248] Fix | Delete
"""renames(old, new)
[249] Fix | Delete
[250] Fix | Delete
Super-rename; create directories as necessary and delete any left
[251] Fix | Delete
empty. Works like rename, except creation of any intermediate
[252] Fix | Delete
directories needed to make the new pathname good is attempted
[253] Fix | Delete
first. After the rename, directories corresponding to rightmost
[254] Fix | Delete
path segments of the old name will be pruned until either the
[255] Fix | Delete
whole path is consumed or a nonempty directory is found.
[256] Fix | Delete
[257] Fix | Delete
Note: this function can fail with the new directory structure made
[258] Fix | Delete
if you lack permissions needed to unlink the leaf directory or
[259] Fix | Delete
file.
[260] Fix | Delete
[261] Fix | Delete
"""
[262] Fix | Delete
head, tail = path.split(new)
[263] Fix | Delete
if head and tail and not path.exists(head):
[264] Fix | Delete
makedirs(head)
[265] Fix | Delete
rename(old, new)
[266] Fix | Delete
head, tail = path.split(old)
[267] Fix | Delete
if head and tail:
[268] Fix | Delete
try:
[269] Fix | Delete
removedirs(head)
[270] Fix | Delete
except OSError:
[271] Fix | Delete
pass
[272] Fix | Delete
[273] Fix | Delete
__all__.extend(["makedirs", "removedirs", "renames"])
[274] Fix | Delete
[275] Fix | Delete
def walk(top, topdown=True, onerror=None, followlinks=False):
[276] Fix | Delete
"""Directory tree generator.
[277] Fix | Delete
[278] Fix | Delete
For each directory in the directory tree rooted at top (including top
[279] Fix | Delete
itself, but excluding '.' and '..'), yields a 3-tuple
[280] Fix | Delete
[281] Fix | Delete
dirpath, dirnames, filenames
[282] Fix | Delete
[283] Fix | Delete
dirpath is a string, the path to the directory. dirnames is a list of
[284] Fix | Delete
the names of the subdirectories in dirpath (excluding '.' and '..').
[285] Fix | Delete
filenames is a list of the names of the non-directory files in dirpath.
[286] Fix | Delete
Note that the names in the lists are just names, with no path components.
[287] Fix | Delete
To get a full path (which begins with top) to a file or directory in
[288] Fix | Delete
dirpath, do os.path.join(dirpath, name).
[289] Fix | Delete
[290] Fix | Delete
If optional arg 'topdown' is true or not specified, the triple for a
[291] Fix | Delete
directory is generated before the triples for any of its subdirectories
[292] Fix | Delete
(directories are generated top down). If topdown is false, the triple
[293] Fix | Delete
for a directory is generated after the triples for all of its
[294] Fix | Delete
subdirectories (directories are generated bottom up).
[295] Fix | Delete
[296] Fix | Delete
When topdown is true, the caller can modify the dirnames list in-place
[297] Fix | Delete
(e.g., via del or slice assignment), and walk will only recurse into the
[298] Fix | Delete
subdirectories whose names remain in dirnames; this can be used to prune the
[299] Fix | Delete
search, or to impose a specific order of visiting. Modifying dirnames when
[300] Fix | Delete
topdown is false is ineffective, since the directories in dirnames have
[301] Fix | Delete
already been generated by the time dirnames itself is generated. No matter
[302] Fix | Delete
the value of topdown, the list of subdirectories is retrieved before the
[303] Fix | Delete
tuples for the directory and its subdirectories are generated.
[304] Fix | Delete
[305] Fix | Delete
By default errors from the os.scandir() call are ignored. If
[306] Fix | Delete
optional arg 'onerror' is specified, it should be a function; it
[307] Fix | Delete
will be called with one argument, an OSError instance. It can
[308] Fix | Delete
report the error to continue with the walk, or raise the exception
[309] Fix | Delete
to abort the walk. Note that the filename is available as the
[310] Fix | Delete
filename attribute of the exception object.
[311] Fix | Delete
[312] Fix | Delete
By default, os.walk does not follow symbolic links to subdirectories on
[313] Fix | Delete
systems that support them. In order to get this functionality, set the
[314] Fix | Delete
optional argument 'followlinks' to true.
[315] Fix | Delete
[316] Fix | Delete
Caution: if you pass a relative pathname for top, don't change the
[317] Fix | Delete
current working directory between resumptions of walk. walk never
[318] Fix | Delete
changes the current directory, and assumes that the client doesn't
[319] Fix | Delete
either.
[320] Fix | Delete
[321] Fix | Delete
Example:
[322] Fix | Delete
[323] Fix | Delete
import os
[324] Fix | Delete
from os.path import join, getsize
[325] Fix | Delete
for root, dirs, files in os.walk('python/Lib/email'):
[326] Fix | Delete
print(root, "consumes", end="")
[327] Fix | Delete
print(sum([getsize(join(root, name)) for name in files]), end="")
[328] Fix | Delete
print("bytes in", len(files), "non-directory files")
[329] Fix | Delete
if 'CVS' in dirs:
[330] Fix | Delete
dirs.remove('CVS') # don't visit CVS directories
[331] Fix | Delete
[332] Fix | Delete
"""
[333] Fix | Delete
top = fspath(top)
[334] Fix | Delete
dirs = []
[335] Fix | Delete
nondirs = []
[336] Fix | Delete
walk_dirs = []
[337] Fix | Delete
[338] Fix | Delete
# We may not have read permission for top, in which case we can't
[339] Fix | Delete
# get a list of the files the directory contains. os.walk
[340] Fix | Delete
# always suppressed the exception then, rather than blow up for a
[341] Fix | Delete
# minor reason when (say) a thousand readable directories are still
[342] Fix | Delete
# left to visit. That logic is copied here.
[343] Fix | Delete
try:
[344] Fix | Delete
# Note that scandir is global in this module due
[345] Fix | Delete
# to earlier import-*.
[346] Fix | Delete
scandir_it = scandir(top)
[347] Fix | Delete
except OSError as error:
[348] Fix | Delete
if onerror is not None:
[349] Fix | Delete
onerror(error)
[350] Fix | Delete
return
[351] Fix | Delete
[352] Fix | Delete
with scandir_it:
[353] Fix | Delete
while True:
[354] Fix | Delete
try:
[355] Fix | Delete
try:
[356] Fix | Delete
entry = next(scandir_it)
[357] Fix | Delete
except StopIteration:
[358] Fix | Delete
break
[359] Fix | Delete
except OSError as error:
[360] Fix | Delete
if onerror is not None:
[361] Fix | Delete
onerror(error)
[362] Fix | Delete
return
[363] Fix | Delete
[364] Fix | Delete
try:
[365] Fix | Delete
is_dir = entry.is_dir()
[366] Fix | Delete
except OSError:
[367] Fix | Delete
# If is_dir() raises an OSError, consider that the entry is not
[368] Fix | Delete
# a directory, same behaviour than os.path.isdir().
[369] Fix | Delete
is_dir = False
[370] Fix | Delete
[371] Fix | Delete
if is_dir:
[372] Fix | Delete
dirs.append(entry.name)
[373] Fix | Delete
else:
[374] Fix | Delete
nondirs.append(entry.name)
[375] Fix | Delete
[376] Fix | Delete
if not topdown and is_dir:
[377] Fix | Delete
# Bottom-up: recurse into sub-directory, but exclude symlinks to
[378] Fix | Delete
# directories if followlinks is False
[379] Fix | Delete
if followlinks:
[380] Fix | Delete
walk_into = True
[381] Fix | Delete
else:
[382] Fix | Delete
try:
[383] Fix | Delete
is_symlink = entry.is_symlink()
[384] Fix | Delete
except OSError:
[385] Fix | Delete
# If is_symlink() raises an OSError, consider that the
[386] Fix | Delete
# entry is not a symbolic link, same behaviour than
[387] Fix | Delete
# os.path.islink().
[388] Fix | Delete
is_symlink = False
[389] Fix | Delete
walk_into = not is_symlink
[390] Fix | Delete
[391] Fix | Delete
if walk_into:
[392] Fix | Delete
walk_dirs.append(entry.path)
[393] Fix | Delete
[394] Fix | Delete
# Yield before recursion if going top down
[395] Fix | Delete
if topdown:
[396] Fix | Delete
yield top, dirs, nondirs
[397] Fix | Delete
[398] Fix | Delete
# Recurse into sub-directories
[399] Fix | Delete
islink, join = path.islink, path.join
[400] Fix | Delete
for dirname in dirs:
[401] Fix | Delete
new_path = join(top, dirname)
[402] Fix | Delete
# Issue #23605: os.path.islink() is used instead of caching
[403] Fix | Delete
# entry.is_symlink() result during the loop on os.scandir() because
[404] Fix | Delete
# the caller can replace the directory entry during the "yield"
[405] Fix | Delete
# above.
[406] Fix | Delete
if followlinks or not islink(new_path):
[407] Fix | Delete
yield from walk(new_path, topdown, onerror, followlinks)
[408] Fix | Delete
else:
[409] Fix | Delete
# Recurse into sub-directories
[410] Fix | Delete
for new_path in walk_dirs:
[411] Fix | Delete
yield from walk(new_path, topdown, onerror, followlinks)
[412] Fix | Delete
# Yield after recursion if going bottom up
[413] Fix | Delete
yield top, dirs, nondirs
[414] Fix | Delete
[415] Fix | Delete
__all__.append("walk")
[416] Fix | Delete
[417] Fix | Delete
if {open, stat} <= supports_dir_fd and {listdir, stat} <= supports_fd:
[418] Fix | Delete
[419] Fix | Delete
def fwalk(top=".", topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=None):
[420] Fix | Delete
"""Directory tree generator.
[421] Fix | Delete
[422] Fix | Delete
This behaves exactly like walk(), except that it yields a 4-tuple
[423] Fix | Delete
[424] Fix | Delete
dirpath, dirnames, filenames, dirfd
[425] Fix | Delete
[426] Fix | Delete
`dirpath`, `dirnames` and `filenames` are identical to walk() output,
[427] Fix | Delete
and `dirfd` is a file descriptor referring to the directory `dirpath`.
[428] Fix | Delete
[429] Fix | Delete
The advantage of fwalk() over walk() is that it's safe against symlink
[430] Fix | Delete
races (when follow_symlinks is False).
[431] Fix | Delete
[432] Fix | Delete
If dir_fd is not None, it should be a file descriptor open to a directory,
[433] Fix | Delete
and top should be relative; top will then be relative to that directory.
[434] Fix | Delete
(dir_fd is always supported for fwalk.)
[435] Fix | Delete
[436] Fix | Delete
Caution:
[437] Fix | Delete
Since fwalk() yields file descriptors, those are only valid until the
[438] Fix | Delete
next iteration step, so you should dup() them if you want to keep them
[439] Fix | Delete
for a longer period.
[440] Fix | Delete
[441] Fix | Delete
Example:
[442] Fix | Delete
[443] Fix | Delete
import os
[444] Fix | Delete
for root, dirs, files, rootfd in os.fwalk('python/Lib/email'):
[445] Fix | Delete
print(root, "consumes", end="")
[446] Fix | Delete
print(sum([os.stat(name, dir_fd=rootfd).st_size for name in files]),
[447] Fix | Delete
end="")
[448] Fix | Delete
print("bytes in", len(files), "non-directory files")
[449] Fix | Delete
if 'CVS' in dirs:
[450] Fix | Delete
dirs.remove('CVS') # don't visit CVS directories
[451] Fix | Delete
"""
[452] Fix | Delete
if not isinstance(top, int) or not hasattr(top, '__index__'):
[453] Fix | Delete
top = fspath(top)
[454] Fix | Delete
# Note: To guard against symlink races, we use the standard
[455] Fix | Delete
# lstat()/open()/fstat() trick.
[456] Fix | Delete
orig_st = stat(top, follow_symlinks=False, dir_fd=dir_fd)
[457] Fix | Delete
topfd = open(top, O_RDONLY, dir_fd=dir_fd)
[458] Fix | Delete
try:
[459] Fix | Delete
if (follow_symlinks or (st.S_ISDIR(orig_st.st_mode) and
[460] Fix | Delete
path.samestat(orig_st, stat(topfd)))):
[461] Fix | Delete
yield from _fwalk(topfd, top, topdown, onerror, follow_symlinks)
[462] Fix | Delete
finally:
[463] Fix | Delete
close(topfd)
[464] Fix | Delete
[465] Fix | Delete
def _fwalk(topfd, toppath, topdown, onerror, follow_symlinks):
[466] Fix | Delete
# Note: This uses O(depth of the directory tree) file descriptors: if
[467] Fix | Delete
# necessary, it can be adapted to only require O(1) FDs, see issue
[468] Fix | Delete
# #13734.
[469] Fix | Delete
[470] Fix | Delete
names = listdir(topfd)
[471] Fix | Delete
dirs, nondirs = [], []
[472] Fix | Delete
for name in names:
[473] Fix | Delete
try:
[474] Fix | Delete
# Here, we don't use AT_SYMLINK_NOFOLLOW to be consistent with
[475] Fix | Delete
# walk() which reports symlinks to directories as directories.
[476] Fix | Delete
# We do however check for symlinks before recursing into
[477] Fix | Delete
# a subdirectory.
[478] Fix | Delete
if st.S_ISDIR(stat(name, dir_fd=topfd).st_mode):
[479] Fix | Delete
dirs.append(name)
[480] Fix | Delete
else:
[481] Fix | Delete
nondirs.append(name)
[482] Fix | Delete
except OSError:
[483] Fix | Delete
try:
[484] Fix | Delete
# Add dangling symlinks, ignore disappeared files
[485] Fix | Delete
if st.S_ISLNK(stat(name, dir_fd=topfd, follow_symlinks=False)
[486] Fix | Delete
.st_mode):
[487] Fix | Delete
nondirs.append(name)
[488] Fix | Delete
except OSError:
[489] Fix | Delete
continue
[490] Fix | Delete
[491] Fix | Delete
if topdown:
[492] Fix | Delete
yield toppath, dirs, nondirs, topfd
[493] Fix | Delete
[494] Fix | Delete
for name in dirs:
[495] Fix | Delete
try:
[496] Fix | Delete
orig_st = stat(name, dir_fd=topfd, follow_symlinks=follow_symlinks)
[497] Fix | Delete
dirfd = open(name, O_RDONLY, dir_fd=topfd)
[498] Fix | Delete
except OSError as err:
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function