/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
1
# Copyright (C) 2006, 2007 Canonical Ltd
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
2
#
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.
7
#
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.
12
#
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
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
1752.2.39 by Martin Pool
[broken] implement upgrade apis on remote bzrdirs
17
# TODO: At some point, handle upgrades by just passing the whole request
18
# across to run on the server.
19
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
20
from cStringIO import StringIO
2018.5.26 by Andrew Bennetts
Extract a simple SmartClient class from RemoteTransport, and a hack to avoid VFS operations when probing for a bzrdir over a smart transport.
21
from urlparse import urlparse
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
22
2018.5.25 by Andrew Bennetts
Make sure RemoteBzrDirFormat is always registered (John Arbash Meinel, Robert Collins, Andrew Bennetts).
23
from bzrlib import branch, errors, repository
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
24
from bzrlib.branch import BranchReferenceFormat
2018.5.25 by Andrew Bennetts
Make sure RemoteBzrDirFormat is always registered (John Arbash Meinel, Robert Collins, Andrew Bennetts).
25
from bzrlib.bzrdir import BzrDir, BzrDirFormat, RemoteBzrDirFormat
2018.14.2 by Andrew Bennetts
All but one repository_implementation tests for RemoteRepository passing.
26
from bzrlib.config import BranchConfig, TreeConfig
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
27
from bzrlib.decorators import needs_read_lock, needs_write_lock
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
28
from bzrlib.errors import NoSuchRevision
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
29
from bzrlib.revision import NULL_REVISION
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
30
from bzrlib.smart import client, vfs
2018.5.32 by Robert Collins
Unescape urls before handing over the wire to the smart server for the probe_transport method.
31
from bzrlib.urlutils import unescape
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
32
2018.5.25 by Andrew Bennetts
Make sure RemoteBzrDirFormat is always registered (John Arbash Meinel, Robert Collins, Andrew Bennetts).
33
# Note: RemoteBzrDirFormat is in bzrdir.py
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
34
35
class RemoteBzrDir(BzrDir):
36
    """Control directory on a remote server, accessed by HPSS."""
37
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
38
    def __init__(self, transport, _client=None):
39
        """Construct a RemoteBzrDir.
40
41
        :param _client: Private parameter for testing. Disables probing and the
42
            use of a real bzrdir.
43
        """
1752.2.39 by Martin Pool
[broken] implement upgrade apis on remote bzrdirs
44
        BzrDir.__init__(self, transport, RemoteBzrDirFormat())
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
45
        # this object holds a delegated bzrdir that uses file-level operations
46
        # to talk to the other side
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
47
        # XXX: We should go into find_format, but not allow it to find
48
        # RemoteBzrDirFormat and make sure it finds the real underlying format.
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
49
        self._real_bzrdir = None
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
50
51
        if _client is None:
52
            self._medium = transport.get_smart_client()
53
            self._client = client.SmartClient(self._medium)
54
        else:
55
            self._client = _client
56
            self._medium = None
57
            return
58
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
59
        self._ensure_real()
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
60
        path = self._path_for_remote_call(self._client)
2018.5.26 by Andrew Bennetts
Extract a simple SmartClient class from RemoteTransport, and a hack to avoid VFS operations when probing for a bzrdir over a smart transport.
61
        #self._real_bzrdir._format.probe_transport(transport)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
62
        response = self._client.call('probe_dont_use', path)
2018.5.26 by Andrew Bennetts
Extract a simple SmartClient class from RemoteTransport, and a hack to avoid VFS operations when probing for a bzrdir over a smart transport.
63
        if response == ('no',):
64
            raise errors.NotBranchError(path=transport.base)
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
65
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
66
    def _ensure_real(self):
67
        """Ensure that there is a _real_bzrdir set.
68
69
        used before calls to self._real_bzrdir.
70
        """
71
        if not self._real_bzrdir:
72
            default_format = BzrDirFormat.get_default_format()
73
            self._real_bzrdir = default_format.open(self.root_transport,
74
                _found=True)
75
1752.2.39 by Martin Pool
[broken] implement upgrade apis on remote bzrdirs
76
    def create_repository(self, shared=False):
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
77
        self._real_bzrdir.create_repository(shared=shared)
78
        return self.open_repository()
1752.2.50 by Andrew Bennetts
Implement RemoteBzrDir.create_{branch,workingtree}
79
80
    def create_branch(self):
1752.2.72 by Andrew Bennetts
Make Remote* classes in remote.py more consistent and remove some dead code.
81
        real_branch = self._real_bzrdir.create_branch()
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
82
        return RemoteBranch(self, self.find_repository(), real_branch)
1752.2.50 by Andrew Bennetts
Implement RemoteBzrDir.create_{branch,workingtree}
83
84
    def create_workingtree(self, revision_id=None):
1752.2.72 by Andrew Bennetts
Make Remote* classes in remote.py more consistent and remove some dead code.
85
        real_workingtree = self._real_bzrdir.create_workingtree(revision_id=revision_id)
86
        return RemoteWorkingTree(self, real_workingtree)
1752.2.39 by Martin Pool
[broken] implement upgrade apis on remote bzrdirs
87
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
88
    def open_branch(self, _unsupported=False):
89
        assert _unsupported == False, 'unsupported flag support not implemented yet.'
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
90
        path = self._path_for_remote_call(self._client)
91
        response = self._client.call('BzrDir.open_branch', path)
92
        if response[0] == 'ok':
93
            if response[1] == '':
94
                # branch at this location.
95
                return RemoteBranch(self, self.find_repository())
96
            else:
97
                # a branch reference, use the existing BranchReference logic.
98
                format = BranchReferenceFormat()
99
                return format.open(self, _found=True, location=response[1])
100
        elif response == ('nobranch',):
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
101
            raise errors.NotBranchError(path=self.root_transport.base)
102
        else:
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
103
            assert False, 'unexpected response code %r' % (response,)
