166
166
# importing directly from posixpath allows us to test this
167
167
# on non-posix platforms
168
from posixpath import normpath
169
return 'file://' + escape(normpath(bzrlib.osutils._posix_abspath(path)))
168
return 'file://' + escape(_posix_normpath(
169
bzrlib.osutils._posix_abspath(path)))
172
172
def _win32_local_path_from_url(url):
173
"""Convert a url like file:///C|/path/to/foo into C:/path/to/foo"""
173
"""Convert a url like file:///C:/path/to/foo into C:/path/to/foo"""
174
174
if not url.startswith('file:///'):
175
175
raise errors.InvalidURL(url, 'local urls must start with file:///')
176
176
# We strip off all 3 slashes
177
177
win32_url = url[len('file:///'):]
178
if (win32_url[0] not in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
178
if (win32_url[0] not in ('abcdefghijklmnopqrstuvwxyz'
179
'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
179
180
or win32_url[1] not in '|:'
180
181
or win32_url[2] != '/'):
181
raise errors.InvalidURL(url, 'Win32 file urls start with file:///X|/, where X is a valid drive letter')
182
# TODO: jam 20060426, we could .upper() or .lower() the drive letter
183
# for better consistency.
182
raise errors.InvalidURL(url, 'Win32 file urls start with'
183
' file:///x:/, where x is a valid drive letter')
184
184
return win32_url[0].upper() + u':' + unescape(win32_url[2:])
187
187
def _win32_local_path_to_url(path):
188
"""Convert a local path like ./foo into a URL like file:///C|/path/to/foo
188
"""Convert a local path like ./foo into a URL like file:///C:/path/to/foo
190
190
This also handles transforming escaping unicode characters, etc.
192
192
# importing directly from ntpath allows us to test this
193
# on non-win32 platforms
193
# on non-win32 platform
194
# FIXME: It turns out that on nt, ntpath.abspath uses nt._getfullpathname
195
# which actually strips trailing space characters.
196
# The worst part is that under linux ntpath.abspath has different
197
# semantics, since 'nt' is not an available module.
194
198
win32_path = bzrlib.osutils._nt_normpath(
195
199
bzrlib.osutils._win32_abspath(path)).replace('\\', '/')
196
200
return 'file:///' + win32_path[0].upper() + ':' + escape(win32_path[2:])
199
203
local_path_to_url = _posix_local_path_to_url
200
204
local_path_from_url = _posix_local_path_from_url
201
205
MIN_ABS_FILEURL_LENGTH = len('file:///')
202
WIN32_MIN_ABS_FILEURL_LENGTH = len('file:///C|/')
206
WIN32_MIN_ABS_FILEURL_LENGTH = len('file:///C:/')
204
208
if sys.platform == 'win32':
205
209
local_path_to_url = _win32_local_path_to_url
376
380
file:///foo/ => file:///foo
377
381
# This is unique on win32 platforms, and is the only URL
378
382
# format which does it differently.
379
file:///C|/ => file:///C|/
383
file:///c|/ => file:///c:/
381
385
if not url.endswith('/'):