diff options
Diffstat (limited to 'meta/lib/oe/path.py')
| -rw-r--r-- | meta/lib/oe/path.py | 44 |
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 @@ | |||
| 1 | def join(*paths): | ||
| 2 | """Like os.path.join but doesn't treat absolute RHS specially""" | ||
| 3 | import os.path | ||
| 4 | return os.path.normpath("/".join(paths)) | ||
| 5 | |||
| 6 | def relative(src, dest): | ||
| 7 | """ Return a relative path from src to dest. | ||
| 8 | |||
| 9 | >>> relative("/usr/bin", "/tmp/foo/bar") | ||
| 10 | ../../tmp/foo/bar | ||
| 11 | |||
| 12 | >>> relative("/usr/bin", "/usr/lib") | ||
| 13 | ../lib | ||
| 14 | |||
| 15 | >>> relative("/tmp", "/tmp/foo/bar") | ||
| 16 | foo/bar | ||
| 17 | """ | ||
| 18 | import os.path | ||
| 19 | |||
| 20 | if hasattr(os.path, "relpath"): | ||
| 21 | return os.path.relpath(dest, src) | ||
| 22 | else: | ||
| 23 | destlist = os.path.normpath(dest).split(os.path.sep) | ||
| 24 | srclist = os.path.normpath(src).split(os.path.sep) | ||
| 25 | |||
| 26 | # Find common section of the path | ||
| 27 | common = os.path.commonprefix([destlist, srclist]) | ||
| 28 | commonlen = len(common) | ||
| 29 | |||
| 30 | # Climb back to the point where they differentiate | ||
| 31 | relpath = [ os.path.pardir ] * (len(srclist) - commonlen) | ||
| 32 | if commonlen < len(destlist): | ||
| 33 | # Add remaining portion | ||
| 34 | relpath += destlist[commonlen:] | ||
| 35 | |||
| 36 | return os.path.sep.join(relpath) | ||
| 37 | |||
| 38 | def format_display(path, metadata): | ||
| 39 | """ Prepare a path for display to the user. """ | ||
| 40 | rel = relative(metadata.getVar("TOPDIR", 1), path) | ||
| 41 | if len(rel) > len(path): | ||
| 42 | return path | ||
| 43 | else: | ||
| 44 | return rel | ||
