~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/urlutils.py

Merge bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
116
116
    if m:
117
117
        scheme = m.group('scheme')
118
118
        path = m.group('path').split('/')
 
119
        if path[-1:] == ['']:
 
120
            # Strip off a trailing slash
 
121
            # This helps both when we are at the root, and when
 
122
            # 'base' has an extra slash at the end
 
123
            path = path[:-1]
119
124
    else:
120
125
        path = base.split('/')
121
126
 
194
199
local_path_to_url = _posix_local_path_to_url
195
200
local_path_from_url = _posix_local_path_from_url
196
201
MIN_ABS_FILEURL_LENGTH = len('file:///')
 
202
WIN32_MIN_ABS_FILEURL_LENGTH = len('file:///C|/')
197
203
 
198
204
if sys.platform == 'win32':
199
205
    local_path_to_url = _win32_local_path_to_url
200
206
    local_path_from_url = _win32_local_path_from_url
201
207
 
202
 
    MIN_ABS_FILEURL_LENGTH = len('file:///C|/')
 
208
    MIN_ABS_FILEURL_LENGTH = WIN32_MIN_ABS_FILEURL_LENGTH
203
209
 
204
210
 
205
211
_url_scheme_re = re.compile(r'^(?P<scheme>[^:/]{2,})://(?P<path>.*)$')
291
297
    return "/".join(output_sections) or "."
292
298
 
293
299
 
 
300
def _win32_extract_drive_letter(url_base, path):
 
301
    """On win32 the drive letter needs to be added to the url base."""
 
302
    # Strip off the drive letter
 
303
    # path is currently /C:/foo
 
304
    if len(path) < 3 or path[2] not in ':|' or path[3] != '/':
 
305
        raise errors.InvalidURL(url_base + path, 
 
306
            'win32 file:/// paths need a drive letter')
 
307
    url_base += path[0:3] # file:// + /C:
 
308
    path = path[3:] # /foo
 
309
    return url_base, path
 
310
 
 
311
 
294
312
def split(url, exclude_trailing_slash=True):
295
313
    """Split a URL into its parent directory and a child directory.
296
314
 
320
338
 
321
339
    if sys.platform == 'win32' and url.startswith('file:///'):
322
340
        # Strip off the drive letter
 
341
        # url_base is currently file://
323
342
        # path is currently /C:/foo
324
 
        if path[2:3] not in ':|' or path[3:4] not in '\\/':
325
 
            raise errors.InvalidURL(url, 
326
 
                'win32 file:/// paths need a drive letter')
327
 
        url_base += path[0:3] # file:// + /C:
328
 
        path = path[3:] # /foo
 
343
        url_base, path = _win32_extract_drive_letter(url_base, path)
 
344
        # now it should be file:///C: and /foo
329
345
 
330
346
    if exclude_trailing_slash and len(path) > 1 and path.endswith('/'):
331
347
        path = path[:-1]
333
349
    return url_base + head, tail
334
350
 
335
351
 
 
352
def _win32_strip_local_trailing_slash(url):
 
353
    """Strip slashes after the drive letter"""
 
354
    if len(url) > WIN32_MIN_ABS_FILEURL_LENGTH:
 
355
        return url[:-1]
 
356
    else:
 
357
        return url
 
358
 
 
359
 
336
360
def strip_trailing_slash(url):
337
361
    """Strip trailing slash, except for root paths.
338
362
 
358
382
        # Nothing to do
359
383
        return url
360
384
    if sys.platform == 'win32' and url.startswith('file:///'):
361
 
        # This gets handled specially, because the 'top-level'
362
 
        # of a win32 path is actually the drive letter
363
 
        if len(url) > MIN_ABS_FILEURL_LENGTH:
364
 
            return url[:-1]
365
 
        else:
366
 
            return url
 
385
        return _win32_strip_local_trailing_slash(url)
367
386
 
368
387
    scheme_loc, first_path_slash = _find_scheme_and_separator(url)
369
388
    if scheme_loc is None: