oe.path.relative: leverage os.path.relpath if available
authorChris Larson <chris_larson@mentor.com>
Thu, 15 Apr 2010 01:25:58 +0000 (18:25 -0700)
committerChris Larson <chris_larson@mentor.com>
Fri, 23 Apr 2010 21:20:42 +0000 (14:20 -0700)
Signed-off-by: Chris Larson <chris_larson@mentor.com>
lib/oe/path.py

index 7dafdb1..48c4b9b 100644 (file)
@@ -15,22 +15,25 @@ def relative(src, dest):
     >>> relative("/tmp", "/tmp/foo/bar")
     foo/bar
     """
-    from os.path import sep, pardir, normpath, commonprefix
+    import os.path
 
-    destlist = normpath(dest).split(sep)
-    srclist = normpath(src).split(sep)
+    if hasattr(os.path, "relpath"):
+        return os.path.relpath(dest, src)
+    else:
+        destlist = os.path.normpath(dest).split(os.path.sep)
+        srclist = os.path.normpath(src).split(os.path.sep)
 
-    # Find common section of the path
-    common = commonprefix([destlist, srclist])
-    commonlen = len(common)
+        # Find common section of the path
+        common = os.path.commonprefix([destlist, srclist])
+        commonlen = len(common)
 
-    # Climb back to the point where they differentiate
-    relpath = [ pardir ] * (len(srclist) - commonlen)
-    if commonlen < len(destlist):
-        # Add remaining portion
-        relpath += destlist[commonlen:]
+        # Climb back to the point where they differentiate
+        relpath = [ pardir ] * (len(srclist) - commonlen)
+        if commonlen < len(destlist):
+            # Add remaining portion
+            relpath += destlist[commonlen:]
 
-    return sep.join(relpath)
+        return sep.join(relpath)
 
 def format_display(path, metadata):
     """ Prepare a path for display to the user. """