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","isdir","isfile",
9 "walk","expanduser","expandvars","normpath","abspath",
10 "curdir","pardir","sep","pathsep","defpath","altsep","extsep",
11 "realpath","supports_unicode_filenames"]
13 # strings representing various path-related bits and pieces
22 # Normalize the case of a pathname. Dummy in Posix, but <s>.lower() here.
29 """Return true if a path is absolute.
30 On the Mac, relative paths begin with a colon,
31 but as a special case, paths with no colons at all are also relative.
32 Anything else is absolute (the string up to the first colon is the
35 return ':' in s
and s
[0] != ':'
41 if (not s
) or isabs(t
):
55 """Split a pathname into two parts: the directory leading up to the final
56 bit, and the basename (the filename, without colons, in that directory).
57 The result (s, t) is such that join(s, t) yields the original argument."""
59 if ':' not in s
: return '', s
61 for i
in range(len(s
)):
62 if s
[i
] == ':': colon
= i
+ 1
63 path
, file = s
[:colon
-1], s
[colon
:]
64 if path
and not ':' in path
:
70 """Split a path into root and extension.
71 The extension is everything starting at the last dot in the last
72 pathname component; the root is everything before that.
73 It is always true that root + ext == p."""
83 """Split a pathname into a drive specification and the rest of the
84 path. Useful on DOS/Windows/NT; on the Mac, the drive is always
85 empty (don't use the volume name -- it doesn't have the same
86 syntactic and semantic oddities as DOS drive letters, such as there
87 being a separate current directory per drive)."""
92 # Short interfaces to split()
94 def dirname(s
): return split(s
)[0]
95 def basename(s
): return split(s
)[1]
100 components
= split(s
)
101 return len(components
) == 2 and components
[1] == ''
104 """Return true if the pathname refers to an existing directory."""
110 return S_ISDIR(st
.st_mode
)
113 # Get size, mtime, atime of files.
115 def getsize(filename
):
116 """Return the size of a file, reported by os.stat()."""
117 return os
.stat(filename
).st_size
119 def getmtime(filename
):
120 """Return the last modification time of a file, reported by os.stat()."""
121 return os
.stat(filename
).st_mtime
123 def getatime(filename
):
124 """Return the last access time of a file, reported by os.stat()."""
125 return os
.stat(filename
).st_atime
129 """Return true if the pathname refers to a symbolic link."""
133 return Carbon
.File
.ResolveAliasFile(s
, 0)[2]
139 """Return true if the pathname refers to an existing regular file."""
145 return S_ISREG(st
.st_mode
)
147 def getctime(filename
):
148 """Return the creation time of a file, reported by os.stat()."""
149 return os
.stat(filename
).st_ctime
152 """Return True if the pathname refers to an existing file or directory."""
160 # Return the longest prefix of all list elements.
163 "Given a list of pathnames, returns the longest common leading component"
167 for i
in range(len(prefix
)):
168 if prefix
[:i
+1] != item
[:i
+1]:
174 def expandvars(path
):
175 """Dummy to retain interface-compatibility with other operating systems."""
179 def expanduser(path
):
180 """Dummy to retain interface-compatibility with other operating systems."""
183 class norm_error(Exception):
184 """Path cannot be normalized"""
187 """Normalize a pathname. Will return the same result for
195 while i
< len(comps
)-1:
196 if comps
[i
] == "" and comps
[i
-1] != "":
201 # best way to handle this is to raise an exception
202 raise norm_error
, 'Cannot use :: immediately after volume name'
208 # remove trailing ":" except for ":" and "Volume:"
209 if s
[-1] == ":" and len(comps
) > 2 and s
!= ":"*len(s
):
214 def walk(top
, func
, arg
):
215 """Directory tree walk with callback function.
217 For each directory in the directory tree rooted at top (including top
218 itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
219 dirname is the name of the directory, and fnames a list of the names of
220 the files and subdirectories in dirname (excluding '.' and '..'). func
221 may modify the fnames list in-place (e.g. via del or slice assignment),
222 and walk will only recurse into the subdirectories whose names remain in
223 fnames; this can be used to implement a filter, or to impose a specific
224 order of visiting. No semantics are defined for, or required of, arg,
225 beyond that arg is always passed to func. It can be used, e.g., to pass
226 a filename pattern, or a mutable object designed to accumulate
227 statistics. Passing None for arg is common."""
230 names
= os
.listdir(top
)
233 func(arg
, top
, names
)
235 name
= join(top
, name
)
236 if isdir(name
) and not islink(name
):
237 walk(name
, func
, arg
)
241 """Return an absolute path."""
243 path
= join(os
.getcwd(), path
)
244 return normpath(path
)
246 # realpath is a no-op on systems without islink support
255 components
= path
.split(':')
256 path
= components
[0] + ':'
257 for c
in components
[1:]:
259 path
= Carbon
.File
.FSResolveAliasFile(path
, 1)[0].as_pathname()
262 supports_unicode_filenames
= False