104
                
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
105
    def open_repository(self):
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
106
        path = self._path_for_remote_call(self._client)
107
        response = self._client.call('BzrDir.find_repository', path)
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
108
        assert response[0] in ('ok', 'norepository'), \
2018.5.52 by Wouter van Heyst
Provide more information when encountering unexpected responses from a smart
109
            'unexpected response code %s' % (response,)
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
110
        if response[0] == 'norepository':
111
            raise errors.NoRepositoryPresent(self)
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
112
        assert len(response) == 4, 'incorrect response length %s' % (response,)
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
113
        if response[1] == '':
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
114
            format = RemoteRepositoryFormat()
115
            format.rich_root_data = response[2] == 'True'
116
            format.support_tree_reference = response[3] == 'True'
117
            return RemoteRepository(self, format)
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
118
        else:
119
            raise errors.NoRepositoryPresent(self)
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
120
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
121
    def open_workingtree(self):
1752.2.72 by Andrew Bennetts
Make Remote* classes in remote.py more consistent and remove some dead code.
122
        return RemoteWorkingTree(self, self._real_bzrdir.open_workingtree())
1752.2.50 by Andrew Bennetts
Implement RemoteBzrDir.create_{branch,workingtree}
123
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
124
    def _path_for_remote_call(self, client):
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
125
        """Return the path to be used for this bzrdir in a remote call."""
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
126
        return client.remote_path_from_transport(self.root_transport)
2018.6.1 by Robert Collins
Implement a BzrDir.open_branch smart server method for opening a branch without VFS.
127
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
128
    def get_branch_transport(self, branch_format):
129
        return self._real_bzrdir.get_branch_transport(branch_format)
130
1752.2.43 by Andrew Bennetts
Fix get_{branch,repository,workingtree}_transport.
131
    def get_repository_transport(self, repository_format):
132
        return self._real_bzrdir.get_repository_transport(repository_format)
133
134
    def get_workingtree_transport(self, workingtree_format):
135
        return self._real_bzrdir.get_workingtree_transport(workingtree_format)
136
1752.2.39 by Martin Pool
[broken] implement upgrade apis on remote bzrdirs
137
    def can_convert_format(self):
138
        """Upgrading of remote bzrdirs is not supported yet."""
139
        return False
140
141
    def needs_format_conversion(self, format=None):
142
        """Upgrading of remote bzrdirs is not supported yet."""
143
        return False
144
2018.5.94 by Andrew Bennetts
Various small changes in aid of making tests pass (including deleting one invalid test).
145
    def clone(self, url, revision_id=None, basis=None, force_new_repo=False):
146
        self._ensure_real()
147
        return self._real_bzrdir.clone(url, revision_id=revision_id,
148
            basis=basis, force_new_repo=force_new_repo)
149
150
    #def sprout(self, url, revision_id=None, basis=None, force_new_repo=False):
151
    #    self._ensure_real()
152
    #    return self._real_bzrdir.sprout(url, revision_id=revision_id,
153
    #        basis=basis, force_new_repo=force_new_repo)
154
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
155
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
156
class RemoteRepositoryFormat(repository.RepositoryFormat):
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
157
    """Format for repositories accessed over a SmartClient.
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
158
1752.2.50 by Andrew Bennetts
Implement RemoteBzrDir.create_{branch,workingtree}
159
    Instances of this repository are represented by RemoteRepository
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
160
    instances.
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
161
162
    The RemoteRepositoryFormat is parameterised during construction
163
    to reflect the capabilities of the real, remote format. Specifically
164
    the attributes rich_root_data and support_tree_reference are set
165
    on a per instance basis, and are not set (and should not be) at
166
    the class level.
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
167
    """
168
169
    _matchingbzrdir = RemoteBzrDirFormat
170
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
171
    def initialize(self, a_bzrdir, shared=False):
1752.2.72 by Andrew Bennetts
Make Remote* classes in remote.py more consistent and remove some dead code.
172
        assert isinstance(a_bzrdir, RemoteBzrDir)
173
        return a_bzrdir.create_repository(shared=shared)
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
174
    
175
    def open(self, a_bzrdir):
1752.2.72 by Andrew Bennetts
Make Remote* classes in remote.py more consistent and remove some dead code.
176
        assert isinstance(a_bzrdir, RemoteBzrDir)
177
        return a_bzrdir.open_repository()
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
178
179
    def get_format_description(self):
180
        return 'bzr remote repository'
181
182
    def __eq__(self, other):
1752.2.87 by Andrew Bennetts
Make tests pass.
183
        return self.__class__ == other.__class__
184
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
185
    def check_conversion_target(self, target_format):
186
        if self.rich_root_data and not target_format.rich_root_data:
187
            raise errors.BadConversionTarget(
188
                'Does not support rich root data.', target_format)
189
        if (self.support_tree_reference and
190
            not getattr(target_format, 'support_tree_reference', False)):
191
            raise errors.BadConversionTarget(
192
                'Does not support nested trees', target_format)
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
193
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
194
1752.2.50 by Andrew Bennetts
Implement RemoteBzrDir.create_{branch,workingtree}
195
class RemoteRepository(object):
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
196
    """Repository accessed over rpc.
197
1752.2.50 by Andrew Bennetts
Implement RemoteBzrDir.create_{branch,workingtree}
198
    For the moment everything is delegated to IO-like operations over
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
199
    the transport.
200
    """
201
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
202
    def __init__(self, remote_bzrdir, format, real_repository=None, _client=None):
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
203
        """Create a RemoteRepository instance.
204
        
205
        :param remote_bzrdir: The bzrdir hosting this repository.
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
206
        :param format: The RemoteFormat object to use.
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
207
        :param real_repository: If not None, a local implementation of the
208
            repository logic for the repository, usually accessing the data
209
            via the VFS.
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
210
        :param _client: Private testing parameter - override the smart client
211
            to be used by the repository.
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
212
        """
