summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPierre-Yves David <pierre-yves.david@logilab.fr>2009-12-07 17:08:56 +0100
committerPierre-Yves David <pierre-yves.david@logilab.fr>2009-12-07 17:08:56 +0100
commit498ab0f5d05b0fe3f7367870e1cce72414be2736 (patch)
treea11e422bdcd15e3c98bd48234bf15da05f9d4aa4
parente4b4d4a0d955ebf8bf323b42f79ab29417c91a58 (diff)
downloadlogilab-common-498ab0f5d05b0fe3f7367870e1cce72414be2736.tar.gz
Add os.path.relpath function in logical.common.compat for pre-2.6 python.
-rw-r--r--compat.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/compat.py b/compat.py
index ad0e63a..81ce4c7 100644
--- a/compat.py
+++ b/compat.py
@@ -286,3 +286,26 @@ except ImportError:
if retcode:
raise CalledProcessError(retcode, cmd)
return retcode
+
+try:
+ from os.path import relpath
+except ImportError: # python < 2.6
+ from os.path import curdir, abspath, sep, commonprefix, pardir, join
+ def relpath(path, start=curdir):
+ """Return a relative version of a path"""
+
+ if not path:
+ raise ValueError("no path specified")
+
+ start_list = abspath(start).split(sep)
+ path_list = abspath(path).split(sep)
+
+ # Work out how much of the filepath is shared by start and path.
+ i = len(commonprefix([start_list, path_list]))
+
+ rel_list = [pardir] * (len(start_list)-i) + path_list[i:]
+ if not rel_list:
+ return curdir
+ return join(*rel_list)
+
+