~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/urlutils.py

  • Committer: Wouter van Heyst
  • Date: 2006-06-06 22:37:30 UTC
  • mto: This revision was merged to the branch mainline in revision 1752.
  • Revision ID: larstiq@larstiq.dyndns.org-20060606223730-a308c5429fc6c617
change branch.{get,set}_parent to store a relative path but return full urls

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Bazaar -- distributed version control
 
1
# Bazaar-NG -- distributed version control
2
2
#
3
 
# Copyright (C) 2006 Canonical Ltd
 
3
# Copyright (C) 2006 by Canonical Ltd
4
4
#
5
5
# This program is free software; you can redistribute it and/or modify
6
6
# it under the terms of the GNU General Public License as published by
19
19
"""A collection of function for handling URL operations."""
20
20
 
21
21
import os
 
22
from posixpath import split as _posix_split
22
23
import re
23
24
import sys
24
 
 
25
 
from bzrlib.lazy_import import lazy_import
26
 
lazy_import(globals(), """
27
 
from posixpath import split as _posix_split, normpath as _posix_normpath
28
25
import urllib
29
 
import urlparse
30
26
 
31
 
from bzrlib import (
32
 
    errors,
33
 
    osutils,
34
 
    )
35
 
""")
 
27
import bzrlib.errors as errors
 
28
import bzrlib.osutils
36
29
 
37
30
 
38
31
def basename(url, exclude_trailing_slash=True):
76
69
    
77
70
    This assumes that both paths are already fully specified file:// URLs.
78
71
    """
79
 
    if len(base) < MIN_ABS_FILEURL_LENGTH:
80
 
        raise ValueError('Length of base must be equal or'
81
 
            ' exceed the platform minimum url length (which is %d)' %
82
 
            MIN_ABS_FILEURL_LENGTH)
 
72
    assert len(base) >= MIN_ABS_FILEURL_LENGTH, ('Length of base must be equal or'
 
73
        ' exceed the platform minimum url length (which is %d)' % 
 
74
        MIN_ABS_FILEURL_LENGTH)
 
75
 
83
76
    base = local_path_from_url(base)
84
77
    path = local_path_from_url(path)
85
 
    return escape(osutils.relpath(base, path))
 
78
    return escape(bzrlib.osutils.relpath(base, path))
86
79
 
87
80
 
88
81
def _find_scheme_and_separator(url):
118
111
        join('http://foo', 'bar') => 'http://foo/bar'
119
112
        join('http://foo', 'bar', '../baz') => 'http://foo/baz'
120
113
    """
121
 
    if not args:
122
 
        return base
123
 
    match = _url_scheme_re.match(base)
 
114
    m = _url_scheme_re.match(base)
124
115
    scheme = None
125
 
    if match:
126
 
        scheme = match.group('scheme')
127
 
        path = match.group('path').split('/')
128
 
        if path[-1:] == ['']:
129
 
            # Strip off a trailing slash
130
 
            # This helps both when we are at the root, and when
131
 
            # 'base' has an extra slash at the end
132
 
            path = path[:-1]
 
116
    if m:
 
117
        scheme = m.group('scheme')
 
118
        path = m.group('path').split('/')
133
119
    else:
134
120
        path = base.split('/')
135
121
 
136
 
    if scheme is not None and len(path) >= 1:
137
 
        host = path[:1]
138
 
        # the path should be represented as an abs path.
139
 
        # we know this must be absolute because of the presence of a URL scheme.
140
 
        remove_root = True
141
 
        path = [''] + path[1:]
142
 
    else:
143
 
        # create an empty host, but dont alter the path - this might be a
144
 
        # relative url fragment.
145
 
        host = []
146
 
        remove_root = False
147
 
 
148
122
    for arg in args:
149
 
        match = _url_scheme_re.match(arg)
150
 
        if match:
 
123
        m = _url_scheme_re.match(arg)
 
124
        if m:
151
125
            # Absolute URL
152
 
            scheme = match.group('scheme')
153
 
            # this skips .. normalisation, making http://host/../../..
154
 
            # be rather strange.
