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.
184
return win32_url[0].upper() + u':' + unescape(win32_url[2:])
182
raise errors.InvalidURL(url, 'Win32 file urls start with'
183
' file:///x:/, where x is a valid drive letter')
184
# Preferentially using .lower() because os.getcwd() returns
185
# paths with lowercase drive letters, and that helps
186
# bzrlib.osutils.relpath() work correctly
187
return win32_url[0].lower() + u':' + unescape(win32_url[2:])
187
190
def _win32_local_path_to_url(path):
188
"""Convert a local path like ./foo into a URL like file:///C|/path/to/foo
191
"""Convert a local path like ./foo into a URL like file:///C:/path/to/foo
190
193
This also handles transforming escaping unicode characters, etc.
192
195
# importing directly from ntpath allows us to test this
193
# on non-win32 platforms
196
# on non-win32 platform
197
# FIXME: It turns out that on nt, ntpath.abspath uses nt._getfullpathname
198
# which actually strips trailing space characters.
199
# The worst part is that under linux ntpath.abspath has different
200
# semantics, since 'nt' is not an available module.
194
201
win32_path = bzrlib.osutils._nt_normpath(
195
202
bzrlib.osutils._win32_abspath(path)).replace('\\', '/')
196
return 'file:///' + win32_path[0].upper() + ':' + escape(win32_path[2:])
203
return 'file:///' + win32_path[0].lower() + ':' + escape(win32_path[2:])
199
206
local_path_to_url = _posix_local_path_to_url
200
207
local_path_from_url = _posix_local_path_from_url
201
208
MIN_ABS_FILEURL_LENGTH = len('file:///')
202
WIN32_MIN_ABS_FILEURL_LENGTH = len('file:///C|/')
209
WIN32_MIN_ABS_FILEURL_LENGTH = len('file:///C:/')
204
211
if sys.platform == 'win32':
205
212
local_path_to_url = _win32_local_path_to_url