/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
4763.2.4 by John Arbash Meinel
merge bzr.2.1 in preparation for NEWS entry.
1
# Copyright (C) 2005-2010 Canonical Ltd
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
2
#
1185.11.19 by John Arbash Meinel
Testing put and append, also testing agaist file-like objects as well as strings.
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
7
#
1185.11.19 by John Arbash Meinel
Testing put and append, also testing agaist file-like objects as well as strings.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
1887.1.1 by Adeodato Simó
Do not separate paragraphs in the copyright statement with blank lines,
12
#
1185.11.19 by John Arbash Meinel
Testing put and append, also testing agaist file-like objects as well as strings.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
1540.3.1 by Martin Pool
First-cut implementation of pycurl. Substantially faster than using urllib.
16
1185.11.19 by John Arbash Meinel
Testing put and append, also testing agaist file-like objects as well as strings.
17
"""Transport is an abstraction layer to handle file access.
18
19
The abstraction is to allow access from the local filesystem, as well
20
as remote (such as http or sftp).
1540.3.12 by Martin Pool
Multiple transports can be registered for any protocol, and they are
21
22
Transports are constructed from a string, being a URL or (as a degenerate
23
case) a local filesystem path.  This is typically the top directory of
24
a bzrdir, repository, or similar object we are interested in working with.
25
The Transport returned has methods to read, write and manipulate files within
26
it.
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
27
"""
28
1996.3.6 by John Arbash Meinel
Find a few places that weren't importing their dependencies.
29
from cStringIO import StringIO
30
import re
31
import sys
32
33
from bzrlib.lazy_import import lazy_import
34
lazy_import(globals(), """
1185.31.44 by John Arbash Meinel
Cleaned up Exceptions for all transports.
35
import errno
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
36
from stat import S_ISDIR
1685.1.70 by Wouter van Heyst
working on get_parent, set_parent and relative urls, broken
37
import urllib
1636.1.1 by Robert Collins
Fix calling relpath() and abspath() on transports at their root.
38
import urlparse
1530.1.1 by Robert Collins
Minimal infrastructure to test TransportTestProviderAdapter.
39
1955.3.6 by John Arbash Meinel
Lots of deprecation warnings, but no errors
40
from bzrlib import (
41
    errors,
42
    osutils,
43
    symbol_versioning,
3882.7.5 by Martin Pool
Further mockup of transport-based activity indicator.
44
    ui,
1955.3.6 by John Arbash Meinel
Lots of deprecation warnings, but no errors
45
    urlutils,
46
    )
1996.3.6 by John Arbash Meinel
Find a few places that weren't importing their dependencies.
47
""")
48
1955.3.6 by John Arbash Meinel
Lots of deprecation warnings, but no errors
49
from bzrlib.symbol_versioning import (
50
        deprecated_method,
51
        deprecated_function,
1773.4.1 by Martin Pool
Add pyflakes makefile target; fix many warnings
52
        DEPRECATED_PARAMETER,
1955.3.6 by John Arbash Meinel
Lots of deprecation warnings, but no errors
53
        )
2164.2.21 by Vincent Ladeuil
Take bundles into account.
54
from bzrlib.trace import (
55
    mutter,
56
    )
2241.2.1 by ghigo
Add the TransportRegistry class
57
from bzrlib import registry
2018.5.104 by Andrew Bennetts
Completely rework chrooted transports.
58
59
2671.3.2 by Robert Collins
Start open_file_stream logic.
60
# a dictionary of open file streams. Keys are absolute paths, values are
61
# transport defined.
62
_file_streams = {}
63
64
1530.1.11 by Robert Collins
Push the transport permutations list into each transport module allowing for automatic testing of new modules that are registered as transports.
65
def _get_protocol_handlers():
1540.3.12 by Martin Pool
Multiple transports can be registered for any protocol, and they are
66
    """Return a dictionary of {urlprefix: [factory]}"""
2241.2.3 by ghigo
removed the the registration of the 'None' transport, instead we use the set_default_transport function
67
    return transport_list_registry
1530.1.11 by Robert Collins
Push the transport permutations list into each transport module allowing for automatic testing of new modules that are registered as transports.
68
2241.3.4 by ghigo
Updates on the basis of John Arbash Meinel comments
69
1530.1.11 by Robert Collins
Push the transport permutations list into each transport module allowing for automatic testing of new modules that are registered as transports.
70
def _set_protocol_handlers(new_handlers):
71
    """Replace the current protocol handlers dictionary.
72
73
    WARNING this will remove all build in protocols. Use with care.
74
    """
2241.2.3 by ghigo
removed the the registration of the 'None' transport, instead we use the set_default_transport function
75
    global transport_list_registry
76
    transport_list_registry = new_handlers
1530.1.11 by Robert Collins
Push the transport permutations list into each transport module allowing for automatic testing of new modules that are registered as transports.
77
2241.3.4 by ghigo
Updates on the basis of John Arbash Meinel comments
78
1540.3.12 by Martin Pool
Multiple transports can be registered for any protocol, and they are
79
def _clear_protocol_handlers():
2241.2.3 by ghigo
removed the the registration of the 'None' transport, instead we use the set_default_transport function
80
    global transport_list_registry
81
    transport_list_registry = TransportListRegistry()
1540.3.12 by Martin Pool
Multiple transports can be registered for any protocol, and they are
82
2241.3.4 by ghigo
Updates on the basis of John Arbash Meinel comments
83
1530.1.11 by Robert Collins
Push the transport permutations list into each transport module allowing for automatic testing of new modules that are registered as transports.
84
def _get_transport_modules():
85
    """Return a list of the modules providing transports."""
86
    modules = set()
3882.1.1 by Martin Pool
Don't call iteritems on transport_list_registry, because it may change during iteration
87
    for prefix, factory_list in transport_list_registry.items():
1540.3.12 by Martin Pool
Multiple transports can be registered for any protocol, and they are
88
        for factory in factory_list:
2241.2.2 by ghigo
Create the TransportList class
89
            if hasattr(factory, "_module_name"):
90
                modules.add(factory._module_name)
1540.3.12 by Martin Pool
Multiple transports can be registered for any protocol, and they are
91
            else:
2241.2.2 by ghigo
Create the TransportList class
92
                modules.add(factory._obj.__module__)
4634.44.1 by Andrew Bennetts
First draft of a generic path-filtering transport decorator.
93
    # Add chroot and pathfilter directly, because there is no handler
94
    # registered for it.
2018.5.104 by Andrew Bennetts
Completely rework chrooted transports.
95
    modules.add('bzrlib.transport.chroot')
4634.44.1 by Andrew Bennetts
First draft of a generic path-filtering transport decorator.
96
    modules.add('bzrlib.transport.pathfilter')
1530.1.19 by Robert Collins
Make transport test adapter tests reliable.
97
    result = list(modules)
98
    result.sort()
99
    return result
1530.1.11 by Robert Collins
Push the transport permutations list into each transport module allowing for automatic testing of new modules that are registered as transports.
100
2241.3.4 by ghigo
Updates on the basis of John Arbash Meinel comments
101
2241.2.3 by ghigo
removed the the registration of the 'None' transport, instead we use the set_default_transport function
102
class TransportListRegistry(registry.Registry):
2241.3.4 by ghigo
Updates on the basis of John Arbash Meinel comments
103
    """A registry which simplifies tracking available Transports.
104
3764.1.1 by Vincent Ladeuil
Fix typo
105
    A registration of a new protocol requires two steps:
2241.3.4 by ghigo
Updates on the basis of John Arbash Meinel comments
106
    1) register the prefix with the function register_transport( )
107
    2) register the protocol provider with the function
108
    register_transport_provider( ) ( and the "lazy" variant )
109
2485.8.38 by Vincent Ladeuil
Finish sftp refactoring. Test suite passing.
110
    This is needed because:
4764.1.1 by Vincent Ladeuil
Fix typos.
111
    a) a single provider can support multiple protocols ( like the ftp
2485.8.38 by Vincent Ladeuil
Finish sftp refactoring. Test suite passing.
112
    provider which supports both the ftp:// and the aftp:// protocols )
2241.3.4 by ghigo
Updates on the basis of John Arbash Meinel comments
113
    b) a single protocol can have multiple providers ( like the http://
2485.8.38 by Vincent Ladeuil
Finish sftp refactoring. Test suite passing.
114
    protocol which is supported by both the urllib and pycurl provider )
2241.3.4 by ghigo
Updates on the basis of John Arbash Meinel comments
115
    """
116
2241.2.3 by ghigo
removed the the registration of the 'None' transport, instead we use the set_default_transport function
117
    def register_transport_provider(self, key, obj):
118
        self.get(key).insert(0, registry._ObjectGetter(obj))
119
120
    def register_lazy_transport_provider(self, key, module_name, member_name):
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
121
        self.get(key).insert(0,
2241.2.1 by ghigo
Add the TransportRegistry class
122
                registry._LazyObjectGetter(module_name, member_name))
123
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
124
    def register_transport(self, key, help=None):
125
        self.register(key, [], help)
2241.2.1 by ghigo
Add the TransportRegistry class
126
127
    def set_default_transport(self, key=None):
128
        """Return either 'key' or the default key if key is None"""
129
        self._default_key = key
130
2241.3.4 by ghigo
Updates on the basis of John Arbash Meinel comments
131
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
132
transport_list_registry = TransportListRegistry()
2241.2.1 by ghigo
Add the TransportRegistry class
133
2241.3.4 by ghigo
Updates on the basis of John Arbash Meinel comments
134
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
135
def register_transport_proto(prefix, help=None, info=None,
2919.2.1 by John Arbash Meinel
Register netloc protocols as soon as the protocol is registered.
136
                             register_netloc=False):
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
137
    transport_list_registry.register_transport(prefix, help)
2919.2.1 by John Arbash Meinel
Register netloc protocols as soon as the protocol is registered.
138
    if register_netloc:
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
139
        if not prefix.endswith('://'):
140
            raise ValueError(prefix)
2919.2.1 by John Arbash Meinel
Register netloc protocols as soon as the protocol is registered.
141
        register_urlparse_netloc_protocol(prefix[:-3])
2241.2.1 by ghigo
Add the TransportRegistry class
142
2241.3.4 by ghigo
Updates on the basis of John Arbash Meinel comments
143
2241.2.1 by ghigo
Add the TransportRegistry class
144
def register_lazy_transport(prefix, module, classname):
2241.2.3 by ghigo
removed the the registration of the 'None' transport, instead we use the set_default_transport function
145
    if not prefix in transport_list_registry:
2241.2.2 by ghigo
Create the TransportList class
146
        register_transport_proto(prefix)
2241.2.3 by ghigo
removed the the registration of the 'None' transport, instead we use the set_default_transport function
147
    transport_list_registry.register_lazy_transport_provider(prefix, module, classname)
2241.3.4 by ghigo
Updates on the basis of John Arbash Meinel comments
148
149
2241.2.1 by ghigo
Add the TransportRegistry class
150
def register_transport(prefix, klass, override=DEPRECATED_PARAMETER):
2241.2.3 by ghigo
removed the the registration of the 'None' transport, instead we use the set_default_transport function
151
    if not prefix in transport_list_registry:
2241.2.2 by ghigo
Create the TransportList class
152
        register_transport_proto(prefix)
2241.2.3 by ghigo
removed the the registration of the 'None' transport, instead we use the set_default_transport function
153
    transport_list_registry.register_transport_provider(prefix, klass)
2241.2.1 by ghigo
Add the TransportRegistry class
154
2241.3.4 by ghigo
Updates on the basis of John Arbash Meinel comments
155
1636.1.2 by Robert Collins
More review fixen to the relpath at '/' fixes.
156
def register_urlparse_netloc_protocol(protocol):
1636.1.1 by Robert Collins
Fix calling relpath() and abspath() on transports at their root.
157
    """Ensure that protocol is setup to be used with urlparse netloc parsing."""
158
    if protocol not in urlparse.uses_netloc:
159
        urlparse.uses_netloc.append(protocol)
160
2241.3.4 by ghigo
Updates on the basis of John Arbash Meinel comments
161
2485.8.61 by Vincent Ladeuil
From review comments, use a private scheme for testing.
162
def _unregister_urlparse_netloc_protocol(protocol):
163
    """Remove protocol from urlparse netloc parsing.
164
165
    Except for tests, you should never use that function. Using it with 'http',
166
    for example, will break all http transports.
167
    """
168
    if protocol in urlparse.uses_netloc:
169
        urlparse.uses_netloc.remove(protocol)
170
171
2241.3.5 by ghigo
update to the latest bzr.dev
172
def unregister_transport(scheme, factory):
173
    """Unregister a transport."""
174
    l = transport_list_registry.get(scheme)
175
    for i in l:
176
        o = i.get_obj( )
177
        if o == factory:
178
            transport_list_registry.get(scheme).remove(i)
179
            break
180
    if len(l) == 0:
181
        transport_list_registry.remove(scheme)
182
183
1864.5.9 by John Arbash Meinel
Switch to returning an object to make the api more understandable.
184
class _CoalescedOffset(object):
185
    """A data container for keeping track of coalesced offsets."""
186
187
    __slots__ = ['start', 'length', 'ranges']
188
189
    def __init__(self, start, length, ranges):
190
        self.start = start
191
        self.length = length
192
        self.ranges = ranges
193
194
    def __cmp__(self, other):
195
        return cmp((self.start, self.length, self.ranges),
196
                   (other.start, other.length, other.ranges))
197
3059.2.2 by Vincent Ladeuil
Read http responses on demand without buffering the whole body
198
    def __repr__(self):
199
        return '%s(%r, %r, %r)' % (self.__class__.__name__,
200
            self.start, self.length, self.ranges)
201
1864.5.9 by John Arbash Meinel
Switch to returning an object to make the api more understandable.
202
2052.6.1 by Robert Collins
``Transport.get`` has had its interface made more clear for ease of use.
203
class LateReadError(object):
204
    """A helper for transports which pretends to be a readable file.
205
206
    When read() is called, errors.ReadError is raised.
207
    """
208
209
    def __init__(self, path):
210
        self._path = path
211
212
    def close(self):
213
        """a no-op - do nothing."""
214
2052.6.2 by Robert Collins
Merge bzr.dev.
215
    def _fail(self):
2052.6.1 by Robert Collins
``Transport.get`` has had its interface made more clear for ease of use.
216
        """Raise ReadError."""
217
        raise errors.ReadError(self._path)
218
2052.6.2 by Robert Collins
Merge bzr.dev.
219
    def __iter__(self):
220
        self._fail()
221
222
    def read(self, count=-1):
223
        self._fail()
224
225
    def readlines(self):
226
        self._fail()
227
2052.6.1 by Robert Collins
``Transport.get`` has had its interface made more clear for ease of use.
228
2671.3.6 by Robert Collins
Review feedback.
229
class FileStream(object):
230
    """Base class for FileStreams."""
231
232
    def __init__(self, transport, relpath):
233
        """Create a FileStream for relpath on transport."""
234
        self.transport = transport
235
        self.relpath = relpath
236
237
    def _close(self):
238
        """A hook point for subclasses that need to take action on close."""
239
240
    def close(self):
241
        self._close()
242
        del _file_streams[self.transport.abspath(self.relpath)]
243
244
245
class FileFileStream(FileStream):
2671.3.9 by Robert Collins
Review feedback and fix VFat emulated transports to not claim to have unix permissions.
246
    """A file stream object returned by open_write_stream.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
247
2671.3.6 by Robert Collins
Review feedback.
248
    This version uses a file like object to perform writes.
249
    """
250
251
    def __init__(self, transport, relpath, file_handle):
252
        FileStream.__init__(self, transport, relpath)
253
        self.file_handle = file_handle
254
255
    def _close(self):
256
        self.file_handle.close()
