summaryrefslogtreecommitdiff
path: root/sphinx/util/pycompat.py
diff options
context:
space:
mode:
Diffstat (limited to 'sphinx/util/pycompat.py')
-rw-r--r--sphinx/util/pycompat.py26
1 files changed, 25 insertions, 1 deletions
diff --git a/sphinx/util/pycompat.py b/sphinx/util/pycompat.py
index 9e081b02..b373c504 100644
--- a/sphinx/util/pycompat.py
+++ b/sphinx/util/pycompat.py
@@ -5,7 +5,7 @@
Stuff for Python version compatibility.
- :copyright: Copyright 2007-2011 by the Sphinx team, see AUTHORS.
+ :copyright: Copyright 2007-2013 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
@@ -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 start
+ return join(*rel_list)
+ del curdir
+
# ------------------------------------------------------------------------------
# Missing builtins and codecs in Python < 2.5