[Python-checkins] CVS: python/dist/src/Lib dospath.py,1.23,1.24 macpath.py,1.29,1.30 ntpath.py,1.41,1.42 posixpath.py,1.43,1.44

Guido van Rossum gvanrossum@users.sourceforge.net
Mon, 17 Sep 2001 08:16:11 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory usw-pr-cvs1:/tmp/cvs-serv22193/Lib

Modified Files:
	dospath.py macpath.py ntpath.py posixpath.py 
Log Message:
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:

   Once upon a time, I put together a little function 
   that tries to find the canonical filename for a given 
   pathname on POSIX. I've finally gotten around to 
   turning it into a proper patch with documentation. 
   On non-POSIX, I made it an alias for 'abspath', as 
   that's the behavior on POSIX when no symlinks are 
   encountered in the path.

   Example:
   >>> os.path.realpath('/usr/bin/X11/X')
   '/usr/X11R6/bin/X'



Index: dospath.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/dospath.py,v
retrieving revision 1.23
retrieving revision 1.24
diff -C2 -d -r1.23 -r1.24
*** dospath.py	2001/07/20 18:54:44	1.23
--- dospath.py	2001/09/17 15:16:09	1.24
***************
*** 331,332 ****
--- 331,335 ----
          path = join(os.getcwd(), path)
      return normpath(path)
+ 
+ # realpath is a no-op on systems without islink support
+ realpath = abspath

Index: macpath.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/macpath.py,v
retrieving revision 1.29
retrieving revision 1.30
diff -C2 -d -r1.29 -r1.30
*** macpath.py	2001/08/08 20:55:10	1.29
--- macpath.py	2001/09/17 15:16:09	1.30
***************
*** 226,227 ****
--- 226,230 ----
          path = join(os.getcwd(), path)
      return normpath(path)
+ 
+ # realpath is a no-op on systems without islink support
+ realpath = abspath

Index: ntpath.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/ntpath.py,v
retrieving revision 1.41
retrieving revision 1.42
diff -C2 -d -r1.41 -r1.42
*** ntpath.py	2001/08/30 22:05:26	1.41
--- ntpath.py	2001/09/17 15:16:09	1.42
***************
*** 452,453 ****
--- 452,456 ----
          path = os.getcwd()
      return normpath(path)
+ 
+ # realpath is a no-op on systems without islink support
+ realpath = abspath

Index: posixpath.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/posixpath.py,v
retrieving revision 1.43
retrieving revision 1.44
diff -C2 -d -r1.43 -r1.44
*** posixpath.py	2001/04/16 18:12:04	1.43
--- posixpath.py	2001/09/17 15:16:09	1.44
***************
*** 380,381 ****
--- 380,402 ----
          path = join(os.getcwd(), path)
      return normpath(path)
+ 
+ 
+ # Return a canonical path (i.e. the absolute location of a file on the
+ # filesystem).
+ 
+ def realpath(filename):
+     """Return the canonical path of the specified filename, eliminating any
+ symbolic links encountered in the path."""
+     filename = abspath(filename)
+ 
+     bits = ['/'] + filename.split('/')[1:]
+     for i in range(2, len(bits)+1):
+         component = join(*bits[0:i])
+         if islink(component):
+             resolved = os.readlink(component)
+             (dir, file) = split(component)
+             resolved = normpath(join(dir, resolved))
+             newpath = join(*([resolved] + bits[i:]))
+             return realpath(newpath)
+         
+     return filename