257
258
    def write(self, bytes):
3635.1.2 by Robert Collins
Add osutils.pump_string_file helper function.
259
        osutils.pump_string_file(bytes, self.file_handle)
2671.3.6 by Robert Collins
Review feedback.
260
261
262
class AppendBasedFileStream(FileStream):
2671.3.9 by Robert Collins
Review feedback and fix VFat emulated transports to not claim to have unix permissions.
263
    """A file stream object returned by open_write_stream.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
264
2671.3.6 by Robert Collins
Review feedback.
265
    This version uses append on a transport to perform writes.
266
    """
267
268
    def write(self, bytes):
269
        self.transport.append_bytes(self.relpath, bytes)
270
271
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
272
class Transport(object):
273
    """This class encapsulates methods for retrieving or putting a file
274
    from/to a storage location.
275
276
    Most functions have a _multi variant, which allows you to queue up
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
277
    multiple requests. They generally have a dumb base implementation
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
278
    which just iterates over the arguments, but smart Transport
279
    implementations can do pipelining.
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
280
    In general implementations should support having a generator or a list
281
    as an argument (ie always iterate, never index)
1910.7.17 by Andrew Bennetts
Various cosmetic changes.
282
283
    :ivar base: Base URL for the transport; should always end in a slash.
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
284
    """
285
1864.5.1 by John Arbash Meinel
Change the readv combining algorithm for one that is easier to test.
286
    # implementations can override this if it is more efficient
287
    # for them to combine larger read chunks together
1864.5.4 by John Arbash Meinel
play around with tuning the partial reads.
288
    _max_readv_combine = 50
1864.5.3 by John Arbash Meinel
Allow collapsing ranges even if they are just 'close'
289
    # It is better to read this much more data in order, rather
290
    # than doing another seek. Even for the local filesystem,
291
    # there is a benefit in just reading.
292
    # TODO: jam 20060714 Do some real benchmarking to figure out
293
    #       where the biggest benefit between combining reads and
1864.5.8 by John Arbash Meinel
Cleanup and NEWS
294
    #       and seeking is. Consider a runtime auto-tune.
1864.5.4 by John Arbash Meinel
play around with tuning the partial reads.
295
    _bytes_to_read_before_seek = 0
1864.5.1 by John Arbash Meinel
Change the readv combining algorithm for one that is easier to test.
296
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
297
    def __init__(self, base):
3734.2.3 by Vincent Ladeuil
Don't use multiple inheritance for http smart medium since we
298
        super(Transport, self).__init__()
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
299
        self.base = base
300
1185.31.44 by John Arbash Meinel
Cleaned up Exceptions for all transports.
301
    def _translate_error(self, e, path, raise_generic=True):
302
        """Translate an IOError or OSError into an appropriate bzr error.
303
304
        This handles things like ENOENT, ENOTDIR, EEXIST, and EACCESS
305
        """
1963.2.6 by Robey Pointer
pychecker is on crack; go back to using 'is None'.
306
        if getattr(e, 'errno', None) is not None:
3146.4.1 by Aaron Bentley
Python 2.5 on win32 can emit errno.EINVAL when listing a non-directory
307
            if e.errno in (errno.ENOENT, errno.ENOTDIR, errno.EINVAL):
1185.31.44 by John Arbash Meinel
Cleaned up Exceptions for all transports.
308
                raise errors.NoSuchFile(path, extra=e)
1185.31.58 by John Arbash Meinel
Updating for new transport tests so that they pass on win32
309
            # I would rather use errno.EFOO, but there doesn't seem to be
310
            # any matching for 267
311
            # This is the error when doing a listdir on a file:
312
            # WindowsError: [Errno 267] The directory name is invalid
313
            if sys.platform == 'win32' and e.errno in (errno.ESRCH, 267):
314
                raise errors.NoSuchFile(path, extra=e)
1185.31.44 by John Arbash Meinel
Cleaned up Exceptions for all transports.
315
            if e.errno == errno.EEXIST:
316
                raise errors.FileExists(path, extra=e)
317
            if e.errno == errno.EACCES:
318
                raise errors.PermissionDenied(path, extra=e)
1553.5.10 by Martin Pool
New DirectoryNotEmpty exception, and raise this from local and memory
319
            if e.errno == errno.ENOTEMPTY:
320
                raise errors.DirectoryNotEmpty(path, extra=e)
1558.10.1 by Aaron Bentley
Handle lockdirs over NFS properly
321
            if e.errno == errno.EBUSY:
322
                raise errors.ResourceBusy(path, extra=e)
1185.31.44 by John Arbash Meinel
Cleaned up Exceptions for all transports.
323
        if raise_generic:
324
            raise errors.TransportError(orig_error=e)
325
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
326
    def clone(self, offset=None):
327
        """Return a new Transport object, cloned from the current location,
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
328
        using a subdirectory or parent directory. This allows connections
1185.11.6 by John Arbash Meinel
Made HttpTransport handle a request for a parent directory differently.
329
        to be pooled, rather than a new one needed for each subdir.
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
330
        """
1540.3.1 by Martin Pool
First-cut implementation of pycurl. Substantially faster than using urllib.
331
        raise NotImplementedError(self.clone)
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
332
4294.2.1 by Robert Collins
Move directory checking for bzr push options into Branch.create_clone_on_transport.
333
    def create_prefix(self):
4294.2.10 by Robert Collins
Review feedback.
334
        """Create all the directories leading down to self.base."""
4294.2.1 by Robert Collins
Move directory checking for bzr push options into Branch.create_clone_on_transport.
335
        cur_transport = self
336
        needed = [cur_transport]
337
        # Recurse upwards until we can create a directory successfully
338
        while True:
339
            new_transport = cur_transport.clone('..')
340
            if new_transport.base == cur_transport.base:
341
                raise errors.BzrCommandError(
342
                    "Failed to create path prefix for %s."
343
                    % cur_transport.base)
344
            try:
345
                new_transport.mkdir('.')
346
            except errors.NoSuchFile:
347
                needed.append(new_transport)
348
                cur_transport = new_transport
349
            except errors.FileExists:
350
                break
351
            else:
352
                break
353
        # Now we only need to create child directories
354
        while needed:
355
            cur_transport = needed.pop()
356
            cur_transport.ensure_base()
357
2475.3.2 by John Arbash Meinel
Add Transport.ensure_base()
358
    def ensure_base(self):
359
        """Ensure that the directory this transport references exists.
360
361
        This will create a directory if it doesn't exist.
362
        :return: True if the directory was created, False otherwise.
363
        """
364
        # The default implementation just uses "Easier to ask for forgiveness
365
        # than permission". We attempt to create the directory, and just
2805.3.1 by Martin Pool
Transport.ensure_base should ignore PermissionDenied when trying to create the directory (Axel Kollmorgen)
366
        # suppress FileExists and PermissionDenied (for Windows) exceptions.
2475.3.2 by John Arbash Meinel
Add Transport.ensure_base()
367
        try:
368
            self.mkdir('.')
2805.3.1 by Martin Pool
Transport.ensure_base should ignore PermissionDenied when trying to create the directory (Axel Kollmorgen)
369
        except (errors.FileExists, errors.PermissionDenied):
2475.3.2 by John Arbash Meinel
Add Transport.ensure_base()
370
            return False
371
        else:
372
            return True
373
2634.1.1 by Robert Collins
(robertc) Reinstate the accidentally backed out external_url patch.
374
    def external_url(self):
375
        """Return a URL for self that can be given to an external process.
376
377
        There is no guarantee that the URL can be accessed from a different
378
        machine - e.g. file:/// urls are only usable on the local machine,
379
        sftp:/// urls when the server is only bound to localhost are only
380
        usable from localhost etc.
381
382
        NOTE: This method may remove security wrappers (e.g. on chroot
383
        transports) and thus should *only* be used when the result will not
384
        be used to obtain a new transport within bzrlib. Ideally chroot
385
        transports would know enough to cause the external url to be the exact
386
        one used that caused the chrooting in the first place, but that is not
387
        currently the case.
388
389
        :return: A URL that can be given to another process.
390
        :raises InProcessTransport: If the transport is one that cannot be
391
            accessed out of the current process (e.g. a MemoryTransport)
392
            then InProcessTransport is raised.
393
        """
394
        raise NotImplementedError(self.external_url)
395
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
396
    def _pump(self, from_file, to_file):
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
397
        """Most children will need to copy from one file-like
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
398
        object or string to another one.
399
        This just gives them something easy to call.
400
        """
2745.5.2 by Robert Collins
* ``bzrlib.transport.Transport.put_file`` now returns the number of bytes
401
        return osutils.pumpfile(from_file, to_file)
1948.3.8 by Vincent LADEUIL
_pump accepts strings finally :)
402
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
403
    def _get_total(self, multi):
404
        """Try to figure out how many entries are in multi,
405
        but if not possible, return None.
406
        """
407
        try:
408
            return len(multi)
409
        except TypeError: # We can't tell how many, because relpaths is a generator
410
            return None
411
3882.7.1 by Martin Pool
Add stub _report_activity method as a transport callback
412
    def _report_activity(self, bytes, direction):
413
        """Notify that this transport has activity.
414
415
        Implementations should call this from all methods that actually do IO.
416
        Be careful that it's not called twice, if one method is implemented on
417
        top of another.
418
419
        :param bytes: Number of bytes read or written.
420
        :param direction: 'read' or 'write' or None.
421
        """
3882.7.5 by Martin Pool
Further mockup of transport-based activity indicator.
422
        ui.ui_factory.report_transport_activity(self, bytes, direction)
3882.7.1 by Martin Pool
Add stub _report_activity method as a transport callback
423
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
424
    def _update_pb(self, pb, msg, count, total):
425
        """Update the progress bar based on the current count
426
        and total available, total may be None if it was
427
        not possible to determine.
428
        """
907.1.14 by John Arbash Meinel
Handling some transport functions which take only a single argument.
429
        if pb is None:
430
            return
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
431
        if total is None:
432
            pb.update(msg, count, count+1)
433
        else:
434
            pb.update(msg, count, total)
435
907.1.14 by John Arbash Meinel
Handling some transport functions which take only a single argument.
436
    def _iterate_over(self, multi, func, pb, msg, expand=True):
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
437
        """Iterate over all entries in multi, passing them to func,
438
        and update the progress bar as you go along.
907.1.14 by John Arbash Meinel
Handling some transport functions which take only a single argument.
439
440
        :param expand:  If True, the entries will be passed to the function
441
                        by expanding the tuple. If False, it will be passed
442
                        as a single parameter.
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
443
        """
444
        total = self._get_total(multi)
1563.2.3 by Robert Collins
Change the return signature of transport.append and append_multi to return the length of the pre-append content.
445
        result = []
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
446
        count = 0
447
        for entry in multi:
448
            self._update_pb(pb, msg, count, total)
907.1.14 by John Arbash Meinel
Handling some transport functions which take only a single argument.
449
            if expand:
1563.2.3 by Robert Collins
Change the return signature of transport.append and append_multi to return the length of the pre-append content.
450
                result.append(func(*entry))
907.1.14 by John Arbash Meinel
Handling some transport functions which take only a single argument.
451
            else:
1563.2.3 by Robert Collins
Change the return signature of transport.append and append_multi to return the length of the pre-append content.
452
                result.append(func(entry))
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
453
            count += 1
1563.2.3 by Robert Collins
Change the return signature of transport.append and append_multi to return the length of the pre-append content.
454
        return tuple(result)
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
455
907.1.34 by John Arbash Meinel
Fixing append(), cleaning up function locations.
456
    def abspath(self, relpath):
457
        """Return the full url to the given relative path.
1910.16.6 by Andrew Bennetts
Update Transport.abspath docstring.
458
459
        :param relpath: a string of a relative path
1910.16.2 by Andrew Bennetts
Reduce transport code duplication by creating a '_combine_paths' method to Transport.
460
        """
1442.1.44 by Robert Collins
Many transport related tweaks:
461
1910.16.2 by Andrew Bennetts
Reduce transport code duplication by creating a '_combine_paths' method to Transport.
462
        # XXX: Robert Collins 20051016 - is this really needed in the public
463
        # interface ?
1540.3.1 by Martin Pool
First-cut implementation of pycurl. Substantially faster than using urllib.
464
        raise NotImplementedError(self.abspath)
907.1.34 by John Arbash Meinel
Fixing append(), cleaning up function locations.
465
1910.16.2 by Andrew Bennetts
Reduce transport code duplication by creating a '_combine_paths' method to Transport.
466
    def _combine_paths(self, base_path, relpath):
467
        """Transform a Transport-relative path to a remote absolute path.
468
469
        This does not handle substitution of ~ but does handle '..' and '.'
470
        components.
471
472
        Examples::
473
1996.3.20 by John Arbash Meinel
[merge] bzr.dev 2063
474
            t._combine_paths('/home/sarah', 'project/foo')
475
                => '/home/sarah/project/foo'
476
            t._combine_paths('/home/sarah', '../../etc')
477
                => '/etc'
2070.3.2 by Andrew Bennetts
Merge from bzr.dev
478
            t._combine_paths('/home/sarah', '/etc')
479
                => '/etc'
1910.16.2 by Andrew Bennetts
Reduce transport code duplication by creating a '_combine_paths' method to Transport.
480
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
481
        :param base_path: urlencoded path for the transport root; typically a
1910.16.2 by Andrew Bennetts
Reduce transport code duplication by creating a '_combine_paths' method to Transport.
482
             URL but need not contain scheme/host/etc.
483
        :param relpath: relative url string for relative part of remote path.
484
        :return: urlencoded string for final path.
485
        """
486
        if not isinstance(relpath, str):
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
487
            raise errors.InvalidURL(relpath)
1910.19.1 by Andrew Bennetts
Support bzr:// urls to work with the new RPC-based transport which will be used
488
        if relpath.startswith('/'):
489
            base_parts = []
490
        else:
491
            base_parts = base_path.split('/')
1910.16.2 by Andrew Bennetts
Reduce transport code duplication by creating a '_combine_paths' method to Transport.
492
        if len(base_parts) > 0 and base_parts[-1] == '':
493
            base_parts = base_parts[:-1]
494
        for p in relpath.split('/'):
495
            if p == '..':
496
                if len(base_parts) == 0:
497
                    # In most filesystems, a request for the parent
498
                    # of root, just returns root.
499
                    continue
500
                base_parts.pop()
501
            elif p == '.':
502
                continue # No-op
503
            elif p != '':
504
                base_parts.append(p)
505
        path = '/'.join(base_parts)
2070.3.1 by Andrew Bennetts
Fix memory_transport.abspath('/foo')
506
        if not path.startswith('/'):
507
            path = '/' + path
1910.16.2 by Andrew Bennetts
Reduce transport code duplication by creating a '_combine_paths' method to Transport.
508
        return path
509
2671.3.1 by Robert Collins
* New method ``bzrlib.transport.Transport.get_recommended_page_size``.
510
    def recommended_page_size(self):
511
        """Return the recommended page size for this transport.
512
513
        This is potentially different for every path in a given namespace.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
514
        For example, local transports might use an operating system call to
2671.3.1 by Robert Collins
* New method ``bzrlib.transport.Transport.get_recommended_page_size``.
515
        get the block size for a given path, which can vary due to mount
516
        points.
517
518
        :return: The page size in bytes.
519
        """
520
        return 4 * 1024
521
907.1.34 by John Arbash Meinel
Fixing append(), cleaning up function locations.
522
    def relpath(self, abspath):
523
        """Return the local path portion from a given absolute path.
1442.1.44 by Robert Collins
Many transport related tweaks:
524
525
        This default implementation is not suitable for filesystems with
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
526
        aliasing, such as that given by symlinks, where a path may not
527
        start with our base, but still be a relpath once aliasing is
1442.1.44 by Robert Collins
Many transport related tweaks:
528
        resolved.
907.1.34 by John Arbash Meinel
Fixing append(), cleaning up function locations.
529
        """
