Withdraw the UNC support from splitdrive(). Instead, a new function
splitunc() parses UNC paths. The contributor of the UNC parsing in splitdrive() doesn't like it, but I haven't heard a good reason to keep it, and it causes some problems. (I think there's a philosophical problem -- to me, the split*() functions are purely syntactical, and the fact that \\foo is not a valid path doesn't mean that it shouldn't be considered an absolute path.) Also (quite separately, but strangely related to the philosophical issue above) fix abspath() so that if win32api exists, it doesn't fail when the path doesn't actually exist -- if GetFullPathName() fails, fall back on the old strategy (join with getcwd() if neccessary, and then use normpath()).
This commit is contained in:
parent
8137680704
commit
f3c695c467
|
@ -49,15 +49,27 @@ def join(a, *p):
|
||||||
|
|
||||||
|
|
||||||
# Split a path in a drive specification (a drive letter followed by a
|
# Split a path in a drive specification (a drive letter followed by a
|
||||||
# colon, or a UNC resource) and the path specification.
|
# colon) and the path specification.
|
||||||
# It is always true that drivespec + pathspec == p
|
# It is always true that drivespec + pathspec == p
|
||||||
def splitdrive(p):
|
def splitdrive(p):
|
||||||
"""Split a pathname into drive and path specifiers.
|
"""Split a pathname into drive and path specifiers. Returns a 2-tuple
|
||||||
|
"(drive,path)"; either part may be empty"""
|
||||||
Return a 2-tuple (drive, path); either part may be empty.
|
|
||||||
This recognizes UNC paths (e.g. '\\\\host\\mountpoint\\dir\\file')"""
|
|
||||||
if p[1:2] == ':':
|
if p[1:2] == ':':
|
||||||
return p[0:2], p[2:]
|
return p[0:2], p[2:]
|
||||||
|
return '', p
|
||||||
|
|
||||||
|
|
||||||
|
# Parse UNC paths
|
||||||
|
def splitunc(p):
|
||||||
|
"""Split a pathname into UNC mount point and relative path specifiers.
|
||||||
|
|
||||||
|
Return a 2-tuple (unc, rest); either part may be empty.
|
||||||
|
If unc is not empty, it has the form '//host/mount' (or similar
|
||||||
|
using backslashes). unc+rest is always the input path.
|
||||||
|
Paths containing drive letters never have an UNC part.
|
||||||
|
"""
|
||||||
|
if p[1:2] == ':':
|
||||||
|
return '', p # Drive letter present
|
||||||
firstTwo = p[0:2]
|
firstTwo = p[0:2]
|
||||||
if firstTwo == '//' or firstTwo == '\\\\':
|
if firstTwo == '//' or firstTwo == '\\\\':
|
||||||
# is a UNC path:
|
# is a UNC path:
|
||||||
|
@ -220,11 +232,14 @@ def isfile(path):
|
||||||
return stat.S_ISREG(st[stat.ST_MODE])
|
return stat.S_ISREG(st[stat.ST_MODE])
|
||||||
|
|
||||||
|
|
||||||
# Is a path a mount point?
|
# Is a path a mount point? Either a root (with or without drive letter)
|
||||||
# XXX This degenerates in: 'is this the root?' on DOS/Windows
|
# or an UNC path with at most a / or \ after the mount point.
|
||||||
|
|
||||||
def ismount(path):
|
def ismount(path):
|
||||||
"""Test whether a path is a mount point (defined as root of drive)"""
|
"""Test whether a path is a mount point (defined as root of drive)"""
|
||||||
|
unc, rest = splitunc(path)
|
||||||
|
if unc:
|
||||||
|
return rest in ("", "/", "\\")
|
||||||
p = splitdrive(path)[1]
|
p = splitdrive(path)[1]
|
||||||
return len(p)==1 and p[0] in '/\\'
|
return len(p)==1 and p[0] in '/\\'
|
||||||
|
|
||||||
|
@ -388,7 +403,10 @@ def abspath(path):
|
||||||
"""Return the absolute version of a path"""
|
"""Return the absolute version of a path"""
|
||||||
try:
|
try:
|
||||||
import win32api
|
import win32api
|
||||||
return win32api.GetFullPathName(path)
|
try:
|
||||||
|
return win32api.GetFullPathName(path)
|
||||||
|
except win32api.error:
|
||||||
|
return path # Bad path - return unchanged.
|
||||||
except ImportError:
|
except ImportError:
|
||||||
if not isabs(path):
|
if not isabs(path):
|
||||||
path = join(os.getcwd(), path)
|
path = join(os.getcwd(), path)
|
||||||
|
|
Loading…
Reference in New Issue