/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/transport/__init__.py

  • Committer: Robert Collins
  • Date: 2006-06-09 09:04:53 UTC
  • mfrom: (1755.2.1 add)
  • mto: (1755.1.2 integration)
  • mto: This revision was merged to the branch mainline in revision 1757.
  • Revision ID: robertc@robertcollins.net-20060609090453-10e94172dc5f670b
MergeĀ currentĀ head.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
import errno
30
30
from collections import deque
31
31
from copy import deepcopy
 
32
import re
32
33
from stat import *
33
34
import sys
34
35
from unittest import TestSuite
38
39
import bzrlib
39
40
import bzrlib.errors as errors
40
41
from bzrlib.errors import DependencyNotPresent
 
42
import bzrlib.osutils as osutils
41
43
from bzrlib.osutils import pumpfile
42
44
from bzrlib.symbol_versioning import *
43
45
from bzrlib.trace import mutter, warning
 
46
import bzrlib.urlutils as urlutils
44
47
 
45
48
# {prefix: [transport_classes]}
46
49
# Transports are inserted onto the list LIFO and tried in order; as a result
129
132
 
130
133
 
131
134
def split_url(url):
 
135
    # TODO: jam 20060606 urls should only be ascii, or they should raise InvalidURL
132
136
    if isinstance(url, unicode):
133
137
        url = url.encode('utf-8')
134
138
    (scheme, netloc, path, params,
285
289
        pl = len(self.base)
286
290
        return abspath[pl:].strip('/')
287
291
 
 
292
    def local_abspath(self, relpath):
 
293
        """Return the absolute path on the local filesystem.
 
294
 
 
295
        This function will only be defined for Transports which have a
 
296
        physical local filesystem representation.
 
297
        """
 
298
        # TODO: jam 20060426 Should this raise NotLocalUrl instead?
 
299
        raise errors.TransportNotPossible('This is not a LocalTransport,'
 
300
            ' so there is no local representation for a path')
 
301
 
288
302
    def has(self, relpath):
289
303
        """Does the file relpath exist?
290
304
        
663
677
        return False
664
678
 
665
679
 
 
680
# jam 20060426 For compatibility we copy the functions here
 
681
# TODO: The should be marked as deprecated
 
682
urlescape = urlutils.escape
 
683
urlunescape = urlutils.unescape
 
684
_urlRE = re.compile(r'^(?P<proto>[^:/\\]+)://(?P<path>.*)$')
 
685
 
 
686
 
666
687
def get_transport(base):
667
688
    """Open a transport to access a URL or directory.
668
689
 
672
693
    # handler for the scheme?
673
694
    global _protocol_handlers
674
695
    if base is None:
675
 
        base = u'.'
676
 
    else:
677
 
        base = unicode(base)
 
696
        base = '.'
 
697
 
 
698
    def convert_path_to_url(base, error_str):
 
699
        m = _urlRE.match(base)
 
700
        if m:
 
701
            # This looks like a URL, but we weren't able to 
 
702
            # instantiate it as such raise an appropriate error
 
703
            raise errors.InvalidURL(base, error_str % m.group('proto'))
 
704
        # This doesn't look like a protocol, consider it a local path
 
705
        new_base = urlutils.local_path_to_url(base)
 
706
        mutter('converting os path %r => url %s' , base, new_base)
 
707
        return new_base
 
708
 
 
709
    # Catch any URLs which are passing Unicode rather than ASCII
 
710
    try:
 
711
        base = base.encode('ascii')
 
712
    except UnicodeError:
 
713
        # Only local paths can be Unicode
 
714
        base = convert_path_to_url(base,
 
715
            'URLs must be properly escaped (protocol: %s)')
 
716
    
678
717
    for proto, factory_list in _protocol_handlers.iteritems():
679
718
        if proto is not None and base.startswith(proto):
680
719
            t = _try_transport_factories(base, factory_list)
681
720
            if t:
682
721
                return t
 
722
 
 
723
    # We tried all the different protocols, now try one last time
 
724
    # as a local protocol
 
725
    base = convert_path_to_url(base, 'Unsupported protocol: %s')
 
726
 
683
727
    # The default handler is the filesystem handler, stored as protocol None
684
728
    return _try_transport_factories(base, _protocol_handlers[None])
685
729
 
695
739
    return None
696
740
 
697
741
 
698
 
def urlescape(relpath):
699
 
    """Escape relpath to be a valid url."""
700
 
    if isinstance(relpath, unicode):
701
 
        relpath = relpath.encode('utf-8')
702
 
    return urllib.quote(relpath)
703
 
 
704
 
 
705
 
def urlunescape(relpath):
706
 
    """Unescape relpath from url format."""
707
 
    return urllib.unquote(relpath)
708
 
    # TODO de-utf8 it last. relpath = utf8relpath.decode('utf8')
709
 
 
710
 
 
711
742
class Server(object):
712
743
    """A Transport Server.
713
744
    
832
863
register_lazy_transport('https://', 'bzrlib.transport.http._pycurl', 'PyCurlTransport')
833
864
register_lazy_transport('ftp://', 'bzrlib.transport.ftp', 'FtpTransport')
834
865
register_lazy_transport('aftp://', 'bzrlib.transport.ftp', 'FtpTransport')
835
 
register_lazy_transport('memory:/', 'bzrlib.transport.memory', 'MemoryTransport')
 
866
register_lazy_transport('memory://', 'bzrlib.transport.memory', 'MemoryTransport')
836
867
register_lazy_transport('readonly+', 'bzrlib.transport.readonly', 'ReadonlyTransportDecorator')
837
868
register_lazy_transport('fakenfs+', 'bzrlib.transport.fakenfs', 'FakeNFSTransportDecorator')
838
869
register_lazy_transport('vfat+',