2000-02-04 11:10:34 -04:00
|
|
|
"""Pathname and path-related operations for the Macintosh."""
|
1990-12-26 11:40:07 -04:00
|
|
|
|
1992-01-14 14:28:18 -04:00
|
|
|
import string
|
1998-03-03 17:49:01 -04:00
|
|
|
import os
|
1990-12-26 11:40:07 -04:00
|
|
|
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
|
|
|
|
|
|
|
|
|
1990-12-26 11:40:07 -04:00
|
|
|
def isabs(s):
|
2000-02-04 11:10:34 -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)."""
|
|
|
|
|
|
|
|
return ':' in s and s[0] <> ':'
|
1990-12-26 11:40:07 -04:00
|
|
|
|
1991-01-01 14:10:40 -04:00
|
|
|
|
1997-02-18 17:53:25 -04:00
|
|
|
def join(s, *p):
|
2000-02-04 11:10:34 -04:00
|
|
|
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
|
|
|
|
|
1991-01-01 14:10:40 -04:00
|
|
|
|
|
|
|
def split(s):
|
2000-02-04 11:10:34 -04:00
|
|
|
"""Split a pathname into two parts: the directory leading up to the final
|
|
|
|
bit, and the basename (the filename, without colons, in that directory).
|
|
|
|
The result (s, t) is such that join(s, t) yields the original argument."""
|
1991-01-01 14:10:40 -04:00
|
|
|
|
2000-02-04 11:10:34 -04:00
|
|
|
if ':' not in s: return '', s
|
|
|
|
colon = 0
|
|
|
|
for i in range(len(s)):
|
|
|
|
if s[i] == ':': colon = i+1
|
|
|
|
path, file = s[:colon-1], s[colon:]
|
|
|
|
if path and not ':' in path:
|
|
|
|
path = path + ':'
|
|
|
|
return path, file
|
1991-01-01 14:10:40 -04:00
|
|
|
|
1996-07-22 23:28:32 -03:00
|
|
|
|
|
|
|
def splitext(p):
|
2000-02-04 11:10:34 -04:00
|
|
|
"""Split a path into 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."""
|
|
|
|
|
|
|
|
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
|
|
|
|
|
1992-11-05 06:43:02 -04:00
|
|
|
|
1995-08-10 15:09:16 -03:00
|
|
|
def splitdrive(p):
|
2000-02-04 11:10:34 -04: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)."""
|
|
|
|
|
|
|
|
return '', p
|
1992-11-05 06:43:02 -04:00
|
|
|
|
|
|
|
|
1995-08-10 15:09:16 -03:00
|
|
|
# Short interfaces to split()
|
1990-12-26 11:40:07 -04:00
|
|
|
|
1995-08-10 15:09:16 -03:00
|
|
|
def dirname(s): return split(s)[0]
|
|
|
|
def basename(s): return split(s)[1]
|
1990-12-26 11:40:07 -04:00
|
|
|
|
1991-01-01 14:10:40 -04:00
|
|
|
|
2000-07-12 13:55:57 -03:00
|
|
|
# Return the longest prefix of all list elements.
|
|
|
|
# XXX completely untested on Mac!!!
|
|
|
|
|
|
|
|
def commonprefix(m):
|
|
|
|
"Given a list of pathnames, returns the longest common leading component"
|
|
|
|
if not m: return ''
|
|
|
|
n = m[:]
|
|
|
|
for i in range(len(n)):
|
|
|
|
n[i] = n[i].split(os.sep)
|
|
|
|
# if os.sep didn't have any effect, try os.altsep
|
|
|
|
if os.altsep and len(n[i]) == 1:
|
|
|
|
n[i] = n[i].split(os.altsep)
|
|
|
|
|
|
|
|
prefix = n[0]
|
|
|
|
for item in n:
|
|
|
|
for i in range(len(prefix)):
|
|
|
|
if prefix[:i+1] <> item[:i+1]:
|
|
|
|
prefix = prefix[:i]
|
|
|
|
if i == 0: return ''
|
|
|
|
break
|
|
|
|
return os.sep.join(prefix)
|
|
|
|
|
|
|
|
|
1990-12-26 11:40:07 -04:00
|
|
|
def isdir(s):
|
2000-02-04 11:10:34 -04:00
|
|
|
"""Return true if the pathname refers to an existing directory."""
|
|
|
|
|
|
|
|
try:
|
|
|
|
st = os.stat(s)
|
|
|
|
except os.error:
|
|
|
|
return 0
|
|
|
|
return S_ISDIR(st[ST_MODE])
|
1990-12-26 11:40:07 -04:00
|
|
|
|
1991-01-01 14:10:40 -04:00
|
|
|
|
1998-07-24 17:49:26 -03: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 12:04:05 -03:00
|
|
|
return st[ST_SIZE]
|
1998-07-24 17:49:26 -03:00
|
|
|
|
|
|
|
def getmtime(filename):
|
|
|
|
"""Return the last modification time of a file, reported by os.stat()."""
|
|
|
|
st = os.stat(filename)
|
1999-07-23 12:04:05 -03:00
|
|
|
return st[ST_MTIME]
|
1998-07-24 17:49:26 -03:00
|
|
|
|
|
|
|
def getatime(filename):
|
|
|
|
"""Return the last access time of a file, reported by os.stat()."""
|
|
|
|
st = os.stat(filename)
|
2000-07-01 07:52:49 -03:00
|
|
|
return st[ST_ATIME]
|
1998-07-24 17:49:26 -03:00
|
|
|
|
|
|
|
|
1995-01-26 22:41:45 -04:00
|
|
|
def islink(s):
|
2000-02-04 11:10:34 -04:00
|
|
|
"""Return true if the pathname refers to a symbolic link.
|
|
|
|
Always false on the Mac, until we understand Aliases.)"""
|
1995-01-26 22:41:45 -04:00
|
|
|
|
2000-02-04 11:10:34 -04:00
|
|
|
return 0
|
1995-01-26 22:41:45 -04:00
|
|
|
|
1991-01-01 14:10:40 -04:00
|
|
|
|
1990-12-26 11:40:07 -04:00
|
|
|
def isfile(s):
|
2000-02-04 11:10:34 -04:00
|
|
|
"""Return true if the pathname refers to an existing regular file."""
|
1990-12-26 11:40:07 -04:00
|
|
|
|
2000-02-04 11:10:34 -04:00
|
|
|
try:
|
|
|
|
st = os.stat(s)
|
|
|
|
except os.error:
|
|
|
|
return 0
|
|
|
|
return S_ISREG(st[ST_MODE])
|
1991-01-01 14:10:40 -04:00
|
|
|
|
|
|
|
|
1990-12-26 11:40:07 -04:00
|
|
|
def exists(s):
|
2000-02-04 11:10:34 -04:00
|
|
|
"""Return true if the pathname refers to an existing file or directory."""
|
|
|
|
|
|
|
|
try:
|
|
|
|
st = os.stat(s)
|
|
|
|
except os.error:
|
|
|
|
return 0
|
|
|
|
return 1
|
|
|
|
|
|
|
|
|
1995-12-15 09:23:37 -04:00
|
|
|
def expandvars(path):
|
2000-02-04 11:10:34 -04:00
|
|
|
"""Dummy to retain interface-compatibility with other operating systems."""
|
|
|
|
return path
|
1995-12-15 09:23:37 -04:00
|
|
|
|
1992-11-05 06:43:02 -04:00
|
|
|
|
2000-02-04 11:10:34 -04:00
|
|
|
def expanduser(path):
|
|
|
|
"""Dummy to retain interface-compatibility with other operating systems."""
|
|
|
|
return path
|
1995-08-10 15:09:16 -03:00
|
|
|
|
|
|
|
norm_error = 'macpath.norm_error: path cannot be normalized'
|
1992-11-05 06:43:02 -04:00
|
|
|
|
|
|
|
def normpath(s):
|
2000-08-06 18:18:35 -03:00
|
|
|
"""Normalize a pathname. Will return the same result for
|
|
|
|
equivalent paths."""
|
|
|
|
|
|
|
|
if ":" not in s:
|
|
|
|
return ":"+s
|
|
|
|
|
|
|
|
comps = string.splitfields(s, ":")
|
|
|
|
i = 1
|
|
|
|
while i < len(comps)-1:
|
|
|
|
if comps[i] == "" and comps[i-1] != "":
|
|
|
|
if i > 1:
|
|
|
|
del comps[i-1:i+1]
|
|
|
|
i = i-1
|
|
|
|
else:
|
|
|
|
# best way to handle this is to raise an exception
|
|
|
|
raise norm_error, 'Cannot use :: immedeately after volume name'
|
2000-02-04 11:10:34 -04:00
|
|
|
else:
|
2000-08-06 18:18:35 -03:00
|
|
|
i = i + 1
|
|
|
|
|
|
|
|
s = string.join(comps, ":")
|
|
|
|
|
|
|
|
# remove trailing ":" except for ":" and "Volume:"
|
|
|
|
if s[-1] == ":" and len(comps) > 2 and s != ":"*len(s):
|
|
|
|
s = s[:-1]
|
2000-02-04 11:10:34 -04:00
|
|
|
return s
|
|
|
|
|
1995-08-07 11:09:27 -03:00
|
|
|
|
|
|
|
def walk(top, func, arg):
|
2000-02-04 11:10:34 -04: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."""
|
|
|
|
|
|
|
|
try:
|
|
|
|
names = os.listdir(top)
|
|
|
|
except os.error:
|
|
|
|
return
|
|
|
|
func(arg, top, names)
|
|
|
|
for name in names:
|
|
|
|
name = join(top, name)
|
|
|
|
if isdir(name):
|
|
|
|
walk(name, func, arg)
|
|
|
|
|
|
|
|
|
1999-01-29 14:05:18 -04:00
|
|
|
def abspath(path):
|
2000-02-04 11:10:34 -04:00
|
|
|
"""Return an absolute path."""
|
1999-01-29 14:05:18 -04:00
|
|
|
if not isabs(path):
|
|
|
|
path = join(os.getcwd(), path)
|
|
|
|
return normpath(path)
|