155
 
            path = match.group('path').split('/')
156
 
            # set the host and path according to new absolute URL, discarding
157
 
            # any previous values.
158
 
            # XXX: duplicates mess from earlier in this function.  This URL
159
 
            # manipulation code needs some cleaning up.
160
 
            if scheme is not None and len(path) >= 1:
161
 
                host = path[:1]
162
 
                path = path[1:]
163
 
                # url scheme implies absolute path.
164
 
                path = [''] + path
165
 
            else:
166
 
                # no url scheme we take the path as is.
167
 
                host = []
 
126
            scheme = m.group('scheme')
 
127
            path = m.group('path').split('/')
168
128
        else:
169
 
            path = '/'.join(path)
170
 
            path = joinpath(path, arg)
171
 
            path = path.split('/')
172
 
    if remove_root and path[0:1] == ['']:
173
 
        del path[0]
174
 
    if host:
175
 
        # Remove the leading slash from the path, so long as it isn't also the
176
 
        # trailing slash, which we want to keep if present.
177
 
        if path and path[0] == '' and len(path) > 1:
178
 
            del path[0]
179
 
        path = host + path
180
 
 
 
129
            for chunk in arg.split('/'):
 
130
                if chunk == '.':
 
131
                    continue
 
132
                elif chunk == '..':
 
133
                    if len(path) >= 2:
 
134
                        # Don't pop off the host portion
 
135
                        path.pop()
 
136
                    else:
 
137
                        raise errors.InvalidURLJoin('Cannot go above root',
 
138
                                base, args)
 
139
                else:
 
140
                    path.append(chunk)
181
141
    if scheme is None:
182
142
        return '/'.join(path)
183
143
    return scheme + '://' + '/'.join(path)
184
144
 
185
145
 
186
 
def joinpath(base, *args):
187
 
    """Join URL path segments to a URL path segment.
188
 
    
189
 
    This is somewhat like osutils.joinpath, but intended for URLs.
190
 
 
191
 
    XXX: this duplicates some normalisation logic, and also duplicates a lot of
192
 
    path handling logic that already exists in some Transport implementations.
193
 
    We really should try to have exactly one place in the code base responsible
194
 
    for combining paths of URLs.
195
 
    """
196
 
    path = base.split('/')
197
 
    if len(path) > 1 and path[-1] == '':
198
 
        #If the path ends in a trailing /, remove it.
199
 
        path.pop()
200
 
    for arg in args:
201
 
        if arg.startswith('/'):
202
 
            path = []
203
 
        for chunk in arg.split('/'):
204
 
            if chunk == '.':
205
 
                continue
206
 
            elif chunk == '..':
207
 
                if path == ['']:
208
 
                    raise errors.InvalidURLJoin('Cannot go above root',
209
 
                            base, args)
210
 
                path.pop()
211
 
            else:
212
 
                path.append(chunk)
213
 
    if path == ['']:
214
 
        return '/'
215
 
    else:
216
 
        return '/'.join(path)
217
 
 
218
 
 
219
146
# jam 20060502 Sorted to 'l' because the final target is 'local_path_from_url'
220
147
def _posix_local_path_from_url(url):
221
148
    """Convert a url like file:///path/to/foo into /path/to/foo"""
232
159
    """
233
160
    # importing directly from posixpath allows us to test this 
234
161
    # on non-posix platforms
235
 
    return 'file://' + escape(_posix_normpath(
236
 
        osutils._posix_abspath(path)))
 
162
    from posixpath import normpath
 
163
    return 'file://' + escape(normpath(bzrlib.osutils._posix_abspath(path)))
237
164
 
238
165
 
239
166
def _win32_local_path_from_url(url):
240
 
    """Convert a url like file:///C:/path/to/foo into C:/path/to/foo"""
241
 
    if not url.startswith('file://'):
242
 
        raise errors.InvalidURL(url, 'local urls must start with file:///, '
243
 
                                     'UNC path urls must start with file://')
 
167
    """Convert a url like file:///C|/path/to/foo into C:/path/to/foo"""
 
168
    if not url.startswith('file:///'):
 
