Skip to content
This repository has been archived by the owner on Sep 23, 2020. It is now read-only.

Commit

Permalink
added pathutil.relpath, to support Python < 2.6
Browse files Browse the repository at this point in the history
  • Loading branch information
labisso committed Feb 25, 2010
1 parent d950d9d commit 2ee52af
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions web/src/python/nimbusweb/setup/pathutil.py
Expand Up @@ -164,4 +164,29 @@ def write_repl_file(path, outputtext, log):

log.debug("Wrote '%s'." % path)

relpath = None
try:
# os.path.relpath is in 2.6+
relpath = getattr(os.path, 'relpath')
except AttributeError:
relpath = _relpath

# from Python 2.6 standard library, slightly modified
def _relpath(path, start):
"""Return a relative version of a path"""

if not path:
raise ValueError("no path specified")
if not start:
raise ValueError("no start specified")

start_list = os.path.abspath(start).split(os.path.sep)
path_list = os.path.abspath(path).split(os.path.sep)

# Work out how much of the filepath is shared by start and path.
i = len(os.path.commonprefix([start_list, path_list]))

rel_list = [os.path.pardir] * (len(start_list)-i) + path_list[i:]
if not rel_list:
return curdir
return os.path.join(*rel_list)

0 comments on commit 2ee52af

Please sign in to comment.