diff options
author | Scott Garman <scott.a.garman@intel.com> | 2012-03-12 17:07:19 -0700 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2012-03-13 11:52:11 +0000 |
commit | 19e92dfe063968154d8744936ece19e6aca3466a (patch) | |
tree | 5f1c887beb5a36ce6019103fdb05744dc5e4d09b /meta/lib | |
parent | 5fff3ea93bf353149e1642360c78cba27de3b174 (diff) | |
download | poky-19e92dfe063968154d8744936ece19e6aca3466a.tar.gz |
path.py: add make_relative_symlink method
This method allows you to convert an absolute symlink into a
relative one.
(From OE-Core rev: 71062c1e0fb45a4b4e58ea5d217706aa2b402d88)
Signed-off-by: Scott Garman <scott.a.garman@intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/lib')
-rw-r--r-- | meta/lib/oe/path.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/meta/lib/oe/path.py b/meta/lib/oe/path.py index 683b09701c..1fdfa8724f 100644 --- a/meta/lib/oe/path.py +++ b/meta/lib/oe/path.py | |||
@@ -40,6 +40,33 @@ def relative(src, dest): | |||
40 | 40 | ||
41 | return os.path.sep.join(relpath) | 41 | return os.path.sep.join(relpath) |
42 | 42 | ||
43 | def make_relative_symlink(path): | ||
44 | """ Convert an absolute symlink to a relative one """ | ||
45 | if not os.path.islink(path): | ||
46 | return | ||
47 | link = os.readlink(path) | ||
48 | if not os.path.isabs(link): | ||
49 | return | ||
50 | |||
51 | # find the common ancestor directory | ||
52 | ancestor = path | ||
53 | depth = 0 | ||
54 | while ancestor and not link.startswith(ancestor): | ||
55 | ancestor = ancestor.rpartition('/')[0] | ||
56 | depth += 1 | ||
57 | |||
58 | if not ancestor: | ||
59 | print "make_relative_symlink() Error: unable to find the common ancestor of %s and its target" % path | ||
60 | return | ||
61 | |||
62 | base = link.partition(ancestor)[2].strip('/') | ||
63 | while depth > 1: | ||
64 | base = "../" + base | ||
65 | depth -= 1 | ||
66 | |||
67 | os.remove(path) | ||
68 | os.symlink(base, path) | ||
69 | |||
43 | def format_display(path, metadata): | 70 | def format_display(path, metadata): |
44 | """ Prepare a path for display to the user. """ | 71 | """ Prepare a path for display to the user. """ |
45 | rel = relative(metadata.getVar("TOPDIR", True), path) | 72 | rel = relative(metadata.getVar("TOPDIR", True), path) |