Mercurial > ~astiob > upreckon > hgweb
comparison 2.00/compat.py @ 34:8fec38b0dd6e
A os.path.relpath implementation for Python 2.5
| author | Oleg Oshmyan <chortos@inbox.lv> |
|---|---|
| date | Tue, 30 Nov 2010 00:18:17 +0000 |
| parents | fe1463e7e24d |
| children | 23aa8da5be5f |
comparison
equal
deleted
inserted
replaced
| 33:f90bd2d1a12b | 34:8fec38b0dd6e |
|---|---|
| 62 if file is None: file = sys.stdout | 62 if file is None: file = sys.stdout |
| 63 if not isinstance(sep, basestring): raise saytypeerror(sep, 'sep') | 63 if not isinstance(sep, basestring): raise saytypeerror(sep, 'sep') |
| 64 if not isinstance(end, basestring): raise saytypeerror(end, 'end') | 64 if not isinstance(end, basestring): raise saytypeerror(end, 'end') |
| 65 file.write(sep.join(map(str, values)) + end) | 65 file.write(sep.join(map(str, values)) + end) |
| 66 | 66 |
| 67 try: | |
| 68 from os.path import relpath | |
| 69 except ImportError: | |
| 70 # Python 2.5 | |
| 71 import os.path as _path | |
| 72 | |
| 73 # Adapted from Python 2.7.1 | |
| 74 | |
| 75 if hasattr(_path, 'splitunc'): | |
| 76 def _abspath_split(path): | |
| 77 abs = _path.abspath(_path.normpath(path)) | |
| 78 prefix, rest = _path.splitunc(abs) | |
| 79 is_unc = bool(prefix) | |
| 80 if not is_unc: | |
| 81 prefix, rest = _path.splitdrive(abs) | |
| 82 return is_unc, prefix, [x for x in rest.split(_path.sep) if x] | |
| 83 else: | |
| 84 def _abspath_split(path): | |
| 85 prefix, rest = _path.splitdrive(_path.abspath(_path.normpath(path))) | |
| 86 return False, prefix, [x for x in rest.split(_path.sep) if x] | |
| 87 | |
| 88 def relpath(path, start=_path.curdir): | |
| 89 """Return a relative version of a path""" | |
| 90 | |
| 91 if not path: | |
| 92 raise ValueError("no path specified") | |
| 93 | |
| 94 start_is_unc, start_prefix, start_list = _abspath_split(start) | |
| 95 path_is_unc, path_prefix, path_list = _abspath_split(path) | |
| 96 | |
| 97 if path_is_unc ^ start_is_unc: | |
| 98 raise ValueError("Cannot mix UNC and non-UNC paths (%s and %s)" | |
| 99 % (path, start)) | |
| 100 if path_prefix.lower() != start_prefix.lower(): | |
| 101 if path_is_unc: | |
| 102 raise ValueError("path is on UNC root %s, start on UNC root %s" | |
| 103 % (path_prefix, start_prefix)) | |
| 104 else: | |
| 105 raise ValueError("path is on drive %s, start on drive %s" | |
| 106 % (path_prefix, start_prefix)) | |
| 107 # Work out how much of the filepath is shared by start and path. | |
| 108 i = 0 | |
| 109 for e1, e2 in zip(start_list, path_list): | |
| 110 if e1.lower() != e2.lower(): | |
| 111 break | |
| 112 i += 1 | |
| 113 | |
| 114 rel_list = [_path.pardir] * (len(start_list)-i) + path_list[i:] | |
| 115 if not rel_list: | |
| 116 return _path.curdir | |
| 117 return _path.join(*rel_list) | |
| 118 | |
| 119 _path.relpath = relpath | |
| 120 | |
| 67 def import_urllib(): | 121 def import_urllib(): |
| 68 try: | 122 try: |
| 69 # Python 3 | 123 # Python 3 |
| 70 import urllib.request | 124 import urllib.request |
| 71 return urllib.request, lambda url: urllib.request.urlopen(url).read().decode() | 125 return urllib.request, lambda url: urllib.request.urlopen(url).read().decode() |
