Commit 534972bc authored by Guido van Rossum's avatar Guido van Rossum

New splitdrive() that knows about UNC paths (e.g., network paths like

\\host\mountpoint\dir\file).  By Larry Hastings.

Also cleaned up some docstrings.
parent 0c9608cf
# Module 'ntpath' -- common operations on WinNT/Win95 pathnames # Module 'ntpath' -- common operations on WinNT/Win95 pathnames
"""Common pathname manipulations, WindowsNT/95 version. """Common pathname manipulations, WindowsNT/95 version.
Instead of importing this module
directly, import os and refer to this module as os.path. Instead of importing this module directly, import os and refer to this
module as os.path.
""" """
import os import os
...@@ -16,17 +17,15 @@ import string ...@@ -16,17 +17,15 @@ import string
def normcase(s): def normcase(s):
"""Normalize case of pathname. """Normalize case of pathname.
Makes all characters lowercase and all slashes into backslashes. Makes all characters lowercase and all slashes into backslashes."""
"""
return string.lower(string.replace(s, "/", "\\")) return string.lower(string.replace(s, "/", "\\"))
# Return wheter a path is absolute. # Return wheter a path is absolute.
# Trivial in Posix, harder on the Mac or MS-DOS. # Trivial in Posix, harder on the Mac or MS-DOS.
# For DOS it is absolute if it starts with a slash or backslash (current # For DOS it is absolute if it starts with a slash or backslash (current
# volume), or if a pathname after the volume letter and colon starts with # volume), or if a pathname after the volume letter and colon / UNC resource
# a slash or backslash. # starts with a slash or backslash.
def isabs(s): def isabs(s):
"""Test whether a path is absolute""" """Test whether a path is absolute"""
...@@ -50,13 +49,30 @@ def join(a, *p): ...@@ -50,13 +49,30 @@ 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) and the path specification. # colon, or a UNC resource) 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. Returns a 2-tuple """Split a pathname into drive and path specifiers.
"(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:]
firstTwo = p[0:2]
if firstTwo == '//' or firstTwo == '\\\\':
# is a UNC path:
# vvvvvvvvvvvvvvvvvvvv equivalent to drive letter
# \\machine\mountpoint\directories...
# directory ^^^^^^^^^^^^^^^
normp = normcase(p)
index = string.find(normp, '\\', 2)
if index == -1:
##raise RuntimeError, 'illegal UNC path: "' + p + '"'
return ("", p)
index = string.find(normp, '\\', index + 1)
if index == -1:
index = len(p)
return p[:index], p[index:]
return '', p return '', p
...@@ -67,8 +83,10 @@ def splitdrive(p): ...@@ -67,8 +83,10 @@ def splitdrive(p):
# The resulting head won't end in '/' unless it is the root. # The resulting head won't end in '/' unless it is the root.
def split(p): def split(p):
"""Split a pathname. Returns tuple "(head, tail)" where "tail" is """Split a pathname.
everything after the final slash. Either part may be empty"""
Return tuple (head, tail) where tail is everything after the final slash.
Either part may be empty."""
d, p = splitdrive(p) d, p = splitdrive(p)
slashes = '' slashes = ''
while p and p[-1:] in '/\\': while p and p[-1:] in '/\\':
...@@ -96,8 +114,10 @@ everything after the final slash. Either part may be empty""" ...@@ -96,8 +114,10 @@ everything after the final slash. Either part may be empty"""
# It is always true that root + ext == p. # It is always true that root + ext == p.
def splitext(p): def splitext(p):
"""Split the extension from a pathname. Extension is everything from the """Split the extension from a pathname.
last dot to the end. Returns "(root, ext)", either part may be empty"""
Extension is everything from the last dot to the end.
Return (root, ext), either part may be empty."""
root, ext = '', '' root, ext = '', ''
for c in p: for c in p:
if c in ['/','\\']: if c in ['/','\\']:
...@@ -146,17 +166,17 @@ def commonprefix(m): ...@@ -146,17 +166,17 @@ def commonprefix(m):
# Get size, mtime, atime of files. # Get size, mtime, atime of files.
def getsize(filename): def getsize(filename):
"""Return the size of a file, reported by os.stat().""" """Return the size of a file, reported by os.stat()"""
st = os.stat(filename) st = os.stat(filename)
return st[stat.ST_SIZE] return st[stat.ST_SIZE]
def getmtime(filename): def getmtime(filename):
"""Return the last modification time of a file, reported by os.stat().""" """Return the last modification time of a file, reported by os.stat()"""
st = os.stat(filename) st = os.stat(filename)
return st[stat.ST_MTIME] return st[stat.ST_MTIME]
def getatime(filename): def getatime(filename):
"""Return the last access time of a file, reported by os.stat().""" """Return the last access time of a file, reported by os.stat()"""
st = os.stat(filename) st = os.stat(filename)
return st[stat.ST_MTIME] return st[stat.ST_MTIME]
...@@ -208,7 +228,7 @@ def isfile(path): ...@@ -208,7 +228,7 @@ def isfile(path):
# Is a path a mount point? # Is a path a mount point?
# XXX This degenerates in: 'is this the root?' on DOS # XXX This degenerates in: 'is this the root?' on DOS/Windows
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)"""
...@@ -225,10 +245,11 @@ def ismount(path): ...@@ -225,10 +245,11 @@ def ismount(path):
# or to impose a different order of visiting. # or to impose a different order of visiting.
def walk(top, func, arg): def walk(top, func, arg):
"""walk(top,func,args) calls func(arg, d, files) for each directory "d" """Directory tree walk whth callback function.
in the tree rooted at "top" (including "top" itself). "files" is a list
of all the files and subdirs in directory "d". walk(top, func, args) calls func(arg, d, files) for each directory d
""" in the tree rooted at top (including top itself); files is a list
of all the files and subdirs in directory d."""
try: try:
names = os.listdir(top) names = os.listdir(top)
except os.error: except os.error:
...@@ -252,8 +273,9 @@ of all the files and subdirs in directory "d". ...@@ -252,8 +273,9 @@ of all the files and subdirs in directory "d".
# variable expansion.) # variable expansion.)
def expanduser(path): def expanduser(path):
"""Expand ~ and ~user constructions. If user or $HOME is unknown, """Expand ~ and ~user constructs.
do nothing"""
If user or $HOME is unknown, do nothing."""
if path[:1] <> '~': if path[:1] <> '~':
return path return path
i, n = 1, len(path) i, n = 1, len(path)
...@@ -287,8 +309,9 @@ do nothing""" ...@@ -287,8 +309,9 @@ do nothing"""
varchars = string.letters + string.digits + '_-' varchars = string.letters + string.digits + '_-'
def expandvars(path): def expandvars(path):
"""Expand shell variables of form $var and ${var}. Unknown variables """Expand shell variables of form $var and ${var}.
are left unchanged"""
Unknown variables are left unchanged."""
if '$' not in path: if '$' not in path:
return path return path
res = '' res = ''
...@@ -369,6 +392,7 @@ def normpath(path): ...@@ -369,6 +392,7 @@ def normpath(path):
# Return an absolute path. # Return an absolute path.
def abspath(path): def abspath(path):
"""Return the absolute version of a path"""
try: try:
import win32api import win32api
return win32api.GetFullPathName(path) return win32api.GetFullPathName(path)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment