diff options
author | Chris Larson <chris_larson@mentor.com> | 2010-04-14 17:59:49 -0700 |
---|---|---|
committer | Chris Larson <chris_larson@mentor.com> | 2010-04-23 14:20:42 -0700 |
commit | bb753c4f0bc7fe463e7939a1f2685504a9a0f883 (patch) | |
tree | 8185a9128e893b44ca124e6d8a6d02e245f0efbe /lib/oe/path.py | |
parent | 5816ec4860aba7289483b87b4aa4909eee50fae9 (diff) | |
download | openembedded-bb753c4f0bc7fe463e7939a1f2685504a9a0f883.tar.gz |
Initial move of common python bits into modules of the 'oe' python package
Signed-off-by: Chris Larson <chris_larson@mentor.com>
Diffstat (limited to 'lib/oe/path.py')
-rw-r--r-- | lib/oe/path.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/oe/path.py b/lib/oe/path.py new file mode 100644 index 0000000000..dbaa08d856 --- /dev/null +++ b/lib/oe/path.py @@ -0,0 +1,46 @@ +def join(a, *p): + """Like os.path.join but doesn't treat absolute RHS specially""" + path = a + for b in p: + if path == '' or path.endswith('/'): + path += b + else: + path += '/' + b + return path + +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 + """ + from os.path import sep, pardir, normpath, commonprefix + + destlist = normpath(dest).split(sep) + srclist = normpath(src).split(sep) + + # Find common section of the path + common = 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:] + + return 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 |