1991-01-01 14:10:40 -04:00
|
|
|
# module 'macpath' -- pathname (or -related) operations for the Macintosh
|
1990-12-26 11:40:07 -04:00
|
|
|
|
1992-01-14 14:28:18 -04:00
|
|
|
import string
|
1990-12-26 11:40:07 -04:00
|
|
|
import mac
|
|
|
|
from stat import *
|
|
|
|
|
1991-01-01 14:10:40 -04:00
|
|
|
|
1992-01-14 14:28:18 -04:00
|
|
|
# Normalize the case of a pathname. Dummy in Posix, but string.lower here.
|
|
|
|
|
|
|
|
normcase = string.lower
|
|
|
|
|
|
|
|
|
1991-01-01 14:10:40 -04: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 11:40:07 -04:00
|
|
|
def isabs(s):
|
|
|
|
return ':' in s and s[0] <> ':'
|
|
|
|
|
1991-01-01 14:10:40 -04:00
|
|
|
|
1991-08-16 10:27:45 -03:00
|
|
|
# Join two pathnames.
|
1991-01-01 14:10:40 -04:00
|
|
|
# The result is equivalent to what the second pathname would refer to
|
|
|
|
# if the first pathname were the current directory.
|
|
|
|
|
1991-08-16 10:27:45 -03:00
|
|
|
def join(s, t):
|
1990-12-26 11:40:07 -04:00
|
|
|
if (not s) or isabs(t): return t
|
1992-01-01 15:35:13 -04:00
|
|
|
if t[:1] == ':': t = t[1:]
|
1990-12-26 11:40:07 -04:00
|
|
|
if ':' not in s:
|
|
|
|
s = ':' + s
|
|
|
|
if s[-1:] <> ':':
|
|
|
|
s = s + ':'
|
|
|
|
return s + t
|
|
|
|
|
1991-01-01 14:10:40 -04: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 10:27:45 -03:00
|
|
|
# The result (s, t) is such that join(s, t) yields the original argument.
|
1991-01-01 14:10:40 -04:00
|
|
|
|
|
|
|
def split(s):
|
|
|
|
if ':' not in s: return '', s
|
|
|
|
colon = 0
|
|
|
|
for i in range(len(s)):
|
1992-01-01 15:35:13 -04:00
|
|
|
if s[i] == ':': colon = i+1
|
1994-08-23 10:32:20 -03:00
|
|
|
return s[:colon-1], s[colon:]
|
1991-01-01 14:10:40 -04:00
|
|
|
|
|
|
|
|
1992-11-05 06:43:02 -04:00
|
|
|
# Short interfaces to split()
|
|
|
|
|
|
|
|
def dirname(s): return split(s)[0]
|
|
|
|
def basename(s): return split(s)[1]
|
|
|
|
|
|
|
|
|
1992-03-31 14:55:00 -04:00
|
|
|
# XXX This is undocumented and may go away!
|
1991-01-01 14:10:40 -04: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'.
|
|
|
|
|
|
|
|
norm_error = 'macpath.norm_error: path cannot be normalized'
|
1990-12-26 11:40:07 -04:00
|
|
|
|
|
|
|
def norm(s):
|
1991-01-01 14:10:40 -04:00
|
|
|
import string
|
1990-12-26 11:40:07 -04:00
|
|
|
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:
|
1991-01-01 14:10:40 -04:00
|
|
|
if not res: raise norm_error, 'path starts with ::'
|
1990-12-26 11:40:07 -04:00
|
|
|
del res[len(res)-1]
|
|
|
|
if not (pre or res):
|
1991-01-01 14:10:40 -04:00
|
|
|
raise norm_error, 'path starts with volume::'
|
1990-12-26 11:40:07 -04:00
|
|
|
if pre: res = pre + res
|
|
|
|
if post: res = res + post
|
|
|
|
s = res[0]
|
|
|
|
for seg in res[1:]:
|
|
|
|
s = s + ':' + seg
|
|
|
|
return s
|
|
|
|
|
1991-01-01 14:10:40 -04:00
|
|
|
|
|
|
|
# Return true if the pathname refers to an existing directory.
|
|
|
|
|
1990-12-26 11:40:07 -04:00
|
|
|
def isdir(s):
|
|
|
|
try:
|
|
|
|
st = mac.stat(s)
|
|
|
|
except mac.error:
|
|
|
|
return 0
|
|
|
|
return S_ISDIR(st[ST_MODE])
|
|
|
|
|
1991-01-01 14:10:40 -04:00
|
|
|
|
1995-01-26 22:41:45 -04: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-01 14:10:40 -04:00
|
|
|
# Return true if the pathname refers to an existing regular file.
|
|
|
|
|
1990-12-26 11:40:07 -04:00
|
|
|
def isfile(s):
|
|
|
|
try:
|
|
|
|
st = mac.stat(s)
|
|
|
|
except mac.error:
|
|
|
|
return 0
|
|
|
|
return S_ISREG(st[ST_MODE])
|
|
|
|
|
1991-01-01 14:10:40 -04:00
|
|
|
|
|
|
|
# Return true if the pathname refers to an existing file or directory.
|
|
|
|
|
1990-12-26 11:40:07 -04:00
|
|
|
def exists(s):
|
|
|
|
try:
|
|
|
|
st = mac.stat(s)
|
|
|
|
except mac.error:
|
|
|
|
return 0
|
|
|
|
return 1
|
1992-11-05 06:43:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Normalize path, removing things like ...:A:..:... (yet to be written)
|
|
|
|
|
|
|
|
def normpath(s):
|
|
|
|
return s
|
1995-08-07 11:09:27 -03: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:
|
|
|
|
names = mac.listdir(top)
|
|
|
|
except mac.error:
|
|
|
|
return
|
|
|
|
func(arg, top, names)
|
|
|
|
for name in names:
|
|
|
|
name = join(top, name)
|
|
|
|
if isdir(name):
|
|
|
|
walk(name, func, arg)
|