1185.31.44 by John Arbash Meinel
Cleaned up Exceptions for all transports.
530
        # TODO: This might want to use bzrlib.osutils.relpath
531
        #       but we have to watch out because of the prefix issues
1530.1.3 by Robert Collins
transport implementations now tested consistently.
532
        if not (abspath == self.base[:-1] or abspath.startswith(self.base)):
1185.31.44 by John Arbash Meinel
Cleaned up Exceptions for all transports.
533
            raise errors.PathNotChild(abspath, self.base)
1442.1.44 by Robert Collins
Many transport related tweaks:
534
        pl = len(self.base)
1530.1.3 by Robert Collins
transport implementations now tested consistently.
535
        return abspath[pl:].strip('/')
907.1.34 by John Arbash Meinel
Fixing append(), cleaning up function locations.
536
1685.1.9 by John Arbash Meinel
Updated LocalTransport so that it's base is now a URL rather than a local path. This helps consistency with all other functions. To do so, I added local_abspath() which returns the local path, and local_path_to/from_url
537
    def local_abspath(self, relpath):
538
        """Return the absolute path on the local filesystem.
539
540
        This function will only be defined for Transports which have a
541
        physical local filesystem representation.
5200.2.1 by Robert Collins
Minor local_abspath docstring improvement.
542
543
        :raises errors.NotLocalUrl: When no local path representation is
544
            available.
1685.1.9 by John Arbash Meinel
Updated LocalTransport so that it's base is now a URL rather than a local path. This helps consistency with all other functions. To do so, I added local_abspath() which returns the local path, and local_path_to/from_url
545
        """
2018.18.4 by Martin Pool
Change Transport.local_abspath to raise NotLocalUrl, and test.
546
        raise errors.NotLocalUrl(self.abspath(relpath))
1685.1.9 by John Arbash Meinel
Updated LocalTransport so that it's base is now a URL rather than a local path. This helps consistency with all other functions. To do so, I added local_abspath() which returns the local path, and local_path_to/from_url
547
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
548
    def has(self, relpath):
1442.1.44 by Robert Collins
Many transport related tweaks:
549
        """Does the file relpath exist?
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
550
1442.1.44 by Robert Collins
Many transport related tweaks:
551
        Note that some transports MAY allow querying on directories, but this
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
552
        is not part of the protocol.  In other words, the results of
1786.1.8 by John Arbash Meinel
[merge] Johan Rydberg test updates
553
        t.has("a_directory_name") are undefined.
1910.7.17 by Andrew Bennetts
Various cosmetic changes.
554
555
        :rtype: bool
1442.1.44 by Robert Collins
Many transport related tweaks:
556
        """
1540.3.1 by Martin Pool
First-cut implementation of pycurl. Substantially faster than using urllib.
557
        raise NotImplementedError(self.has)
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
558
907.1.36 by John Arbash Meinel
Moving the multi-get functionality higher up into the Branch class.
559
    def has_multi(self, relpaths, pb=None):
907.1.34 by John Arbash Meinel
Fixing append(), cleaning up function locations.
560
        """Return True/False for each entry in relpaths"""
561
        total = self._get_total(relpaths)
562
        count = 0
563
        for relpath in relpaths:
907.1.36 by John Arbash Meinel
Moving the multi-get functionality higher up into the Branch class.
564
            self._update_pb(pb, 'has', count, total)
907.1.34 by John Arbash Meinel
Fixing append(), cleaning up function locations.
565
            yield self.has(relpath)
566
            count += 1
567
1185.16.155 by John Arbash Meinel
Added a has_any function to the Transport API
568
    def has_any(self, relpaths):
569
        """Return True if any of the paths exist."""
570
        for relpath in relpaths:
571
            if self.has(relpath):
572
                return True
573
        return False
574
1442.1.44 by Robert Collins
Many transport related tweaks:
575
    def iter_files_recursive(self):
576
        """Iter the relative paths of files in the transports sub-tree.
1553.5.13 by Martin Pool
New Transport.rename that mustn't overwrite
577
578
        *NOTE*: This only lists *files*, not subdirectories!
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
579
1442.1.44 by Robert Collins
Many transport related tweaks:
580
        As with other listing functions, only some transports implement this,.
3484.1.1 by Vincent Ladeuil
Trivial drive-by cleanups.
581
        you may check via listable() to determine if it will.
1442.1.44 by Robert Collins
Many transport related tweaks:
582
        """
1530.1.4 by Robert Collins
integrate Memory tests into transport interface tests.
583
        raise errors.TransportNotPossible("This transport has not "
1530.1.21 by Robert Collins
Review feedback fixes.
584
                                          "implemented iter_files_recursive "
1530.1.4 by Robert Collins
integrate Memory tests into transport interface tests.
585
                                          "(but must claim to be listable "
586
                                          "to trigger this error).")
1442.1.44 by Robert Collins
Many transport related tweaks:
587
2164.2.15 by Vincent Ladeuil
Http redirections are not followed by default. Do not use hints
588
    def get(self, relpath):
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
589
        """Get the file at the given relative path.
907.1.20 by John Arbash Meinel
Removed Transport.open(), making get + put encode/decode to utf-8
590
2052.6.1 by Robert Collins
``Transport.get`` has had its interface made more clear for ease of use.
591
        This may fail in a number of ways:
592
         - HTTP servers may return content for a directory. (unexpected
593
           content failure)
594
         - FTP servers may indicate NoSuchFile for a directory.
595
         - SFTP servers may give a file handle for a directory that will
596
           fail on read().
597
598
        For correct use of the interface, be sure to catch errors.PathError
599
        when calling it and catch errors.ReadError when reading from the
600
        returned object.
601
907.1.20 by John Arbash Meinel
Removed Transport.open(), making get + put encode/decode to utf-8
602
        :param relpath: The relative path to the file
1910.19.2 by Andrew Bennetts
Add a new method ``Transport.get_smart_client()``. This is provided to allow
603
        :rtype: File-like object.
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
604
        """
1540.3.1 by Martin Pool
First-cut implementation of pycurl. Substantially faster than using urllib.
605
        raise NotImplementedError(self.get)
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
606
1955.3.3 by John Arbash Meinel
Implement and test 'get_bytes'
607
    def get_bytes(self, relpath):
608
        """Get a raw string of the bytes for a file at the given location.
609
610
        :param relpath: The relative path to the file
611
        """
3882.7.3 by Martin Pool
transport.get should specifically close the file handle
612
        f = self.get(relpath)
613
        try:
614
            return f.read()
615
        finally:
616
            f.close()
1910.19.2 by Andrew Bennetts
Add a new method ``Transport.get_smart_client()``. This is provided to allow
617
2018.2.3 by Andrew Bennetts
Starting factoring out the smart server client "medium" from the protocol.
618
    def get_smart_medium(self):
619
        """Return a smart client medium for this transport if possible.
620
621
        A smart medium doesn't imply the presence of a smart server: it implies
622
        that the smart protocol can be tunnelled via this transport.
623
624
        :raises NoSmartMedium: if no smart server medium is available.
625
        """
626
        raise errors.NoSmartMedium(self)
627
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
628
    def readv(self, relpath, offsets, adjust_for_latency=False,
629
        upper_limit=None):
2745.5.1 by Robert Collins
* New parameter on ``bzrlib.transport.Transport.readv``
630
        """Get parts of the file at the given relative path.
631
632
        :param relpath: The path to read data from.
633
        :param offsets: A list of (offset, size) tuples.
3024.2.1 by Vincent Ladeuil
Fix 165061 by using the correct _max_readv_combine attribute.
634
        :param adjust_for_latency: Adjust the requested offsets to accomodate
2745.5.1 by Robert Collins
* New parameter on ``bzrlib.transport.Transport.readv``
635
            transport latency. This may re-order the offsets, expand them to
636
            grab adjacent data when there is likely a high cost to requesting
637
            data relative to delivering it.
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
638
        :param upper_limit: When adjust_for_latency is True setting upper_limit
639
            allows the caller to tell the transport about the length of the
640
            file, so that requests are not issued for ranges beyond the end of
641
            the file. This matters because some servers and/or transports error
642
            in such a case rather than just satisfying the available ranges.
643
            upper_limit should always be provided when adjust_for_latency is
644
            True, and should be the size of the file in bytes.
2745.5.1 by Robert Collins
* New parameter on ``bzrlib.transport.Transport.readv``
645
        :return: A list or generator of (offset, data) tuples
646
        """
647
        if adjust_for_latency:
2890.1.3 by Robert Collins
Review feedback and discussion with Martin - split out the readv offset adjustment into a new helper and document where the design might/should go next.
648
            # Design note: We may wish to have different algorithms for the
649
            # expansion of the offsets per-transport. E.g. for local disk to
3024.2.1 by Vincent Ladeuil
Fix 165061 by using the correct _max_readv_combine attribute.
650
            # use page-aligned expansion. If that is the case consider the
651
            # following structure:
652
            #  - a test that transport.readv uses self._offset_expander or some
653
            #    similar attribute, to do the expansion
654
            #  - a test for each transport that it has some known-good offset
655
            #    expander
2890.1.3 by Robert Collins
Review feedback and discussion with Martin - split out the readv offset adjustment into a new helper and document where the design might/should go next.
656
            #  - unit tests for each offset expander
657
            #  - a set of tests for the offset expander interface, giving
658
            #    baseline behaviour (which the current transport
659
            #    adjust_for_latency tests could be repurposed to).
660
            offsets = self._sort_expand_and_combine(offsets, upper_limit)
2745.5.1 by Robert Collins
* New parameter on ``bzrlib.transport.Transport.readv``
661
        return self._readv(relpath, offsets)
662
663
    def _readv(self, relpath, offsets):
1594.2.5 by Robert Collins
Readv patch from Johan Rydberg giving knits partial download support.
664
        """Get parts of the file at the given relative path.
665
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
666
        :param relpath: The path to read.
667
        :param offsets: A list of (offset, size) tuples.
1594.2.5 by Robert Collins
Readv patch from Johan Rydberg giving knits partial download support.
668
        :return: A list or generator of (offset, data) tuples
669
        """
1864.5.1 by John Arbash Meinel
Change the readv combining algorithm for one that is easier to test.
670
        if not offsets:
1594.2.16 by Robert Collins
Coalesce readv requests on file based transports.
671
            return
1864.5.1 by John Arbash Meinel
Change the readv combining algorithm for one that is easier to test.
672
1864.5.7 by John Arbash Meinel
remove disable prefetch support
673
        fp = self.get(relpath)
2001.3.2 by John Arbash Meinel
Force all transports to raise ShortReadvError if they can
674
        return self._seek_and_read(fp, offsets, relpath)
1864.5.11 by John Arbash Meinel
Factor out the workhorse of Transport.readv() into a helper function, and re-use that function in sftp but with a non-prefetch file.
675
2001.3.2 by John Arbash Meinel
Force all transports to raise ShortReadvError if they can
676
    def _seek_and_read(self, fp, offsets, relpath='<unknown>'):
1864.5.11 by John Arbash Meinel
Factor out the workhorse of Transport.readv() into a helper function, and re-use that function in sftp but with a non-prefetch file.
677
        """An implementation of readv that uses fp.seek and fp.read.
678
679
        This uses _coalesce_offsets to issue larger reads and fewer seeks.
680
681
        :param fp: A file-like object that supports seek() and read(size)
682
        :param offsets: A list of offsets to be read from the given file.
683
        :return: yield (pos, data) tuples for each request
684
        """
1864.5.2 by John Arbash Meinel
always read in sorted order, and return in requested order, but only cache what is currently out of order
685
        # We are going to iterate multiple times, we need a list
686
        offsets = list(offsets)
687
        sorted_offsets = sorted(offsets)
688
689
        # turn the list of offsets into a stack
1864.5.9 by John Arbash Meinel
Switch to returning an object to make the api more understandable.
690
        offset_stack = iter(offsets)
691
        cur_offset_and_size = offset_stack.next()
1864.5.2 by John Arbash Meinel
always read in sorted order, and return in requested order, but only cache what is currently out of order
692
        coalesced = self._coalesce_offsets(sorted_offsets,
1864.5.3 by John Arbash Meinel
Allow collapsing ranges even if they are just 'close'
693
                               limit=self._max_readv_combine,
694
                               fudge_factor=self._bytes_to_read_before_seek)
1864.5.2 by John Arbash Meinel
always read in sorted order, and return in requested order, but only cache what is currently out of order
695
696
        # Cache the results, but only until they have been fulfilled
697
        data_map = {}
1864.5.9 by John Arbash Meinel
Switch to returning an object to make the api more understandable.
698
        for c_offset in coalesced:
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
699
            # TODO: jam 20060724 it might be faster to not issue seek if
1864.5.11 by John Arbash Meinel
Factor out the workhorse of Transport.readv() into a helper function, and re-use that function in sftp but with a non-prefetch file.
700
            #       we are already at the right location. This should be
701
            #       benchmarked.
1864.5.9 by John Arbash Meinel
Switch to returning an object to make the api more understandable.
702
            fp.seek(c_offset.start)
703
            data = fp.read(c_offset.length)
2001.3.2 by John Arbash Meinel
Force all transports to raise ShortReadvError if they can
704
            if len(data) < c_offset.length:
705
                raise errors.ShortReadvError(relpath, c_offset.start,
2001.3.3 by John Arbash Meinel
review feedback: add the actual count written to ShortReadvError
706
                            c_offset.length, actual=len(data))
1864.5.9 by John Arbash Meinel
Switch to returning an object to make the api more understandable.
707
            for suboffset, subsize in c_offset.ranges:
708
                key = (c_offset.start+suboffset, subsize)
709
                data_map[key] = data[suboffset:suboffset+subsize]
1864.5.1 by John Arbash Meinel
Change the readv combining algorithm for one that is easier to test.
710
1864.5.8 by John Arbash Meinel
Cleanup and NEWS
711
            # Now that we've read some data, see if we can yield anything back
1864.5.9 by John Arbash Meinel
Switch to returning an object to make the api more understandable.
712
            while cur_offset_and_size in data_map:
713
                this_data = data_map.pop(cur_offset_and_size)
3794.2.2 by John Arbash Meinel
Instead of counting the remaining offsets, trap the offset exception
714
                this_offset = cur_offset_and_size[0]
715
                try:
716
                    cur_offset_and_size = offset_stack.next()
717
                except StopIteration:
718
                    # Close the file handle as there will be no more data
719
                    # The handle would normally be cleaned up as this code goes
720
                    # out of scope, but as we are a generator, not all code
721
                    # will re-enter once we have consumed all the expected
722
                    # data. For example:
723
                    #   zip(range(len(requests)), readv(foo, requests))
724
                    # Will stop because the range is done, and not run the
725
                    # cleanup code for the readv().
726
                    fp.close()
727
                    cur_offset_and_size = None
728
                yield this_offset, this_data
1864.5.1 by John Arbash Meinel
Change the readv combining algorithm for one that is easier to test.
729
2890.1.3 by Robert Collins
Review feedback and discussion with Martin - split out the readv offset adjustment into a new helper and document where the design might/should go next.
730
    def _sort_expand_and_combine(self, offsets, upper_limit):
731
        """Helper for readv.
732
733
        :param offsets: A readv vector - (offset, length) tuples.
734
        :param upper_limit: The highest byte offset that may be requested.
735
        :return: A readv vector that will read all the regions requested by
736
            offsets, in start-to-end order, with no duplicated regions,
737
            expanded by the transports recommended page size.
738
        """
739
        offsets = sorted(offsets)
740
        # short circuit empty requests
741
        if len(offsets) == 0:
