]> granicus.if.org Git - python/commitdiff
SF patch #461781 by Chris Lawrence: os.path.realpath - Resolve symlinks:
authorGuido van Rossum <guido@python.org>
Mon, 17 Sep 2001 15:16:09 +0000 (15:16 +0000)
committerGuido van Rossum <guido@python.org>
Mon, 17 Sep 2001 15:16:09 +0000 (15:16 +0000)
   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'

Doc/lib/libposixpath.tex
Lib/dospath.py
Lib/macpath.py
Lib/ntpath.py
Lib/plat-riscos/riscospath.py
Lib/posixpath.py

index 658b4eaa0c077a3316e9faa2219c5ad01e4f5153..9cd1a58fd73c641b5ff3390910fc701910c25d72 100644 (file)
@@ -137,6 +137,13 @@ case (use \function{normcase()} for that).  On Windows, it converts
 forward slashes to backward slashes.
 \end{funcdesc}
 
+\begin{funcdesc}{realpath}{path}
+Return the canonical path of the specified filename, eliminating any
+symbolic links encountered in the path.
+Availability:  \UNIX{}.
+\versionadded{2.2}
+\end{funcdesc}
+
 \begin{funcdesc}{samefile}{path1, path2}
 Return true if both pathname arguments refer to the same file or
 directory (as indicated by device number and i-node number).
index 958f9f69b0f776e2cc9e684a509646bb54cda21d..5f607057a35b0f7d505531fce07aa465a256a12f 100644 (file)
@@ -330,3 +330,6 @@ def abspath(path):
     if not isabs(path):
         path = join(os.getcwd(), path)
     return normpath(path)
+
+# realpath is a no-op on systems without islink support
+realpath = abspath
index 46ae76488e8608333d9b27e00f3954ecc34e431b..1ef35ef11bdd042efac6a03dfb3b4ef4cf1e5831 100644 (file)
@@ -225,3 +225,6 @@ def abspath(path):
     if not isabs(path):
         path = join(os.getcwd(), path)
     return normpath(path)
+
+# realpath is a no-op on systems without islink support
+realpath = abspath
index d55cc7c44f3ec693cce49046d66bc596888ce262..a1baf83d2c1c3a73266cfdb4e7583a8f65d0bca3 100644 (file)
@@ -451,3 +451,6 @@ def abspath(path):
     else:
         path = os.getcwd()
     return normpath(path)
+
+# realpath is a no-op on systems without islink support
+realpath = abspath
index 32f39ba72963bd850bed3317db215e98ef0569e8..8eda834d564894838e5d901bac80d13e1c5f2703 100644 (file)
@@ -315,6 +315,10 @@ def abspath(p):
     return normpath(join(os.getcwd(), p))
 
 
+# realpath is a no-op on systems without islink support
+realpath = abspath
+
+
 # Normalize a path. Only special path element under RISC OS is "^" for "..".
 
 def normpath(p):
index 6bf40f8336e56488739b0501fa08a009ef64bce0..0f6b6a76dbc1cb4184fac1ef3215f9e484007e83 100644 (file)
@@ -379,3 +379,24 @@ def abspath(path):
     if not isabs(path):
         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