30
30
from collections import deque
31
31
from copy import deepcopy
33
from stat import S_ISDIR
34
35
from unittest import TestSuite
38
from bzrlib.trace import mutter, warning
39
41
import bzrlib.errors as errors
40
42
from bzrlib.errors import DependencyNotPresent
41
from bzrlib.symbol_versioning import *
43
import bzrlib.osutils as osutils
44
from bzrlib.osutils import pumpfile
45
from bzrlib.symbol_versioning import (deprecated_passed, deprecated_method, deprecated_function,
48
from bzrlib.trace import mutter, warning
49
import bzrlib.urlutils as urlutils
43
51
# {prefix: [transport_classes]}
44
52
# Transports are inserted onto the list LIFO and tried in order; as a result
59
67
global _protocol_handlers
60
68
if deprecated_passed(override):
61
warn("register_transport(override) is deprecated")
69
warnings.warn("register_transport(override) is deprecated")
62
70
_protocol_handlers.setdefault(prefix, []).insert(0, klass)
129
137
def split_url(url):
138
# TODO: jam 20060606 urls should only be ascii, or they should raise InvalidURL
130
139
if isinstance(url, unicode):
131
140
url = url.encode('utf-8')
132
141
(scheme, netloc, path, params,
216
225
if isinstance(from_file, basestring):
217
226
to_file.write(from_file)
219
from bzrlib.osutils import pumpfile
220
228
pumpfile(from_file, to_file)
222
230
def _get_total(self, multi):
284
292
pl = len(self.base)
285
293
return abspath[pl:].strip('/')
295
def local_abspath(self, relpath):
296
"""Return the absolute path on the local filesystem.
298
This function will only be defined for Transports which have a
299
physical local filesystem representation.
301
# TODO: jam 20060426 Should this raise NotLocalUrl instead?
302
raise errors.TransportNotPossible('This is not a LocalTransport,'
303
' so there is no local representation for a path')
287
305
def has(self, relpath):
288
306
"""Does the file relpath exist?
683
# jam 20060426 For compatibility we copy the functions here
684
# TODO: The should be marked as deprecated
685
urlescape = urlutils.escape
686
urlunescape = urlutils.unescape
687
_urlRE = re.compile(r'^(?P<proto>[^:/\\]+)://(?P<path>.*)$')
665
690
def get_transport(base):
666
691
"""Open a transport to access a URL or directory.
671
696
# handler for the scheme?
672
697
global _protocol_handlers
701
def convert_path_to_url(base, error_str):
702
m = _urlRE.match(base)
704
# This looks like a URL, but we weren't able to
705
# instantiate it as such raise an appropriate error
706
raise errors.InvalidURL(base, error_str % m.group('proto'))
707
# This doesn't look like a protocol, consider it a local path
708
new_base = urlutils.local_path_to_url(base)
709
mutter('converting os path %r => url %s', base, new_base)
712
# Catch any URLs which are passing Unicode rather than ASCII
714
base = base.encode('ascii')
716
# Only local paths can be Unicode
717
base = convert_path_to_url(base,
718
'URLs must be properly escaped (protocol: %s)')
677
720
for proto, factory_list in _protocol_handlers.iteritems():
678
721
if proto is not None and base.startswith(proto):
679
722
t = _try_transport_factories(base, factory_list)
726
# We tried all the different protocols, now try one last time
727
# as a local protocol
728
base = convert_path_to_url(base, 'Unsupported protocol: %s')
682
730
# The default handler is the filesystem handler, stored as protocol None
683
731
return _try_transport_factories(base, _protocol_handlers[None])
697
def urlescape(relpath):
698
"""Escape relpath to be a valid url."""
699
if isinstance(relpath, unicode):
700
relpath = relpath.encode('utf-8')
701
return urllib.quote(relpath)
704
def urlunescape(relpath):
705
"""Unescape relpath from url format."""
706
return urllib.unquote(relpath)
707
# TODO de-utf8 it last. relpath = utf8relpath.decode('utf8')
710
745
class Server(object):
711
746
"""A Transport Server.
831
866
register_lazy_transport('https://', 'bzrlib.transport.http._pycurl', 'PyCurlTransport')
832
867
register_lazy_transport('ftp://', 'bzrlib.transport.ftp', 'FtpTransport')
833
868
register_lazy_transport('aftp://', 'bzrlib.transport.ftp', 'FtpTransport')
834
register_lazy_transport('memory:/', 'bzrlib.transport.memory', 'MemoryTransport')
869
register_lazy_transport('memory://', 'bzrlib.transport.memory', 'MemoryTransport')
835
870
register_lazy_transport('readonly+', 'bzrlib.transport.readonly', 'ReadonlyTransportDecorator')
836
871
register_lazy_transport('fakenfs+', 'bzrlib.transport.fakenfs', 'FakeNFSTransportDecorator')
837
872
register_lazy_transport('vfat+',