742
            def empty_yielder():
743
                # Quick thunk to stop this function becoming a generator
744
                # itself, rather we return a generator that has nothing to
745
                # yield.
746
                if False:
747
                    yield None
748
            return empty_yielder()
749
        # expand by page size at either end
750
        maximum_expansion = self.recommended_page_size()
751
        new_offsets = []
752
        for offset, length in offsets:
753
            expansion = maximum_expansion - length
754
            if expansion < 0:
755
                # we're asking for more than the minimum read anyway.
756
                expansion = 0
757
            reduction = expansion / 2
758
            new_offset = offset - reduction
759
            new_length = length + expansion
760
            if new_offset < 0:
761
                # don't ask for anything < 0
762
                new_offset = 0
763
            if (upper_limit is not None and
764
                new_offset + new_length > upper_limit):
765
                new_length = upper_limit - new_offset
766
            new_offsets.append((new_offset, new_length))
767
        # combine the expanded offsets
768
        offsets = []
769
        current_offset, current_length = new_offsets[0]
770
        current_finish = current_length + current_offset
771
        for offset, length in new_offsets[1:]:
772
            finish = offset + length
3805.4.2 by John Arbash Meinel
Revert the changes to _sort_expand_and_combine
773
            if offset > current_finish:
774
                # there is a gap, output the current accumulator and start
775
                # a new one for the region we're examining.
2890.1.3 by Robert Collins
Review feedback and discussion with Martin - split out the readv offset adjustment into a new helper and document where the design might/should go next.
776
                offsets.append((current_offset, current_length))
777
                current_offset = offset
778
                current_length = length
779
                current_finish = finish
780
                continue
781
            if finish > current_finish:
782
                # extend the current accumulator to the end of the region
783
                # we're examining.
784
                current_finish = finish
785
                current_length = finish - current_offset
786
        offsets.append((current_offset, current_length))
787
        return offsets
788
1864.5.1 by John Arbash Meinel
Change the readv combining algorithm for one that is easier to test.
789
    @staticmethod
3686.1.6 by John Arbash Meinel
Respond to Martin's review comments.
790
    def _coalesce_offsets(offsets, limit=0, fudge_factor=0, max_size=0):
1864.5.1 by John Arbash Meinel
Change the readv combining algorithm for one that is easier to test.
791
        """Yield coalesced offsets.
792
793
        With a long list of neighboring requests, combine them
794
        into a single large request, while retaining the original
795
        offsets.
796
        Turns  [(15, 10), (25, 10)] => [(15, 20, [(0, 10), (10, 10)])]
3686.1.6 by John Arbash Meinel
Respond to Martin's review comments.
797
        Note that overlapping requests are not permitted. (So [(15, 10), (20,
798
        10)] will raise a ValueError.) This is because the data we access never
799
        overlaps, and it allows callers to trust that we only need any byte of
800
        data for 1 request (so nothing needs to be buffered to fulfill a second
801
        request.)
1864.5.1 by John Arbash Meinel
Change the readv combining algorithm for one that is easier to test.
802
803
        :param offsets: A list of (start, length) pairs
3059.2.17 by Vincent Ladeuil
Limit GET requests by body size instead of number of ranges.
804
        :param limit: Only combine a maximum of this many pairs Some transports
805
                penalize multiple reads more than others, and sometimes it is
806
                better to return early.
807
                0 means no limit
1864.5.3 by John Arbash Meinel
Allow collapsing ranges even if they are just 'close'
808
        :param fudge_factor: All transports have some level of 'it is
3120.2.1 by John Arbash Meinel
Change the sftp_readv loop to buffer even less.
809
                better to read some more data and throw it away rather
1864.5.3 by John Arbash Meinel
Allow collapsing ranges even if they are just 'close'
810
                than seek', so collapse if we are 'close enough'
3059.2.17 by Vincent Ladeuil
Limit GET requests by body size instead of number of ranges.
811
        :param max_size: Create coalesced offsets no bigger than this size.
812
                When a single offset is bigger than 'max_size', it will keep
813
                its size and be alone in the coalesced offset.
814
                0 means no maximum size.
3120.2.1 by John Arbash Meinel
Change the sftp_readv loop to buffer even less.
815
        :return: return a list of _CoalescedOffset objects, which have members
816
            for where to start, how much to read, and how to split those chunks
817
            back up
1864.5.1 by John Arbash Meinel
Change the readv combining algorithm for one that is easier to test.
818
        """
819
        last_end = None
1864.5.9 by John Arbash Meinel
Switch to returning an object to make the api more understandable.
820
        cur = _CoalescedOffset(None, None, [])
3120.2.1 by John Arbash Meinel
Change the sftp_readv loop to buffer even less.
821
        coalesced_offsets = []
1864.5.1 by John Arbash Meinel
Change the readv combining algorithm for one that is easier to test.
822
3876.1.1 by John Arbash Meinel
Fix bug #303538 by capping the max per-hunk read size at 100MB.
823
        if max_size <= 0:
824
            # 'unlimited', but we actually take this to mean 100MB buffer limit
825
            max_size = 100*1024*1024
826
1864.5.1 by John Arbash Meinel
Change the readv combining algorithm for one that is easier to test.
827
        for start, size in offsets:
828
            end = start + size
3059.2.17 by Vincent Ladeuil
Limit GET requests by body size instead of number of ranges.
829
            if (last_end is not None
1864.5.3 by John Arbash Meinel
Allow collapsing ranges even if they are just 'close'
830
                and start <= last_end + fudge_factor
1864.5.9 by John Arbash Meinel
Switch to returning an object to make the api more understandable.
831
                and start >= cur.start
3059.2.17 by Vincent Ladeuil
Limit GET requests by body size instead of number of ranges.
832
                and (limit <= 0 or len(cur.ranges) < limit)
833
                and (max_size <= 0 or end - cur.start <= max_size)):
3686.1.6 by John Arbash Meinel
Respond to Martin's review comments.
834
                if start < last_end:
3686.1.9 by John Arbash Meinel
Overlapping ranges are not allowed anymore.
835
                    raise ValueError('Overlapping range not allowed:'
3686.1.6 by John Arbash Meinel
Respond to Martin's review comments.
836
                        ' last range ended at %s, new one starts at %s'
837
                        % (last_end, start))
1864.5.9 by John Arbash Meinel
Switch to returning an object to make the api more understandable.
838
                cur.length = end - cur.start
839
                cur.ranges.append((start-cur.start, size))
1594.2.16 by Robert Collins
Coalesce readv requests on file based transports.
840
            else:
1864.5.9 by John Arbash Meinel
Switch to returning an object to make the api more understandable.
841
                if cur.start is not None:
3120.2.1 by John Arbash Meinel
Change the sftp_readv loop to buffer even less.
842
                    coalesced_offsets.append(cur)
1864.5.9 by John Arbash Meinel
Switch to returning an object to make the api more understandable.
843
                cur = _CoalescedOffset(start, size, [(0, size)])
1864.5.1 by John Arbash Meinel
Change the readv combining algorithm for one that is easier to test.
844
            last_end = end
845
1864.5.9 by John Arbash Meinel
Switch to returning an object to make the api more understandable.
846
        if cur.start is not None:
3120.2.1 by John Arbash Meinel
Change the sftp_readv loop to buffer even less.
847
            coalesced_offsets.append(cur)
848
        return coalesced_offsets
1594.2.5 by Robert Collins
Readv patch from Johan Rydberg giving knits partial download support.
849
907.1.50 by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown.
850
    def get_multi(self, relpaths, pb=None):
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
851
        """Get a list of file-like objects, one for each entry in relpaths.
852
853
        :param relpaths: A list of relative paths.
4463.1.1 by Martin Pool
Update docstrings for recent progress changes
854
        :param pb:  An optional ProgressTask for indicating percent done.
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
855
        :return: A list or generator of file-like objects
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
856
        """
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
857
        # TODO: Consider having this actually buffer the requests,
858
        # in the default mode, it probably won't give worse performance,
859
        # and all children wouldn't have to implement buffering
907.1.16 by John Arbash Meinel
Fixing a few cut&paste typos.
860
        total = self._get_total(relpaths)
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
861
        count = 0
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
862
        for relpath in relpaths:
907.1.16 by John Arbash Meinel
Fixing a few cut&paste typos.
863
            self._update_pb(pb, 'get', count, total)
907.1.50 by John Arbash Meinel
Removed encode/decode from Transport.put/get, added more exceptions that can be thrown.
864
            yield self.get(relpath)
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
865
            count += 1
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
866
1955.3.27 by John Arbash Meinel
rename non_atomic_put_* to put_*non_atomic, and re-order the functions
867
    def put_bytes(self, relpath, bytes, mode=None):
868
        """Atomically put the supplied bytes into the given location.
869
870
        :param relpath: The location to put the contents, relative to the
871
            transport base.
872
        :param bytes: A bytestring of data.
873
        :param mode: Create the file with the given mode.
874
        :return: None
875
        """
2414.1.3 by Andrew Bennetts
Fix test failures under 'python2.4 -O' ('python2.5 -O' was already passing).
876
        if not isinstance(bytes, str):
877
            raise AssertionError(
878
                'bytes must be a plain string, not %s' % type(bytes))
1955.3.27 by John Arbash Meinel
rename non_atomic_put_* to put_*non_atomic, and re-order the functions
879
        return self.put_file(relpath, StringIO(bytes), mode=mode)
880
881
    def put_bytes_non_atomic(self, relpath, bytes, mode=None,
1946.2.12 by John Arbash Meinel
Add ability to pass a directory mode to non_atomic_put
882
                             create_parent_dir=False,
883
                             dir_mode=None):
1955.3.27 by John Arbash Meinel
rename non_atomic_put_* to put_*non_atomic, and re-order the functions
884
        """Copy the string into the target location.
885
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
886
        This function is not strictly safe to use. See
1955.3.27 by John Arbash Meinel
rename non_atomic_put_* to put_*non_atomic, and re-order the functions
887
        Transport.put_bytes_non_atomic for more information.
888
889
        :param relpath: The remote location to put the contents.
890
        :param bytes:   A string object containing the raw bytes to write into
891
                        the target file.
892
        :param mode:    Possible access permissions for new file.
893
                        None means do not set remote permissions.
894
        :param create_parent_dir: If we cannot create the target file because
895
                        the parent directory does not exist, go ahead and
896
                        create it, and then try again.
1946.2.12 by John Arbash Meinel
Add ability to pass a directory mode to non_atomic_put
897
        :param dir_mode: Possible access permissions for new directories.
1955.3.27 by John Arbash Meinel
rename non_atomic_put_* to put_*non_atomic, and re-order the functions
898
        """
2414.1.3 by Andrew Bennetts
Fix test failures under 'python2.4 -O' ('python2.5 -O' was already passing).
899
        if not isinstance(bytes, str):
900
            raise AssertionError(
901
                'bytes must be a plain string, not %s' % type(bytes))
1955.3.27 by John Arbash Meinel
rename non_atomic_put_* to put_*non_atomic, and re-order the functions
902
        self.put_file_non_atomic(relpath, StringIO(bytes), mode=mode,
1946.2.12 by John Arbash Meinel
Add ability to pass a directory mode to non_atomic_put
903
                                 create_parent_dir=create_parent_dir,
904
                                 dir_mode=dir_mode)
1955.3.27 by John Arbash Meinel
rename non_atomic_put_* to put_*non_atomic, and re-order the functions
905
1955.3.6 by John Arbash Meinel
Lots of deprecation warnings, but no errors
906
    def put_file(self, relpath, f, mode=None):
907
        """Copy the file-like object into the location.
908
909
        :param relpath: Location to put the contents, relative to base.
910
        :param f:       File-like object.
911
        :param mode: The mode for the newly created file,
912
                     None means just use the default.
2745.5.2 by Robert Collins
* ``bzrlib.transport.Transport.put_file`` now returns the number of bytes
913
        :return: The length of the file that was written.
1955.3.6 by John Arbash Meinel
Lots of deprecation warnings, but no errors
914
        """
915
        # We would like to mark this as NotImplemented, but most likely
916
        # transports have defined it in terms of the old api.
917
        symbol_versioning.warn('Transport %s should implement put_file,'
918
                               ' rather than implementing put() as of'
919
                               ' version 0.11.'
920
                               % (self.__class__.__name__,),
921
                               DeprecationWarning)
922
        return self.put(relpath, f, mode=mode)
923
        #raise NotImplementedError(self.put_file)
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
924
1955.3.27 by John Arbash Meinel
rename non_atomic_put_* to put_*non_atomic, and re-order the functions
925
    def put_file_non_atomic(self, relpath, f, mode=None,
1946.2.12 by John Arbash Meinel
Add ability to pass a directory mode to non_atomic_put
926
                            create_parent_dir=False,
927
                            dir_mode=None):
1946.1.1 by John Arbash Meinel
Stub out the test and basic implementation for 'non_atomic_put'
928
        """Copy the file-like object into the target location.
929
930
        This function is not strictly safe to use. It is only meant to
931
        be used when you already know that the target does not exist.
932
        It is not safe, because it will open and truncate the remote
933
        file. So there may be a time when the file has invalid contents.
934
935
        :param relpath: The remote location to put the contents.
936
        :param f:       File-like object.
937
        :param mode:    Possible access permissions for new file.
938
                        None means do not set remote permissions.
1946.1.8 by John Arbash Meinel
Update non_atomic_put to have a create_parent_dir flag
939
        :param create_parent_dir: If we cannot create the target file because
940
                        the parent directory does not exist, go ahead and
941
                        create it, and then try again.
1946.2.12 by John Arbash Meinel
Add ability to pass a directory mode to non_atomic_put
942
        :param dir_mode: Possible access permissions for new directories.
1946.1.1 by John Arbash Meinel
Stub out the test and basic implementation for 'non_atomic_put'
943
        """
944
        # Default implementation just does an atomic put.
1946.1.8 by John Arbash Meinel
Update non_atomic_put to have a create_parent_dir flag
945
        try:
1955.3.19 by John Arbash Meinel
rename non_atomic_put => non_atomic_put_file
946
            return self.put_file(relpath, f, mode=mode)
1946.1.8 by John Arbash Meinel
Update non_atomic_put to have a create_parent_dir flag
947
        except errors.NoSuchFile:
948
            if not create_parent_dir:
949
                raise
950
            parent_dir = osutils.dirname(relpath)
951
            if parent_dir:
1946.2.12 by John Arbash Meinel
Add ability to pass a directory mode to non_atomic_put
952
                self.mkdir(parent_dir, mode=dir_mode)
1955.3.19 by John Arbash Meinel
rename non_atomic_put => non_atomic_put_file
953
                return self.put_file(relpath, f, mode=mode)
1946.1.1 by John Arbash Meinel
Stub out the test and basic implementation for 'non_atomic_put'
954
1185.58.2 by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work.
955
    def mkdir(self, relpath, mode=None):
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
956
        """Create a directory at the given path."""
1540.3.1 by Martin Pool
First-cut implementation of pycurl. Substantially faster than using urllib.
957
        raise NotImplementedError(self.mkdir)
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
958
1185.58.2 by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work.
959
    def mkdir_multi(self, relpaths, mode=None, pb=None):
907.1.47 by John Arbash Meinel
Created mkdir_multi, modified branch to use _multi forms when initializing a branch, creating a full test routine for transports
960
        """Create a group of directories"""
1185.58.2 by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work.
961
        def mkdir(path):
962
            self.mkdir(path, mode=mode)
1563.2.3 by Robert Collins
Change the return signature of transport.append and append_multi to return the length of the pre-append content.
963
        return len(self._iterate_over(relpaths, mkdir, pb, 'mkdir', expand=False))
