Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/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(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
userhome = pwd.getpwuid(os.getuid()).pw_dir
[261] Fix | Delete
else:
[262] Fix | Delete
userhome = os.environ['HOME']
[263] Fix | Delete
else:
[264] Fix | Delete
import pwd
[265] Fix | Delete
try:
[266] Fix | Delete
pwent = pwd.getpwnam(path[1:i])
[267] Fix | Delete
except KeyError:
[268] Fix | Delete
return path
[269] Fix | Delete
userhome = pwent.pw_dir
[270] Fix | Delete
userhome = userhome.rstrip('/')
[271] Fix | Delete
return (userhome + path[i:]) or '/'
[272] Fix | Delete
[273] Fix | Delete
[274] Fix | Delete
# Expand paths containing shell variable substitutions.
[275] Fix | Delete
# This expands the forms $variable and ${variable} only.
[276] Fix | Delete
# Non-existent variables are left unchanged.
[277] Fix | Delete
[278] Fix | Delete
_varprog = None
[279] Fix | Delete
_uvarprog = None
[280] Fix | Delete
[281] Fix | Delete
def expandvars(path):
[282] Fix | Delete
"""Expand shell variables of form $var and ${var}. Unknown variables
[283] Fix | Delete
are left unchanged."""
[284] Fix | Delete
global _varprog, _uvarprog
[285] Fix | Delete
if '$' not in path:
[286] Fix | Delete
return path
[287] Fix | Delete
if isinstance(path, _unicode):
[288] Fix | Delete
if not _uvarprog:
[289] Fix | Delete
import re
[290] Fix | Delete
_uvarprog = re.compile(ur'\$(\w+|\{[^}]*\})', re.UNICODE)
[291] Fix | Delete
varprog = _uvarprog
[292] Fix | Delete
encoding = sys.getfilesystemencoding()
[293] Fix | Delete
else:
[294] Fix | Delete
if not _varprog:
[295] Fix | Delete
import re
[296] Fix | Delete
_varprog = re.compile(r'\$(\w+|\{[^}]*\})')
[297] Fix | Delete
varprog = _varprog
[298] Fix | Delete
encoding = None
[299] Fix | Delete
i = 0
[300] Fix | Delete
while True:
[301] Fix | Delete
m = varprog.search(path, i)
[302] Fix | Delete
if not m:
[303] Fix | Delete
break
[304] Fix | Delete
i, j = m.span(0)
[305] Fix | Delete
name = m.group(1)
[306] Fix | Delete
if name.startswith('{') and name.endswith('}'):
[307] Fix | Delete
name = name[1:-1]
[308] Fix | Delete
if encoding:
[309] Fix | Delete
name = name.encode(encoding)
[310] Fix | Delete
if name in os.environ:
[311] Fix | Delete
tail = path[j:]
[312] Fix | Delete
value = os.environ[name]
[313] Fix | Delete
if encoding:
[314] Fix | Delete
value = value.decode(encoding)
[315] Fix | Delete
path = path[:i] + value
[316] Fix | Delete
i = len(path)
[317] Fix | Delete
path += tail
[318] Fix | Delete
else:
[319] Fix | Delete
i = j
[320] Fix | Delete
return path
[321] Fix | Delete
[322] Fix | Delete
[323] Fix | Delete
# Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
[324] Fix | Delete
# It should be understood that this may change the meaning of the path
[325] Fix | Delete
# if it contains symbolic links!
[326] Fix | Delete
[327] Fix | Delete
def normpath(path):
[328] Fix | Delete
"""Normalize path, eliminating double slashes, etc."""
[329] Fix | Delete
# Preserve unicode (if path is unicode)
[330] Fix | Delete
slash, dot = (u'/', u'.') if isinstance(path, _unicode) else ('/', '.')
[331] Fix | Delete
if path == '':
[332] Fix | Delete
return dot
[333] Fix | Delete
initial_slashes = path.startswith('/')
[334] Fix | Delete
# POSIX allows one or two initial slashes, but treats three or more
[335] Fix | Delete
# as single slash.
[336] Fix | Delete
if (initial_slashes and
[337] Fix | Delete
path.startswith('//') and not path.startswith('///')):
[338] Fix | Delete
initial_slashes = 2
[339] Fix | Delete
comps = path.split('/')
[340] Fix | Delete
new_comps = []
[341] Fix | Delete
for comp in comps:
[342] Fix | Delete
if comp in ('', '.'):
[343] Fix | Delete
continue
[344] Fix | Delete
if (comp != '..' or (not initial_slashes and not new_comps) or
[345] Fix | Delete
(new_comps and new_comps[-1] == '..')):
[346] Fix | Delete
new_comps.append(comp)
[347] Fix | Delete
elif new_comps:
[348] Fix | Delete
new_comps.pop()
[349] Fix | Delete
comps = new_comps
[350] Fix | Delete
path = slash.join(comps)
[351] Fix | Delete
if initial_slashes:
[352] Fix | Delete
path = slash*initial_slashes + path
[353] Fix | Delete
return path or dot
[354] Fix | Delete
[355] Fix | Delete
[356] Fix | Delete
def abspath(path):
[357] Fix | Delete
"""Return an absolute path."""
[358] Fix | Delete
if not isabs(path):
[359] Fix | Delete
if isinstance(path, _unicode):
[360] Fix | Delete
cwd = os.getcwdu()
[361] Fix | Delete
else:
[362] Fix | Delete
cwd = os.getcwd()
[363] Fix | Delete
path = join(cwd, path)
[364] Fix | Delete
return normpath(path)
[365] Fix | Delete
[366] Fix | Delete
[367] Fix | Delete
# Return a canonical path (i.e. the absolute location of a file on the
[368] Fix | Delete
# filesystem).
[369] Fix | Delete
[370] Fix | Delete
def realpath(filename):
[371] Fix | Delete
"""Return the canonical path of the specified filename, eliminating any
[372] Fix | Delete
symbolic links encountered in the path."""
[373] Fix | Delete
path, ok = _joinrealpath('', filename, {})
[374] Fix | Delete
return abspath(path)
[375] Fix | Delete
[376] Fix | Delete
# Join two paths, normalizing and eliminating any symbolic links
[377] Fix | Delete
# encountered in the second path.
[378] Fix | Delete
def _joinrealpath(path, rest, seen):
[379] Fix | Delete
if isabs(rest):
[380] Fix | Delete
rest = rest[1:]
[381] Fix | Delete
path = sep
[382] Fix | Delete
[383] Fix | Delete
while rest:
[384] Fix | Delete
name, _, rest = rest.partition(sep)
[385] Fix | Delete
if not name or name == curdir:
[386] Fix | Delete
# current dir
[387] Fix | Delete
continue
[388] Fix | Delete
if name == pardir:
[389] Fix | Delete
# parent dir
[390] Fix | Delete
if path:
[391] Fix | Delete
path, name = split(path)
[392] Fix | Delete
if name == pardir:
[393] Fix | Delete
path = join(path, pardir, pardir)
[394] Fix | Delete
else:
[395] Fix | Delete
path = pardir
[396] Fix | Delete
continue
[397] Fix | Delete
newpath = join(path, name)
[398] Fix | Delete
if not islink(newpath):
[399] Fix | Delete
path = newpath
[400] Fix | Delete
continue
[401] Fix | Delete
# Resolve the symbolic link
[402] Fix | Delete
if newpath in seen:
[403] Fix | Delete
# Already seen this path
[404] Fix | Delete
path = seen[newpath]
[405] Fix | Delete
if path is not None:
[406] Fix | Delete
# use cached value
[407] Fix | Delete
continue
[408] Fix | Delete
# The symlink is not resolved, so we must have a symlink loop.
[409] Fix | Delete
# Return already resolved part + rest of the path unchanged.
[410] Fix | Delete
return join(newpath, rest), False
[411] Fix | Delete
seen[newpath] = None # not resolved symlink
[412] Fix | Delete
path, ok = _joinrealpath(path, os.readlink(newpath), seen)
[413] Fix | Delete
if not ok:
[414] Fix | Delete
return join(path, rest), False
[415] Fix | Delete
seen[newpath] = path # resolved symlink
[416] Fix | Delete
[417] Fix | Delete
return path, True
[418] Fix | Delete
[419] Fix | Delete
[420] Fix | Delete
supports_unicode_filenames = (sys.platform == 'darwin')
[421] Fix | Delete
[422] Fix | Delete
def relpath(path, start=curdir):
[423] Fix | Delete
"""Return a relative version of a path"""
[424] Fix | Delete
[425] Fix | Delete
if not path:
[426] Fix | Delete
raise ValueError("no path specified")
[427] Fix | Delete
[428] Fix | Delete
start_list = [x for x in abspath(start).split(sep) if x]
[429] Fix | Delete
path_list = [x for x in abspath(path).split(sep) if x]
[430] Fix | Delete
[431] Fix | Delete
# Work out how much of the filepath is shared by start and path.
[432] Fix | Delete
i = len(commonprefix([start_list, path_list]))
[433] Fix | Delete
[434] Fix | Delete
rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
[435] Fix | Delete
if not rel_list:
[436] Fix | Delete
return curdir
[437] Fix | Delete
return join(*rel_list)
[438] Fix | Delete
[439] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function