213
        if real_repository:
2018.5.36 by Andrew Bennetts
Fix typo, and clean up some ununsed import warnings from pyflakes at the same time.
214
            self._real_repository = real_repository
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
215
        else:
216
            self._real_repository = None
1752.2.50 by Andrew Bennetts
Implement RemoteBzrDir.create_{branch,workingtree}
217
        self.bzrdir = remote_bzrdir
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
218
        if _client is None:
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
219
            self._client = client.SmartClient(self.bzrdir._medium)
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
220
        else:
221
            self._client = _client
2018.5.118 by Robert Collins
Fix RemoteRepositoryFormat to have appropriate rich_root_data and support_tree_reference.
222
        self._format = format
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
223
        self._lock_mode = None
224
        self._lock_token = None
225
        self._lock_count = 0
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
226
        self._leave_lock = False
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
227
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
228
    def _ensure_real(self):
229
        """Ensure that there is a _real_repository set.
230
231
        used before calls to self._real_repository.
232
        """
233
        if not self._real_repository:
234
            self.bzrdir._ensure_real()
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
235
            #self._real_repository = self.bzrdir._real_bzrdir.open_repository()
236
            self._set_real_repository(self.bzrdir._real_bzrdir.open_repository())
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
237
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
238
    def get_revision_graph(self, revision_id=None):
239
        """See Repository.get_revision_graph()."""
240
        if revision_id is None:
241
            revision_id = ''
242
        elif revision_id == NULL_REVISION:
243
            return {}
244
245
        path = self.bzrdir._path_for_remote_call(self._client)
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
246
        assert type(revision_id) is str
247
        response = self._client.call2(
248
            'Repository.get_revision_graph', path, revision_id)
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
249
        assert response[0][0] in ('ok', 'nosuchrevision'), 'unexpected response code %s' % (response[0],)
250
        if response[0][0] == 'ok':
251
            coded = response[1].read_body_bytes()
2018.5.105 by Andrew Bennetts
Implement revision_history caching for RemoteBranch.
252
            if coded == '':
253
                # no revisions in this repository!
254
                return {}
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
255
            lines = coded.split('\n')
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
256
            revision_graph = {}
257
            # FIXME
258
            for line in lines:
259
                d = list(line.split())
260
                revision_graph[d[0]] = d[1:]
261
                
262
            return revision_graph
263
        else:
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
264
            response_body = response[1].read_body_bytes()
265
            assert response_body == ''
2018.5.67 by Wouter van Heyst
Implement RemoteRepository.get_revision_graph (Wouter van Heyst, Robert Collins)
266
            raise NoSuchRevision(self, revision_id)
267
2018.5.40 by Robert Collins
Implement a remote Repository.has_revision method.
268
    def has_revision(self, revision_id):
269
        """See Repository.has_revision()."""
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
270
        if revision_id is None:
271
            # The null revision is always present.
272
            return True
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
273
        path = self.bzrdir._path_for_remote_call(self._client)
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
274
        response = self._client.call('Repository.has_revision', path, revision_id)
2018.5.52 by Wouter van Heyst
Provide more information when encountering unexpected responses from a smart
275
        assert response[0] in ('ok', 'no'), 'unexpected response code %s' % (response,)
2018.5.40 by Robert Collins
Implement a remote Repository.has_revision method.
276
        return response[0] == 'ok'
277
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
278
    def gather_stats(self, revid=None, committers=None):
2018.5.62 by Robert Collins
Stub out RemoteRepository.gather_stats while its implemented in parallel.
279
        """See Repository.gather_stats()."""
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
280
        path = self.bzrdir._path_for_remote_call(self._client)
281
        if revid in (None, NULL_REVISION):
282
            fmt_revid = ''
283
        else:
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
284
            fmt_revid = revid
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
285
        if committers is None or not committers:
2018.10.3 by v.ladeuil+lp at free
more tests for gather_stats
286
            fmt_committers = 'no'
287
        else:
288
            fmt_committers = 'yes'
289
        response = self._client.call2('Repository.gather_stats', path,
290
                                      fmt_revid, fmt_committers)
291
        assert response[0][0] == 'ok', \
292
            'unexpected response code %s' % (response[0],)
293
294
        body = response[1].read_body_bytes()
295
        result = {}
296
        for line in body.split('\n'):
297
            if not line:
298
                continue
299
            key, val_text = line.split(':')
300
            if key in ('revisions', 'size', 'committers'):
301
                result[key] = int(val_text)
302
            elif key in ('firstrev', 'latestrev'):
303
                values = val_text.split(' ')[1:]
304
                result[key] = (float(values[0]), long(values[1]))
305
306
        return result
2018.5.62 by Robert Collins
Stub out RemoteRepository.gather_stats while its implemented in parallel.
307
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
308
    def get_physical_lock_status(self):
309
        """See Repository.get_physical_lock_status()."""
310
        return False
311
2018.5.57 by Robert Collins
Implement RemoteRepository.is_shared (Robert Collins, Vincent Ladeuil).
312
    def is_shared(self):
313
        """See Repository.is_shared()."""
314
        path = self.bzrdir._path_for_remote_call(self._client)
315
        response = self._client.call('Repository.is_shared', path)
316
        assert response[0] in ('yes', 'no'), 'unexpected response code %s' % (response,)
317
        return response[0] == 'yes'
318
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
319
    def lock_read(self):
320
        # wrong eventually - want a local lock cache context
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
321
        if not self._lock_mode:
322
            self._lock_mode = 'r'
323
            self._lock_count = 1
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
324
            if self._real_repository is not None:
325
                self._real_repository.lock_read()
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
326
        else:
327
            self._lock_count += 1
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
328
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
329
    def _remote_lock_write(self, token):
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
330
        path = self.bzrdir._path_for_remote_call(self._client)
331
        if token is None:
332
            token = ''