907.1.47 by John Arbash Meinel
Created mkdir_multi, modified branch to use _multi forms when initializing a branch, creating a full test routine for transports
964
2671.3.9 by Robert Collins
Review feedback and fix VFat emulated transports to not claim to have unix permissions.
965
    def open_write_stream(self, relpath, mode=None):
966
        """Open a writable file stream at relpath.
2671.3.2 by Robert Collins
Start open_file_stream logic.
967
2671.3.9 by Robert Collins
Review feedback and fix VFat emulated transports to not claim to have unix permissions.
968
        A file stream is a file like object with a write() method that accepts
969
        bytes to write.. Buffering may occur internally until the stream is
970
        closed with stream.close().  Calls to readv or the get_* methods will
971
        be synchronised with any internal buffering that may be present.
2671.3.2 by Robert Collins
Start open_file_stream logic.
972
973
        :param relpath: The relative path to the file.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
974
        :param mode: The mode for the newly created file,
2671.3.3 by Robert Collins
Add mode parameter to Transport.open_file_stream.
975
                     None means just use the default
2671.3.6 by Robert Collins
Review feedback.
976
        :return: A FileStream. FileStream objects have two methods, write() and
977
            close(). There is no guarantee that data is committed to the file
978
            if close() has not been called (even if get() is called on the same
979
            path).
2671.3.2 by Robert Collins
Start open_file_stream logic.
980
        """
2671.3.9 by Robert Collins
Review feedback and fix VFat emulated transports to not claim to have unix permissions.
981
        raise NotImplementedError(self.open_write_stream)
2671.3.2 by Robert Collins
Start open_file_stream logic.
982
1955.3.15 by John Arbash Meinel
Deprecate 'Transport.append' in favor of Transport.append_file or Transport.append_bytes
983
    def append_file(self, relpath, f, mode=None):
1910.7.18 by Andrew Bennetts
Merge from bzr.dev
984
        """Append bytes from a file-like object to a file at relpath.
985
986
        The file is created if it does not already exist.
987
988
        :param f: a file-like object of the bytes to append.
989
        :param mode: Unix mode for newly created files.  This is not used for
990
            existing files.
991
992
        :returns: the length of relpath before the content was written to it.
1955.3.15 by John Arbash Meinel
Deprecate 'Transport.append' in favor of Transport.append_file or Transport.append_bytes
993
        """
994
        symbol_versioning.warn('Transport %s should implement append_file,'
995
                               ' rather than implementing append() as of'
996
                               ' version 0.11.'
997
                               % (self.__class__.__name__,),
998
                               DeprecationWarning)
999
        return self.append(relpath, f, mode=mode)
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
1000
1955.3.2 by John Arbash Meinel
Implement and test 'Transport.append_bytes', cleanup the tests of plain append
1001
    def append_bytes(self, relpath, bytes, mode=None):
1910.7.18 by Andrew Bennetts
Merge from bzr.dev
1002
        """Append bytes to a file at relpath.
1003
1004
        The file is created if it does not already exist.
1005
1006
        :type f: str
1007
        :param f: a string of the bytes to append.
1008
        :param mode: Unix mode for newly created files.  This is not used for
1009
            existing files.
1010
1011
        :returns: the length of relpath before the content was written to it.
1955.3.2 by John Arbash Meinel
Implement and test 'Transport.append_bytes', cleanup the tests of plain append
1012
        """
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
1013
        if not isinstance(bytes, str):
1014
            raise TypeError(
1015
                'bytes must be a plain string, not %s' % type(bytes))
1955.3.15 by John Arbash Meinel
Deprecate 'Transport.append' in favor of Transport.append_file or Transport.append_bytes
1016
        return self.append_file(relpath, StringIO(bytes), mode=mode)
1955.3.2 by John Arbash Meinel
Implement and test 'Transport.append_bytes', cleanup the tests of plain append
1017
1185.11.19 by John Arbash Meinel
Testing put and append, also testing agaist file-like objects as well as strings.
1018
    def append_multi(self, files, pb=None):
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
1019
        """Append the text in each file-like or string object to
1020
        the supplied location.
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
1021
1022
        :param files: A set of (path, f) entries
4463.1.1 by Martin Pool
Update docstrings for recent progress changes
1023
        :param pb:  An optional ProgressTask for indicating percent done.
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
1024
        """
1955.3.15 by John Arbash Meinel
Deprecate 'Transport.append' in favor of Transport.append_file or Transport.append_bytes
1025
        return self._iterate_over(files, self.append_file, pb, 'append', expand=True)
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
1026
1027
    def copy(self, rel_from, rel_to):
1530.1.3 by Robert Collins
transport implementations now tested consistently.
1028
        """Copy the item at rel_from to the location at rel_to.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1029
1030
        Override this for efficiency if a specific transport can do it
1530.1.3 by Robert Collins
transport implementations now tested consistently.
1031
        faster than this default implementation.
1032
        """
1955.3.11 by John Arbash Meinel
Clean up the rest of the api calls to deprecated functions in the test suite, and make sure Transport._pump is only accepting files, not strings
1033
        self.put_file(rel_to, self.get(rel_from))
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
1034
907.1.28 by John Arbash Meinel
Added pb to function that were missing, implemented a basic double-dispatch copy_to function.
1035
    def copy_multi(self, relpaths, pb=None):
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
1036
        """Copy a bunch of entries.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1037
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
1038
        :param relpaths: A list of tuples of the form [(from, to), (from, to),...]
1039
        """
1040
        # This is the non-pipelined implementation, so that
1041
        # implementors don't have to implement everything.
907.1.14 by John Arbash Meinel
Handling some transport functions which take only a single argument.
1042
        return self._iterate_over(relpaths, self.copy, pb, 'copy', expand=True)
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
1043
1185.58.2 by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work.
1044
    def copy_to(self, relpaths, other, mode=None, pb=None):
907.1.28 by John Arbash Meinel
Added pb to function that were missing, implemented a basic double-dispatch copy_to function.
1045
        """Copy a set of entries from self into another Transport.
1046
1047
        :param relpaths: A list/generator of entries to be copied.
1185.58.2 by John Arbash Meinel
Added mode to the appropriate transport functions, and tests to make sure they work.
1048
        :param mode: This is the target mode for the newly created files
1185.16.156 by John Arbash Meinel
Adding a note about changing copy_to's interface
1049
        TODO: This interface needs to be updated so that the target location
1050
              can be different from the source location.
907.1.28 by John Arbash Meinel
Added pb to function that were missing, implemented a basic double-dispatch copy_to function.
1051
        """
1052
        # The dummy implementation just does a simple get + put
1053
        def copy_entry(path):
1955.3.11 by John Arbash Meinel
Clean up the rest of the api calls to deprecated functions in the test suite, and make sure Transport._pump is only accepting files, not strings
1054
            other.put_file(path, self.get(path), mode=mode)
907.1.28 by John Arbash Meinel
Added pb to function that were missing, implemented a basic double-dispatch copy_to function.
1055
1563.2.3 by Robert Collins
Change the return signature of transport.append and append_multi to return the length of the pre-append content.
1056
        return len(self._iterate_over(relpaths, copy_entry, pb, 'copy_to', expand=False))
907.1.28 by John Arbash Meinel
Added pb to function that were missing, implemented a basic double-dispatch copy_to function.
1057
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
1058
    def copy_tree(self, from_relpath, to_relpath):
1059
        """Copy a subtree from one relpath to another.
1060
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1061
        If a faster implementation is available, specific transports should
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
1062
        implement it.
1063
        """
1064
        source = self.clone(from_relpath)
1065
        target = self.clone(to_relpath)
4634.148.5 by Martin Pool
Additional fix for setting permissions across ftp (thanks Parth)
1066
1067
        # create target directory with the same rwx bits as source.
1068
        # use mask to ensure that bits other than rwx are ignored.
4634.148.1 by Martin Pool
Backport fix for permissions of backup.bzr
1069
        stat = self.stat(from_relpath)
4634.148.5 by Martin Pool
Additional fix for setting permissions across ftp (thanks Parth)
1070
        target.mkdir('.', stat.st_mode & 0777)
1551.21.5 by Aaron Bentley
Implement copy_tree on copy_tree_to_transport
1071
        source.copy_tree_to_transport(target)
1072
1073
    def copy_tree_to_transport(self, to_transport):
1074
        """Copy a subtree from one transport to another.
1075
1076
        self.base is used as the source tree root, and to_transport.base
1077
        is used as the target.  to_transport.base must exist (and be a
1078
        directory).
1079
        """
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
1080
        files = []
1081
        directories = ['.']
1082
        while directories:
1083
            dir = directories.pop()
1084
            if dir != '.':
1551.21.5 by Aaron Bentley
Implement copy_tree on copy_tree_to_transport
1085
                to_transport.mkdir(dir)
1086
            for path in self.list_dir(dir):
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
1087
                path = dir + '/' + path
1551.21.5 by Aaron Bentley
Implement copy_tree on copy_tree_to_transport
1088
                stat = self.stat(path)
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
1089
                if S_ISDIR(stat.st_mode):
1090
                    directories.append(path)
1091
                else:
1092
                    files.append(path)
1551.21.5 by Aaron Bentley
Implement copy_tree on copy_tree_to_transport
1093
        self.copy_to(files, to_transport)
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
1094
1553.5.13 by Martin Pool
New Transport.rename that mustn't overwrite
1095
    def rename(self, rel_from, rel_to):
1096
        """Rename a file or directory.
1097
1098
        This *must* fail if the destination is a nonempty directory - it must
1099
        not automatically remove it.  It should raise DirectoryNotEmpty, or
1100
        some other PathError if the case can't be specifically detected.
1101
1102
        If the destination is an empty directory or a file this function may
1103
        either fail or succeed, depending on the underlying transport.  It
1104
        should not attempt to remove the destination if overwriting is not the
1105
        native transport behaviour.  If at all possible the transport should
1106
        ensure that the rename either completes or not, without leaving the
1107
        destination deleted and the new file not moved in place.
1108
1109
        This is intended mainly for use in implementing LockDir.
1110
        """
1111
        # transports may need to override this
1553.5.17 by Martin Pool
Transport.rename should be unimplemented in base class
1112
        raise NotImplementedError(self.rename)
1553.5.13 by Martin Pool
New Transport.rename that mustn't overwrite
1113
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
1114
    def move(self, rel_from, rel_to):
1530.1.3 by Robert Collins
transport implementations now tested consistently.
1115
        """Move the item at rel_from to the location at rel_to.
1553.5.13 by Martin Pool
New Transport.rename that mustn't overwrite
1116
1117
        The destination is deleted if possible, even if it's a non-empty
1118
        directory tree.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1119
1530.1.3 by Robert Collins
transport implementations now tested consistently.
1120
        If a transport can directly implement this it is suggested that
1121
        it do so for efficiency.
1122
        """
1534.4.26 by Robert Collins
Move working tree initialisation out from Branch.initialize, deprecated Branch.initialize to Branch.create.
1123
        if S_ISDIR(self.stat(rel_from).st_mode):
1124
            self.copy_tree(rel_from, rel_to)
1125
            self.delete_tree(rel_from)
1126
        else:
1127
            self.copy(rel_from, rel_to)
1128
            self.delete(rel_from)
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
1129
907.1.28 by John Arbash Meinel
Added pb to function that were missing, implemented a basic double-dispatch copy_to function.
1130
    def move_multi(self, relpaths, pb=None):
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
1131
        """Move a bunch of entries.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1132
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
1133
        :param relpaths: A list of tuples of the form [(from1, to1), (from2, to2),...]
1134
        """
907.1.14 by John Arbash Meinel
Handling some transport functions which take only a single argument.
1135
        return self._iterate_over(relpaths, self.move, pb, 'move', expand=True)
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
1136
1137
    def move_multi_to(self, relpaths, rel_to):
1138
        """Move a bunch of entries to a single location.
1139
        This differs from move_multi in that you give a list of from, and
1140
        a single destination, rather than multiple destinations.
1141
1142
        :param relpaths: A list of relative paths [from1, from2, from3, ...]
1143
        :param rel_to: A directory where each entry should be placed.
1144
        """
1145
        # This is not implemented, because you need to do special tricks to
1146
        # extract the basename, and add it to rel_to
1540.3.1 by Martin Pool
First-cut implementation of pycurl. Substantially faster than using urllib.
1147
        raise NotImplementedError(self.move_multi_to)
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
1148
1149
    def delete(self, relpath):
1150
        """Delete the item at relpath"""
1540.3.1 by Martin Pool
First-cut implementation of pycurl. Substantially faster than using urllib.
1151
        raise NotImplementedError(self.delete)
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
1152
907.1.28 by John Arbash Meinel
Added pb to function that were missing, implemented a basic double-dispatch copy_to function.
1153
    def delete_multi(self, relpaths, pb=None):
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
1154
        """Queue up a bunch of deletes to be done.
1155
        """
907.1.14 by John Arbash Meinel
Handling some transport functions which take only a single argument.
1156
        return self._iterate_over(relpaths, self.delete, pb, 'delete', expand=False)
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
1157
1534.4.15 by Robert Collins
Remove shutil dependency in upgrade - create a delete_tree method for transports.
1158
    def delete_tree(self, relpath):
1159
        """Delete an entire tree. This may require a listable transport."""
1160
        subtree = self.clone(relpath)
1161
        files = []
1162
        directories = ['.']
1163
        pending_rmdirs = []
1164
        while directories:
1165
            dir = directories.pop()
1166
            if dir != '.':
1167
                pending_rmdirs.append(dir)
1168
            for path in subtree.list_dir(dir):
1169
                path = dir + '/' + path
1170
                stat = subtree.stat(path)
1171
                if S_ISDIR(stat.st_mode):
1172
                    directories.append(path)
1173
                else:
1174
                    files.append(path)
1175
        subtree.delete_multi(files)
1176
        pending_rmdirs.reverse()
1177
        for dir in pending_rmdirs:
1178
            subtree.rmdir(dir)
1179
        self.rmdir(relpath)
1180
1534.5.7 by Robert Collins
Start factoring out the upgrade policy logic.
1181
    def __repr__(self):
1182
        return "<%s.%s url=%s>" % (self.__module__, self.__class__.__name__, self.base)
1183
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
1184
    def stat(self, relpath):
1185
        """Return the stat information for a file.
1186
        WARNING: This may not be implementable for all protocols, so use
1187
        sparingly.
1442.1.44 by Robert Collins
Many transport related tweaks:
1188
        NOTE: This returns an object with fields such as 'st_size'. It MAY
1189
        or MAY NOT return the literal result of an os.stat() call, so all
1190
        access should be via named fields.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1191
        ALSO NOTE: Stats of directories may not be supported on some
1442.1.44 by Robert Collins
Many transport related tweaks:
1192
        transports.
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
1193
        """
1540.3.1 by Martin Pool
First-cut implementation of pycurl. Substantially faster than using urllib.
1194
        raise NotImplementedError(self.stat)
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
1195
1534.4.15 by Robert Collins
Remove shutil dependency in upgrade - create a delete_tree method for transports.
1196
    def rmdir(self, relpath):
1197
        """Remove a directory at the given path."""
1198
        raise NotImplementedError
1199
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
1200
    def stat_multi(self, relpaths, pb=None):
1201
        """Stat multiple files and return the information.
1202
        """
1203
        #TODO:  Is it worth making this a generator instead of a
1204
        #       returning a list?
1205
        stats = []
1206
        def gather(path):
1207
            stats.append(self.stat(path))
1208
907.1.14 by John Arbash Meinel
Handling some transport functions which take only a single argument.
1209
        count = self._iterate_over(relpaths, gather, pb, 'stat', expand=False)
907.1.2 by John Arbash Meinel
Working on making Branch() do all of it's work over a Transport.
1210
        return stats
1211
5056.1.4 by Neil Santos
Changed stat() in SFTPTransport and LocalTransport back to calling plain stat(), instead of lstat().
1212
    def readlink(self, relpath):
