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","getctime", "islink","exists","lexists","isdir","isfile",
9 "walk","expanduser","expandvars","normpath","abspath",
10 "curdir","pardir","sep","pathsep","defpath","altsep","extsep",
11 "devnull","realpath","supports_unicode_filenames"]
13 # strings representing various path-related bits and pieces
23 # Normalize the case of a pathname. Dummy in Posix, but <s>.lower() here.
30 """Return true if a path is absolute.
31 On the Mac, relative paths begin with a colon,
32 but as a special case, paths with no colons at all are also relative.
33 Anything else is absolute (the string up to the first colon is the
36 return ':' in s
and s
[0] != ':'
42 if (not s
) or isabs(t
):
56 """Split a pathname into two parts: the directory leading up to the final
57 bit, and the basename (the filename, without colons, in that directory).
58 The result (s, t) is such that join(s, t) yields the original argument."""
60 if ':' not in s
: return '', s
62 for i
in range(len(s
)):
63 if s
[i
] == ':': colon
= i
+ 1
64 path
, file = s
[:colon
-1], s
[colon
:]
65 if path
and not ':' in path
:
71 """Split a path into root and extension.
72 The extension is everything starting at the last dot in the last
73 pathname component; the root is everything before that.
74 It is always true that root + ext == p."""
84 """Split a pathname into a drive specification and the rest of the
85 path. Useful on DOS/Windows/NT; on the Mac, the drive is always
86 empty (don't use the volume name -- it doesn't have the same
87 syntactic and semantic oddities as DOS drive letters, such as there
88 being a separate current directory per drive)."""
93 # Short interfaces to split()
95 def dirname(s
): return split(s
)[0]
96 def basename(s
): return split(s
)[1]
101 components
= split(s
)
102 return len(components
) == 2 and components
[1] == ''
105 """Return true if the pathname refers to an existing directory."""
111 return S_ISDIR(st
.st_mode
)
114 # Get size, mtime, atime of files.
116 def getsize(filename
):
117 """Return the size of a file, reported by os.stat()."""
118 return os
.stat(filename
).st_size
120 def getmtime(filename
):
121 """Return the last modification time of a file, reported by os.stat()."""
122 return os
.stat(filename
).st_mtime
124 def getatime(filename
):
125 """Return the last access time of a file, reported by os.stat()."""
126 return os
.stat(filename
).st_atime
130 """Return true if the pathname refers to a symbolic link."""
134 return Carbon
.File
.ResolveAliasFile(s
, 0)[2]
140 """Return true if the pathname refers to an existing regular file."""
146 return S_ISREG(st
.st_mode
)
148 def getctime(filename
):
149 """Return the creation time of a file, reported by os.stat()."""
150 return os
.stat(filename
).st_ctime
153 """Test whether a path exists. Returns False for broken symbolic links"""
161 # Is `stat`/`lstat` a meaningful difference on the Mac? This is safe in any
165 """Test whether a path exists. Returns True for broken symbolic links"""
173 # Return the longest prefix of all list elements.
176 "Given a list of pathnames, returns the longest common leading component"
180 n
= min(len(s1
), len(s2
))
187 def expandvars(path
):
188 """Dummy to retain interface-compatibility with other operating systems."""
192 def expanduser(path
):
193 """Dummy to retain interface-compatibility with other operating systems."""
196 class norm_error(Exception):
197 """Path cannot be normalized"""
200 """Normalize a pathname. Will return the same result for
208 while i
< len(comps
)-1:
209 if comps
[i
] == "" and comps
[i
-1] != "":
214 # best way to handle this is to raise an exception
215 raise norm_error
, 'Cannot use :: immediately after volume name'
221 # remove trailing ":" except for ":" and "Volume:"
222 if s
[-1] == ":" and len(comps
) > 2 and s
!= ":"*len(s
):
227 def walk(top
, func
, arg
):
228 """Directory tree walk with callback function.
230 For each directory in the directory tree rooted at top (including top
231 itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
232 dirname is the name of the directory, and fnames a list of the names of
233 the files and subdirectories in dirname (excluding '.' and '..'). func
234 may modify the fnames list in-place (e.g. via del or slice assignment),
235 and walk will only recurse into the subdirectories whose names remain in
236 fnames; this can be used to implement a filter, or to impose a specific
237 order of visiting. No semantics are defined for, or required of, arg,
238 beyond that arg is always passed to func. It can be used, e.g., to pass
239 a filename pattern, or a mutable object designed to accumulate
240 statistics. Passing None for arg is common."""
243 names
= os
.listdir(top
)
246 func(arg
, top
, names
)
248 name
= join(top
, name
)
249 if isdir(name
) and not islink(name
):
250 walk(name
, func
, arg
)
254 """Return an absolute path."""
256 path
= join(os
.getcwd(), path
)
257 return normpath(path
)
259 # realpath is a no-op on systems without islink support
268 components
= path
.split(':')
269 path
= components
[0] + ':'
270 for c
in components
[1:]:
272 path
= Carbon
.File
.FSResolveAliasFile(path
, 1)[0].as_pathname()
275 supports_unicode_filenames
= False