1 """Pathname and path-related operations for the Macintosh."""
7 # Normalize the case of a pathname. Dummy in Posix, but <s>.lower() here.
14 """Return true if a path is absolute.
15 On the Mac, relative paths begin with a colon,
16 but as a special case, paths with no colons at all are also relative.
17 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
):
40 """Split a pathname into two parts: the directory leading up to the final
41 bit, and the basename (the filename, without colons, in that directory).
42 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
:
55 """Split a path into root and extension.
56 The extension is everything starting at the last dot in the last
57 pathname component; the root is everything before that.
58 It is always true that root + ext == p."""
63 root
, ext
= root
+ ext
+ c
, ''
66 root
, ext
= root
+ ext
, c
77 """Split a pathname into a drive specification and the rest of the
78 path. Useful on DOS/Windows/NT; on the Mac, the drive is always
79 empty (don't use the volume name -- it doesn't have the same
80 syntactic and semantic oddities as DOS drive letters, such as there
81 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]
93 """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
)
109 def getmtime(filename
):
110 """Return the last modification time of a file, reported by os.stat()."""
111 st
= os
.stat(filename
)
114 def getatime(filename
):
115 """Return the last access time of a file, reported by os.stat()."""
116 st
= os
.stat(filename
)
121 """Return true if the pathname refers to a symbolic link.
122 Always false on the Mac, until we understand Aliases.)"""
128 """Return true if the pathname refers to an existing regular file."""
134 return S_ISREG(st
[ST_MODE
])
138 """Return true if the pathname refers to an existing file or directory."""
146 # Return the longest prefix of all list elements.
149 "Given a list of pathnames, returns the longest common leading component"
153 for i
in range(len(prefix
)):
154 if prefix
[:i
+1] <> item
[:i
+1]:
160 def expandvars(path
):
161 """Dummy to retain interface-compatibility with other operating systems."""
165 def expanduser(path
):
166 """Dummy to retain interface-compatibility with other operating systems."""
169 norm_error
= 'macpath.norm_error: path cannot be normalized'
172 """Normalize a pathname. Will return the same result for
180 while i
< len(comps
)-1:
181 if comps
[i
] == "" and comps
[i
-1] != "":
186 # best way to handle this is to raise an exception
187 raise norm_error
, 'Cannot use :: immedeately after volume name'
193 # remove trailing ":" except for ":" and "Volume:"
194 if s
[-1] == ":" and len(comps
) > 2 and s
!= ":"*len(s
):
199 def walk(top
, func
, arg
):
200 """Directory tree walk.
201 For each directory under top (including top itself),
202 func(arg, dirname, filenames) is called, where
203 dirname is the name of the directory and filenames is the list
204 of files (and subdirectories etc.) in the directory.
205 The func may modify the filenames list, to implement a filter,
206 or to impose a different order of visiting."""
209 names
= os
.listdir(top
)
212 func(arg
, top
, names
)
214 name
= join(top
, name
)
216 walk(name
, func
, arg
)
220 """Return an absolute path."""
222 path
= join(os
.getcwd(), path
)
223 return normpath(path
)