From bb753c4f0bc7fe463e7939a1f2685504a9a0f883 Mon Sep 17 00:00:00 2001 From: Chris Larson Date: Wed, 14 Apr 2010 17:59:49 -0700 Subject: Initial move of common python bits into modules of the 'oe' python package Signed-off-by: Chris Larson --- lib/oe/path.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 lib/oe/path.py (limited to 'lib/oe/path.py') 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 -- cgit 1.2.3-korg From 38cf80fce6aff11422770d84f21113f38c8dc57a Mon Sep 17 00:00:00 2001 From: Chris Larson Date: Wed, 14 Apr 2010 18:04:55 -0700 Subject: oe.path.join: simplify a bit Signed-off-by: Chris Larson --- lib/oe/path.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'lib/oe/path.py') diff --git a/lib/oe/path.py b/lib/oe/path.py index dbaa08d856..7dafdb173e 100644 --- a/lib/oe/path.py +++ b/lib/oe/path.py @@ -1,12 +1,7 @@ -def join(a, *p): +def join(*paths): """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 + import os.path + return os.path.normpath("/".join(paths)) def relative(src, dest): """ Return a relative path from src to dest. -- cgit 1.2.3-korg From 707bbb0667edfc9df15863286a02f64adfb5544d Mon Sep 17 00:00:00 2001 From: Chris Larson Date: Wed, 14 Apr 2010 18:25:58 -0700 Subject: oe.path.relative: leverage os.path.relpath if available Signed-off-by: Chris Larson --- lib/oe/path.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'lib/oe/path.py') diff --git a/lib/oe/path.py b/lib/oe/path.py index 7dafdb173e..48c4b9b633 100644 --- a/lib/oe/path.py +++ b/lib/oe/path.py @@ -15,22 +15,25 @@ def relative(src, dest): >>> relative("/tmp", "/tmp/foo/bar") foo/bar """ - from os.path import sep, pardir, normpath, commonprefix + import os.path - destlist = normpath(dest).split(sep) - srclist = normpath(src).split(sep) + 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 = commonprefix([destlist, srclist]) - commonlen = len(common) + # Find common section of the path + common = os.path.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:] + # 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) + return sep.join(relpath) def format_display(path, metadata): """ Prepare a path for display to the user. """ -- cgit 1.2.3-korg From 5c923fd35c369bae929fc0e110121abeaffab493 Mon Sep 17 00:00:00 2001 From: Chris Larson Date: Sun, 18 Apr 2010 20:02:21 -0700 Subject: oe.path.relative: switch to a different appraoch Signed-off-by: Chris Larson --- lib/oe/path.py | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) (limited to 'lib/oe/path.py') diff --git a/lib/oe/path.py b/lib/oe/path.py index 48c4b9b633..8433ee11f5 100644 --- a/lib/oe/path.py +++ b/lib/oe/path.py @@ -1,10 +1,12 @@ def join(*paths): """Like os.path.join but doesn't treat absolute RHS specially""" - import os.path - return os.path.normpath("/".join(paths)) + from os import sep + from os.path import normpath -def relative(src, dest): - """ Return a relative path from src to dest. + return normpath(sep.join(paths)) + +def relative(src, dest=None): + """ Return a relative path from src to dest(default=cwd). >>> relative("/usr/bin", "/tmp/foo/bar") ../../tmp/foo/bar @@ -15,25 +17,20 @@ def relative(src, dest): >>> relative("/tmp", "/tmp/foo/bar") foo/bar """ - import os.path + if dest is None: + dest = getcwd() 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 = [ pardir ] * (len(srclist) - commonlen) - if commonlen < len(destlist): - # Add remaining portion - relpath += destlist[commonlen:] - - return sep.join(relpath) + from os import getcwd, sep + from os.path import abspath, normpath + + srclist = abspath(src).split(sep) + destlist = abspath(dest).split(sep) + loc = [spath == dpath for spath, dpath in zip(srclist, destlist)].index(False) + rellist = ([ ".." ] * (len(srclist) - loc)) + destlist[loc:] + return sep.join(rellist) def format_display(path, metadata): """ Prepare a path for display to the user. """ -- cgit 1.2.3-korg From 22f3b74fec790847c3e353aad84b51252637a90f Mon Sep 17 00:00:00 2001 From: Chris Larson Date: Sat, 24 Apr 2010 14:14:22 -0700 Subject: Revert "oe.path.relative: switch to a different appraoch" Drop this for now, seems to cause issues with python 2.5. This reverts commit 5c923fd35c369bae929fc0e110121abeaffab493. --- lib/oe/path.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) (limited to 'lib/oe/path.py') diff --git a/lib/oe/path.py b/lib/oe/path.py index 8433ee11f5..48c4b9b633 100644 --- a/lib/oe/path.py +++ b/lib/oe/path.py @@ -1,12 +1,10 @@ def join(*paths): """Like os.path.join but doesn't treat absolute RHS specially""" - from os import sep - from os.path import normpath + import os.path + return os.path.normpath("/".join(paths)) - return normpath(sep.join(paths)) - -def relative(src, dest=None): - """ Return a relative path from src to dest(default=cwd). +def relative(src, dest): + """ Return a relative path from src to dest. >>> relative("/usr/bin", "/tmp/foo/bar") ../../tmp/foo/bar @@ -17,20 +15,25 @@ def relative(src, dest=None): >>> relative("/tmp", "/tmp/foo/bar") foo/bar """ - if dest is None: - dest = getcwd() + import os.path if hasattr(os.path, "relpath"): return os.path.relpath(dest, src) else: - from os import getcwd, sep - from os.path import abspath, normpath - - srclist = abspath(src).split(sep) - destlist = abspath(dest).split(sep) - loc = [spath == dpath for spath, dpath in zip(srclist, destlist)].index(False) - rellist = ([ ".." ] * (len(srclist) - loc)) + destlist[loc:] - return sep.join(rellist) + 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 = [ 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. """ -- cgit 1.2.3-korg From dd7c2be839a8db7d4afaab6e700b4f81c24fb489 Mon Sep 17 00:00:00 2001 From: Chris Larson Date: Sat, 24 Apr 2010 16:51:11 -0700 Subject: oe.path.relative: add missing imports Signed-off-by: Chris Larson --- lib/oe/path.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'lib/oe/path.py') diff --git a/lib/oe/path.py b/lib/oe/path.py index 48c4b9b633..8902951581 100644 --- a/lib/oe/path.py +++ b/lib/oe/path.py @@ -28,12 +28,12 @@ def relative(src, dest): commonlen = len(common) # Climb back to the point where they differentiate - relpath = [ pardir ] * (len(srclist) - commonlen) + relpath = [ os.path.pardir ] * (len(srclist) - commonlen) if commonlen < len(destlist): # Add remaining portion relpath += destlist[commonlen:] - return sep.join(relpath) + return os.path.sep.join(relpath) def format_display(path, metadata): """ Prepare a path for display to the user. """ -- cgit 1.2.3-korg