1991-01-02 02:10:40 +08:00
|
|
|
# module 'macpath' -- pathname (or -related) operations for the Macintosh
|
1990-12-26 23:40:07 +08:00
|
|
|
|
1992-01-15 02:28:18 +08:00
|
|
|
import string
|
1998-03-04 05:49:01 +08:00
|
|
|
import os
|
1990-12-26 23:40:07 +08:00
|
|
|
from stat import *
|
|
|
|
|
1991-01-02 02:10:40 +08:00
|
|
|
|
1992-01-15 02:28:18 +08:00
|
|
|
# Normalize the case of a pathname. Dummy in Posix, but string.lower here.
|
|
|
|
|
|
|
|
normcase = string.lower
|
|
|
|
|
|
|
|
|
1991-01-02 02:10:40 +08:00
|
|
|
# Return true if a path is absolute.
|
|
|
|
# On the Mac, relative paths begin with a colon,
|
|
|
|
# but as a special case, paths with no colons at all are also relative.
|
|
|
|
# Anything else is absolute (the string up to the first colon is the
|
|
|
|
# volume name).
|
|
|
|
|
1990-12-26 23:40:07 +08:00
|
|
|
def isabs(s):
|
|
|
|
return ':' in s and s[0] <> ':'
|
|
|
|
|
1991-01-02 02:10:40 +08:00
|
|
|
|
1997-02-19 05:53:25 +08:00
|
|
|
def join(s, *p):
|
|
|
|
path = s
|
|
|
|
for t in p:
|
|
|
|
if (not s) or isabs(t):
|
|
|
|
path = t
|
|
|
|
continue
|
|
|
|
if t[:1] == ':':
|
|
|
|
t = t[1:]
|
|
|
|
if ':' not in path:
|
|
|
|
path = ':' + path
|
|
|
|
if path[-1:] <> ':':
|
|
|
|
path = path + ':'
|
|
|
|
path = path + t
|
|
|
|
return path
|
1990-12-26 23:40:07 +08:00
|
|
|
|
1991-01-02 02:10:40 +08:00
|
|
|
|
|
|
|
# Split a pathname in two parts: the directory leading up to the final bit,
|
|
|
|
# and the basename (the filename, without colons, in that directory).
|
1991-08-16 22:27:45 +09:00
|
|
|
# The result (s, t) is such that join(s, t) yields the original argument.
|
1991-01-02 02:10:40 +08:00
|
|
|
|
|
|
|
def split(s):
|
|
|
|
if ':' not in s: return '', s
|
|
|
|
colon = 0
|
|
|
|
for i in range(len(s)):
|
1992-01-02 03:35:13 +08:00
|
|
|
if s[i] == ':': colon = i+1
|
1997-05-21 22:59:17 +08:00
|
|
|
path, file = s[:colon-1], s[colon:]
|
|
|
|
if path and not ':' in path:
|
|
|
|
path = path + ':'
|
|
|
|
return path, file
|
1991-01-02 02:10:40 +08:00
|
|
|
|
|
|
|
|
1996-07-23 10:28:32 +08:00
|
|
|
# Split a path in root and extension.
|
|
|
|
# The extension is everything starting at the last dot in the last
|
|
|
|
# pathname component; the root is everything before that.
|
|
|
|
# It is always true that root + ext == p.
|
|
|
|
|
|
|
|
def splitext(p):
|
|
|
|
root, ext = '', ''
|
|
|
|
for c in p:
|
|
|
|
if c == ':':
|
|
|
|
root, ext = root + ext + c, ''
|
|
|
|
elif c == '.':
|
|
|
|
if ext:
|
|
|
|
root, ext = root + ext, c
|
|
|
|
else:
|
|
|
|
ext = c
|
|
|
|
elif ext:
|
|
|
|
ext = ext + c
|
|
|
|
else:
|
|
|
|
root = root + c
|
|
|
|
return root, ext
|
|
|
|
|
|
|
|
|
1995-08-11 02:09:16 +08:00
|
|
|
# Split a pathname into a drive specification and the rest of the
|
|
|
|
# path. Useful on DOS/Windows/NT; on the Mac, the drive is always
|
|
|
|
# empty (don't use the volume name -- it doesn't have the same
|
|
|
|
# syntactic and semantic oddities as DOS drive letters, such as there
|
|
|
|
# being a separate current directory per drive).
|
1992-11-05 18:43:02 +08:00
|
|
|
|
1995-08-11 02:09:16 +08:00
|
|
|
def splitdrive(p):
|
|
|
|
return '', p
|
1992-11-05 18:43:02 +08:00
|
|
|
|
|
|
|
|
1995-08-11 02:09:16 +08:00
|
|
|
# Short interfaces to split()
|
1990-12-26 23:40:07 +08:00
|
|
|
|
1995-08-11 02:09:16 +08:00
|
|
|
def dirname(s): return split(s)[0]
|
|
|
|
def basename(s): return split(s)[1]
|
1990-12-26 23:40:07 +08:00
|
|
|
|
1991-01-02 02:10:40 +08:00
|
|
|
|
|
|
|
# Return true if the pathname refers to an existing directory.
|
|
|
|
|
1990-12-26 23:40:07 +08:00
|
|
|
def isdir(s):
|
|
|
|
try:
|
1998-03-04 05:49:01 +08:00
|
|
|
st = os.stat(s)
|
|
|
|
except os.error:
|
1990-12-26 23:40:07 +08:00
|
|
|
return 0
|
|
|
|
return S_ISDIR(st[ST_MODE])
|
|
|
|
|
1991-01-02 02:10:40 +08:00
|
|
|
|
1998-07-25 04:49:26 +08:00
|
|
|
# Get size, mtime, atime of files.
|
|
|
|
|
|
|
|
def getsize(filename):
|
|
|
|
"""Return the size of a file, reported by os.stat()."""
|
|
|
|
st = os.stat(filename)
|
1999-07-23 23:04:05 +08:00
|
|
|
return st[ST_SIZE]
|
1998-07-25 04:49:26 +08:00
|
|
|
|
|
|
|
def getmtime(filename):
|
|
|
|
"""Return the last modification time of a file, reported by os.stat()."""
|
|
|
|
st = os.stat(filename)
|
1999-07-23 23:04:05 +08:00
|
|
|
return st[ST_MTIME]
|
1998-07-25 04:49:26 +08:00
|
|
|
|
|
|
|
def getatime(filename):
|
|
|
|
"""Return the last access time of a file, reported by os.stat()."""
|
|
|
|
st = os.stat(filename)
|
1999-07-23 23:04:05 +08:00
|
|
|
return st[ST_MTIME]
|
1998-07-25 04:49:26 +08:00
|
|
|
|
|
|
|
|
1995-01-27 10:41:45 +08:00
|
|
|
# Return true if the pathname refers to a symbolic link.
|
|
|
|
# (Always false on the Mac, until we understand Aliases.)
|
|
|
|
|
|
|
|
def islink(s):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
1991-01-02 02:10:40 +08:00
|
|
|
# Return true if the pathname refers to an existing regular file.
|
|
|
|
|
1990-12-26 23:40:07 +08:00
|
|
|
def isfile(s):
|
|
|
|
try:
|
1998-03-04 05:49:01 +08:00
|
|
|
st = os.stat(s)
|
|
|
|
except os.error:
|
1990-12-26 23:40:07 +08:00
|
|
|
return 0
|
|
|
|
return S_ISREG(st[ST_MODE])
|
|
|
|
|
1991-01-02 02:10:40 +08:00
|
|
|
|
|
|
|
# Return true if the pathname refers to an existing file or directory.
|
|
|
|
|
1990-12-26 23:40:07 +08:00
|
|
|
def exists(s):
|
|
|
|
try:
|
1998-03-04 05:49:01 +08:00
|
|
|
st = os.stat(s)
|
|
|
|
except os.error:
|
1990-12-26 23:40:07 +08:00
|
|
|
return 0
|
|
|
|
return 1
|
1992-11-05 18:43:02 +08:00
|
|
|
|
1995-12-15 21:23:37 +08:00
|
|
|
#
|
|
|
|
# dummy expandvars to retain interface-compatability with other
|
|
|
|
# operating systems.
|
|
|
|
def expandvars(path):
|
|
|
|
return path
|
|
|
|
|
|
|
|
#
|
|
|
|
# dummy expanduser to retain interface-compatability with other
|
|
|
|
# operating systems.
|
|
|
|
def expanduser(path):
|
|
|
|
return path
|
1992-11-05 18:43:02 +08:00
|
|
|
|
1995-08-11 02:09:16 +08:00
|
|
|
# Normalize a pathname: get rid of '::' sequences by backing up,
|
|
|
|
# e.g., 'foo:bar::bletch' becomes 'foo:bletch'.
|
|
|
|
# Raise the exception norm_error below if backing up is impossible,
|
|
|
|
# e.g., for '::foo'.
|
|
|
|
# XXX The Unix version doesn't raise an exception but simply
|
|
|
|
# returns an unnormalized path. Should do so here too.
|
|
|
|
|
|
|
|
norm_error = 'macpath.norm_error: path cannot be normalized'
|
1992-11-05 18:43:02 +08:00
|
|
|
|
|
|
|
def normpath(s):
|
1995-08-11 02:09:16 +08:00
|
|
|
import string
|
|
|
|
if ':' not in s:
|
|
|
|
return ':' + s
|
|
|
|
f = string.splitfields(s, ':')
|
|
|
|
pre = []
|
|
|
|
post = []
|
|
|
|
if not f[0]:
|
|
|
|
pre = f[:1]
|
|
|
|
f = f[1:]
|
|
|
|
if not f[len(f)-1]:
|
|
|
|
post = f[-1:]
|
|
|
|
f = f[:-1]
|
|
|
|
res = []
|
|
|
|
for seg in f:
|
|
|
|
if seg:
|
|
|
|
res.append(seg)
|
|
|
|
else:
|
|
|
|
if not res: raise norm_error, 'path starts with ::'
|
|
|
|
del res[len(res)-1]
|
|
|
|
if not (pre or res):
|
|
|
|
raise norm_error, 'path starts with volume::'
|
|
|
|
if pre: res = pre + res
|
|
|
|
if post: res = res + post
|
|
|
|
s = res[0]
|
|
|
|
for seg in res[1:]:
|
|
|
|
s = s + ':' + seg
|
1992-11-05 18:43:02 +08:00
|
|
|
return s
|
1995-08-07 22:09:27 +08:00
|
|
|
|
1995-08-11 02:09:16 +08:00
|
|
|
|
1995-08-07 22:09:27 +08:00
|
|
|
# Directory tree walk.
|
|
|
|
# For each directory under top (including top itself),
|
|
|
|
# func(arg, dirname, filenames) is called, where
|
|
|
|
# dirname is the name of the directory and filenames is the list
|
|
|
|
# of files (and subdirectories etc.) in the directory.
|
|
|
|
# The func may modify the filenames list, to implement a filter,
|
|
|
|
# or to impose a different order of visiting.
|
|
|
|
|
|
|
|
def walk(top, func, arg):
|
|
|
|
try:
|
1998-03-04 05:49:01 +08:00
|
|
|
names = os.listdir(top)
|
|
|
|
except os.error:
|
1995-08-07 22:09:27 +08:00
|
|
|
return
|
|
|
|
func(arg, top, names)
|
|
|
|
for name in names:
|
|
|
|
name = join(top, name)
|
|
|
|
if isdir(name):
|
|
|
|
walk(name, func, arg)
|
1999-01-30 02:05:18 +08:00
|
|
|
|
|
|
|
|
|
|
|
# Return an absolute path.
|
|
|
|
def abspath(path):
|
|
|
|
if not isabs(path):
|
|
|
|
path = join(os.getcwd(), path)
|
|
|
|
return normpath(path)
|