This commit was manufactured by cvs2svn to create tag 'r22a4-fork'.
[python/dscho.git] / Lib / macpath.py
blob1ef35ef11bdd042efac6a03dfb3b4ef4cf1e5831
1 """Pathname and path-related operations for the Macintosh."""
3 import os
4 from stat import *
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.
13 def normcase(path):
14 return path.lower()
17 def isabs(s):
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
22 volume name)."""
24 return ':' in s and s[0] != ':'
27 def join(s, *p):
28 path = s
29 for t in p:
30 if (not s) or isabs(t):
31 path = t
32 continue
33 if t[:1] == ':':
34 t = t[1:]
35 if ':' not in path:
36 path = ':' + path
37 if path[-1:] != ':':
38 path = path + ':'
39 path = path + t
40 return path
43 def split(s):
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
49 colon = 0
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:
54 path = path + ':'
55 return path, file
58 def splitext(p):
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."""
64 root, ext = '', ''
65 for c in p:
66 if c == ':':
67 root, ext = root + ext + c, ''
68 elif c == '.':
69 if ext:
70 root, ext = root + ext, c
71 else:
72 ext = c
73 elif ext:
74 ext = ext + c
75 else:
76 root = root + c
77 return root, ext
80 def splitdrive(p):
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)."""
87 return '', p
90 # Short interfaces to split()
92 def dirname(s): return split(s)[0]
93 def basename(s): return split(s)[1]
96 def isdir(s):
97 """Return true if the pathname refers to an existing directory."""
99 try:
100 st = os.stat(s)
101 except os.error:
102 return 0
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)
111 return st[ST_SIZE]
113 def getmtime(filename):
114 """Return the last modification time of a file, reported by os.stat()."""
115 st = os.stat(filename)
116 return st[ST_MTIME]
118 def getatime(filename):
119 """Return the last access time of a file, reported by os.stat()."""
120 st = os.stat(filename)
121 return st[ST_ATIME]
124 def islink(s):
125 """Return true if the pathname refers to a symbolic link.
126 Always false on the Mac, until we understand Aliases.)"""
128 return 0
131 def isfile(s):
132 """Return true if the pathname refers to an existing regular file."""
134 try:
135 st = os.stat(s)
136 except os.error:
137 return 0
138 return S_ISREG(st[ST_MODE])
141 def exists(s):
142 """Return true if the pathname refers to an existing file or directory."""
144 try:
145 st = os.stat(s)
146 except os.error:
147 return 0
148 return 1
150 # Return the longest prefix of all list elements.
152 def commonprefix(m):
153 "Given a list of pathnames, returns the longest common leading component"
154 if not m: return ''
155 prefix = m[0]
156 for item in m:
157 for i in range(len(prefix)):
158 if prefix[:i+1] != item[:i+1]:
159 prefix = prefix[:i]
160 if i == 0: return ''
161 break
162 return prefix
164 def expandvars(path):
165 """Dummy to retain interface-compatibility with other operating systems."""
166 return path
169 def expanduser(path):
170 """Dummy to retain interface-compatibility with other operating systems."""
171 return path
173 norm_error = 'macpath.norm_error: path cannot be normalized'
175 def normpath(s):
176 """Normalize a pathname. Will return the same result for
177 equivalent paths."""
179 if ":" not in s:
180 return ":"+s
182 comps = s.split(":")
183 i = 1
184 while i < len(comps)-1:
185 if comps[i] == "" and comps[i-1] != "":
186 if i > 1:
187 del comps[i-1:i+1]
188 i = i - 1
189 else:
190 # best way to handle this is to raise an exception
191 raise norm_error, 'Cannot use :: immediately after volume name'
192 else:
193 i = i + 1
195 s = ":".join(comps)
197 # remove trailing ":" except for ":" and "Volume:"
198 if s[-1] == ":" and len(comps) > 2 and s != ":"*len(s):
199 s = s[:-1]
200 return 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."""
212 try:
213 names = os.listdir(top)
214 except os.error:
215 return
216 func(arg, top, names)
217 for name in names:
218 name = join(top, name)
219 if isdir(name):
220 walk(name, func, arg)
223 def abspath(path):
224 """Return an absolute path."""
225 if not isabs(path):
226 path = join(os.getcwd(), path)
227 return normpath(path)
229 # realpath is a no-op on systems without islink support
230 realpath = abspath