Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
rdiankov
committed
Jan 6, 2012
1 parent
33a19b4
commit 7335bee
Showing
1 changed file
with
19 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
"""relpath is not present in python 2.5 and below, so hold an implementation of it. | ||
""" | ||
try: | ||
from os.path import relpath | ||
except ImportError: | ||
from posixpath import curdir, sep, pardir, join, abspath, commonprefix | ||
|
||
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) |