Make uuid1 and uuid4 tests conditional on whether ctypes can be imported;
[python.git] / Lib / macpath.py
blobf54ffa07d71a654c4d252249384e52834efcdbf2
1 """Pathname and path-related operations for the Macintosh."""
3 import os
4 from stat import *
5 import genericpath
6 from genericpath import *
8 __all__ = ["normcase","isabs","join","splitdrive","split","splitext",
9 "basename","dirname","commonprefix","getsize","getmtime",
10 "getatime","getctime", "islink","exists","lexists","isdir","isfile",
11 "walk","expanduser","expandvars","normpath","abspath",
12 "curdir","pardir","sep","pathsep","defpath","altsep","extsep",
13 "devnull","realpath","supports_unicode_filenames"]
15 # strings representing various path-related bits and pieces
16 curdir = ':'
17 pardir = '::'
18 extsep = '.'
19 sep = ':'
20 pathsep = '\n'
21 defpath = ':'
22 altsep = None
23 devnull = 'Dev:Null'
25 # Normalize the case of a pathname. Dummy in Posix, but <s>.lower() here.
27 def normcase(path):
28 return path.lower()
31 def isabs(s):
32 """Return true if a path is absolute.
33 On the Mac, relative paths begin with a colon,
34 but as a special case, paths with no colons at all are also relative.
35 Anything else is absolute (the string up to the first colon is the
36 volume name)."""
38 return ':' in s and s[0] != ':'
41 def join(s, *p):
42 path = s
43 for t in p:
44 if (not s) or isabs(t):
45 path = t
46 continue
47 if t[:1] == ':':
48 t = t[1:]
49 if ':' not in path:
50 path = ':' + path
51 if path[-1:] != ':':
52 path = path + ':'
53 path = path + t
54 return path
57 def split(s):
58 """Split a pathname into two parts: the directory leading up to the final
59 bit, and the basename (the filename, without colons, in that directory).
60 The result (s, t) is such that join(s, t) yields the original argument."""
62 if ':' not in s: return '', s
63 colon = 0
64 for i in range(len(s)):
65 if s[i] == ':': colon = i + 1
66 path, file = s[:colon-1], s[colon:]
67 if path and not ':' in path:
68 path = path + ':'
69 return path, file
72 def splitext(p):
73 return genericpath._splitext(p, sep, altsep, extsep)
74 splitext.__doc__ = genericpath._splitext.__doc__
76 def splitdrive(p):
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)."""
83 return '', p
86 # Short interfaces to split()
88 def dirname(s): return split(s)[0]
89 def basename(s): return split(s)[1]
91 def ismount(s):
92 if not isabs(s):
93 return False
94 components = split(s)
95 return len(components) == 2 and components[1] == ''
97 def islink(s):
98 """Return true if the pathname refers to a symbolic link."""
100 try:
101 import Carbon.File
102 return Carbon.File.ResolveAliasFile(s, 0)[2]
103 except:
104 return False
106 # Is `stat`/`lstat` a meaningful difference on the Mac? This is safe in any
107 # case.
109 def lexists(path):
110 """Test whether a path exists. Returns True for broken symbolic links"""
112 try:
113 st = os.lstat(path)
114 except os.error:
115 return False
116 return True
118 def expandvars(path):
119 """Dummy to retain interface-compatibility with other operating systems."""
120 return path
123 def expanduser(path):
124 """Dummy to retain interface-compatibility with other operating systems."""
125 return path
127 class norm_error(Exception):
128 """Path cannot be normalized"""
130 def normpath(s):
131 """Normalize a pathname. Will return the same result for
132 equivalent paths."""
134 if ":" not in s:
135 return ":"+s
137 comps = s.split(":")
138 i = 1
139 while i < len(comps)-1:
140 if comps[i] == "" and comps[i-1] != "":
141 if i > 1:
142 del comps[i-1:i+1]
143 i = i - 1
144 else:
145 # best way to handle this is to raise an exception
146 raise norm_error, 'Cannot use :: immediately after volume name'
147 else:
148 i = i + 1
150 s = ":".join(comps)
152 # remove trailing ":" except for ":" and "Volume:"
153 if s[-1] == ":" and len(comps) > 2 and s != ":"*len(s):
154 s = s[:-1]
155 return s
158 def walk(top, func, arg):
159 """Directory tree walk with callback function.
161 For each directory in the directory tree rooted at top (including top
162 itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
163 dirname is the name of the directory, and fnames a list of the names of
164 the files and subdirectories in dirname (excluding '.' and '..'). func
165 may modify the fnames list in-place (e.g. via del or slice assignment),
166 and walk will only recurse into the subdirectories whose names remain in
167 fnames; this can be used to implement a filter, or to impose a specific
168 order of visiting. No semantics are defined for, or required of, arg,
169 beyond that arg is always passed to func. It can be used, e.g., to pass
170 a filename pattern, or a mutable object designed to accumulate
171 statistics. Passing None for arg is common."""
173 try:
174 names = os.listdir(top)
175 except os.error:
176 return
177 func(arg, top, names)
178 for name in names:
179 name = join(top, name)
180 if isdir(name) and not islink(name):
181 walk(name, func, arg)
184 def abspath(path):
185 """Return an absolute path."""
186 if not isabs(path):
187 path = join(os.getcwd(), path)
188 return normpath(path)
190 # realpath is a no-op on systems without islink support
191 def realpath(path):
192 path = abspath(path)
193 try:
194 import Carbon.File
195 except ImportError:
196 return path
197 if not path:
198 return path
199 components = path.split(':')
200 path = components[0] + ':'
201 for c in components[1:]:
202 path = join(path, c)
203 path = Carbon.File.FSResolveAliasFile(path, 1)[0].as_pathname()
204 return path
206 supports_unicode_filenames = False