333
        response = self._client.call('Repository.lock_write', path, token)
334
        if response[0] == 'ok':
335
            ok, token = response
336
            return token
337
        elif response[0] == 'LockContention':
338
            raise errors.LockContention('(remote lock)')
2018.5.95 by Andrew Bennetts
Add a Transport.is_readonly remote call, let {Branch,Repository}.lock_write remote call return UnlockableTransport, and miscellaneous test fixes.
339
        elif response[0] == 'UnlockableTransport':
340
            raise errors.UnlockableTransport(self.bzrdir.root_transport)
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
341
        else:
342
            assert False, 'unexpected response code %s' % (response,)
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
343
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
344
    def lock_write(self, token=None):
345
        if not self._lock_mode:
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
346
            self._lock_token = self._remote_lock_write(token)
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
347
            assert self._lock_token, 'Remote server did not return a token!'
348
            if self._real_repository is not None:
349
                self._real_repository.lock_write(token=self._lock_token)
350
            if token is not None:
351
                self._leave_lock = True
352
            else:
353
                self._leave_lock = False
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
354
            self._lock_mode = 'w'
355
            self._lock_count = 1
356
        elif self._lock_mode == 'r':
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
357
            raise errors.ReadOnlyError(self)
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
358
        else:
359
            self._lock_count += 1
360
        return self._lock_token
361
362
    def leave_lock_in_place(self):
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
363
        self._leave_lock = True
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
364
365
    def dont_leave_lock_in_place(self):
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
366
        self._leave_lock = False
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
367
368
    def _set_real_repository(self, repository):
369
        """Set the _real_repository for this repository.
370
371
        :param repository: The repository to fallback to for non-hpss
372
            implemented operations.
373
        """
2018.5.97 by Andrew Bennetts
Fix more tests.
374
        assert not isinstance(repository, RemoteRepository)
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
375
        self._real_repository = repository
376
        if self._lock_mode == 'w':
377
            # if we are already locked, the real repository must be able to
378
            # acquire the lock with our token.
379
            self._real_repository.lock_write(self._lock_token)
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
380
        elif self._lock_mode == 'r':
381
            self._real_repository.lock_read()
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
382
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
383
    def _unlock(self, token):
384
        path = self.bzrdir._path_for_remote_call(self._client)
385
        response = self._client.call('Repository.unlock', path, token)
386
        if response == ('ok',):
387
            return
388
        elif response[0] == 'TokenMismatch':
389
            raise errors.TokenMismatch(token, '(remote token)')
390
        else:
391
            assert False, 'unexpected response code %s' % (response,)
392
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
393
    def unlock(self):
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
394
        self._lock_count -= 1
395
        if not self._lock_count:
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
396
            mode = self._lock_mode
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
397
            self._lock_mode = None
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
398
            if self._real_repository is not None:
399
                self._real_repository.unlock()
400
            if mode != 'w':
401
                return
402
            assert self._lock_token, 'Locked, but no token!'
403
            token = self._lock_token
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
404
            self._lock_token = None
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
405
            if not self._leave_lock:
406
                self._unlock(token)
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
407
408
    def break_lock(self):
2018.5.78 by Andrew Bennetts
Implement RemoteRepository.lock_write/unlock to expect and send tokens over the
409
        # should hand off to the network
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
410
        self._ensure_real()
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
411
        return self._real_repository.break_lock()
412
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
413
    ### These methods are just thin shims to the VFS object for now.
414
415
    def revision_tree(self, revision_id):
416
        self._ensure_real()
417
        return self._real_repository.revision_tree(revision_id)
418
419
    def get_commit_builder(self, branch, parents, config, timestamp=None,
420
                           timezone=None, committer=None, revprops=None,
421
                           revision_id=None):
422
        # FIXME: It ought to be possible to call this without immediately
423
        # triggering _ensure_real.  For now it's the easiest thing to do.
424
        self._ensure_real()
425
        builder = self._real_repository.get_commit_builder(branch, parents,
426
                config, timestamp=timestamp, timezone=timezone,
427
                committer=committer, revprops=revprops, revision_id=revision_id)
428
        # Make the builder use this RemoteRepository rather than the real one.
429
        builder.repository = self
430
        return builder
431
432
    @needs_write_lock
433
    def add_inventory(self, revid, inv, parents):
434
        self._ensure_real()
435
        return self._real_repository.add_inventory(revid, inv, parents)
436
437
    @needs_write_lock
438
    def add_revision(self, rev_id, rev, inv=None, config=None):
439
        self._ensure_real()
440
        return self._real_repository.add_revision(
441
            rev_id, rev, inv=inv, config=config)
442
443
    @needs_read_lock
444
    def get_inventory(self, revision_id):
445
        self._ensure_real()
446
        return self._real_repository.get_inventory(revision_id)
447
448
    @needs_read_lock
449
    def get_revision(self, revision_id):
450
        self._ensure_real()
451
        return self._real_repository.get_revision(revision_id)
452
453
    @property
454
    def weave_store(self):
455
        self._ensure_real()
456
        return self._real_repository.weave_store
457
458
    def get_transaction(self):
459
        self._ensure_real()
460
        return self._real_repository.get_transaction()
461
462
    @needs_read_lock
463
    def clone(self, a_bzrdir, revision_id=None, basis=None):
464
        self._ensure_real()
465
        return self._real_repository.clone(
466
            a_bzrdir, revision_id=revision_id, basis=basis)
467
468
    def make_working_trees(self):
2018.5.120 by Robert Collins
The Repository API ``make_working_trees`` is now permitted to return
469
        """RemoteRepositories never create working trees by default."""
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
470
        return False
471
472
    def fetch(self, source, revision_id=None, pb=None):
473
        self._ensure_real()
474
        return self._real_repository.fetch(
475
            source, revision_id=revision_id, pb=pb)
476
477
    @property
478
    def control_weaves(self):
