Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2....
File: posixpath.py
"""Common operations on Posix pathnames.
[0] Fix | Delete
[1] Fix | Delete
Instead of importing this module directly, import os and refer to
[2] Fix | Delete
this module as os.path. The "os.path" name is an alias for this
[3] Fix | Delete
module on Posix systems; on other systems (e.g. Mac, Windows),
[4] Fix | Delete
os.path provides the same operations in a manner specific to that
[5] Fix | Delete
platform, and is an alias to another module (e.g. macpath, ntpath).
[6] Fix | Delete
[7] Fix | Delete
Some of this can actually be useful on non-Posix systems too, e.g.
[8] Fix | Delete
for manipulation of the pathname component of URLs.
[9] Fix | Delete
"""
[10] Fix | Delete
[11] Fix | Delete
import os
[12] Fix | Delete
import sys
[13] Fix | Delete
import stat
[14] Fix | Delete
import genericpath
[15] Fix | Delete
import warnings
[16] Fix | Delete
from genericpath import *
[17] Fix | Delete
from genericpath import _unicode
[18] Fix | Delete
[19] Fix | Delete
__all__ = ["normcase","isabs","join","splitdrive","split","splitext",
[20] Fix | Delete
"basename","dirname","commonprefix","getsize","getmtime",
[21] Fix | Delete
"getatime","getctime","islink","exists","lexists","isdir","isfile",
[22] Fix | Delete
"ismount","walk","expanduser","expandvars","normpath","abspath",
[23] Fix | Delete
"samefile","sameopenfile","samestat",
[24] Fix | Delete
"curdir","pardir","sep","pathsep","defpath","altsep","extsep",
[25] Fix | Delete
"devnull","realpath","supports_unicode_filenames","relpath"]
[26] Fix | Delete
[27] Fix | Delete
# strings representing various path-related bits and pieces
[28] Fix | Delete
curdir = '.'
[29] Fix | Delete
pardir = '..'
[30] Fix | Delete
extsep = '.'
[31] Fix | Delete
sep = '/'
[32] Fix | Delete
pathsep = ':'
[33] Fix | Delete
defpath = ':/bin:/usr/bin'
[34] Fix | Delete
altsep = None
[35] Fix | Delete
devnull = '/dev/null'
[36] Fix | Delete
[37] Fix | Delete
# Normalize the case of a pathname. Trivial in Posix, string.lower on Mac.
[38] Fix | Delete
# On MS-DOS this may also turn slashes into backslashes; however, other
[39] Fix | Delete
# normalizations (such as optimizing '../' away) are not allowed
[40] Fix | Delete
# (another function should be defined to do that).
[41] Fix | Delete
[42] Fix | Delete
def normcase(s):
[43] Fix | Delete
"""Normalize case of pathname. Has no effect under Posix"""
[44] Fix | Delete
return s
[45] Fix | Delete
[46] Fix | Delete
[47] Fix | Delete
# Return whether a path is absolute.
[48] Fix | Delete
# Trivial in Posix, harder on the Mac or MS-DOS.
[49] Fix | Delete
[50] Fix | Delete
def isabs(s):
[51] Fix | Delete
"""Test whether a path is absolute"""
[52] Fix | Delete
return s.startswith('/')
[53] Fix | Delete
[54] Fix | Delete
[55] Fix | Delete
# Join pathnames.
[56] Fix | Delete
# Ignore the previous parts if a part is absolute.
[57] Fix | Delete
# Insert a '/' unless the first part is empty or already ends in '/'.
[58] Fix | Delete
[59] Fix | Delete
def join(a, *p):
[60] Fix | Delete
"""Join two or more pathname components, inserting '/' as needed.
[61] Fix | Delete
If any component is an absolute path, all previous path components
[62] Fix | Delete
will be discarded. An empty last part will result in a path that
[63] Fix | Delete
ends with a separator."""
[64] Fix | Delete
path = a
[65] Fix | Delete
for b in p:
[66] Fix | Delete
if b.startswith('/'):
[67] Fix | Delete
path = b
[68] Fix | Delete
elif path == '' or path.endswith('/'):
[69] Fix | Delete
path += b
[70] Fix | Delete
else:
[71] Fix | Delete
path += '/' + b
[72] Fix | Delete
return path
[73] Fix | Delete
[74] Fix | Delete
[75] Fix | Delete
# Split a path in head (everything up to the last '/') and tail (the
[76] Fix | Delete
# rest). If the path ends in '/', tail will be empty. If there is no
[77] Fix | Delete
# '/' in the path, head will be empty.
[78] Fix | Delete
# Trailing '/'es are stripped from head unless it is the root.
[79] Fix | Delete
[80] Fix | Delete
def split(p):
[81] Fix | Delete
"""Split a pathname. Returns tuple "(head, tail)" where "tail" is
[82] Fix | Delete
everything after the final slash. Either part may be empty."""
[83] Fix | Delete
i = p.rfind('/') + 1
[84] Fix | Delete
head, tail = p[:i], p[i:]
[85] Fix | Delete
if head and head != '/'*len(head):
[86] Fix | Delete
head = head.rstrip('/')
[87] Fix | Delete
return head, tail
[88] Fix | Delete
[89] Fix | Delete
[90] Fix | Delete
# Split a path in root and extension.
[91] Fix | Delete
# The extension is everything starting at the last dot in the last
[92] Fix | Delete
# pathname component; the root is everything before that.
[93] Fix | Delete
# It is always true that root + ext == p.
[94] Fix | Delete
[95] Fix | Delete
def splitext(p):
[96] Fix | Delete
return genericpath._splitext(p, sep, altsep, extsep)
[97] Fix | Delete
splitext.__doc__ = genericpath._splitext.__doc__
[98] Fix | Delete
[99] Fix | Delete
# Split a pathname into a drive specification and the rest of the
[100] Fix | Delete
# path. Useful on DOS/Windows/NT; on Unix, the drive is always empty.
[101] Fix | Delete
[102] Fix | Delete
def splitdrive(p):
[103] Fix | Delete
"""Split a pathname into drive and path. On Posix, drive is always
[104] Fix | Delete
empty."""
[105] Fix | Delete
return '', p
[106] Fix | Delete
[107] Fix | Delete
[108] Fix | Delete
# Return the tail (basename) part of a path, same as split(path)[1].
[109] Fix | Delete
[110] Fix | Delete
def basename(p):
[111] Fix | Delete
"""Returns the final component of a pathname"""
[112] Fix | Delete
i = p.rfind('/') + 1
[113] Fix | Delete
return p[i:]
[114] Fix | Delete
[115] Fix | Delete
[116] Fix | Delete
# Return the head (dirname) part of a path, same as split(path)[0].
[117] Fix | Delete
[118] Fix | Delete
def dirname(p):
[119] Fix | Delete
"""Returns the directory component of a pathname"""
[120] Fix | Delete
i = p.rfind('/') + 1
[121] Fix | Delete
head = p[:i]
[122] Fix | Delete
if head and head != '/'*len(head):
[123] Fix | Delete
head = head.rstrip('/')
[124] Fix | Delete
return head
[125] Fix | Delete
[126] Fix | Delete
[127] Fix | Delete
# Is a path a symbolic link?
[128] Fix | Delete
# This will always return false on systems where os.lstat doesn't exist.
[129] Fix | Delete
[130] Fix | Delete
def islink(path):
[131] Fix | Delete
"""Test whether a path is a symbolic link"""
[132] Fix | Delete
try:
[133] Fix | Delete
st = os.lstat(path)
[134] Fix | Delete
except (os.error, AttributeError):
[135] Fix | Delete
return False
[136] Fix | Delete
return stat.S_ISLNK(st.st_mode)
[137] Fix | Delete
[138] Fix | Delete
# Being true for dangling symbolic links is also useful.
[139] Fix | Delete
[140] Fix | Delete
def lexists(path):
[141] Fix | Delete
"""Test whether a path exists. Returns True for broken symbolic links"""
[142] Fix | Delete
try:
[143] Fix | Delete
os.lstat(path)
[144] Fix | Delete
except os.error:
[145] Fix | Delete
return False
[146] Fix | Delete
return True
[147] Fix | Delete
[148] Fix | Delete
[149] Fix | Delete
# Are two filenames really pointing to the same file?
[150] Fix | Delete
[151] Fix | Delete
def samefile(f1, f2):
[152] Fix | Delete
"""Test whether two pathnames reference the same actual file"""
[153] Fix | Delete
s1 = os.stat(f1)
[154] Fix | Delete
s2 = os.stat(f2)
[155] Fix | Delete
return samestat(s1, s2)
[156] Fix | Delete
[157] Fix | Delete
[158] Fix | Delete
# Are two open files really referencing the same file?
[159] Fix | Delete
# (Not necessarily the same file descriptor!)
[160] Fix | Delete
[161] Fix | Delete
def sameopenfile(fp1, fp2):
[162] Fix | Delete
"""Test whether two open file objects reference the same file"""
[163] Fix | Delete
s1 = os.fstat(fp1)
[164] Fix | Delete
s2 = os.fstat(fp2)
[165] Fix | Delete
return samestat(s1, s2)
[166] Fix | Delete
[167] Fix | Delete
[168] Fix | Delete
# Are two stat buffers (obtained from stat, fstat or lstat)
[169] Fix | Delete
# describing the same file?
[170] Fix | Delete
[171] Fix | Delete
def samestat(s1, s2):
[172] Fix | Delete
"""Test whether two stat buffers reference the same file"""
[173] Fix | Delete
return s1.st_ino == s2.st_ino and \
[174] Fix | Delete
s1.st_dev == s2.st_dev
[175] Fix | Delete
[176] Fix | Delete
[177] Fix | Delete
# Is a path a mount point?
[178] Fix | Delete
# (Does this work for all UNIXes? Is it even guaranteed to work by Posix?)
[179] Fix | Delete
[180] Fix | Delete
def ismount(path):
[181] Fix | Delete
"""Test whether a path is a mount point"""
[182] Fix | Delete
if islink(path):
[183] Fix | Delete
# A symlink can never be a mount point
[184] Fix | Delete
return False
[185] Fix | Delete
try:
[186] Fix | Delete
s1 = os.lstat(path)
[187] Fix | Delete
s2 = os.lstat(realpath(join(path, '..')))
[188] Fix | Delete
except os.error:
[189] Fix | Delete
return False # It doesn't exist -- so not a mount point :-)
[190] Fix | Delete
dev1 = s1.st_dev
[191] Fix | Delete
dev2 = s2.st_dev
[192] Fix | Delete
if dev1 != dev2:
[193] Fix | Delete
return True # path/.. on a different device as path
[194] Fix | Delete
ino1 = s1.st_ino
[195] Fix | Delete
ino2 = s2.st_ino
[196] Fix | Delete
if ino1 == ino2:
[197] Fix | Delete
return True # path/.. is the same i-node as path
[198] Fix | Delete
return False
[199] Fix | Delete
[200] Fix | Delete
[201] Fix | Delete
# Directory tree walk.
[202] Fix | Delete
# For each directory under top (including top itself, but excluding
[203] Fix | Delete
# '.' and '..'), func(arg, dirname, filenames) is called, where
[204] Fix | Delete
# dirname is the name of the directory and filenames is the list
[205] Fix | Delete
# of files (and subdirectories etc.) in the directory.
[206] Fix | Delete
# The func may modify the filenames list, to implement a filter,
[207] Fix | Delete
# or to impose a different order of visiting.
[208] Fix | Delete
[209] Fix | Delete
def walk(top, func, arg):
[210] Fix | Delete
"""Directory tree walk with callback function.
[211] Fix | Delete
[212] Fix | Delete
For each directory in the directory tree rooted at top (including top
[213] Fix | Delete
itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
[214] Fix | Delete
dirname is the name of the directory, and fnames a list of the names of
[215] Fix | Delete
the files and subdirectories in dirname (excluding '.' and '..'). func
[216] Fix | Delete
may modify the fnames list in-place (e.g. via del or slice assignment),
[217] Fix | Delete
and walk will only recurse into the subdirectories whose names remain in
[218] Fix | Delete
fnames; this can be used to implement a filter, or to impose a specific
[219] Fix | Delete
order of visiting. No semantics are defined for, or required of, arg,
[220] Fix | Delete
beyond that arg is always passed to func. It can be used, e.g., to pass
[221] Fix | Delete
a filename pattern, or a mutable object designed to accumulate
[222] Fix | Delete
statistics. Passing None for arg is common."""
[223] Fix | Delete
warnings.warnpy3k("In 3.x, os.path.walk is removed in favor of os.walk.",
[224] Fix | Delete
stacklevel=2)
[225] Fix | Delete
try:
[226] Fix | Delete
names = os.listdir(top)
[227] Fix | Delete
except os.error:
[228] Fix | Delete
return
[229] Fix | Delete
func(arg, top, names)
[230] Fix | Delete
for name in names:
[231] Fix | Delete
name = join(top, name)
[232] Fix | Delete
try:
[233] Fix | Delete
st = os.lstat(name)
[234] Fix | Delete
except os.error:
[235] Fix | Delete
continue
[236] Fix | Delete
if stat.S_ISDIR(st.st_mode):
[237] Fix | Delete
walk(name, func, arg)
[238] Fix | Delete
[239] Fix | Delete
[240] Fix | Delete
# Expand paths beginning with '~' or '~user'.
[241] Fix | Delete
# '~' means $HOME; '~user' means that user's home directory.
[242] Fix | Delete
# If the path doesn't begin with '~', or if the user or $HOME is unknown,
[243] Fix | Delete
# the path is returned unchanged (leaving error reporting to whatever
[244] Fix | Delete
# function is called with the expanded path as argument).
[245] Fix | Delete
# See also module 'glob' for expansion of *, ? and [...] in pathnames.
[246] Fix | Delete
# (A function should also be defined to do full *sh-style environment
[247] Fix | Delete
# variable expansion.)
[248] Fix | Delete
[249] Fix | Delete
def expanduser(path):
[250] Fix | Delete
"""Expand ~ and ~user constructions. If user or $HOME is unknown,
[251] Fix | Delete
do nothing."""
[252] Fix | Delete
if not path.startswith('~'):
[253] Fix | Delete
return path
[254] Fix | Delete
i = path.find('/', 1)
[255] Fix | Delete
if i < 0:
[256] Fix | Delete
i = len(path)
[257] Fix | Delete
if i == 1:
[258] Fix | Delete
if 'HOME' not in os.environ:
[259] Fix | Delete
import pwd
[260] Fix | Delete
try:
[261] Fix | Delete
userhome = pwd.getpwuid(os.getuid()).pw_dir
[262] Fix | Delete
except KeyError:
[263] Fix | Delete
# bpo-10496: if the current user identifier doesn't exist in the
[264] Fix | Delete
# password database, return the path unchanged
[265] Fix | Delete
return path
[266] Fix | Delete
else:
[267] Fix | Delete
userhome = os.environ['HOME']
[268] Fix | Delete
else:
[269] Fix | Delete
import pwd
[270] Fix | Delete
try:
[271] Fix | Delete
pwent = pwd.getpwnam(path[1:i])
[272] Fix | Delete
except KeyError:
[273] Fix | Delete
# bpo-10496: if the user name from the path doesn't exist in the
[274] Fix | Delete
# password database, return the path unchanged
[275] Fix | Delete
return path
[276] Fix | Delete
userhome = pwent.pw_dir
[277] Fix | Delete
userhome = userhome.rstrip('/')
[278] Fix | Delete
return (userhome + path[i:]) or '/'
[279] Fix | Delete
[280] Fix | Delete
[281] Fix | Delete
# Expand paths containing shell variable substitutions.
[282] Fix | Delete
# This expands the forms $variable and ${variable} only.
[283] Fix | Delete
# Non-existent variables are left unchanged.
[284] Fix | Delete
[285] Fix | Delete
_varprog = None
[286] Fix | Delete
_uvarprog = None
[287] Fix | Delete
[288] Fix | Delete
def expandvars(path):
[289] Fix | Delete
"""Expand shell variables of form $var and ${var}. Unknown variables
[290] Fix | Delete
are left unchanged."""
[291] Fix | Delete
global _varprog, _uvarprog
[292] Fix | Delete
if '$' not in path:
[293] Fix | Delete
return path
[294] Fix | Delete
if isinstance(path, _unicode):
[295] Fix | Delete
if not _uvarprog:
[296] Fix | Delete
import re
[297] Fix | Delete
_uvarprog = re.compile(ur'\$(\w+|\{[^}]*\})', re.UNICODE)
[298] Fix | Delete
varprog = _uvarprog
[299] Fix | Delete
encoding = sys.getfilesystemencoding()
[300] Fix | Delete
else:
[301] Fix | Delete
if not _varprog:
[302] Fix | Delete
import re
[303] Fix | Delete
_varprog = re.compile(r'\$(\w+|\{[^}]*\})')
[304] Fix | Delete
varprog = _varprog
[305] Fix | Delete
encoding = None
[306] Fix | Delete
i = 0
[307] Fix | Delete
while True:
[308] Fix | Delete
m = varprog.search(path, i)
[309] Fix | Delete
if not m:
[310] Fix | Delete
break
[311] Fix | Delete
i, j = m.span(0)
[312] Fix | Delete
name = m.group(1)
[313] Fix | Delete
if name.startswith('{') and name.endswith('}'):
[314] Fix | Delete
name = name[1:-1]
[315] Fix | Delete
if encoding:
[316] Fix | Delete
name = name.encode(encoding)
[317] Fix | Delete
if name in os.environ:
[318] Fix | Delete
tail = path[j:]
[319] Fix | Delete
value = os.environ[name]
[320] Fix | Delete
if encoding:
[321] Fix | Delete
value = value.decode(encoding)
[322] Fix | Delete
path = path[:i] + value
[323] Fix | Delete
i = len(path)
[324] Fix | Delete
path += tail
[325] Fix | Delete
else:
[326] Fix | Delete
i = j
[327] Fix | Delete
return path
[328] Fix | Delete
[329] Fix | Delete
[330] Fix | Delete
# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
[331] Fix | Delete
# It should be understood that this may change the meaning of the path
[332] Fix | Delete
# if it contains symbolic links!
[333] Fix | Delete
[334] Fix | Delete
def normpath(path):
[335] Fix | Delete
"""Normalize path, eliminating double slashes, etc."""
[336] Fix | Delete
# Preserve unicode (if path is unicode)
[337] Fix | Delete
slash, dot = (u'/', u'.') if isinstance(path, _unicode) else ('/', '.')
[338] Fix | Delete
if path == '':
[339] Fix | Delete
return dot
[340] Fix | Delete
initial_slashes = path.startswith('/')
[341] Fix | Delete
# POSIX allows one or two initial slashes, but treats three or more
[342] Fix | Delete
# as single slash.
[343] Fix | Delete
if (initial_slashes and
[344] Fix | Delete
path.startswith('//') and not path.startswith('///')):
[345] Fix | Delete
initial_slashes = 2
[346] Fix | Delete
comps = path.split('/')
[347] Fix | Delete
new_comps = []
[348] Fix | Delete
for comp in comps:
[349] Fix | Delete
if comp in ('', '.'):
[350] Fix | Delete
continue
[351] Fix | Delete
if (comp != '..' or (not initial_slashes and not new_comps) or
[352] Fix | Delete
(new_comps and new_comps[-1] == '..')):
[353] Fix | Delete
new_comps.append(comp)
[354] Fix | Delete
elif new_comps:
[355] Fix | Delete
new_comps.pop()
[356] Fix | Delete
comps = new_comps
[357] Fix | Delete
path = slash.join(comps)
[358] Fix | Delete
if initial_slashes:
[359] Fix | Delete
path = slash*initial_slashes + path
[360] Fix | Delete
return path or dot
[361] Fix | Delete
[362] Fix | Delete
[363] Fix | Delete
def abspath(path):
[364] Fix | Delete
"""Return an absolute path."""
[365] Fix | Delete
if not isabs(path):
[366] Fix | Delete
if isinstance(path, _unicode):
[367] Fix | Delete
cwd = os.getcwdu()
[368] Fix | Delete
else:
[369] Fix | Delete
cwd = os.getcwd()
[370] Fix | Delete
path = join(cwd, path)
[371] Fix | Delete
return normpath(path)
[372] Fix | Delete
[373] Fix | Delete
[374] Fix | Delete
# Return a canonical path (i.e. the absolute location of a file on the
[375] Fix | Delete
# filesystem).
[376] Fix | Delete
[377] Fix | Delete
def realpath(filename):
[378] Fix | Delete
"""Return the canonical path of the specified filename, eliminating any
[379] Fix | Delete
symbolic links encountered in the path."""
[380] Fix | Delete
path, ok = _joinrealpath('', filename, {})
[381] Fix | Delete
return abspath(path)
[382] Fix | Delete
[383] Fix | Delete
# Join two paths, normalizing and eliminating any symbolic links
[384] Fix | Delete
# encountered in the second path.
[385] Fix | Delete
def _joinrealpath(path, rest, seen):
[386] Fix | Delete
if isabs(rest):
[387] Fix | Delete
rest = rest[1:]
[388] Fix | Delete
path = sep
[389] Fix | Delete
[390] Fix | Delete
while rest:
[391] Fix | Delete
name, _, rest = rest.partition(sep)
[392] Fix | Delete
if not name or name == curdir:
[393] Fix | Delete
# current dir
[394] Fix | Delete
continue
[395] Fix | Delete
if name == pardir:
[396] Fix | Delete
# parent dir
[397] Fix | Delete
if path:
[398] Fix | Delete
path, name = split(path)
[399] Fix | Delete
if name == pardir:
[400] Fix | Delete
path = join(path, pardir, pardir)
[401] Fix | Delete
else:
[402] Fix | Delete
path = pardir
[403] Fix | Delete
continue
[404] Fix | Delete
newpath = join(path, name)
[405] Fix | Delete
if not islink(newpath):
[406] Fix | Delete
path = newpath
[407] Fix | Delete
continue
[408] Fix | Delete
# Resolve the symbolic link
[409] Fix | Delete
if newpath in seen:
[410] Fix | Delete
# Already seen this path
[411] Fix | Delete
path = seen[newpath]
[412] Fix | Delete
if path is not None:
[413] Fix | Delete
# use cached value
[414] Fix | Delete
continue
[415] Fix | Delete
# The symlink is not resolved, so we must have a symlink loop.
[416] Fix | Delete
# Return already resolved part + rest of the path unchanged.
[417] Fix | Delete
return join(newpath, rest), False
[418] Fix | Delete
seen[newpath] = None # not resolved symlink
[419] Fix | Delete
path, ok = _joinrealpath(path, os.readlink(newpath), seen)
[420] Fix | Delete
if not ok:
[421] Fix | Delete
return join(path, rest), False
[422] Fix | Delete
seen[newpath] = path # resolved symlink
[423] Fix | Delete
[424] Fix | Delete
return path, True
[425] Fix | Delete
[426] Fix | Delete
[427] Fix | Delete
supports_unicode_filenames = (sys.platform == 'darwin')
[428] Fix | Delete
[429] Fix | Delete
def relpath(path, start=curdir):
[430] Fix | Delete
"""Return a relative version of a path"""
[431] Fix | Delete
[432] Fix | Delete
if not path:
[433] Fix | Delete
raise ValueError("no path specified")
[434] Fix | Delete
[435] Fix | Delete
start_list = [x for x in abspath(start).split(sep) if x]
[436] Fix | Delete
path_list = [x for x in abspath(path).split(sep) if x]
[437] Fix | Delete
[438] Fix | Delete
# Work out how much of the filepath is shared by start and path.
[439] Fix | Delete
i = len(commonprefix([start_list, path_list]))
[440] Fix | Delete
[441] Fix | Delete
rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
[442] Fix | Delete
if not rel_list:
[443] Fix | Delete
return curdir
[444] Fix | Delete
return join(*rel_list)
[445] Fix | Delete
[446] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function