1 """Pathname and path-related operations for the Macintosh."""
6 __all__
= ["normcase","isabs","join","splitdrive","split","splitext",
7 "basename","dirname","commonprefix","getsize","getmtime",
8 "getatime","islink","exists","isdir","isfile",
9 "walk","expanduser","expandvars","normpath","abspath"]
11 # Normalize the case of a pathname. Dummy in Posix, but <s>.lower() here.
18 """Return true if a path is absolute.
19 On the Mac, relative paths begin with a colon,
20 but as a special case, paths with no colons at all are also relative.
21 Anything else is absolute (the string up to the first colon is the
24 return ':' in s
and s
[0] != ':'
30 if (not s
) or isabs(t
):
44 """Split a pathname into two parts: the directory leading up to the final
45 bit, and the basename (the filename, without colons, in that directory).
46 The result (s, t) is such that join(s, t) yields the original argument."""
48 if ':' not in s
: return '', s
50 for i
in range(len(s
)):
51 if s
[i
] == ':': colon
= i
+ 1
52 path
, file = s
[:colon
-1], s
[colon
:]
53 if path
and not ':' in path
:
59 """Split a path into root and extension.
60 The extension is everything starting at the last dot in the last
61 pathname component; the root is everything before that.
62 It is always true that root + ext == p."""
67 root
, ext
= root
+ ext
+ c
, ''
70 root
, ext
= root
+ ext
, c
81 """Split a pathname into a drive specification and the rest of the
82 path. Useful on DOS/Windows/NT; on the Mac, the drive is always
83 empty (don't use the volume name -- it doesn't have the same
84 syntactic and semantic oddities as DOS drive letters, such as there
85 being a separate current directory per drive)."""
90 # Short interfaces to split()
92 def dirname(s
): return split(s
)[0]
93 def basename(s
): return split(s
)[1]
97 """Return true if the pathname refers to an existing directory."""
103 return S_ISDIR(st
[ST_MODE
])
106 # Get size, mtime, atime of files.
108 def getsize(filename
):
109 """Return the size of a file, reported by os.stat()."""
110 st
= os
.stat(filename
)
113 def getmtime(filename
):
114 """Return the last modification time of a file, reported by os.stat()."""
115 st
= os
.stat(filename
)
118 def getatime(filename
):
119 """Return the last access time of a file, reported by os.stat()."""
120 st
= os
.stat(filename
)
125 """Return true if the pathname refers to a symbolic link.
126 Always false on the Mac, until we understand Aliases.)"""
132 """Return true if the pathname refers to an existing regular file."""
138 return S_ISREG(st
[ST_MODE
])
142 """Return true if the pathname refers to an existing file or directory."""
150 # Return the longest prefix of all list elements.
153 "Given a list of pathnames, returns the longest common leading component"
157 for i
in range(len(prefix
)):
158 if prefix
[:i
+1] != item
[:i
+1]:
164 def expandvars(path
):
165 """Dummy to retain interface-compatibility with other operating systems."""
169 def expanduser(path
):
170 """Dummy to retain interface-compatibility with other operating systems."""
173 norm_error
= 'macpath.norm_error: path cannot be normalized'
176 """Normalize a pathname. Will return the same result for
184 while i
< len(comps
)-1:
185 if comps
[i
] == "" and comps
[i
-1] != "":
190 # best way to handle this is to raise an exception
191 raise norm_error
, 'Cannot use :: immediately after volume name'
197 # remove trailing ":" except for ":" and "Volume:"
198 if s
[-1] == ":" and len(comps
) > 2 and s
!= ":"*len(s
):
203 def walk(top
, func
, arg
):
204 """Directory tree walk.
205 For each directory under top (including top itself),
206 func(arg, dirname, filenames) is called, where
207 dirname is the name of the directory and filenames is the list
208 of files (and subdirectories etc.) in the directory.
209 The func may modify the filenames list, to implement a filter,
210 or to impose a different order of visiting."""
213 names
= os
.listdir(top
)
216 func(arg
, top
, names
)
218 name
= join(top
, name
)
220 walk(name
, func
, arg
)
224 """Return an absolute path."""
226 path
= join(os
.getcwd(), path
)
227 return normpath(path
)
229 # realpath is a no-op on systems without islink support