479
        self._ensure_real()
480
        return self._real_repository.control_weaves
481
482
    @needs_read_lock
483
    def get_ancestry(self, revision_id):
484
        self._ensure_real()
485
        return self._real_repository.get_ancestry(revision_id)
486
487
    @needs_read_lock
488
    def get_inventory_weave(self):
489
        self._ensure_real()
490
        return self._real_repository.get_inventory_weave()
491
492
    def fileids_altered_by_revision_ids(self, revision_ids):
493
        self._ensure_real()
494
        return self._real_repository.fileids_altered_by_revision_ids(revision_ids)
495
496
    @needs_read_lock
497
    def get_signature_text(self, revision_id):
498
        self._ensure_real()
499
        return self._real_repository.get_signature_text(revision_id)
500
501
    @needs_read_lock
502
    def get_revision_graph_with_ghosts(self, revision_ids=None):
503
        self._ensure_real()
504
        return self._real_repository.get_revision_graph_with_ghosts(
505
            revision_ids=revision_ids)
506
507
    @needs_read_lock
508
    def get_inventory_xml(self, revision_id):
509
        self._ensure_real()
510
        return self._real_repository.get_inventory_xml(revision_id)
511
512
    def deserialise_inventory(self, revision_id, xml):
513
        self._ensure_real()
514
        return self._real_repository.deserialise_inventory(revision_id, xml)
515
516
    def reconcile(self, other=None, thorough=False):
517
        self._ensure_real()
518
        return self._real_repository.reconcile(other=other, thorough=thorough)
519
        
520
    def all_revision_ids(self):
521
        self._ensure_real()
522
        return self._real_repository.all_revision_ids()
523
    
524
    @needs_read_lock
525
    def get_deltas_for_revisions(self, revisions):
526
        self._ensure_real()
527
        return self._real_repository.get_deltas_for_revisions(revisions)
528
529
    @needs_read_lock
530
    def get_revision_delta(self, revision_id):
531
        self._ensure_real()
532
        return self._real_repository.get_revision_delta(revision_id)
533
534
    @needs_read_lock
535
    def revision_trees(self, revision_ids):
536
        self._ensure_real()
537
        return self._real_repository.revision_trees(revision_ids)
538
539
    @needs_read_lock
540
    def get_revision_reconcile(self, revision_id):
541
        self._ensure_real()
542
        return self._real_repository.get_revision_reconcile(revision_id)
543
544
    @needs_read_lock
545
    def check(self, revision_ids):
546
        self._ensure_real()
547
        return self._real_repository.check(revision_ids)
548
549
    def copy_content_into(self, destination, revision_id=None, basis=None):
550
        self._ensure_real()
551
        return self._real_repository.copy_content_into(
552
            destination, revision_id=revision_id, basis=basis)
553
554
    def set_make_working_trees(self, new_value):
555
        raise NotImplementedError(self.set_make_working_trees)
556
557
    @needs_write_lock
558
    def sign_revision(self, revision_id, gpg_strategy):
559
        self._ensure_real()
560
        return self._real_repository.sign_revision(revision_id, gpg_strategy)
561
562
    @needs_read_lock
563
    def get_revisions(self, revision_ids):
564
        self._ensure_real()
565
        return self._real_repository.get_revisions(revision_ids)
566
567
    def supports_rich_root(self):
2018.5.84 by Andrew Bennetts
Merge in supports-rich-root, another test passing.
568
        self._ensure_real()
569
        return self._real_repository.supports_rich_root()
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
570
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
571
    def iter_reverse_revision_history(self, revision_id):
572
        self._ensure_real()
573
        return self._real_repository.iter_reverse_revision_history(revision_id)
574
2018.5.96 by Andrew Bennetts
Merge from bzr.dev, resolving the worst of the semantic conflicts, but there's
575
    @property
576
    def _serializer(self):
577
        self._ensure_real()
578
        return self._real_repository._serializer
579
2018.5.97 by Andrew Bennetts
Fix more tests.
580
    def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
581
        self._ensure_real()
582
        return self._real_repository.store_revision_signature(
583
            gpg_strategy, plaintext, revision_id)
584
585
    def has_signature_for_revision_id(self, revision_id):
586
        self._ensure_real()
587
        return self._real_repository.has_signature_for_revision_id(revision_id)
588
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
589
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
590
class RemoteBranchLockableFiles(object):
591
    """A 'LockableFiles' implementation that talks to a smart server.
592
    
593
    This is not a public interface class.
594
    """
595
596
    def __init__(self, bzrdir, _client):
597
        self.bzrdir = bzrdir
598
        self._client = _client
599
600
    def get(self, path):
601
        """'get' a remote path as per the LockableFiles interface.
602
603
        :param path: the file to 'get'. If this is 'branch.conf', we do not
604
             just retrieve a file, instead we ask the smart server to generate
605
             a configuration for us - which is retrieved as an INI file.
606
        """
607
        assert path == 'branch.conf'
608
        path = self.bzrdir._path_for_remote_call(self._client)
609
        response = self._client.call2('Branch.get_config_file', path)
2018.10.1 by v.ladeuil+lp at free
Line too long
610
        assert response[0][0] == 'ok', \
611
            'unexpected response code %s' % (response[0],)
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
612
        return StringIO(response[1].read_body_bytes())
613
614
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
615
class RemoteBranchFormat(branch.BranchFormat):
616
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
617
    def get_format_description(self):
618
        return 'Remote BZR Branch'
619
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
620
    def get_format_string(self):
621
        return 'Remote BZR Branch'
622
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
623
    def open(self, a_bzrdir):
1752.2.72 by Andrew Bennetts
Make Remote* classes in remote.py more consistent and remove some dead code.
624
        assert isinstance(a_bzrdir, RemoteBzrDir)
625
        return a_bzrdir.open_branch()
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
626
627
    def initialize(self, a_bzrdir):
