This commit was manufactured by cvs2svn to create tag 'r23b1-mac'.
[python/dscho.git] / Lib / posixpath.py
blob50073dc99d50465226f280babfcf9d48a9ff4de8
1 """Common operations on Posix pathnames.
3 Instead of importing this module directly, import os and refer to
4 this module as os.path. The "os.path" name is an alias for this
5 module on Posix systems; on other systems (e.g. Mac, Windows),
6 os.path provides the same operations in a manner specific to that
7 platform, and is an alias to another module (e.g. macpath, ntpath).
9 Some of this can actually be useful on non-Posix systems too, e.g.
10 for manipulation of the pathname component of URLs.
11 """
13 import os
14 import stat
16 __all__ = ["normcase","isabs","join","splitdrive","split","splitext",
17 "basename","dirname","commonprefix","getsize","getmtime",
18 "getatime","getctime","islink","exists","isdir","isfile","ismount",
19 "walk","expanduser","expandvars","normpath","abspath",
20 "samefile","sameopenfile","samestat",
21 "curdir","pardir","sep","pathsep","defpath","altsep","extsep",
22 "realpath","supports_unicode_filenames"]
24 # strings representing various path-related bits and pieces
25 curdir = '.'
26 pardir = '..'
27 extsep = '.'
28 sep = '/'
29 pathsep = ':'
30 defpath = ':/bin:/usr/bin'
31 altsep = None
33 # Normalize the case of a pathname. Trivial in Posix, string.lower on Mac.
34 # On MS-DOS this may also turn slashes into backslashes; however, other
35 # normalizations (such as optimizing '../' away) are not allowed
36 # (another function should be defined to do that).
38 def normcase(s):
39 """Normalize case of pathname. Has no effect under Posix"""
40 return s
43 # Return whether a path is absolute.
44 # Trivial in Posix, harder on the Mac or MS-DOS.
46 def isabs(s):
47 """Test whether a path is absolute"""
48 return s[:1] == '/'
51 # Join pathnames.
52 # Ignore the previous parts if a part is absolute.
53 # Insert a '/' unless the first part is empty or already ends in '/'.
55 def join(a, *p):
56 """Join two or more pathname components, inserting '/' as needed"""
57 path = a
58 for b in p:
59 if b[:1] == '/':
60 path = b
61 elif path == '' or path[-1:] == '/':
62 path = path + b
63 else:
64 path = path + '/' + b
65 return path
68 # Split a path in head (everything up to the last '/') and tail (the
69 # rest). If the path ends in '/', tail will be empty. If there is no
70 # '/' in the path, head will be empty.
71 # Trailing '/'es are stripped from head unless it is the root.
73 def split(p):
74 """Split a pathname. Returns tuple "(head, tail)" where "tail" is
75 everything after the final slash. Either part may be empty."""
76 i = p.rfind('/') + 1
77 head, tail = p[:i], p[i:]
78 if head and head != '/'*len(head):
79 while head[-1] == '/':
80 head = head[:-1]
81 return head, tail
84 # Split a path in root and extension.
85 # The extension is everything starting at the last dot in the last
86 # pathname component; the root is everything before that.
87 # It is always true that root + ext == p.
89 def splitext(p):
90 """Split the extension from a pathname. Extension is everything from the
91 last dot to the end. Returns "(root, ext)", either part may be empty."""
92 i = p.rfind('.')
93 if i<=p.rfind('/'):
94 return p, ''
95 else:
96 return p[:i], p[i:]
99 # Split a pathname into a drive specification and the rest of the
100 # path. Useful on DOS/Windows/NT; on Unix, the drive is always empty.
102 def splitdrive(p):
103 """Split a pathname into drive and path. On Posix, drive is always
104 empty."""
105 return '', p
108 # Return the tail (basename) part of a path.
110 def basename(p):
111 """Returns the final component of a pathname"""
112 return split(p)[1]
115 # Return the head (dirname) part of a path.
117 def dirname(p):
118 """Returns the directory component of a pathname"""
119 return split(p)[0]
122 # Return the longest prefix of all list elements.
124 def commonprefix(m):
125 "Given a list of pathnames, returns the longest common leading component"
126 if not m: return ''
127 prefix = m[0]
128 for item in m:
129 for i in range(len(prefix)):
130 if prefix[:i+1] != item[:i+1]:
131 prefix = prefix[:i]
132 if i == 0: return ''
133 break
134 return prefix
137 # Get size, mtime, atime of files.
139 def getsize(filename):
140 """Return the size of a file, reported by os.stat()."""
141 return os.stat(filename).st_size
143 def getmtime(filename):
144 """Return the last modification time of a file, reported by os.stat()."""
145 return os.stat(filename).st_mtime
147 def getatime(filename):
148 """Return the last access time of a file, reported by os.stat()."""
149 return os.stat(filename).st_atime
151 def getctime(filename):
152 """Return the creation time of a file, reported by os.stat()."""
153 return os.stat(filename).st_ctime
155 # Is a path a symbolic link?
156 # This will always return false on systems where os.lstat doesn't exist.
158 def islink(path):
159 """Test whether a path is a symbolic link"""
160 try:
161 st = os.lstat(path)
162 except (os.error, AttributeError):
163 return False
164 return stat.S_ISLNK(st.st_mode)
167 # Does a path exist?
168 # This is false for dangling symbolic links.
170 def exists(path):
171 """Test whether a path exists. Returns False for broken symbolic links"""
172 try:
173 st = os.stat(path)
174 except os.error:
175 return False
176 return True
179 # Is a path a directory?
180 # This follows symbolic links, so both islink() and isdir() can be true
181 # for the same path.
183 def isdir(path):
184 """Test whether a path is a directory"""
185 try:
186 st = os.stat(path)
187 except os.error:
188 return False
189 return stat.S_ISDIR(st.st_mode)
192 # Is a path a regular file?
193 # This follows symbolic links, so both islink() and isfile() can be true
194 # for the same path.
196 def isfile(path):
197 """Test whether a path is a regular file"""
198 try:
199 st = os.stat(path)
200 except os.error:
201 return False
202 return stat.S_ISREG(st.st_mode)
205 # Are two filenames really pointing to the same file?
207 def samefile(f1, f2):
208 """Test whether two pathnames reference the same actual file"""
209 s1 = os.stat(f1)
210 s2 = os.stat(f2)
211 return samestat(s1, s2)
214 # Are two open files really referencing the same file?
215 # (Not necessarily the same file descriptor!)
217 def sameopenfile(fp1, fp2):
218 """Test whether two open file objects reference the same file"""
219 s1 = os.fstat(fp1)
220 s2 = os.fstat(fp2)
221 return samestat(s1, s2)
224 # Are two stat buffers (obtained from stat, fstat or lstat)
225 # describing the same file?
227 def samestat(s1, s2):
228 """Test whether two stat buffers reference the same file"""
229 return s1.st_ino == s2.st_ino and \
230 s1.st_dev == s2.st_dev
233 # Is a path a mount point?
234 # (Does this work for all UNIXes? Is it even guaranteed to work by Posix?)
236 def ismount(path):
237 """Test whether a path is a mount point"""
238 try:
239 s1 = os.stat(path)
240 s2 = os.stat(join(path, '..'))
241 except os.error:
242 return False # It doesn't exist -- so not a mount point :-)
243 dev1 = s1.st_dev
244 dev2 = s2.st_dev
245 if dev1 != dev2:
246 return True # path/.. on a different device as path
247 ino1 = s1.st_ino
248 ino2 = s2.st_ino
249 if ino1 == ino2:
250 return True # path/.. is the same i-node as path
251 return False
254 # Directory tree walk.
255 # For each directory under top (including top itself, but excluding
256 # '.' and '..'), func(arg, dirname, filenames) is called, where
257 # dirname is the name of the directory and filenames is the list
258 # of files (and subdirectories etc.) in the directory.
259 # The func may modify the filenames list, to implement a filter,
260 # or to impose a different order of visiting.
262 def walk(top, func, arg):
263 """Directory tree walk with callback function.
265 For each directory in the directory tree rooted at top (including top
266 itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
267 dirname is the name of the directory, and fnames a list of the names of
268 the files and subdirectories in dirname (excluding '.' and '..'). func
269 may modify the fnames list in-place (e.g. via del or slice assignment),
270 and walk will only recurse into the subdirectories whose names remain in
271 fnames; this can be used to implement a filter, or to impose a specific
272 order of visiting. No semantics are defined for, or required of, arg,
273 beyond that arg is always passed to func. It can be used, e.g., to pass
274 a filename pattern, or a mutable object designed to accumulate
275 statistics. Passing None for arg is common."""
277 try:
278 names = os.listdir(top)
279 except os.error:
280 return
281 func(arg, top, names)
282 for name in names:
283 name = join(top, name)
284 try:
285 st = os.lstat(name)
286 except os.error:
287 continue
288 if stat.S_ISDIR(st.st_mode):
289 walk(name, func, arg)
292 # Expand paths beginning with '~' or '~user'.
293 # '~' means $HOME; '~user' means that user's home directory.
294 # If the path doesn't begin with '~', or if the user or $HOME is unknown,
295 # the path is returned unchanged (leaving error reporting to whatever
296 # function is called with the expanded path as argument).
297 # See also module 'glob' for expansion of *, ? and [...] in pathnames.
298 # (A function should also be defined to do full *sh-style environment
299 # variable expansion.)
301 def expanduser(path):
302 """Expand ~ and ~user constructions. If user or $HOME is unknown,
303 do nothing."""
304 if path[:1] != '~':
305 return path
306 i, n = 1, len(path)
307 while i < n and path[i] != '/':
308 i = i + 1
309 if i == 1:
310 if not 'HOME' in os.environ:
311 import pwd
312 userhome = pwd.getpwuid(os.getuid())[5]
313 else:
314 userhome = os.environ['HOME']
315 else:
316 import pwd
317 try:
318 pwent = pwd.getpwnam(path[1:i])
319 except KeyError:
320 return path
321 userhome = pwent[5]
322 if userhome[-1:] == '/': i = i + 1
323 return userhome + path[i:]
326 # Expand paths containing shell variable substitutions.
327 # This expands the forms $variable and ${variable} only.
328 # Non-existent variables are left unchanged.
330 _varprog = None
332 def expandvars(path):
333 """Expand shell variables of form $var and ${var}. Unknown variables
334 are left unchanged."""
335 global _varprog
336 if '$' not in path:
337 return path
338 if not _varprog:
339 import re
340 _varprog = re.compile(r'\$(\w+|\{[^}]*\})')
341 i = 0
342 while True:
343 m = _varprog.search(path, i)
344 if not m:
345 break
346 i, j = m.span(0)
347 name = m.group(1)
348 if name[:1] == '{' and name[-1:] == '}':
349 name = name[1:-1]
350 if name in os.environ:
351 tail = path[j:]
352 path = path[:i] + os.environ[name]
353 i = len(path)
354 path = path + tail
355 else:
356 i = j
357 return path
360 # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
361 # It should be understood that this may change the meaning of the path
362 # if it contains symbolic links!
364 def normpath(path):
365 """Normalize path, eliminating double slashes, etc."""
366 if path == '':
367 return '.'
368 initial_slashes = path.startswith('/')
369 # POSIX allows one or two initial slashes, but treats three or more
370 # as single slash.
371 if (initial_slashes and
372 path.startswith('//') and not path.startswith('///')):
373 initial_slashes = 2
374 comps = path.split('/')
375 new_comps = []
376 for comp in comps:
377 if comp in ('', '.'):
378 continue
379 if (comp != '..' or (not initial_slashes and not new_comps) or
380 (new_comps and new_comps[-1] == '..')):
381 new_comps.append(comp)
382 elif new_comps:
383 new_comps.pop()
384 comps = new_comps
385 path = '/'.join(comps)
386 if initial_slashes:
387 path = '/'*initial_slashes + path
388 return path or '.'
391 def abspath(path):
392 """Return an absolute path."""
393 if not isabs(path):
394 path = join(os.getcwd(), path)
395 return normpath(path)
398 # Return a canonical path (i.e. the absolute location of a file on the
399 # filesystem).
401 def realpath(filename):
402 """Return the canonical path of the specified filename, eliminating any
403 symbolic links encountered in the path."""
404 filename = abspath(filename)
406 bits = ['/'] + filename.split('/')[1:]
407 for i in range(2, len(bits)+1):
408 component = join(*bits[0:i])
409 if islink(component):
410 resolved = os.readlink(component)
411 (dir, file) = split(component)
412 resolved = normpath(join(dir, resolved))
413 newpath = join(*([resolved] + bits[i:]))
414 return realpath(newpath)
416 return filename
418 supports_unicode_filenames = False