aboutsummaryrefslogtreecommitdiffstats
path: root/meta/lib/oe/path.py
diff options
context:
space:
mode:
authorJoshua Lock <josh@linux.intel.com>2010-04-30 16:35:50 +0100
committerJoshua Lock <josh@linux.intel.com>2010-05-06 12:48:05 +0100
commitac023d775b651c9b1e28a7a725e72949fe54ad47 (patch)
treeda69e91c7d2ce4785ddf6af6b756758d1789d5a2 /meta/lib/oe/path.py
parent14196cb03190d9dac93be309763e3076385eb831 (diff)
downloadopenembedded-core-ac023d775b651c9b1e28a7a725e72949fe54ad47.tar.gz
lib/oe: Import oe lib from OE.dev
This library moves the common Python methods into modules of an 'oe' Python package. Signed-off-by: Joshua Lock <josh@linux.intel.com>
Diffstat (limited to 'meta/lib/oe/path.py')
-rw-r--r--meta/lib/oe/path.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/meta/lib/oe/path.py b/meta/lib/oe/path.py
new file mode 100644
index 0000000000..8902951581
--- /dev/null
+++ b/meta/lib/oe/path.py
@@ -0,0 +1,44 @@
+def join(*paths):
+ """Like os.path.join but doesn't treat absolute RHS specially"""
+ import os.path
+ return os.path.normpath("/".join(paths))
+
+def relative(src, dest):
+ """ Return a relative path from src to dest.
+
+ >>> relative("/usr/bin", "/tmp/foo/bar")
+ ../../tmp/foo/bar
+
+ >>> relative("/usr/bin", "/usr/lib")
+ ../lib
+
+ >>> relative("/tmp", "/tmp/foo/bar")
+ foo/bar
+ """
+ import os.path
+
+ 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 = os.path.commonprefix([destlist, srclist])
+ commonlen = len(common)
+
+ # Climb back to the point where they differentiate
+ relpath = [ os.path.pardir ] * (len(srclist) - commonlen)
+ if commonlen < len(destlist):
+ # Add remaining portion
+ relpath += destlist[commonlen:]
+
+ return os.path.sep.join(relpath)
+
+def format_display(path, metadata):
+ """ Prepare a path for display to the user. """
+ rel = relative(metadata.getVar("TOPDIR", 1), path)
+ if len(rel) > len(path):
+ return path
+ else:
+ return rel