628
        assert isinstance(a_bzrdir, RemoteBzrDir)
629
        return a_bzrdir.create_branch()
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
630
631
632
class RemoteBranch(branch.Branch):
633
    """Branch stored on a server accessed by HPSS RPC.
634
635
    At the moment most operations are mapped down to simple file operations.
636
    """
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
637
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
638
    def __init__(self, remote_bzrdir, remote_repository, real_branch=None,
639
        _client=None):
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
640
        """Create a RemoteBranch instance.
641
642
        :param real_branch: An optional local implementation of the branch
643
            format, usually accessing the data via the VFS.
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
644
        :param _client: Private parameter for testing.
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
645
        """
2018.5.105 by Andrew Bennetts
Implement revision_history caching for RemoteBranch.
646
        #branch.Branch.__init__(self)
647
        self._revision_history_cache = None
1752.2.64 by Andrew Bennetts
Improve how RemoteBzrDir.open_branch works to handle references and not double-open repositories.
648
        self.bzrdir = remote_bzrdir
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
649
        if _client is not None:
650
            self._client = _client
651
        else:
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
652
            self._client = client.SmartClient(self.bzrdir._medium)
1752.2.64 by Andrew Bennetts
Improve how RemoteBzrDir.open_branch works to handle references and not double-open repositories.
653
        self.repository = remote_repository
2018.5.34 by Robert Collins
Get test_remote.BasicRemoteObjectTests.test_open_remote_branch passing by implementing a remote method BzrDir.find_repository.
654
        if real_branch is not None:
655
            self._real_branch = real_branch
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
656
            # Give the remote repository the matching real repo.
2018.5.97 by Andrew Bennetts
Fix more tests.
657
            real_repo = self._real_branch.repository
658
            if isinstance(real_repo, RemoteRepository):
659
                real_repo._ensure_real()
660
                real_repo = real_repo._real_repository
661
            self.repository._set_real_repository(real_repo)
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
662
            # Give the branch the remote repository to let fast-pathing happen.
663
            self._real_branch.repository = self.repository
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
664
        else:
665
            self._real_branch = None
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
666
        # Fill out expected attributes of branch for bzrlib api users.
1752.2.64 by Andrew Bennetts
Improve how RemoteBzrDir.open_branch works to handle references and not double-open repositories.
667
        self._format = RemoteBranchFormat()
2018.5.55 by Robert Collins
Give RemoteBranch a base url in line with the Branch protocol.
668
        self.base = self.bzrdir.root_transport.base
2018.5.59 by Robert Collins
Get BranchConfig working somewhat on RemoteBranches (Robert Collins, Vincent Ladeuil).
669
        self.control_files = RemoteBranchLockableFiles(self.bzrdir, self._client)
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
670
        self._lock_mode = None
671
        self._lock_token = None
672
        self._lock_count = 0
673
        self._leave_lock = False
1752.2.64 by Andrew Bennetts
Improve how RemoteBzrDir.open_branch works to handle references and not double-open repositories.
674
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
675
    def _ensure_real(self):
676
        """Ensure that there is a _real_branch set.
677
678
        used before calls to self._real_branch.
679
        """
680
        if not self._real_branch:
681
            assert vfs.vfs_enabled()
682
            self.bzrdir._ensure_real()
683
            self._real_branch = self.bzrdir._real_bzrdir.open_branch()
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
684
            # Give the remote repository the matching real repo.
2018.5.97 by Andrew Bennetts
Fix more tests.
685
            real_repo = self._real_branch.repository
686
            if isinstance(real_repo, RemoteRepository):
687
                real_repo._ensure_real()
688
                real_repo = real_repo._real_repository
689
            self.repository._set_real_repository(real_repo)
2018.5.75 by Andrew Bennetts
Add Repository.{dont_,}leave_lock_in_place.
690
            # Give the branch the remote repository to let fast-pathing happen.
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
691
            self._real_branch.repository = self.repository
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
692
            # XXX: deal with _lock_mode == 'w'
693
            if self._lock_mode == 'r':
694
                self._real_branch.lock_read()
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
695
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
696
    def get_physical_lock_status(self):
697
        """See Branch.get_physical_lock_status()."""
698
        # should be an API call to the server, as branches must be lockable.
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
699
        self._ensure_real()
2018.5.60 by Robert Collins
More missing methods from RemoteBranch and RemoteRepository to let 'info' get further.
700
        return self._real_branch.get_physical_lock_status()
701
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
702
    def lock_read(self):
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
703
        if not self._lock_mode:
704
            self._lock_mode = 'r'
705
            self._lock_count = 1
706
            if self._real_branch is not None:
707
                self._real_branch.lock_read()
708
        else:
709
            self._lock_count += 1
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
710
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
711
    def _remote_lock_write(self, tokens):
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
712
        if tokens is None:
713
            branch_token = repo_token = ''
714
        else:
715
            branch_token, repo_token = tokens
716
        path = self.bzrdir._path_for_remote_call(self._client)
717
        response = self._client.call('Branch.lock_write', path, branch_token,
718
                                     repo_token)
719
        if response[0] == 'ok':
720
            ok, branch_token, repo_token = response
721
            return branch_token, repo_token
722
        elif response[0] == 'LockContention':
723
            raise errors.LockContention('(remote lock)')
724
        elif response[0] == 'TokenMismatch':
725
            raise errors.TokenMismatch(tokens, '(remote tokens)')
2018.5.95 by Andrew Bennetts
Add a Transport.is_readonly remote call, let {Branch,Repository}.lock_write remote call return UnlockableTransport, and miscellaneous test fixes.
726
        elif response[0] == 'UnlockableTransport':
727
            raise errors.UnlockableTransport(self.bzrdir.root_transport)
2018.5.123 by Robert Collins
Translate ReadOnlyError in RemoteBranch._remote_lock_write.
728
        elif response[0] == 'ReadOnlyError':