169
        raise errors.InvalidURL(url, 'local urls must start with file:///')
244
170
    # We strip off all 3 slashes
245
 
    win32_url = url[len('file:'):]
246
 
    # check for UNC path: //HOST/path
247
 
    if not win32_url.startswith('///'):
248
 
        if (win32_url[2] == '/'
249
 
            or win32_url[3] in '|:'):
250
 
            raise errors.InvalidURL(url, 'Win32 UNC path urls'
251
 
                ' have form file://HOST/path')
252
 
        return unescape(win32_url)
253
 
 
254
 
    # allow empty paths so we can serve all roots
255
 
    if win32_url == '///':
256
 
        return '/'
257
 
    
258
 
    # usual local path with drive letter
259
 
    if (win32_url[3] not in ('abcdefghijklmnopqrstuvwxyz'
260
 
                             'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
261
 
        or win32_url[4] not in  '|:'
262
 
        or win32_url[5] != '/'):
263
 
        raise errors.InvalidURL(url, 'Win32 file urls start with'
264
 
                ' file:///x:/, where x is a valid drive letter')
265
 
    return win32_url[3].upper() + u':' + unescape(win32_url[5:])
 
171
    win32_url = url[len('file:///'):]
 
172
    if (win32_url[0] not in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
 
173
        or win32_url[1] not in  '|:'
 
174
        or win32_url[2] != '/'):
 
175
        raise errors.InvalidURL(url, 'Win32 file urls start with file:///X|/, where X is a valid drive letter')
 
176
    # TODO: jam 20060426, we could .upper() or .lower() the drive letter
 
177
    #       for better consistency.
 
178
    return win32_url[0].upper() + u':' + unescape(win32_url[2:])
266
179
 
267
180
 
268
181
def _win32_local_path_to_url(path):
269
 
    """Convert a local path like ./foo into a URL like file:///C:/path/to/foo
 
182
    """Convert a local path like ./foo into a URL like file:///C|/path/to/foo
270
183
 
271
184
    This also handles transforming escaping unicode characters, etc.
272
185
    """
273
186
    # importing directly from ntpath allows us to test this 
274
 
    # on non-win32 platform
275
 
    # FIXME: It turns out that on nt, ntpath.abspath uses nt._getfullpathname
276
 
    #       which actually strips trailing space characters.
277
 
    #       The worst part is that under linux ntpath.abspath has different
278
 
    #       semantics, since 'nt' is not an available module.
279
 
    if path == '/':
280
 
        return 'file:///'
281
 
 
282
 
    win32_path = osutils._win32_abspath(path)
283
 
    # check for UNC path \\HOST\path
284
 
    if win32_path.startswith('//'):
285
 
        return 'file:' + escape(win32_path)
286
 
    return ('file:///' + str(win32_path[0].upper()) + ':' +
287
 
        escape(win32_path[2:]))
 
187
    # on non-win32 platforms
 
188
    # TODO: jam 20060426 consider moving this import outside of the function
 
189
    win32_path = bzrlib.osutils._nt_normpath(
 
190
        bzrlib.osutils._win32_abspath(path)).replace('\\', '/')
 
191
    return 'file:///' + win32_path[0].upper() + '|' + escape(win32_path[2:])
288
192
 
289
193
 
290
194
local_path_to_url = _posix_local_path_to_url
291
195
local_path_from_url = _posix_local_path_from_url
292
196
MIN_ABS_FILEURL_LENGTH = len('file:///')
293
 
WIN32_MIN_ABS_FILEURL_LENGTH = len('file:///C:/')
294
197
 
295
198
if sys.platform == 'win32':
296
199
    local_path_to_url = _win32_local_path_to_url
297
200
    local_path_from_url = _win32_local_path_from_url
298
201
 
299
 
    MIN_ABS_FILEURL_LENGTH = WIN32_MIN_ABS_FILEURL_LENGTH
 
202
    MIN_ABS_FILEURL_LENGTH = len('file:///C|/')
300
203
 
301
204
 
302
205
_url_scheme_re = re.compile(r'^(?P<scheme>[^:/]{2,})://(?P<path>.*)$')
303
 
_url_hex_escapes_re = re.compile(r'(%[0-9a-fA-F]{2})')
304
 
 
305
 
 
306
 
def _unescape_safe_chars(matchobj):
307
 
    """re.sub callback to convert hex-escapes to plain characters (if safe).
308
 
    
309
 
    e.g. '%7E' will be converted to '~'.
310
 
    """
311
 
    hex_digits = matchobj.group(0)[1:]
312
 
    char = chr(int(hex_digits, 16))
313
 
    if char in _url_dont_escape_characters:
314
 
        return char
315
 
    else:
316
 
        return matchobj.group(0).upper()
317
206
 
318
207
 
319
208
def normalize_url(url):
320
209
    """Make sure that a path string is in fully normalized URL form.
321
210
    
322
 
    This handles URLs which have unicode characters, spaces,
 
211
    This handles URLs which have unicode characters, spaces, 
323
212
    special characters, etc.
324
213
 
325
214
    It has two basic modes of operation, depending on whether the
338
227
    m = _url_scheme_re.match(url)
339
228
    if not m:
340
229
        return local_path_to_url(url)
341
 
    scheme = m.group('scheme')
342
 
    path = m.group('path')
343
230
    if not isinstance(url, unicode):
 
231
        # TODO: jam 20060510 We need to test for ascii characters that
 
232
        #       shouldn't be allowed in URLs
344
233
        for c in url:
345
234
            if c not in _url_safe_characters:
346
235
                raise errors.InvalidURL(url, 'URLs can only contain specific'
347
236
                                            ' safe characters (not %r)' % c)
348
 
        path = _url_hex_escapes_re.sub(_unescape_safe_chars, path)
349
 
        return str(scheme + '://' + ''.join(path))
350
 
 
 
237
        return url
351
238
    # We have a unicode (hybrid) url
352
 
    path_chars = list(path)
 
239
    scheme = m.group('scheme')
 
240
    path = list(m.group('path'))
353
241
 
354
 
    for i in xrange(len(path_chars)):
355
 
        if path_chars[i] not in _url_safe_characters:
356
 
            chars = path_chars[i].encode('utf-8')
357
 
            path_chars[i] = ''.join(
358
 
                ['%%%02X' % ord(c) for c in path_chars[i].encode('utf-8')])
359
 
    path = ''.join(path_chars)
360
 
    path = _url_hex_escapes_re.sub(_unescape_safe_chars, path)
361
 
    return str(scheme + '://' + path)
 
242
    for i in xrange(len(path)):
 
243
        if path[i] not in _url_safe_characters:
 
244
            chars = path[i].encode('utf-8')
 
245
            path[i] = ''.join(['%%%02X' % ord(c) for c in path[i].encode('utf-8')])
 
246
    return scheme + '://' + ''.join(path)
362
247
 
363
248
 
364
249
def relative_url(base, other):
371
256
    if base_first_slash is None:
372
257
        return other
373
258
    
 
259
    # TODO: shorter names
374
260
    dummy, other_first_slash = _find_scheme_and_separator(other)
375
261
    if other_first_slash is None:
376
262
        return other
380
266
    other_scheme = other[:other_first_slash]
381
267
    if base_scheme != other_scheme:
382
268
        return other
383
 
    elif sys.platform == 'win32' and base_scheme == 'file://':
384
 
        base_drive = base[base_first_slash+1:base_first_slash+3]
385
 
        other_drive = other[other_first_slash+1:other_first_slash+3]
386
 
        if base_drive != other_drive:
387
 
            return other
388
269
 
389
270
    base_path = base[base_first_slash+1:]
390
271
    other_path = other[other_first_slash+1:]
394
275
 
395
276
    base_sections = base_path.split('/')
396
277
    other_sections = other_path.split('/')
 
278
    # TODO: handle the case where base has a host part but no trailing slash
 
279
    #           that might not be allowed, it only makes sense to have a link
 
280
    #           relative to a directory-ish part, not to files
397
281
 
398
282
    if base_sections == ['']:
399
283
        base_sections = []
413
297
    return "/".join(output_sections) or "."
414
298
 
415
299
 
416
 
def _win32_extract_drive_letter(url_base, path):
417
 
    """On win32 the drive letter needs to be added to the url base."""
418
 
    # Strip off the drive letter
419
 
    # path is currently /C:/foo
420
 
    if len(path) < 3 or path[2] not in ':|' or path[3] != '/':
421
 
        raise errors.InvalidURL(url_base + path, 
422
 
            'win32 file:/// paths need a drive letter')
423
 
    url_base += path[0:3] # file:// + /C:
424
 
    path = path[3:] # /foo
425
 
    return url_base, path
426
 
 
427
 
 
428
300
def split(url, exclude_trailing_slash=True):
429
301
    """Split a URL into its parent directory and a child directory.
430
302
 
454
326
 
455
327
    if sys.platform == 'win32' and url.startswith('file:///'):
456
328
        # Strip off the drive letter
457
 
        # url_base is currently file://
458
 
        # path is currently /C:/foo
459
 
        url_base, path = _win32_extract_drive_letter(url_base, path)
460
 
        # now it should be file:///C: and /foo
 
329
        if path[2:3] not in '\\/':
 
330
            raise errors.InvalidURL(url, 
 
331
                'win32 file:/// paths need a drive letter')
 
332
        url_base += path[1:4] # file:///C|/
 
333
        path = path[3:]
461
334
 
462
335
    if exclude_trailing_slash and len(path) > 1 and path.endswith('/'):
463
336
        path = path[:-1]
465
338
    return url_base + head, tail
466
339
 
467
340
 
468
 
def _win32_strip_local_trailing_slash(url):
469
 
    """Strip slashes after the drive letter"""
470
 
    if len(url) > WIN32_MIN_ABS_FILEURL_LENGTH:
471
 
        return url[:-1]
472
 
    else:
473
 
        return url
474
 
 
475
 
 
476
341
def strip_trailing_slash(url):
477
342
    """Strip trailing slash, except for root paths.
478
343
 
492
357
        file:///foo/      => file:///foo
493
358
        # This is unique on win32 platforms, and is the only URL
494
359
        # format which does it differently.
495
 
        file:///c|/       => file:///c:/
 
360
        file:///C|/       => file:///C|/
496
361
    """
497
362
    if not url.endswith('/'):
498
363
        # Nothing to do
499
364
        return url
500
 
    if sys.platform == 'win32' and url.startswith('file://'):
501
 
        return _win32_strip_local_trailing_slash(url)
502
 
 
 
365
    if sys.platform == 'win32' and url.startswith('file:///'):
 
366
        # This gets handled specially, because the 'top-level'
 
367
        # of a win32 path is actually the drive letter
 
368
        if len(url) > MIN_ABS_FILEURL_LENGTH:
 
369
            return url[:-1]
 
370
        else:
 
371
            return url
503
372
    scheme_loc, first_path_slash = _find_scheme_and_separator(url)
504
373
    if scheme_loc is None:
505
374
        # This is a relative path, as it has no scheme
530
399
        url = str(url)
531
400
    except UnicodeError, e:
532
401
        raise errors.InvalidURL(url, 'URL was not a plain ASCII url: %s' % (e,))
533
 
 
534
402
    unquoted = urllib.unquote(url)
535
403
    try:
536
404
        unicode_path = unquoted.decode('utf-8')
549
417
#These entries get mapped to themselves
550
418
_hex_display_map.update((hex,'%'+hex) for hex in _no_decode_hex)
551
419
 
552
 
# These characters shouldn't be percent-encoded, and it's always safe to
553
 
# unencode them if they are.
554
 
_url_dont_escape_characters = set(
555
 
   "abcdefghijklmnopqrstuvwxyz" # Lowercase alpha
556
 
   "ABCDEFGHIJKLMNOPQRSTUVWXYZ" # Uppercase alpha
557
 
   "0123456789" # Numbers
558
 
   "-._~"  # Unreserved characters
559
 
)
560
 
 
561
420
# These characters should not be escaped
562
 
_url_safe_characters = set(
563
 
   "abcdefghijklmnopqrstuvwxyz" # Lowercase alpha
564
 
   "ABCDEFGHIJKLMNOPQRSTUVWXYZ" # Uppercase alpha
565
 
   "0123456789" # Numbers
566
 
   "_.-!~*'()"  # Unreserved characters
567
 
   "/;?:@&=+$," # Reserved characters
568
 
   "%#"         # Extra reserved characters
569
 
)
 
421
_url_safe_characters = set('abcdefghijklmnopqrstuvwxyz'
 
422
                        'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
 
423
                        '0123456789' '_.-/'
 
424
                        ';?:@&=+$,%#')
 
425
 
570
426
 
571
427
def unescape_for_display(url, encoding):
572
428
    """Decode what you can for a URL, so that we get a nice looking path.
583
439
    :return: A unicode string which can be safely encoded into the 
584
440
         specified encoding.
585
441
    """
586
 
    if encoding is None:
587
 
        raise ValueError('you cannot specify None for the display encoding')
588
442
    if url.startswith('file://'):
589
443
        try:
590
444
            path = local_path_from_url(url)
624
478
                # Otherwise take the url decoded one
625
479
                res[i] = decoded
626
480
    return u'/'.join(res)
627
 
 
628
 
 
629
 
def derive_to_location(from_location):
630
 
    """Derive a TO_LOCATION given a FROM_LOCATION.
631
 
 
632
 
    The normal case is a FROM_LOCATION of http://foo/bar => bar.
633
 
    The Right Thing for some logical destinations may differ though
634
 
    because no / may be present at all. In that case, the result is
635
 
    the full name without the scheme indicator, e.g. lp:foo-bar => foo-bar.
636
 
    This latter case also applies when a Windows drive
637
 
    is used without a path, e.g. c:foo-bar => foo-bar.
638
 
    If no /, path separator or : is found, the from_location is returned.
639
 
    """
640
 
    if from_location.find("/") >= 0 or from_location.find(os.sep) >= 0:
641
 
        return os.path.basename(from_location.rstrip("/\\"))
642
 
    else:
643
 
        sep = from_location.find(":")
644
 
        if sep > 0:
645
 
            return from_location[sep+1:]
646
 
        else:
647
 
            return from_location
648
 
 
649
 
 
650
 
def _is_absolute(url):
651
 
    return (osutils.pathjoin('/foo', url) == url)
652
 
 
653
 
 
654
 
def rebase_url(url, old_base, new_base):
655
 
    """Convert a relative path from an old base URL to a new base URL.
656
 
 
657
 
    The result will be a relative path.
658
 
    Absolute paths and full URLs are returned unaltered.
659
 
    """
660
 
    scheme, separator = _find_scheme_and_separator(url)
661
 
    if scheme is not None:
662
 
        return url
663
 
    if _is_absolute(url):
664
 
        return url
665
 
    old_parsed = urlparse.urlparse(old_base)
666
 
    new_parsed = urlparse.urlparse(new_base)
667
 
    if (old_parsed[:2]) != (new_parsed[:2]):
668
 
        raise errors.InvalidRebaseURLs(old_base, new_base)
669
 
    return determine_relative_path(new_parsed[2],
670
 
                                   join(old_parsed[2], url))
671
 
 
672
 
 
673
 
def determine_relative_path(from_path, to_path):
674
 
    """Determine a relative path from from_path to to_path."""
675
 
    from_segments = osutils.splitpath(from_path)
676
 
    to_segments = osutils.splitpath(to_path)
677
 
    count = -1
678
 
    for count, (from_element, to_element) in enumerate(zip(from_segments,
679
 
                                                       to_segments)):
680
 
        if from_element != to_element:
681
 
            break
682
 
    else:
683
 
        count += 1
684
 
    unique_from = from_segments[count:]
685
 
    unique_to = to_segments[count:]
686
 
    segments = (['..'] * len(unique_from) + unique_to)
687
 
    if len(segments) == 0:
688
 
        return '.'
689
 
    return osutils.pathjoin(*segments)