Fix the tag.
[python/dscho.git] / Lib / posixpath.py
blobee6d0f2407ab8e09d8ba9cda0a1c404e4df3de0e
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
15 import genericpath
16 from genericpath import *
18 __all__ = ["normcase","isabs","join","splitdrive","split","splitext",
19 "basename","dirname","commonprefix","getsize","getmtime",
20 "getatime","getctime","islink","exists","lexists","isdir","isfile",
21 "ismount","walk","expanduser","expandvars","normpath","abspath",
22 "samefile","sameopenfile","samestat",
23 "curdir","pardir","sep","pathsep","defpath","altsep","extsep",
24 "devnull","realpath","supports_unicode_filenames","relpath"]
26 # strings representing various path-related bits and pieces
27 curdir = '.'
28 pardir = '..'
29 extsep = '.'
30 sep = '/'
31 pathsep = ':'
32 defpath = ':/bin:/usr/bin'
33 altsep = None
34 devnull = '/dev/null'
36 # Normalize the case of a pathname. Trivial in Posix, string.lower on Mac.
37 # On MS-DOS this may also turn slashes into backslashes; however, other
38 # normalizations (such as optimizing '../' away) are not allowed
39 # (another function should be defined to do that).
41 def normcase(s):
42 """Normalize case of pathname. Has no effect under Posix"""
43 return s
46 # Return whether a path is absolute.
47 # Trivial in Posix, harder on the Mac or MS-DOS.
49 def isabs(s):
50 """Test whether a path is absolute"""
51 return s.startswith('/')
54 # Join pathnames.
55 # Ignore the previous parts if a part is absolute.
56 # Insert a '/' unless the first part is empty or already ends in '/'.
58 def join(a, *p):
59 """Join two or more pathname components, inserting '/' as needed.
60 If any component is an absolute path, all previous path components
61 will be discarded."""
62 path = a
63 for b in p:
64 if b.startswith('/'):
65 path = b
66 elif path == '' or path.endswith('/'):
67 path += b
68 else:
69 path += '/' + b
70 return path
73 # Split a path in head (everything up to the last '/') and tail (the
74 # rest). If the path ends in '/', tail will be empty. If there is no
75 # '/' in the path, head will be empty.
76 # Trailing '/'es are stripped from head unless it is the root.
78 def split(p):
79 """Split a pathname. Returns tuple "(head, tail)" where "tail" is
80 everything after the final slash. Either part may be empty."""
81 i = p.rfind('/') + 1
82 head, tail = p[:i], p[i:]
83 if head and head != '/'*len(head):
84 head = head.rstrip('/')
85 return head, tail
88 # Split a path in root and extension.
89 # The extension is everything starting at the last dot in the last
90 # pathname component; the root is everything before that.
91 # It is always true that root + ext == p.
93 def splitext(p):
94 return genericpath._splitext(p, sep, altsep, extsep)
95 splitext.__doc__ = genericpath._splitext.__doc__
97 # Split a pathname into a drive specification and the rest of the
98 # path. Useful on DOS/Windows/NT; on Unix, the drive is always empty.
100 def splitdrive(p):
101 """Split a pathname into drive and path. On Posix, drive is always
102 empty."""
103 return '', p
106 # Return the tail (basename) part of a path, same as split(path)[1].
108 def basename(p):
109 """Returns the final component of a pathname"""
110 i = p.rfind('/') + 1
111 return p[i:]
114 # Return the head (dirname) part of a path, same as split(path)[0].
116 def dirname(p):
117 """Returns the directory component of a pathname"""
118 i = p.rfind('/') + 1
119 head = p[:i]
120 if head and head != '/'*len(head):
121 head = head.rstrip('/')
122 return head
125 # Is a path a symbolic link?
126 # This will always return false on systems where os.lstat doesn't exist.
128 def islink(path):
129 """Test whether a path is a symbolic link"""
130 try:
131 st = os.lstat(path)
132 except (os.error, AttributeError):
133 return False
134 return stat.S_ISLNK(st.st_mode)
136 # Being true for dangling symbolic links is also useful.
138 def lexists(path):
139 """Test whether a path exists. Returns True for broken symbolic links"""
140 try:
141 st = os.lstat(path)
142 except os.error:
143 return False
144 return True
147 # Are two filenames really pointing to the same file?
149 def samefile(f1, f2):
150 """Test whether two pathnames reference the same actual file"""
151 s1 = os.stat(f1)
152 s2 = os.stat(f2)
153 return samestat(s1, s2)
156 # Are two open files really referencing the same file?
157 # (Not necessarily the same file descriptor!)
159 def sameopenfile(fp1, fp2):
160 """Test whether two open file objects reference the same file"""
161 s1 = os.fstat(fp1)
162 s2 = os.fstat(fp2)
163 return samestat(s1, s2)
166 # Are two stat buffers (obtained from stat, fstat or lstat)
167 # describing the same file?
169 def samestat(s1, s2):
170 """Test whether two stat buffers reference the same file"""
171 return s1.st_ino == s2.st_ino and \
172 s1.st_dev == s2.st_dev
175 # Is a path a mount point?
176 # (Does this work for all UNIXes? Is it even guaranteed to work by Posix?)
178 def ismount(path):
179 """Test whether a path is a mount point"""
180 try:
181 s1 = os.lstat(path)
182 s2 = os.lstat(join(path, '..'))
183 except os.error:
184 return False # It doesn't exist -- so not a mount point :-)
185 dev1 = s1.st_dev
186 dev2 = s2.st_dev
187 if dev1 != dev2:
188 return True # path/.. on a different device as path
189 ino1 = s1.st_ino
190 ino2 = s2.st_ino
191 if ino1 == ino2:
192 return True # path/.. is the same i-node as path
193 return False
196 # Directory tree walk.
197 # For each directory under top (including top itself, but excluding
198 # '.' and '..'), func(arg, dirname, filenames) is called, where
199 # dirname is the name of the directory and filenames is the list
200 # of files (and subdirectories etc.) in the directory.
201 # The func may modify the filenames list, to implement a filter,
202 # or to impose a different order of visiting.
204 def walk(top, func, arg):
205 """Directory tree walk with callback function.
207 For each directory in the directory tree rooted at top (including top
208 itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
209 dirname is the name of the directory, and fnames a list of the names of
210 the files and subdirectories in dirname (excluding '.' and '..'). func
211 may modify the fnames list in-place (e.g. via del or slice assignment),
212 and walk will only recurse into the subdirectories whose names remain in
213 fnames; this can be used to implement a filter, or to impose a specific
214 order of visiting. No semantics are defined for, or required of, arg,
215 beyond that arg is always passed to func. It can be used, e.g., to pass
216 a filename pattern, or a mutable object designed to accumulate
217 statistics. Passing None for arg is common."""
219 try:
220 names = os.listdir(top)
221 except os.error:
222 return
223 func(arg, top, names)
224 for name in names:
225 name = join(top, name)
226 try:
227 st = os.lstat(name)
228 except os.error:
229 continue
230 if stat.S_ISDIR(st.st_mode):
231 walk(name, func, arg)
234 # Expand paths beginning with '~' or '~user'.
235 # '~' means $HOME; '~user' means that user's home directory.
236 # If the path doesn't begin with '~', or if the user or $HOME is unknown,
237 # the path is returned unchanged (leaving error reporting to whatever
238 # function is called with the expanded path as argument).
239 # See also module 'glob' for expansion of *, ? and [...] in pathnames.
240 # (A function should also be defined to do full *sh-style environment
241 # variable expansion.)
243 def expanduser(path):
244 """Expand ~ and ~user constructions. If user or $HOME is unknown,
245 do nothing."""
246 if not path.startswith('~'):
247 return path
248 i = path.find('/', 1)
249 if i < 0:
250 i = len(path)
251 if i == 1:
252 if 'HOME' not in os.environ:
253 import pwd
254 userhome = pwd.getpwuid(os.getuid()).pw_dir
255 else:
256 userhome = os.environ['HOME']
257 else:
258 import pwd
259 try:
260 pwent = pwd.getpwnam(path[1:i])
261 except KeyError:
262 return path
263 userhome = pwent.pw_dir
264 userhome = userhome.rstrip('/')
265 return userhome + path[i:]
268 # Expand paths containing shell variable substitutions.
269 # This expands the forms $variable and ${variable} only.
270 # Non-existent variables are left unchanged.
272 _varprog = None
274 def expandvars(path):
275 """Expand shell variables of form $var and ${var}. Unknown variables
276 are left unchanged."""
277 global _varprog
278 if '$' not in path:
279 return path
280 if not _varprog:
281 import re
282 _varprog = re.compile(r'\$(\w+|\{[^}]*\})')
283 i = 0
284 while True:
285 m = _varprog.search(path, i)
286 if not m:
287 break
288 i, j = m.span(0)
289 name = m.group(1)
290 if name.startswith('{') and name.endswith('}'):
291 name = name[1:-1]
292 if name in os.environ:
293 tail = path[j:]
294 path = path[:i] + os.environ[name]
295 i = len(path)
296 path += tail
297 else:
298 i = j
299 return path
302 # Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
303 # It should be understood that this may change the meaning of the path
304 # if it contains symbolic links!
306 def normpath(path):
307 """Normalize path, eliminating double slashes, etc."""
308 if path == '':
309 return '.'
310 initial_slashes = path.startswith('/')
311 # POSIX allows one or two initial slashes, but treats three or more
312 # as single slash.
313 if (initial_slashes and
314 path.startswith('//') and not path.startswith('///')):
315 initial_slashes = 2
316 comps = path.split('/')
317 new_comps = []
318 for comp in comps:
319 if comp in ('', '.'):
320 continue
321 if (comp != '..' or (not initial_slashes and not new_comps) or
322 (new_comps and new_comps[-1] == '..')):
323 new_comps.append(comp)
324 elif new_comps:
325 new_comps.pop()
326 comps = new_comps
327 path = '/'.join(comps)
328 if initial_slashes:
329 path = '/'*initial_slashes + path
330 return path or '.'
333 def abspath(path):
334 """Return an absolute path."""
335 if not isabs(path):
336 path = join(os.getcwd(), path)
337 return normpath(path)
340 # Return a canonical path (i.e. the absolute location of a file on the
341 # filesystem).
343 def realpath(filename):
344 """Return the canonical path of the specified filename, eliminating any
345 symbolic links encountered in the path."""
346 if isabs(filename):
347 bits = ['/'] + filename.split('/')[1:]
348 else:
349 bits = [''] + filename.split('/')
351 for i in range(2, len(bits)+1):
352 component = join(*bits[0:i])
353 # Resolve symbolic links.
354 if islink(component):
355 resolved = _resolve_link(component)
356 if resolved is None:
357 # Infinite loop -- return original component + rest of the path
358 return abspath(join(*([component] + bits[i:])))
359 else:
360 newpath = join(*([resolved] + bits[i:]))
361 return realpath(newpath)
363 return abspath(filename)
366 def _resolve_link(path):
367 """Internal helper function. Takes a path and follows symlinks
368 until we either arrive at something that isn't a symlink, or
369 encounter a path we've seen before (meaning that there's a loop).
371 paths_seen = []
372 while islink(path):
373 if path in paths_seen:
374 # Already seen this path, so we must have a symlink loop
375 return None
376 paths_seen.append(path)
377 # Resolve where the link points to
378 resolved = os.readlink(path)
379 if not isabs(resolved):
380 dir = dirname(path)
381 path = normpath(join(dir, resolved))
382 else:
383 path = normpath(resolved)
384 return path
386 supports_unicode_filenames = False
388 def relpath(path, start=curdir):
389 """Return a relative version of a path"""
391 if not path:
392 raise ValueError("no path specified")
394 start_list = abspath(start).split(sep)
395 path_list = abspath(path).split(sep)
397 # Work out how much of the filepath is shared by start and path.
398 i = len(commonprefix([start_list, path_list]))
400 rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
401 if not rel_list:
402 return curdir
403 return join(*rel_list)