729
            raise errors.ReadOnlyError(self)
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
730
        else:
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
731
            assert False, 'unexpected response code %r' % (response,)
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
732
            
733
    def lock_write(self, tokens=None):
734
        if not self._lock_mode:
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
735
            remote_tokens = self._remote_lock_write(tokens)
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
736
            self._lock_token, self._repo_lock_token = remote_tokens
737
            assert self._lock_token, 'Remote server did not return a token!'
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
738
            # TODO: We really, really, really don't want to call _ensure_real
739
            # here, but it's the easiest way to ensure coherency between the
740
            # state of the RemoteBranch and RemoteRepository objects and the
741
            # physical locks.  If we don't materialise the real objects here,
742
            # then getting everything in the right state later is complex, so
743
            # for now we just do it the lazy way.
744
            #   -- Andrew Bennetts, 2007-02-22.
745
            self._ensure_real()
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
746
            if self._real_branch is not None:
747
                self._real_branch.lock_write(tokens=remote_tokens)
748
            if tokens is not None:
749
                self._leave_lock = True
750
            else:
751
                # XXX: this case seems to be unreachable; tokens cannot be None.
752
                self._leave_lock = False
753
            self._lock_mode = 'w'
754
            self._lock_count = 1
755
        elif self._lock_mode == 'r':
756
            raise errors.ReadOnlyTransaction
757
        else:
758
            if tokens is not None:
759
                # Tokens were given to lock_write, and we're relocking, so check
760
                # that the given tokens actually match the ones we already have.
761
                held_tokens = (self._lock_token, self._repo_lock_token)
762
                if tokens != held_tokens:
763
                    raise errors.TokenMismatch(str(tokens), str(held_tokens))
764
            self._lock_count += 1
765
        return self._lock_token, self._repo_lock_token
766
767
    def _unlock(self, branch_token, repo_token):
768
        path = self.bzrdir._path_for_remote_call(self._client)
769
        response = self._client.call('Branch.unlock', path, branch_token,
770
                                     repo_token)
771
        if response == ('ok',):
772
            return
773
        elif response[0] == 'TokenMismatch':
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
774
            raise errors.TokenMismatch(
775
                str((branch_token, repo_token)), '(remote tokens)')
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
776
        else:
777
            assert False, 'unexpected response code %s' % (response,)
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
778
779
    def unlock(self):
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
780
        self._lock_count -= 1
781
        if not self._lock_count:
2018.5.105 by Andrew Bennetts
Implement revision_history caching for RemoteBranch.
782
            self._clear_cached_state()
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
783
            mode = self._lock_mode
784
            self._lock_mode = None
785
            if self._real_branch is not None:
2018.15.1 by Andrew Bennetts
All branch_implementations/test_locking tests passing.
786
                if not self._leave_lock:
787
                    # If this RemoteBranch will remove the physical lock for the
788
                    # repository, make sure the _real_branch doesn't do it
789
                    # first.  (Because the _real_branch's repository is set to
790
                    # be the RemoteRepository.)
791
                    self._real_branch.repository.leave_lock_in_place()
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
792
                self._real_branch.unlock()
793
            if mode != 'w':
794
                return
795
            assert self._lock_token, 'Locked, but no token!'
796
            branch_token = self._lock_token
797
            repo_token = self._repo_lock_token
798
            self._lock_token = None
799
            self._repo_lock_token = None
800
            if not self._leave_lock:
801
                self._unlock(branch_token, repo_token)
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
802
803
    def break_lock(self):
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
804
        self._ensure_real()
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
805
        return self._real_branch.break_lock()
1752.2.31 by Martin Pool
[broken] some support for write operations over hpss
806
2018.5.79 by Andrew Bennetts
Implement RemoteBranch.lock_write/unlock as smart operations.
807
    def leave_lock_in_place(self):
808
        self._leave_lock = True
809
810
    def dont_leave_lock_in_place(self):
811
        self._leave_lock = False
812
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
813
    def last_revision_info(self):
814
        """See Branch.last_revision_info()."""
815
        path = self.bzrdir._path_for_remote_call(self._client)
816
        response = self._client.call('Branch.last_revision_info', path)
2018.5.52 by Wouter van Heyst
Provide more information when encountering unexpected responses from a smart
817
        assert response[0] == 'ok', 'unexpected response code %s' % (response,)
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
818
        revno = int(response[1])
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
819
        last_revision = response[2]
2018.5.51 by Wouter van Heyst
Test and implement RemoteBranch.last_revision_info()
820
        if last_revision == '':
821
            last_revision = NULL_REVISION
822
        return (revno, last_revision)
823
2018.5.105 by Andrew Bennetts
Implement revision_history caching for RemoteBranch.
824
    def _gen_revision_history(self):
825
        """See Branch._gen_revision_history()."""
2018.5.42 by Robert Collins
Various hopefully improvements, but wsgi is broken, handing over to spiv :).
826
        path = self.bzrdir._path_for_remote_call(self._client)
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
827
        response = self._client.call2('Branch.revision_history', path)
2018.5.52 by Wouter van Heyst
Provide more information when encountering unexpected responses from a smart
828
        assert response[0][0] == 'ok', 'unexpected response code %s' % (response[0],)
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
829
        result = response[1].read_body_bytes().split('\x00')
2018.5.38 by Robert Collins
Implement RemoteBranch.revision_history().
830
        if result == ['']:
831
            return []
832
        return result
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
833
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
834
    @needs_write_lock
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
835
    def set_revision_history(self, rev_history):
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
836
        # Send just the tip revision of the history; the server will generate
837
        # the full history from that.  If the revision doesn't exist in this
838
        # branch, NoSuchRevision will be raised.
839
        path = self.bzrdir._path_for_remote_call(self._client)
840
        if rev_history == []:
841
            rev_id = ''
842
        else:
