diff options
author | Pierre-Yves David <pierre-yves.david@logilab.fr> | 2009-12-07 17:08:56 +0100 |
---|---|---|
committer | Pierre-Yves David <pierre-yves.david@logilab.fr> | 2009-12-07 17:08:56 +0100 |
commit | 498ab0f5d05b0fe3f7367870e1cce72414be2736 (patch) | |
tree | a11e422bdcd15e3c98bd48234bf15da05f9d4aa4 | |
parent | e4b4d4a0d955ebf8bf323b42f79ab29417c91a58 (diff) | |
download | logilab-common-498ab0f5d05b0fe3f7367870e1cce72414be2736.tar.gz |
Add os.path.relpath function in logical.common.compat for pre-2.6 python.
-rw-r--r-- | compat.py | 23 |
1 files changed, 23 insertions, 0 deletions
@@ -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) + + |