1213
        """Return a string representing the path to which the symbolic link points."""
1214
        raise errors.TransportNotPossible("Dereferencing symlinks is not supported on %s" % self)
1215
5056.1.9 by Neil Santos
Renamed link() methods to hardlink(), as per mbp's suggestion
1216
    def hardlink(self, source, link_name):
5056.1.4 by Neil Santos
Changed stat() in SFTPTransport and LocalTransport back to calling plain stat(), instead of lstat().
1217
        """Create a hardlink pointing to source named link_name."""
5056.1.1 by Neil Santos
Added default link() and symlink() methods to Transport.
1218
        raise errors.TransportNotPossible("Hard links are not supported on %s" % self)
1219
1220
    def symlink(self, source, link_name):
5056.1.4 by Neil Santos
Changed stat() in SFTPTransport and LocalTransport back to calling plain stat(), instead of lstat().
1221
        """Create a symlink pointing to source named link_name."""
5056.1.1 by Neil Santos
Added default link() and symlink() methods to Transport.
1222
        raise errors.TransportNotPossible("Symlinks are not supported on %s" % self)
1223
1400.1.1 by Robert Collins
implement a basic test for the ui branch command from http servers
1224
    def listable(self):
1225
        """Return True if this store supports listing."""
1540.3.1 by Martin Pool
First-cut implementation of pycurl. Substantially faster than using urllib.
1226
        raise NotImplementedError(self.listable)
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
1227
1228
    def list_dir(self, relpath):
1229
        """Return a list of all files at the given location.
1230
        WARNING: many transports do not support this, so trying avoid using
1231
        it if at all possible.
1232
        """
1910.16.1 by Andrew Bennetts
lock_read and lock_write may raise TransportNotPossible.
1233
        raise errors.TransportNotPossible("Transport %r has not "
1530.1.21 by Robert Collins
Review feedback fixes.
1234
                                          "implemented list_dir "
1530.1.3 by Robert Collins
transport implementations now tested consistently.
1235
                                          "(but must claim to be listable "
1910.16.1 by Andrew Bennetts
lock_read and lock_write may raise TransportNotPossible.
1236
                                          "to trigger this error)."
1237
                                          % (self))
907.1.1 by John Arbash Meinel
Reworking the Branch and Store code to support an abstracted filesystem layer.
1238
907.1.24 by John Arbash Meinel
Remote functionality work.
1239
    def lock_read(self, relpath):
1240
        """Lock the given file for shared (read) access.
1910.16.1 by Andrew Bennetts
lock_read and lock_write may raise TransportNotPossible.
1241
1242
        WARNING: many transports do not support this, so trying avoid using it.
1243
        These methods may be removed in the future.
1244
1245
        Transports may raise TransportNotPossible if OS-level locks cannot be
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1246
        taken over this transport.
907.1.24 by John Arbash Meinel
Remote functionality work.
1247
1248
        :return: A lock object, which should contain an unlock() function.
1249
        """
1910.16.1 by Andrew Bennetts
lock_read and lock_write may raise TransportNotPossible.
1250
        raise errors.TransportNotPossible("transport locks not supported on %s" % self)
907.1.24 by John Arbash Meinel
Remote functionality work.
1251
1252
    def lock_write(self, relpath):
1253
        """Lock the given file for exclusive (write) access.
1910.16.1 by Andrew Bennetts
lock_read and lock_write may raise TransportNotPossible.
1254
1255
        WARNING: many transports do not support this, so trying avoid using it.
1256
        These methods may be removed in the future.
1257
1258
        Transports may raise TransportNotPossible if OS-level locks cannot be
1259
        taken over this transport.
907.1.24 by John Arbash Meinel
Remote functionality work.
1260
1261
        :return: A lock object, which should contain an unlock() function.
1262
        """
1910.16.1 by Andrew Bennetts
lock_read and lock_write may raise TransportNotPossible.
1263
        raise errors.TransportNotPossible("transport locks not supported on %s" % self)
907.1.24 by John Arbash Meinel
Remote functionality work.
1264
1530.1.3 by Robert Collins
transport implementations now tested consistently.
1265
    def is_readonly(self):
1266
        """Return true if this connection cannot be written to."""
1267
        return False
1268
1608.2.7 by Martin Pool
Rename supports_unix_modebits to _can_roundtrip_unix_modebits for clarity
1269
    def _can_roundtrip_unix_modebits(self):
1608.2.5 by Martin Pool
Add Transport.supports_unix_modebits, so tests can
1270
        """Return true if this transport can store and retrieve unix modebits.
1271
1272
        (For example, 0700 to make a directory owner-private.)
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1273
1274
        Note: most callers will not want to switch on this, but should rather
1608.2.5 by Martin Pool
Add Transport.supports_unix_modebits, so tests can
1275
        just try and set permissions and let them be either stored or not.
1276
        This is intended mainly for the use of the test suite.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1277
1278
        Warning: this is not guaranteed to be accurate as sometimes we can't
1608.2.5 by Martin Pool
Add Transport.supports_unix_modebits, so tests can
1279
        be sure: for example with vfat mounted on unix, or a windows sftp
1280
        server."""
1281
        # TODO: Perhaps return a e.g. TransportCharacteristics that can answer
1282
        # several questions about the transport.
1283
        return False