843
            rev_id = rev_history[-1]
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
844
        response = self._client.call('Branch.set_last_revision',
845
            path, self._lock_token, self._repo_lock_token, rev_id)
2018.12.3 by Andrew Bennetts
Add a Branch.set_last_revision smart method, and make RemoteBranch.set_revision_history use it.
846
        if response[0] == 'NoSuchRevision':
847
            raise NoSuchRevision(self, rev_id)
848
        else:
849
            assert response == ('ok',), (
850
                'unexpected response code %r' % (response,))
2018.5.105 by Andrew Bennetts
Implement revision_history caching for RemoteBranch.
851
        self._cache_revision_history(rev_history)
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
852
853
    def get_parent(self):
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
854
        self._ensure_real()
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
855
        return self._real_branch.get_parent()
856
        
1752.2.63 by Andrew Bennetts
Delegate set_parent.
857
    def set_parent(self, url):
2018.5.70 by Robert Collins
Only try to get real repositories when an operation requires them.
858
        self._ensure_real()
1752.2.63 by Andrew Bennetts
Delegate set_parent.
859
        return self._real_branch.set_parent(url)
860
        
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
861
    def get_config(self):
862
        return RemoteBranchConfig(self)
863
2018.5.94 by Andrew Bennetts
Various small changes in aid of making tests pass (including deleting one invalid test).
864
    def sprout(self, to_bzrdir, revision_id=None):
865
        # Like Branch.sprout, except that it sprouts a branch in the default
866
        # format, because RemoteBranches can't be created at arbitrary URLs.
867
        # XXX: if to_bzrdir is a RemoteBranch, this should perhaps do
868
        # to_bzrdir.create_branch...
869
        self._ensure_real()
870
        result = branch.BranchFormat.get_default_format().initialize(to_bzrdir)
871
        self._real_branch.copy_content_into(result, revision_id=revision_id)
872
        result.set_parent(self.bzrdir.root_transport.base)
873
        return result
874
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
875
    @needs_write_lock
876
    def append_revision(self, *revision_ids):
877
        self._ensure_real()
878
        return self._real_branch.append_revision(*revision_ids)
879
880
    @needs_write_lock
881
    def pull(self, source, overwrite=False, stop_revision=None):
882
        self._ensure_real()
883
        self._real_branch.pull(
884
            source, overwrite=overwrite, stop_revision=stop_revision)
885
2018.14.3 by Andrew Bennetts
Make a couple more branch_implementations tests pass.
886
    @needs_read_lock
887
    def push(self, target, overwrite=False, stop_revision=None):
888
        self._ensure_real()
2018.5.97 by Andrew Bennetts
Fix more tests.
889
        return self._real_branch.push(
2018.14.3 by Andrew Bennetts
Make a couple more branch_implementations tests pass.
890
            target, overwrite=overwrite, stop_revision=stop_revision)
891
892
    def is_locked(self):
893
        return self._lock_count >= 1
894
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
895
    def set_last_revision_info(self, revno, revision_id):
896
        self._ensure_real()
2018.5.105 by Andrew Bennetts
Implement revision_history caching for RemoteBranch.
897
        self._clear_cached_state()
2018.5.83 by Andrew Bennetts
Fix some test failures caused by the switch from unicode to UTF-8-encoded strs for revision IDs.
898
        return self._real_branch.set_last_revision_info(revno, revision_id)
899
2018.5.95 by Andrew Bennetts
Add a Transport.is_readonly remote call, let {Branch,Repository}.lock_write remote call return UnlockableTransport, and miscellaneous test fixes.
900
    def generate_revision_history(self, revision_id, last_rev=None,
901
                                  other_branch=None):
902
        self._ensure_real()
903
        return self._real_branch.generate_revision_history(
904
            revision_id, last_rev=last_rev, other_branch=other_branch)
905
2018.5.96 by Andrew Bennetts
Merge from bzr.dev, resolving the worst of the semantic conflicts, but there's
906
    @property
907
    def tags(self):
908
        self._ensure_real()
909
        return self._real_branch.tags
910
2018.5.97 by Andrew Bennetts
Fix more tests.
911
    def set_push_location(self, location):
912
        self._ensure_real()
913
        return self._real_branch.set_push_location(location)
914
915
    def update_revisions(self, other, stop_revision=None):
916
        self._ensure_real()
917
        return self._real_branch.update_revisions(
918
            other, stop_revision=stop_revision)
919
1752.2.30 by Martin Pool
Start adding a RemoteBzrDir, etc
920
1752.2.50 by Andrew Bennetts
Implement RemoteBzrDir.create_{branch,workingtree}
921
class RemoteWorkingTree(object):
922
1752.2.72 by Andrew Bennetts
Make Remote* classes in remote.py more consistent and remove some dead code.
923
    def __init__(self, remote_bzrdir, real_workingtree):
1752.2.50 by Andrew Bennetts
Implement RemoteBzrDir.create_{branch,workingtree}
924
        self.real_workingtree = real_workingtree
925
        self.bzrdir = remote_bzrdir
926
927
    def __getattr__(self, name):
928
        # XXX: temporary way to lazily delegate everything to the real
929
        # workingtree
930
        return getattr(self.real_workingtree, name)
931
1752.2.52 by Andrew Bennetts
Flesh out more Remote* methods needed to open and initialise remote branches/trees/repositories.
932
2018.14.1 by Andrew Bennetts
Update to current hpss branch? Fix lots of test failures.
933
class RemoteBranchConfig(BranchConfig):
934
935
    def username(self):
936
        self.branch._ensure_real()
937
        return self.branch._real_branch.get_config().username()
938
2018.14.2 by Andrew Bennetts
All but one repository_implementation tests for RemoteRepository passing.
939
    def _get_branch_data_config(self):
940
        self.branch._ensure_real()
941
        if self._branch_data_config is None:
942
            self._branch_data_config = TreeConfig(self.branch._real_branch)
943
        return self._branch_data_config
944