diff options
Diffstat (limited to 'sphinx/util/pycompat.py')
-rw-r--r-- | sphinx/util/pycompat.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/sphinx/util/pycompat.py b/sphinx/util/pycompat.py index 9e081b02..c2c6fe8b 100644 --- a/sphinx/util/pycompat.py +++ b/sphinx/util/pycompat.py @@ -82,6 +82,10 @@ if sys.version_info >= (2, 6): except ImportError: from itertools import izip_longest as zip_longest + import os + relpath = os.path.relpath + del os + else: # Python < 2.6 from itertools import izip, repeat, chain @@ -114,6 +118,26 @@ else: except IndexError: pass + from os.path import curdir + def relpath(path, start=curdir): + """Return a relative version of a path""" + from os.path import sep, abspath, commonprefix, join, pardir + + 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) + del curdir + # ------------------------------------------------------------------------------ # Missing builtins and codecs in Python < 2.5 |