From ac023d775b651c9b1e28a7a725e72949fe54ad47 Mon Sep 17 00:00:00 2001 From: Joshua Lock Date: Fri, 30 Apr 2010 16:35:50 +0100 Subject: 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 --- meta/lib/oe/path.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 meta/lib/oe/path.py (limited to 'meta/lib/oe/path.py') 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 -- cgit 1.2.3-korg