diff options
author | Joshua Lock <josh@linux.intel.com> | 2010-04-30 16:35:50 +0100 |
---|---|---|
committer | Joshua Lock <josh@linux.intel.com> | 2010-05-06 12:48:05 +0100 |
commit | ac023d775b651c9b1e28a7a725e72949fe54ad47 (patch) | |
tree | da69e91c7d2ce4785ddf6af6b756758d1789d5a2 /meta/lib/oe/path.py | |
parent | 14196cb03190d9dac93be309763e3076385eb831 (diff) | |
download | poky-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.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 | ||