1 # module 'macpath' -- pathname (or -related) operations for the Macintosh
8 # Normalize the case of a pathname. Dummy in Posix, but string.lower here.
10 normcase
= string
.lower
13 # Return true if a path is absolute.
14 # On the Mac, relative paths begin with a colon,
15 # but as a special case, paths with no colons at all are also relative.
16 # Anything else is absolute (the string up to the first colon is the
20 return ':' in s
and s
[0] <> ':'
26 if (not s
) or isabs(t
):
39 # Split a pathname in two parts: the directory leading up to the final bit,
40 # and the basename (the filename, without colons, in that directory).
41 # The result (s, t) is such that join(s, t) yields the original argument.
44 if ':' not in s
: return '', s
46 for i
in range(len(s
)):
47 if s
[i
] == ':': colon
= i
+1
48 path
, file = s
[:colon
-1], s
[colon
:]
49 if path
and not ':' in path
:
54 # Split a path in root and extension.
55 # The extension is everything starting at the last dot in the last
56 # pathname component; the root is everything before that.
57 # It is always true that root + ext == p.
63 root
, ext
= root
+ ext
+ c
, ''
66 root
, ext
= root
+ ext
, c
76 # Split a pathname into a drive specification and the rest of the
77 # path. Useful on DOS/Windows/NT; on the Mac, the drive is always
78 # empty (don't use the volume name -- it doesn't have the same
79 # syntactic and semantic oddities as DOS drive letters, such as there
80 # being a separate current directory per drive).
86 # Short interfaces to split()
88 def dirname(s
): return split(s
)[0]
89 def basename(s
): return split(s
)[1]
92 # Return true if the pathname refers to an existing directory.
99 return S_ISDIR(st
[ST_MODE
])
102 # Get size, mtime, atime of files.
104 def getsize(filename
):
105 """Return the size of a file, reported by os.stat()."""
106 st
= os
.stat(filename
)
107 return st
[stat
.ST_SIZE
]
109 def getmtime(filename
):
110 """Return the last modification time of a file, reported by os.stat()."""
111 st
= os
.stat(filename
)
112 return st
[stat
.ST_MTIME
]
114 def getatime(filename
):
115 """Return the last access time of a file, reported by os.stat()."""
116 st
= os
.stat(filename
)
117 return st
[stat
.ST_MTIME
]
120 # Return true if the pathname refers to a symbolic link.
121 # (Always false on the Mac, until we understand Aliases.)
127 # Return true if the pathname refers to an existing regular file.
134 return S_ISREG(st
[ST_MODE
])
137 # Return true if the pathname refers to an existing file or directory.
147 # dummy expandvars to retain interface-compatability with other
149 def expandvars(path
):
153 # dummy expanduser to retain interface-compatability with other
155 def expanduser(path
):
158 # Normalize a pathname: get rid of '::' sequences by backing up,
159 # e.g., 'foo:bar::bletch' becomes 'foo:bletch'.
160 # Raise the exception norm_error below if backing up is impossible,
162 # XXX The Unix version doesn't raise an exception but simply
163 # returns an unnormalized path. Should do so here too.
165 norm_error
= 'macpath.norm_error: path cannot be normalized'
171 f
= string
.splitfields(s
, ':')
185 if not res
: raise norm_error
, 'path starts with ::'
188 raise norm_error
, 'path starts with volume::'
189 if pre
: res
= pre
+ res
190 if post
: res
= res
+ post
197 # Directory tree walk.
198 # For each directory under top (including top itself),
199 # func(arg, dirname, filenames) is called, where
200 # dirname is the name of the directory and filenames is the list
201 # of files (and subdirectories etc.) in the directory.
202 # The func may modify the filenames list, to implement a filter,
203 # or to impose a different order of visiting.
205 def walk(top
, func
, arg
):
207 names
= os
.listdir(top
)
210 func(arg
, top
, names
)
212 name
= join(top
, name
)
214 walk(name
, func
, arg
)