1284
2485.8.37 by Vincent Ladeuil
Fix merge multiple connections. Test suite *not* passing (sftp
1285
    def _reuse_for(self, other_base):
2485.8.43 by Vincent Ladeuil
Cleaning.
1286
        # This is really needed for ConnectedTransport only, but it's easier to
1287
        # have Transport refuses to be reused than testing that the reuse
1288
        # should be asked to ConnectedTransport only.
2485.8.37 by Vincent Ladeuil
Fix merge multiple connections. Test suite *not* passing (sftp
1289
        return None
907.1.24 by John Arbash Meinel
Remote functionality work.
1290
3878.4.5 by Vincent Ladeuil
Don't use the exception as a parameter for _redirected_to.
1291
    def _redirected_to(self, source, target):
3878.4.1 by Vincent Ladeuil
Fix bug #245964 by preserving decorators during redirections (when
1292
        """Returns a transport suitable to re-issue a redirected request.
1293
3878.4.5 by Vincent Ladeuil
Don't use the exception as a parameter for _redirected_to.
1294
        :param source: The source url as returned by the server.
1295
        :param target: The target url as returned by the server.
3878.4.1 by Vincent Ladeuil
Fix bug #245964 by preserving decorators during redirections (when
1296
1297
        The redirection can be handled only if the relpath involved is not
1298
        renamed by the redirection.
1299
1300
        :returns: A transport or None.
1301
        """
1302
        # This returns None by default, meaning the transport can't handle the
1303
        # redirection.
1304
        return None
1305
1306
2485.8.54 by Vincent Ladeuil
Refactor medium uses by making a distinction betweem shared and real medium.
1307
2485.8.55 by Vincent Ladeuil
Cleanup connection accessors.
1308
class _SharedConnection(object):
1309
    """A connection shared between several transports."""
1310
3104.4.1 by Andrew Bennetts
Fix paths sent by bzr+http client to correctly adjust for shared medium.
1311
    def __init__(self, connection=None, credentials=None, base=None):
2485.8.55 by Vincent Ladeuil
Cleanup connection accessors.
1312
        """Constructor.
1313
1314
        :param connection: An opaque object specific to each transport.
1315
1316
        :param credentials: An opaque object containing the credentials used to
1317
            create the connection.
1318
        """
1319
        self.connection = connection
1320
        self.credentials = credentials
3104.4.1 by Andrew Bennetts
Fix paths sent by bzr+http client to correctly adjust for shared medium.
1321
        self.base = base
2485.8.55 by Vincent Ladeuil
Cleanup connection accessors.
1322
1323
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
1324
class ConnectedTransport(Transport):
1325
    """A transport connected to a remote server.
1326
2485.8.28 by Vincent Ladeuil
Further simplifications and doc updates.
1327
    This class provide the basis to implement transports that need to connect
1328
    to a remote server.
1329
1330
    Host and credentials are available as private attributes, cloning preserves
1331
    them and share the underlying, protocol specific, connection.
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
1332
    """
1333
2485.8.59 by Vincent Ladeuil
Update from review comments.
1334
    def __init__(self, base, _from_transport=None):
2485.8.28 by Vincent Ladeuil
Further simplifications and doc updates.
1335
        """Constructor.
1336
2485.8.62 by Vincent Ladeuil
From review comments, fix typos and deprecate some functions.
1337
        The caller should ensure that _from_transport points at the same host
1338
        as the new base.
1339
2485.8.28 by Vincent Ladeuil
Further simplifications and doc updates.
1340
        :param base: transport root URL
1341
2485.8.59 by Vincent Ladeuil
Update from review comments.
1342
        :param _from_transport: optional transport to build from. The built
2485.8.28 by Vincent Ladeuil
Further simplifications and doc updates.
1343
            transport will share the connection with this transport.
1344
        """
2485.8.59 by Vincent Ladeuil
Update from review comments.
1345
        if not base.endswith('/'):
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
1346
            base += '/'
1347
        (self._scheme,
1348
         self._user, self._password,
1349
         self._host, self._port,
2485.8.47 by Vincent Ladeuil
Remove _initial_split_url hack.
1350
         self._path) = self._split_url(base)
2485.8.59 by Vincent Ladeuil
Update from review comments.
1351
        if _from_transport is not None:
2485.8.28 by Vincent Ladeuil
Further simplifications and doc updates.
1352
            # Copy the password as it does not appear in base and will be lost
2485.8.59 by Vincent Ladeuil
Update from review comments.
1353
            # otherwise. It can appear in the _split_url above if the user
1354
            # provided it on the command line. Otherwise, daughter classes will
1355
            # prompt the user for one when appropriate.
1356
            self._password = _from_transport._password
2485.8.25 by Vincent Ladeuil
Separate abspath from _remote_path, the intents are different.
1357
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
1358
        base = self._unsplit_url(self._scheme,
1359
                                 self._user, self._password,
1360
                                 self._host, self._port,
2485.8.28 by Vincent Ladeuil
Further simplifications and doc updates.
1361
                                 self._path)
2485.8.25 by Vincent Ladeuil
Separate abspath from _remote_path, the intents are different.
1362
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
1363
        super(ConnectedTransport, self).__init__(base)
2485.8.59 by Vincent Ladeuil
Update from review comments.
1364
        if _from_transport is None:
2485.8.55 by Vincent Ladeuil
Cleanup connection accessors.
1365
            self._shared_connection = _SharedConnection()
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
1366
        else:
2485.8.59 by Vincent Ladeuil
Update from review comments.
1367
            self._shared_connection = _from_transport._shared_connection
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
1368
2485.8.25 by Vincent Ladeuil
Separate abspath from _remote_path, the intents are different.
1369
    def clone(self, offset=None):
1370
        """Return a new transport with root at self.base + offset
1371
1372
        We leave the daughter classes take advantage of the hint
1373
        that it's a cloning not a raw creation.
1374
        """
1375
        if offset is None:
2485.8.59 by Vincent Ladeuil
Update from review comments.
1376
            return self.__class__(self.base, _from_transport=self)
2485.8.25 by Vincent Ladeuil
Separate abspath from _remote_path, the intents are different.
1377
        else:
2485.8.59 by Vincent Ladeuil
Update from review comments.
1378
            return self.__class__(self.abspath(offset), _from_transport=self)
2485.8.25 by Vincent Ladeuil
Separate abspath from _remote_path, the intents are different.
1379
2485.8.47 by Vincent Ladeuil
Remove _initial_split_url hack.
1380
    @staticmethod
1381
    def _split_url(url):
3873.3.1 by Martin Pool
Move Transport._split_url to urlutils, and ad a simple test
1382
        return urlutils.parse_url(url)
2485.8.25 by Vincent Ladeuil
Separate abspath from _remote_path, the intents are different.
1383
2485.8.47 by Vincent Ladeuil
Remove _initial_split_url hack.
1384
    @staticmethod
1385
    def _unsplit_url(scheme, user, password, host, port, path):
2485.8.28 by Vincent Ladeuil
Further simplifications and doc updates.
1386
        """
1387
        Build the full URL for the given already URL encoded path.
1388
1389
        user, password, host and path will be quoted if they contain reserved
1390
        chars.
1391
1392
        :param scheme: protocol
1393
1394
        :param user: login
1395
1396
        :param password: associated password
1397
1398
        :param host: the server address
1399
1400
        :param port: the associated port
1401
1402
        :param path: the absolute path on the server
1403
1404
        :return: The corresponding URL.
1405
        """
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
1406
        netloc = urllib.quote(host)
1407
        if user is not None:
1408
            # Note that we don't put the password back even if we
1409
            # have one so that it doesn't get accidentally
1410
            # exposed.
1411
            netloc = '%s@%s' % (urllib.quote(user), netloc)
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
1412
        if port is not None:
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
1413
            netloc = '%s:%d' % (netloc, port)
4505.3.1 by Andy Kilner
Fix up another tilde bug.
1414
        path = urlutils.escape(path)
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
1415
        return urlparse.urlunparse((scheme, netloc, path, None, None, None))
1416
1417
    def relpath(self, abspath):
2485.8.29 by Vincent Ladeuil
Cometic changes (and a typo).
1418
        """Return the local path portion from a given absolute path"""
1419
        scheme, user, password, host, port, path = self._split_url(abspath)
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
1420
        error = []
1421
        if (scheme != self._scheme):
1422
            error.append('scheme mismatch')
1423
        if (user != self._user):
2485.8.25 by Vincent Ladeuil
Separate abspath from _remote_path, the intents are different.
1424
            error.append('user name mismatch')
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
1425
        if (host != self._host):
1426
            error.append('host mismatch')
1427
        if (port != self._port):
1428
            error.append('port mismatch')
1429
        if not (path == self._path[:-1] or path.startswith(self._path)):
1430
            error.append('path mismatch')
1431
        if error:
1432
            extra = ', '.join(error)
1433
            raise errors.PathNotChild(abspath, self.base, extra=extra)
1434
        pl = len(self._path)
1435
        return path[pl:].strip('/')
1436
1437
    def abspath(self, relpath):
1438
        """Return the full url to the given relative path.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1439
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
1440
        :param relpath: the relative path urlencoded
2485.8.28 by Vincent Ladeuil
Further simplifications and doc updates.
1441
2485.8.25 by Vincent Ladeuil
Separate abspath from _remote_path, the intents are different.
1442
        :returns: the Unicode version of the absolute path for relpath.
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
1443
        """
2485.8.25 by Vincent Ladeuil
Separate abspath from _remote_path, the intents are different.
1444
        relative = urlutils.unescape(relpath).encode('utf-8')
1445
        path = self._combine_paths(self._path, relative)
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
1446
        return self._unsplit_url(self._scheme, self._user, self._password,
1447
                                 self._host, self._port,
2485.8.28 by Vincent Ladeuil
Further simplifications and doc updates.
1448
                                 path)
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
1449
1450
    def _remote_path(self, relpath):
1451
        """Return the absolute path part of the url to the given relative path.
2485.8.28 by Vincent Ladeuil
Further simplifications and doc updates.
1452
1453
        This is the path that the remote server expect to receive in the
1454
        requests, daughter classes should redefine this method if needed and
1455
        use the result to build their requests.
1456
1457
        :param relpath: the path relative to the transport base urlencoded.
1458
1459
        :return: the absolute Unicode path on the server,
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
1460
        """
1461
        relative = urlutils.unescape(relpath).encode('utf-8')
1462
        remote_path = self._combine_paths(self._path, relative)
1463
        return remote_path
1464
2485.8.54 by Vincent Ladeuil
Refactor medium uses by making a distinction betweem shared and real medium.
1465
    def _get_shared_connection(self):
1466
        """Get the object shared amongst cloned transports.
1467
1468
        This should be used only by classes that needs to extend the sharing
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
1469
        with objects other than transports.
2485.8.54 by Vincent Ladeuil
Refactor medium uses by making a distinction betweem shared and real medium.
1470
1471
        Use _get_connection to get the connection itself.
1472
        """
1473
        return self._shared_connection
2485.8.32 by Vincent Ladeuil
Keep credentials used at connection creation for reconnection purposes.
1474
1475
    def _set_connection(self, connection, credentials=None):
2485.8.34 by Vincent Ladeuil
Refactor mutiple connections detection and fix false positives. Only
1476
        """Record a newly created connection with its associated credentials.
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
1477
2485.8.30 by Vincent Ladeuil
Implement reliable connection sharing.
1478
        Note: To ensure that connection is still shared after a temporary
1479
        failure and a new one needs to be created, daughter classes should
2485.8.34 by Vincent Ladeuil
Refactor mutiple connections detection and fix false positives. Only
1480
        always call this method to set the connection and do so each time a new
1481
        connection is created.
2485.8.32 by Vincent Ladeuil
Keep credentials used at connection creation for reconnection purposes.
1482
1483
        :param connection: An opaque object representing the connection used by
1484
            the daughter class.
1485
1486
        :param credentials: An opaque object representing the credentials
1487
            needed to create the connection.
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
1488
        """
2485.8.55 by Vincent Ladeuil
Cleanup connection accessors.
1489
        self._shared_connection.connection = connection
1490
        self._shared_connection.credentials = credentials
2485.8.32 by Vincent Ladeuil
Keep credentials used at connection creation for reconnection purposes.
1491
1492
    def _get_connection(self):
2485.8.30 by Vincent Ladeuil
Implement reliable connection sharing.
1493
        """Returns the transport specific connection object."""
2485.8.55 by Vincent Ladeuil
Cleanup connection accessors.
1494
        return self._shared_connection.connection
2485.8.30 by Vincent Ladeuil
Implement reliable connection sharing.
1495
2485.8.32 by Vincent Ladeuil
Keep credentials used at connection creation for reconnection purposes.
1496
    def _get_credentials(self):
2485.8.34 by Vincent Ladeuil
Refactor mutiple connections detection and fix false positives. Only
1497
        """Returns the credentials used to establish the connection."""
2485.8.55 by Vincent Ladeuil
Cleanup connection accessors.
1498
        return self._shared_connection.credentials
2485.8.19 by Vincent Ladeuil
Add a new ConnectedTransport class refactored from [s]ftp and http.
1499
2485.8.39 by Vincent Ladeuil
Add tests around connection reuse.
1500
    def _update_credentials(self, credentials):
1501
        """Update the credentials of the current connection.
1502
1503
        Some protocols can renegociate the credentials within a connection,
1504
        this method allows daughter classes to share updated credentials.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1505
2485.8.39 by Vincent Ladeuil
Add tests around connection reuse.
1506
        :param credentials: the updated credentials.
1507
        """
1508
        # We don't want to call _set_connection here as we are only updating
1509
        # the credentials not creating a new connection.
2485.8.55 by Vincent Ladeuil
Cleanup connection accessors.
1510
        self._shared_connection.credentials = credentials
2485.8.39 by Vincent Ladeuil
Add tests around connection reuse.
1511
2485.8.37 by Vincent Ladeuil
Fix merge multiple connections. Test suite *not* passing (sftp
1512
    def _reuse_for(self, other_base):
1513
        """Returns a transport sharing the same connection if possible.
1514
1515
        Note: we share the connection if the expected credentials are the
1516
        same: (host, port, user). Some protocols may disagree and redefine the
1517
        criteria in daughter classes.
1518
1519
        Note: we don't compare the passwords here because other_base may have
1520
        been obtained from an existing transport.base which do not mention the
1521
        password.
1522
1523
        :param other_base: the URL we want to share the connection with.
1524
1525
        :return: A new transport or None if the connection cannot be shared.
1526
        """
2990.2.2 by Vincent Ladeuil
Detect invalid transport reuse attempts by catching invalid URLs.
1527
        try:
1528
            (scheme, user, password,
1529
             host, port, path) = self._split_url(other_base)
1530
        except errors.InvalidURL:
1531
            # No hope in trying to reuse an existing transport for an invalid
1532
            # URL
1533
            return None
1534
2485.8.37 by Vincent Ladeuil
Fix merge multiple connections. Test suite *not* passing (sftp
1535
        transport = None
2485.8.39 by Vincent Ladeuil
Add tests around connection reuse.
1536
        # Don't compare passwords, they may be absent from other_base or from
1537
        # self and they don't carry more information than user anyway.
2485.8.59 by Vincent Ladeuil
Update from review comments.
1538
        if (scheme == self._scheme
1539
            and user == self._user
1540
            and host == self._host
1541
            and port == self._port):
2485.8.37 by Vincent Ladeuil
Fix merge multiple connections. Test suite *not* passing (sftp
1542
            if not path.endswith('/'):
1543
                # This normally occurs at __init__ time, but it's easier to do
2485.8.39 by Vincent Ladeuil
Add tests around connection reuse.
1544
                # it now to avoid creating two transports for the same base.
2485.8.37 by Vincent Ladeuil
Fix merge multiple connections. Test suite *not* passing (sftp
1545
                path += '/'
1546
            if self._path  == path:
1547
                # shortcut, it's really the same transport
1548
                return self
1549
            # We don't call clone here because the intent is different: we
1550
            # build a new transport on a different base (which may be totally
1551
            # unrelated) but we share the connection.
2485.8.59 by Vincent Ladeuil
Update from review comments.
1552
            transport = self.__class__(other_base, _from_transport=self)
2485.8.37 by Vincent Ladeuil
Fix merge multiple connections. Test suite *not* passing (sftp
1553
        return transport
1554
1555
1556
# We try to recognize an url lazily (ignoring user, password, etc)
1557
_urlRE = re.compile(r'^(?P<proto>[^:/\\]+)://(?P<rest>.*)$')
1685.1.9 by John Arbash Meinel
Updated LocalTransport so that it's base is now a URL rather than a local path. This helps consistency with all other functions. To do so, I added local_abspath() which returns the local path, and local_path_to/from_url
1558
2476.3.8 by Vincent Ladeuil
Mark transports that need to be instrumented or refactored to check
1559
def get_transport(base, possible_transports=None):
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
1560
    """Open a transport to access a URL or directory.
1561
2476.3.5 by Vincent Ladeuil
Naive implementation of transport reuse by Transport.get_transport().
1562
    :param base: either a URL or a directory name.
2485.8.37 by Vincent Ladeuil
Fix merge multiple connections. Test suite *not* passing (sftp
1563
1564
    :param transports: optional reusable transports list. If not None, created
2485.8.59 by Vincent Ladeuil
Update from review comments.
1565
        transports will be added to the list.
2485.8.37 by Vincent Ladeuil
Fix merge multiple connections. Test suite *not* passing (sftp
1566
1567
    :return: A new transport optionally sharing its connection with one of
2485.8.59 by Vincent Ladeuil
Update from review comments.
1568
        possible_transports.
1185.70.3 by Martin Pool
Various updates to make storage branch mergeable:
1569
    """
907.1.13 by John Arbash Meinel
Fixed bzr root.
1570
    if base is None:
1685.1.11 by John Arbash Meinel
Not all of the registered transports use :// as part of their path, specifically memory:/
1571
        base = '.'
1843.1.1 by John Arbash Meinel
Update get_transport to raise a nicer error which includes dependency info
1572
    last_err = None
3251.3.1 by Aaron Bentley
Add support for directory services
1573
    from bzrlib.directory_service import directories
1574
    base = directories.dereference(base)
1843.1.1 by John Arbash Meinel
Update get_transport to raise a nicer error which includes dependency info
1575
1685.1.33 by John Arbash Meinel
Be more of a Nazi about URLs not being unicode
1576
    def convert_path_to_url(base, error_str):
1577
        m = _urlRE.match(base)
1578
        if m:
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1579
            # This looks like a URL, but we weren't able to
1685.1.33 by John Arbash Meinel
Be more of a Nazi about URLs not being unicode
1580
            # instantiate it as such raise an appropriate error
2485.8.38 by Vincent Ladeuil
Finish sftp refactoring. Test suite passing.
1581
            # FIXME: we have a 'error_str' unused and we use last_err below
1843.1.1 by John Arbash Meinel
Update get_transport to raise a nicer error which includes dependency info
1582
            raise errors.UnsupportedProtocol(base, last_err)
1685.1.33 by John Arbash Meinel
Be more of a Nazi about URLs not being unicode
1583
        # This doesn't look like a protocol, consider it a local path
1685.1.45 by John Arbash Meinel
Moved url functions into bzrlib.urlutils
1584
        new_base = urlutils.local_path_to_url(base)
1908.3.1 by Carl Friedrich Bolz
Clean up some mutter() calls.
1585
        # mutter('converting os path %r => url %s', base, new_base)
1685.1.33 by John Arbash Meinel
Be more of a Nazi about URLs not being unicode
1586
        return new_base
1587
1588
    # Catch any URLs which are passing Unicode rather than ASCII
1589
    try:
1590
        base = base.encode('ascii')
1591
    except UnicodeError:
1592
        # Only local paths can be Unicode
1593
        base = convert_path_to_url(base,
1594
            'URLs must be properly escaped (protocol: %s)')
2476.3.5 by Vincent Ladeuil
Naive implementation of transport reuse by Transport.get_transport().
1595
1596
    transport = None
1551.18.10 by Aaron Bentley
get_transport appends to possible_transports if it's an empty list
1597
    if possible_transports is not None:
2476.3.8 by Vincent Ladeuil
Mark transports that need to be instrumented or refactored to check
1598
        for t in possible_transports:
2485.8.37 by Vincent Ladeuil
Fix merge multiple connections. Test suite *not* passing (sftp
1599
            t_same_connection = t._reuse_for(base)
1600
            if t_same_connection is not None:
1601
                # Add only new transports
1602
                if t_same_connection not in possible_transports:
1603
                    possible_transports.append(t_same_connection)
1604
                return t_same_connection
1605
3882.1.1 by Martin Pool
Don't call iteritems on transport_list_registry, because it may change during iteration
1606
    for proto, factory_list in transport_list_registry.items():
2485.8.37 by Vincent Ladeuil
Fix merge multiple connections. Test suite *not* passing (sftp
1607
        if proto is not None and base.startswith(proto):
1608
            transport, last_err = _try_transport_factories(base, factory_list)
1609
            if transport:
1551.18.10 by Aaron Bentley
get_transport appends to possible_transports if it's an empty list
1610
                if possible_transports is not None:
3376.2.4 by Martin Pool
Remove every assert statement from bzrlib!
1611
                    if transport in possible_transports:
1612
                        raise AssertionError()
2485.8.37 by Vincent Ladeuil
Fix merge multiple connections. Test suite *not* passing (sftp
1613
                    possible_transports.append(transport)
1614
                return transport
1615
2485.8.43 by Vincent Ladeuil
Cleaning.
1616
    # We tried all the different protocols, now try one last time
1617
    # as a local protocol
2485.8.37 by Vincent Ladeuil
Fix merge multiple connections. Test suite *not* passing (sftp
1618
    base = convert_path_to_url(base, 'Unsupported protocol: %s')
1619
2485.8.43 by Vincent Ladeuil
Cleaning.
1620
    # The default handler is the filesystem handler, stored as protocol None
2485.8.37 by Vincent Ladeuil
Fix merge multiple connections. Test suite *not* passing (sftp
1621
    factory_list = transport_list_registry.get(None)
1622
    transport, last_err = _try_transport_factories(base, factory_list)
1623
2476.3.5 by Vincent Ladeuil
Naive implementation of transport reuse by Transport.get_transport().
1624
    return transport
1625
1626
2485.8.37 by Vincent Ladeuil
Fix merge multiple connections. Test suite *not* passing (sftp
1627
def _try_transport_factories(base, factory_list):
1628
    last_err = None
1629
    for factory in factory_list:
1630
        try:
1631
            return factory.get_obj()(base), None
1632
        except errors.DependencyNotPresent, e:
1633
            mutter("failed to instantiate transport %r for %r: %r" %
1634
                    (factory, base, e))
1635
            last_err = e
1636
            continue
1637
    return None, last_err
1638
1639
2164.2.22 by Vincent Ladeuil
Take Aaron's review comments into account.
1640
def do_catching_redirections(action, transport, redirected):
2164.2.21 by Vincent Ladeuil
Take bundles into account.
1641
    """Execute an action with given transport catching redirections.
1642
1643
    This is a facility provided for callers needing to follow redirections
2164.2.25 by Vincent Ladeuil
Fix typos noticed by Aaron.
1644
    silently. The silence is relative: it is the caller responsability to
2164.2.21 by Vincent Ladeuil
Take bundles into account.
1645
    inform the user about each redirection or only inform the user of a user
1646
    via the exception parameter.
1647
1648
    :param action: A callable, what the caller want to do while catching
1649
                  redirections.
1650
    :param transport: The initial transport used.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1651
    :param redirected: A callable receiving the redirected transport and the
2164.2.21 by Vincent Ladeuil
Take bundles into account.
1652
                  RedirectRequested exception.
1653
1654
    :return: Whatever 'action' returns
1655
    """
1656
    MAX_REDIRECTIONS = 8
1657
1658
    # If a loop occurs, there is little we can do. So we don't try to detect
1659
    # them, just getting out if too much redirections occurs. The solution
1660
    # is outside: where the loop is defined.
1661
    for redirections in range(MAX_REDIRECTIONS):
1662
        try:
2164.2.22 by Vincent Ladeuil
Take Aaron's review comments into account.
1663
            return action(transport)
2164.2.21 by Vincent Ladeuil
Take bundles into account.
1664
        except errors.RedirectRequested, e:
1665
            redirection_notice = '%s is%s redirected to %s' % (
2164.2.22 by Vincent Ladeuil
Take Aaron's review comments into account.
1666
                e.source, e.permanently, e.target)
1667
            transport = redirected(transport, e, redirection_notice)
2164.2.21 by Vincent Ladeuil
Take bundles into account.
1668
    else:
2164.2.22 by Vincent Ladeuil
Take Aaron's review comments into account.
1669
        # Loop exited without resolving redirect ? Either the
1670
        # user has kept a very very very old reference or a loop
2164.2.25 by Vincent Ladeuil
Fix typos noticed by Aaron.
1671
        # occurred in the redirections.  Nothing we can cure here:
2164.2.22 by Vincent Ladeuil
Take Aaron's review comments into account.
1672
        # tell the user. Note that as the user has been informed
1673
        # about each redirection (it is the caller responsibility
1674
        # to do that in redirected via the provided
1675
        # redirection_notice). The caller may provide more
2164.2.25 by Vincent Ladeuil
Fix typos noticed by Aaron.
1676
        # information if needed (like what file or directory we
2164.2.22 by Vincent Ladeuil
Take Aaron's review comments into account.
1677
        # were trying to act upon when the redirection loop
2164.2.25 by Vincent Ladeuil
Fix typos noticed by Aaron.
1678
        # occurred).
2164.2.22 by Vincent Ladeuil
Take Aaron's review comments into account.
1679
        raise errors.TooManyRedirections
2164.2.21 by Vincent Ladeuil
Take bundles into account.
1680
1681
1530.1.3 by Robert Collins
transport implementations now tested consistently.
1682
class Server(object):
1530.1.21 by Robert Collins
Review feedback fixes.
1683
    """A Transport Server.
3943.8.1 by Marius Kruger
remove all trailing whitespace from bzr source
1684
5017.3.1 by Vincent Ladeuil
Create a tests.test_server.TestServer class out of transport.Server (while retaining the later for some special non-tests usages).
1685
    The Server interface provides a server for a given transport type.
1530.1.21 by Robert Collins
Review feedback fixes.
1686
    """
1530.1.3 by Robert Collins
transport implementations now tested consistently.
1687
4934.3.3 by Martin Pool
Rename Server.setUp to Server.start_server
1688
    def start_server(self):
1530.1.3 by Robert Collins
transport implementations now tested consistently.
1689
        """Setup the server to service requests."""
1690
4934.3.1 by Martin Pool
Rename Server.tearDown to .stop_server
1691
    def stop_server(self):
1530.1.3 by Robert Collins
transport implementations now tested consistently.
1692
        """Remove the server and cleanup any resources it owns."""
1693
1694
1185.16.81 by mbp at sourcefrog
[merge] robert
1695
# None is the default transport, for things with no url scheme
2241.2.9 by ghigo
add the 'file://' prefix to the urlspec topic
1696
register_transport_proto('file://',
1697
            help="Access using the standard filesystem (default)")
1185.16.79 by Martin Pool
Load transports when they're first used.
1698
register_lazy_transport('file://', 'bzrlib.transport.local', 'LocalTransport')
2241.2.3 by ghigo
removed the the registration of the 'None' transport, instead we use the set_default_transport function
1699
transport_list_registry.set_default_transport("file://")
2241.2.4 by ghigo
removed commented line
1700
2241.2.5 by ghigo
add the topics transport
1701
register_transport_proto('sftp://',
2919.2.1 by John Arbash Meinel
Register netloc protocols as soon as the protocol is registered.
1702
            help="Access using SFTP (most SSH servers provide SFTP).",
1703
            register_netloc=True)
1185.16.79 by Martin Pool
Load transports when they're first used.
1704
register_lazy_transport('sftp://', 'bzrlib.transport.sftp', 'SFTPTransport')
2164.2.7 by v.ladeuil+lp at free
First implementation of transport hints.
1705
# Decorated http transport
2241.2.5 by ghigo
add the topics transport
1706
register_transport_proto('http+urllib://',
2241.2.11 by ghigo
On the basis of Robert Collins and John Arbash Meinel
1707
#                help="Read-only access of branches exported on the web."
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
1708
                         register_netloc=True)
1540.3.23 by Martin Pool
Allow urls like http+pycurl://host/ to use a particular impl
1709
register_lazy_transport('http+urllib://', 'bzrlib.transport.http._urllib',
1540.3.26 by Martin Pool
[merge] bzr.dev; pycurl not updated for readv yet
1710
                        'HttpTransport_urllib')
2241.2.5 by ghigo
add the topics transport
1711
register_transport_proto('https+urllib://',
2241.2.11 by ghigo
On the basis of Robert Collins and John Arbash Meinel
1712
#                help="Read-only access of branches exported on the web using SSL."
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
1713
                         register_netloc=True)
1540.3.23 by Martin Pool
Allow urls like http+pycurl://host/ to use a particular impl
1714
register_lazy_transport('https+urllib://', 'bzrlib.transport.http._urllib',
1540.3.26 by Martin Pool
[merge] bzr.dev; pycurl not updated for readv yet
1715
                        'HttpTransport_urllib')
2241.2.5 by ghigo
add the topics transport
1716
register_transport_proto('http+pycurl://',
2241.2.11 by ghigo
On the basis of Robert Collins and John Arbash Meinel
1717
#                help="Read-only access of branches exported on the web."
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
1718
                         register_netloc=True)
1540.3.26 by Martin Pool
[merge] bzr.dev; pycurl not updated for readv yet
1719
register_lazy_transport('http+pycurl://', 'bzrlib.transport.http._pycurl',
1720
                        'PyCurlTransport')
2241.2.5 by ghigo
add the topics transport
1721
register_transport_proto('https+pycurl://',
2241.2.11 by ghigo
On the basis of Robert Collins and John Arbash Meinel
1722
#                help="Read-only access of branches exported on the web using SSL."
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
1723
                         register_netloc=True)
1540.3.26 by Martin Pool
[merge] bzr.dev; pycurl not updated for readv yet
1724
register_lazy_transport('https+pycurl://', 'bzrlib.transport.http._pycurl',
1725
                        'PyCurlTransport')
2164.2.7 by v.ladeuil+lp at free
First implementation of transport hints.
1726
# Default http transports (last declared wins (if it can be imported))
2241.2.5 by ghigo
add the topics transport
1727
register_transport_proto('http://',
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
1728
                 help="Read-only access of branches exported on the web.")
2241.2.5 by ghigo
add the topics transport
1729
register_transport_proto('https://',
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
1730
            help="Read-only access of branches exported on the web using SSL.")
3882.2.1 by John Arbash Meinel
Use urllib for http connections, pycurl for https connections.
1731
# The default http implementation is urllib, but https is pycurl if available
1732
register_lazy_transport('http://', 'bzrlib.transport.http._pycurl',
1733
                        'PyCurlTransport')
1540.3.26 by Martin Pool
[merge] bzr.dev; pycurl not updated for readv yet
1734
register_lazy_transport('http://', 'bzrlib.transport.http._urllib',
1735
                        'HttpTransport_urllib')
1736
register_lazy_transport('https://', 'bzrlib.transport.http._urllib',
1737
                        'HttpTransport_urllib')
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
1738
register_lazy_transport('https://', 'bzrlib.transport.http._pycurl',
1739
                        'PyCurlTransport')
2241.2.4 by ghigo
removed commented line
1740
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
1741
register_transport_proto('ftp://', help="Access using passive FTP.")
1185.36.4 by Daniel Silverstone
Add FTP transport
1742
register_lazy_transport('ftp://', 'bzrlib.transport.ftp', 'FtpTransport')
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
1743
register_transport_proto('aftp://', help="Access using active FTP.")
1185.36.4 by Daniel Silverstone
Add FTP transport
1744
register_lazy_transport('aftp://', 'bzrlib.transport.ftp', 'FtpTransport')
2241.2.4 by ghigo
removed commented line
1745
5193.4.6 by Mattias Eriksson
Register a generic gio+ handler and raise an InvalidURL inside the gio init
1746
register_transport_proto('gio+', help="Access using any GIO supported protocols.")
5193.4.21 by Mattias Eriksson
Moved the gio transport from bzrlib/transport/gio/__init_.py to bzrlib/transport/gio_transport.py
1747
register_lazy_transport('gio+', 'bzrlib.transport.gio_transport', 'GioTransport')
5193.4.1 by Mattias Eriksson
A GIO transport based on the FTP and SFTP transport implementations
1748
3508.1.10 by Vincent Ladeuil
Start supporting pyftpdlib as an ftp test server.
1749
try:
1750
    import kerberos
1751
    kerberos_available = True
1752
except ImportError:
1753
    kerberos_available = False
1754
1755
if kerberos_available:
1756
    # Default to trying GSSAPI authentication (if the kerberos module is
1757
    # available)
1758
    register_transport_proto('ftp+gssapi://', register_netloc=True)
1759
    register_lazy_transport('ftp+gssapi://', 'bzrlib.transport.ftp._gssapi',
1760
                            'GSSAPIFtpTransport')
1761
    register_transport_proto('aftp+gssapi://', register_netloc=True)
1762
    register_lazy_transport('aftp+gssapi://', 'bzrlib.transport.ftp._gssapi',
1763
                            'GSSAPIFtpTransport')
1764
    register_transport_proto('ftp+nogssapi://', register_netloc=True)
1765
    register_transport_proto('aftp+nogssapi://', register_netloc=True)
1766
1767
    register_lazy_transport('ftp://', 'bzrlib.transport.ftp._gssapi',
1768
                            'GSSAPIFtpTransport')
1769
    register_lazy_transport('aftp://', 'bzrlib.transport.ftp._gssapi',
1770
                            'GSSAPIFtpTransport')
1771
    register_lazy_transport('ftp+nogssapi://', 'bzrlib.transport.ftp',
1772
                            'FtpTransport')
1773
    register_lazy_transport('aftp+nogssapi://', 'bzrlib.transport.ftp',
1774
                            'FtpTransport')
3331.2.4 by Jelmer Vernooij
Move GSSAPI support to a separate file.
1775
2241.2.1 by ghigo
Add the TransportRegistry class
1776
register_transport_proto('memory://')
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
1777
register_lazy_transport('memory://', 'bzrlib.transport.memory',
1778
                        'MemoryTransport')
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
1779
1780
# chroots cannot be implicitly accessed, they must be explicitly created:
2241.2.1 by ghigo
Add the TransportRegistry class
1781
register_transport_proto('chroot+')
2241.3.6 by ghigo
Upgraded to the latest bzr.dev
1782
2241.2.5 by ghigo
add the topics transport
1783
register_transport_proto('readonly+',
2241.2.11 by ghigo
On the basis of Robert Collins and John Arbash Meinel
1784
#              help="This modifier converts any transport to be readonly."
1785
            )
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
1786
register_lazy_transport('readonly+', 'bzrlib.transport.readonly',
1787
                        'ReadonlyTransportDecorator')
2555.3.8 by Martin Pool
Add new BrokenRenameTransportDecorator
1788
2617.4.5 by Robert Collins
Reinstate the fakenfs+ decorator registration.
1789
register_transport_proto('fakenfs+')
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
1790
register_lazy_transport('fakenfs+', 'bzrlib.transport.fakenfs',
1791
                        'FakeNFSTransportDecorator')
2617.4.5 by Robert Collins
Reinstate the fakenfs+ decorator registration.
1792
3675.1.1 by Martin Pool
Merge and update log+ transport decorator
1793
register_transport_proto('log+')
1794
register_lazy_transport('log+', 'bzrlib.transport.log', 'TransportLogDecorator')
1795
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
1796
register_transport_proto('trace+')
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
1797
register_lazy_transport('trace+', 'bzrlib.transport.trace',
1798
                        'TransportTraceDecorator')
2745.5.3 by Robert Collins
* Move transport logging into a new transport class
1799
2617.4.1 by Robert Collins
Add a new transport decorator unlistable+ for testing.
1800
register_transport_proto('unlistable+')
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
1801
register_lazy_transport('unlistable+', 'bzrlib.transport.unlistable',
1802
                        'UnlistableTransportDecorator')
2555.3.8 by Martin Pool
Add new BrokenRenameTransportDecorator
1803
1804
register_transport_proto('brokenrename+')
1805
register_lazy_transport('brokenrename+', 'bzrlib.transport.brokenrename',
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
1806
                        'BrokenRenameTransportDecorator')
2555.3.8 by Martin Pool
Add new BrokenRenameTransportDecorator
1807
2241.2.1 by ghigo
Add the TransportRegistry class
1808
register_transport_proto('vfat+')
1910.19.1 by Andrew Bennetts
Support bzr:// urls to work with the new RPC-based transport which will be used
1809
register_lazy_transport('vfat+',
1608.2.4 by Martin Pool
[broken] Add FakeFVATTransport
1810
                        'bzrlib.transport.fakevfat',
1811
                        'FakeVFATTransportDecorator')
2919.2.1 by John Arbash Meinel
Register netloc protocols as soon as the protocol is registered.
1812
3313.3.4 by Andrew Bennetts
Add a 'nosmart+' transport decorator.
1813
register_transport_proto('nosmart+')
1814
register_lazy_transport('nosmart+', 'bzrlib.transport.nosmart',
1815
                        'NoSmartTransportDecorator')
1816
2919.2.1 by John Arbash Meinel
Register netloc protocols as soon as the protocol is registered.
1817
# These two schemes were registered, but don't seem to have an actual transport
1818
# protocol registered
1819
for scheme in ['ssh', 'bzr+loopback']:
2919.2.2 by John Arbash Meinel
fix a simple typo
1820
    register_urlparse_netloc_protocol(scheme)
2919.2.1 by John Arbash Meinel
Register netloc protocols as soon as the protocol is registered.
1821
del scheme
1822
2241.2.5 by ghigo
add the topics transport
1823
register_transport_proto('bzr://',
2811.1.1 by Andrew Bennetts
Cherrypick fix proposed for 0.90.
1824
            help="Fast access using the Bazaar smart server.",
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
1825
                         register_netloc=True)
2241.2.5 by ghigo
add the topics transport
1826
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
1827
register_lazy_transport('bzr://', 'bzrlib.transport.remote',
2413.2.1 by Andrew Bennetts
Rename Smart.*Transport classes to RemoteTransport, RemoteTCPTransport, etc.
1828
                        'RemoteTCPTransport')
3453.5.1 by Andrew Bennetts
Add {bzrdir,repository,branch}_implementations tests for Remote objects using protocol v2 and pre-1.6 RPCs.
1829
register_transport_proto('bzr-v2://', register_netloc=True)
1830
1831
register_lazy_transport('bzr-v2://', 'bzrlib.transport.remote',
1832
                        'RemoteTCPTransportV2Only')
2241.2.5 by ghigo
add the topics transport
1833
register_transport_proto('bzr+http://',
2241.2.11 by ghigo
On the basis of Robert Collins and John Arbash Meinel
1834
#                help="Fast access using the Bazaar smart server over HTTP."
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
1835
                         register_netloc=True)
1836
register_lazy_transport('bzr+http://', 'bzrlib.transport.remote',
2413.2.1 by Andrew Bennetts
Rename Smart.*Transport classes to RemoteTransport, RemoteTCPTransport, etc.
1837
                        'RemoteHTTPTransport')
2651.1.1 by John Ferlito
bzr+https support
1838
register_transport_proto('bzr+https://',
1839
#                help="Fast access using the Bazaar smart server over HTTPS."
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
1840
                         register_netloc=True)
2651.1.1 by John Ferlito
bzr+https support
1841
register_lazy_transport('bzr+https://',
1842
                        'bzrlib.transport.remote',
1843
                        'RemoteHTTPTransport')
2241.2.5 by ghigo
add the topics transport
1844
register_transport_proto('bzr+ssh://',
2919.2.1 by John Arbash Meinel
Register netloc protocols as soon as the protocol is registered.
1845
            help="Fast access using the Bazaar smart server over SSH.",
1846
            register_netloc=True)
3004.2.1 by Vincent Ladeuil
Fix 150860 by leaving port as user specified it.
1847
register_lazy_transport('bzr+ssh://', 'bzrlib.transport.remote',
2413.2.1 by Andrew Bennetts
Rename Smart.*Transport classes to RemoteTransport, RemoteTCPTransport, etc.
1848
                        'RemoteSSHTransport')
4011.4.1 by Jelmer Vernooij
Point out bzr+ssh:// to the user when they use ssh://.
1849
4011.4.2 by Jelmer Vernooij
Register ssh: rather than ssh://, avoid referring to smart server, which users may not be familiar with.
1850
register_transport_proto('ssh:')
4032.1.2 by John Arbash Meinel
Track down a few more files that have trailing whitespace.
1851
register_lazy_transport('ssh:', 'bzrlib.transport.remote',
4011.4.1 by Jelmer Vernooij
Point out bzr+ssh:// to the user when they use ssh://.
1852
                        'HintingSSHTransport')
4370.4.6 by Jelmer Vernooij
Move server protocol registry to bzrlib.transport.
1853
1854
1855
transport_server_registry = registry.Registry()
5056.1.4 by Neil Santos
Changed stat() in SFTPTransport and LocalTransport back to calling plain stat(), instead of lstat().
1856
transport_server_registry.register_lazy('bzr', 'bzrlib.smart.server',
4370.4.6 by Jelmer Vernooij
Move server protocol registry to bzrlib.transport.
1857
    'serve_bzr', help="The Bazaar smart server protocol over TCP. (default port: 4155)")
1858
transport_server_registry.default_key = 'bzr'