From ce74a6438d4729f8e4ae9b0a308479d325202ee2 Mon Sep 17 00:00:00 2001 From: Dane Date: Tue, 2 Nov 2010 14:44:26 -0700 Subject: [PATCH] get os.path.relpath working for py25 --- cascadenik/compile.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/cascadenik/compile.py b/cascadenik/compile.py index 5ab6365..b55989a 100644 --- a/cascadenik/compile.py +++ b/cascadenik/compile.py @@ -18,11 +18,26 @@ from urlparse import urlparse, urljoin from operator import lt, le, eq, ge, gt +# os.path.relpath was added in Python 2.6 +def _relpath(path, start=posixpath.curdir): + """Return a relative version of a path""" + if not path: + raise ValueError("no path specified") + start_list = posixpath.abspath(start).split(posixpath.sep) + path_list = posixpath.abspath(path).split(posixpath.sep) + i = len(posixpath.commonprefix([start_list, path_list])) + rel_list = [posixpath.pardir] * (len(start_list)-i) + path_list[i:] + if not rel_list: + return posixpath.curdir + return posixpath.join(*rel_list) + # timeout parameter to HTTPConnection was added in Python 2.6 if sys.hexversion >= 0x020600F0: from httplib import HTTPConnection else: + posixpath.relpath = _relpath + from httplib import HTTPConnection as _HTTPConnection import socket @@ -31,6 +46,8 @@ def HTTPConnection(host, port=None, strict=None, timeout=None): socket.setdefaulttimeout(timeout) return _HTTPConnection(host, port=port, strict=strict) + + # cascadenik import safe64 import style @@ -153,9 +170,8 @@ def output_path(self, path_name): assert (path == path_name), "path_name passed to output_path must be in posix format" if posixpath.isabs(path): - if self.output == self.cache and sys.hexversion >= 0x020600F0: + if self.output == self.cache: # worth seeing if an absolute path can be avoided - # only python 2.6 has relpath function path = posixpath.relpath(path, self.output) else: